root/drivers/sound/sb_dsp.c

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

DEFINITIONS

This source file includes following definitions.
  1. sb_dsp_command
  2. sbintr
  3. sb_get_irq
  4. sb_free_irq
  5. sb_reset_dsp
  6. dsp_speaker
  7. dsp_speed
  8. dsp_set_stereo
  9. sb_dsp_output_block
  10. sb_dsp_start_input
  11. sb_dsp_trigger
  12. dsp_cleanup
  13. sb_dsp_prepare_for_input
  14. sb_dsp_prepare_for_output
  15. sb_dsp_halt_xfer
  16. verify_irq
  17. sb_dsp_open
  18. sb_dsp_close
  19. dsp_set_bits
  20. sb_dsp_ioctl
  21. sb_dsp_reset
  22. get_sb_byte
  23. smw_putmem
  24. smw_getmem
  25. initialize_smw
  26. initialize_ProSonic16
  27. sb_dsp_detect
  28. sb_dsp_init
  29. sb_dsp_unload
  30. sb_dsp_disable_midi

   1 /*
   2  * sound/sb_dsp.c
   3  *
   4  * The low level driver for the SoundBlaster DSP chip (SB1.0 to 2.1, SB Pro).
   5  *
   6  * Copyright by Hannu Savolainen 1994
   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  * Modified:
  29  *      Hunyue Yau      Jan 6 1994
  30  *      Added code to support Sound Galaxy NX Pro
  31  *
  32  *      JRA Gibson      April 1995
  33  *      Code added for MV ProSonic/Jazz 16 in 16 bit mode
  34  */
  35 
  36 #include "sound_config.h"
  37 
  38 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_SB)
  39 
  40 #ifdef SM_WAVE
  41 #define JAZZ16
  42 #endif
  43 
  44 #include "sb.h"
  45 #include "sb_mixer.h"
  46 #undef SB_TEST_IRQ
  47 
  48 int             sbc_base = 0;
  49 static int      sbc_irq = 0;
  50 static int      open_mode = 0;  /* Read, write or both */
  51 int             Jazz16_detected = 0;
  52 int             sb_no_recording = 0;
  53 static int      dsp_count = 0;
  54 static int      trigger_bits;
  55 
  56 /*
  57  * The DSP channel can be used either for input or output. Variable
  58  * 'sb_irq_mode' will be set when the program calls read or write first time
  59  * after open. Current version doesn't support mode changes without closing
  60  * and reopening the device. Support for this feature may be implemented in a
  61  * future version of this driver.
  62  */
  63 
  64 int             sb_dsp_ok = 0;  /*
  65 
  66 
  67                                  * *  * * Set to 1 after successful
  68                                  * initialization  *  */
  69 static int      midi_disabled = 0;
  70 int             sb_dsp_highspeed = 0;
  71 int             sbc_major = 1, sbc_minor = 0;   /*
  72 
  73 
  74                                                    * *  * * DSP version   */
  75 static int      dsp_stereo = 0;
  76 static int      dsp_current_speed = DSP_DEFAULT_SPEED;
  77 static int      sb16 = 0;
  78 static int      irq_verified = 0;
  79 
  80 int             sb_midi_mode = NORMAL_MIDI;
  81 int             sb_midi_busy = 0;       /*
  82 
  83 
  84                                          * *  * * 1 if the process has output
  85                                          * to *  * MIDI   */
  86 int             sb_dsp_busy = 0;
  87 
  88 volatile int    sb_irq_mode = IMODE_NONE;       /*
  89 
  90 
  91                                                  * *  * * IMODE_INPUT, *
  92                                                  * IMODE_OUTPUT * * or *
  93                                                  * IMODE_NONE   */
  94 static volatile int irq_ok = 0;
  95 
  96 static int      dma8 = 1;
  97 
  98 #ifdef JAZZ16
  99 /* 16 bit support
 100  */
 101 
 102 static int      dsp_16bit = 0;
 103 static int      dma16 = 5;
 104 
 105 static int      dsp_set_bits (int arg);
 106 static int      initialize_ProSonic16 (void);
 107 
 108 /* end of 16 bit support
 109  */
 110 #endif
 111 
 112 int             sb_duplex_midi = 0;
 113 static int      my_dev = 0;
 114 
 115 volatile int    sb_intr_active = 0;
 116 
 117 static int      dsp_speed (int);
 118 static int      dsp_set_stereo (int mode);
 119 int             sb_dsp_command (unsigned char val);
 120 static void     sb_dsp_reset (int dev);
 121 sound_os_info  *sb_osp = NULL;
 122 
 123 #if !defined(EXCLUDE_MIDI) || !defined(EXCLUDE_AUDIO)
 124 
 125 /*
 126  * Common code for the midi and pcm functions
 127  */
 128 
 129 int
 130 sb_dsp_command (unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 131 {
 132   int             i;
 133   unsigned long   limit;
 134 
 135   limit = jiffies + HZ / 10;    /*
 136                                    * The timeout is 0.1 secods
 137                                  */
 138 
 139   /*
 140    * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
 141    * called while interrupts are disabled. This means that the timer is
 142    * disabled also. However the timeout situation is a abnormal condition.
 143    * Normally the DSP should be ready to accept commands after just couple of
 144    * loops.
 145    */
 146 
 147   for (i = 0; i < 500000 && jiffies < limit; i++)
 148     {
 149       if ((inb (DSP_STATUS) & 0x80) == 0)
 150         {
 151           outb (val, DSP_COMMAND);
 152           return 1;
 153         }
 154     }
 155 
 156   printk ("SoundBlaster: DSP Command(%x) Timeout.\n", val);
 157   printk ("IRQ conflict???\n");
 158   return 0;
 159 }
 160 
 161 void
 162 sbintr (int irq, struct pt_regs *dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164   int             status;
 165 
 166 #ifndef EXCLUDE_SBPRO
 167   if (sb16)
 168     {
 169       unsigned char   src = sb_getmixer (IRQ_STAT);     /* Interrupt source register */
 170 
 171 #ifndef EXCLUDE_SB16
 172       if (src & 3)
 173         sb16_dsp_interrupt (irq);
 174 
 175 #ifndef EXCLUDE_MIDI
 176       if (src & 4)
 177         sb16midiintr (irq);     /*
 178                                  * SB MPU401 interrupt
 179                                  */
 180 #endif
 181 
 182 #endif
 183 
 184       if (!(src & 1))
 185         return;                 /*
 186                                  * Not a DSP interupt
 187                                  */
 188     }
 189 #endif
 190 
 191   status = inb (DSP_DATA_AVAIL);        /*
 192                                            * Clear interrupt
 193                                          */
 194 
 195   if (sb_intr_active)
 196     switch (sb_irq_mode)
 197       {
 198       case IMODE_OUTPUT:
 199         sb_intr_active = 0;
 200         DMAbuf_outputintr (my_dev, 1);
 201         break;
 202 
 203       case IMODE_INPUT:
 204         sb_intr_active = 0;
 205         DMAbuf_inputintr (my_dev);
 206         /*
 207          * A complete buffer has been input. Let's start new one
 208          */
 209         break;
 210 
 211       case IMODE_INIT:
 212         sb_intr_active = 0;
 213         irq_ok = 1;
 214         break;
 215 
 216       case IMODE_MIDI:
 217 #ifndef EXCLUDE_MIDI
 218         sb_midi_interrupt (irq);
 219 #endif
 220         break;
 221 
 222       default:
 223         printk ("SoundBlaster: Unexpected interrupt\n");
 224       }
 225 }
 226 
 227 int
 228 sb_get_irq (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 229 {
 230   return 0;
 231 }
 232 
 233 void
 234 sb_free_irq (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236 }
 237 
 238 int
 239 sb_reset_dsp (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241   int             loopc;
 242 
 243   outb (1, DSP_RESET);
 244   tenmicrosec ();
 245   outb (0, DSP_RESET);
 246   tenmicrosec ();
 247   tenmicrosec ();
 248   tenmicrosec ();
 249 
 250   for (loopc = 0; loopc < 1000 && !(inb (DSP_DATA_AVAIL) & 0x80); loopc++);
 251 
 252   if (inb (DSP_READ) != 0xAA)
 253     return 0;                   /* Sorry */
 254 
 255   return 1;
 256 }
 257 
 258 #endif
 259 
 260 #ifndef EXCLUDE_AUDIO
 261 
 262 static void
 263 dsp_speaker (char state)
     /* [previous][next][first][last][top][bottom][index][help] */
 264 {
 265   if (state)
 266     sb_dsp_command (DSP_CMD_SPKON);
 267   else
 268     sb_dsp_command (DSP_CMD_SPKOFF);
 269 }
 270 
 271 static int
 272 dsp_speed (int speed)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274   unsigned char   tconst;
 275   unsigned long   flags;
 276   int             max_speed = 44100;
 277 
 278   if (speed < 4000)
 279     speed = 4000;
 280 
 281   /*
 282      * Older SB models don't support higher speeds than 22050.
 283    */
 284 
 285   if (sbc_major < 2 ||
 286       (sbc_major == 2 && sbc_minor == 0))
 287     max_speed = 22050;
 288 
 289   /*
 290      * SB models earlier than SB Pro have low limit for the input speed.
 291    */
 292   if (open_mode != OPEN_WRITE)  /* Recording is possible */
 293     if (sbc_major < 3)          /* Limited input speed with these cards */
 294       if (sbc_major == 2 && sbc_minor > 0)
 295         max_speed = 15000;
 296       else
 297         max_speed = 13000;
 298 
 299   if (speed > max_speed)
 300     speed = max_speed;          /*
 301                                  * Invalid speed
 302                                  */
 303 
 304   /* Logitech SoundMan Games and Jazz16 cards can support 44.1kHz stereo */
 305 #if !defined (SM_GAMES)
 306   /*
 307    * Max. stereo speed is 22050
 308    */
 309   if (dsp_stereo && speed > 22050 && Jazz16_detected == 0)
 310     speed = 22050;
 311 #endif
 312 
 313   if ((speed > 22050) && sb_midi_busy)
 314     {
 315       printk ("SB Warning: High speed DSP not possible simultaneously with MIDI output\n");
 316       speed = 22050;
 317     }
 318 
 319   if (dsp_stereo)
 320     speed *= 2;
 321 
 322   /*
 323    * Now the speed should be valid
 324    */
 325 
 326   if (speed > 22050)
 327     {                           /*
 328                                  * High speed mode
 329                                  */
 330       int             tmp;
 331 
 332       tconst = (unsigned char) ((65536 -
 333                                  ((256000000 + speed / 2) / speed)) >> 8);
 334       sb_dsp_highspeed = 1;
 335 
 336       save_flags (flags);
 337       cli ();
 338       if (sb_dsp_command (0x40))
 339         sb_dsp_command (tconst);
 340       restore_flags (flags);
 341 
 342       tmp = 65536 - (tconst << 8);
 343       speed = (256000000 + tmp / 2) / tmp;
 344     }
 345   else
 346     {
 347       int             tmp;
 348 
 349       sb_dsp_highspeed = 0;
 350       tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
 351 
 352       save_flags (flags);
 353       cli ();
 354       if (sb_dsp_command (0x40))        /*
 355                                          * Set time constant
 356                                          */
 357         sb_dsp_command (tconst);
 358       restore_flags (flags);
 359 
 360       tmp = 256 - tconst;
 361       speed = (1000000 + tmp / 2) / tmp;
 362     }
 363 
 364   if (dsp_stereo)
 365     speed /= 2;
 366 
 367   dsp_current_speed = speed;
 368   return speed;
 369 }
 370 
 371 static int
 372 dsp_set_stereo (int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 373 {
 374   dsp_stereo = 0;
 375 
 376 #ifdef EXCLUDE_SBPRO
 377   return 0;
 378 #else
 379   if (sbc_major < 3 || sb16)
 380     return 0;                   /*
 381                                  * Sorry no stereo
 382                                  */
 383 
 384   if (mode && sb_midi_busy)
 385     {
 386       printk ("SB Warning: Stereo DSP not possible simultaneously with MIDI output\n");
 387       return 0;
 388     }
 389 
 390   dsp_stereo = !!mode;
 391   return dsp_stereo;
 392 #endif
 393 }
 394 
 395 static void
 396 sb_dsp_output_block (int dev, unsigned long buf, int count,
     /* [previous][next][first][last][top][bottom][index][help] */
 397                      int intrflag, int restart_dma)
 398 {
 399   unsigned long   flags;
 400 
 401   if (!sb_irq_mode)
 402     dsp_speaker (ON);
 403 
 404   DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE);
 405 
 406   sb_irq_mode = 0;
 407 
 408   if (audio_devs[dev]->dmachan1 > 3)
 409     count >>= 1;
 410   count--;
 411   dsp_count = count;
 412 
 413   sb_irq_mode = IMODE_OUTPUT;
 414   if (sb_dsp_highspeed)
 415     {
 416       save_flags (flags);
 417       cli ();
 418       if (sb_dsp_command (0x48))        /*
 419                                            * High speed size
 420                                          */
 421         {
 422           sb_dsp_command ((unsigned char) (dsp_count & 0xff));
 423           sb_dsp_command ((unsigned char) ((dsp_count >> 8) & 0xff));
 424           sb_dsp_command (0x91);        /*
 425                                            * High speed 8 bit DAC
 426                                          */
 427         }
 428       else
 429         printk ("SB Error: Unable to start (high speed) DAC\n");
 430       restore_flags (flags);
 431     }
 432   else
 433     {
 434       save_flags (flags);
 435       cli ();
 436       if (sb_dsp_command (0x14))        /*
 437                                            * 8-bit DAC (DMA)
 438                                          */
 439         {
 440           sb_dsp_command ((unsigned char) (dsp_count & 0xff));
 441           sb_dsp_command ((unsigned char) ((dsp_count >> 8) & 0xff));
 442         }
 443       else
 444         printk ("SB Error: Unable to start DAC\n");
 445       restore_flags (flags);
 446     }
 447   sb_intr_active = 1;
 448 }
 449 
 450 static void
 451 sb_dsp_start_input (int dev, unsigned long buf, int count, int intrflag,
     /* [previous][next][first][last][top][bottom][index][help] */
 452                     int restart_dma)
 453 {
 454   unsigned long   flags;
 455 
 456   if (sb_no_recording)
 457     {
 458       printk ("SB Error: This device doesn't support recording\n");
 459       return;
 460     }
 461 
 462   /*
 463    * Start a DMA input to the buffer pointed by dmaqtail
 464    */
 465 
 466   if (!sb_irq_mode)
 467     dsp_speaker (OFF);
 468 
 469   DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
 470   sb_irq_mode = 0;
 471 
 472   if (audio_devs[dev]->dmachan1 > 3)
 473     count >>= 1;
 474   count--;
 475   dsp_count = count;
 476 
 477   sb_irq_mode = IMODE_INPUT;
 478   if (sb_dsp_highspeed)
 479     {
 480       save_flags (flags);
 481       cli ();
 482       if (sb_dsp_command (0x48))        /*
 483                                            * High speed size
 484                                          */
 485         {
 486           sb_dsp_command ((unsigned char) (dsp_count & 0xff));
 487           sb_dsp_command ((unsigned char) ((dsp_count >> 8) & 0xff));
 488           sb_dsp_command (0x99);        /*
 489                                            * High speed 8 bit ADC
 490                                          */
 491         }
 492       else
 493         printk ("SB Error: Unable to start (high speed) ADC\n");
 494       restore_flags (flags);
 495     }
 496   else
 497     {
 498       save_flags (flags);
 499       cli ();
 500       if (sb_dsp_command (0x24))        /*
 501                                            * 8-bit ADC (DMA)
 502                                          */
 503         {
 504           sb_dsp_command ((unsigned char) (dsp_count & 0xff));
 505           sb_dsp_command ((unsigned char) ((dsp_count >> 8) & 0xff));
 506         }
 507       else
 508         printk ("SB Error: Unable to start ADC\n");
 509       restore_flags (flags);
 510     }
 511 
 512   sb_intr_active = 1;
 513 }
 514 
 515 static void
 516 sb_dsp_trigger (int dev, int bits)
     /* [previous][next][first][last][top][bottom][index][help] */
 517 {
 518   if (bits == trigger_bits)
 519     return;
 520 
 521   if (!bits)
 522     sb_dsp_command (0xd0);      /* Halt DMA */
 523   else if (bits & sb_irq_mode)
 524     sb_dsp_command (0xd4);      /* Continue DMA */
 525 
 526   trigger_bits = bits;
 527 }
 528 
 529 static void
 530 dsp_cleanup (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 531 {
 532   sb_intr_active = 0;
 533 }
 534 
 535 static int
 536 sb_dsp_prepare_for_input (int dev, int bsize, int bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 537 {
 538   dsp_cleanup ();
 539   dsp_speaker (OFF);
 540 
 541   if (sbc_major == 3)           /*
 542                                  * SB Pro
 543                                  */
 544     {
 545 #ifdef JAZZ16
 546       /* Select correct dma channel
 547          * for 16/8 bit acccess
 548        */
 549       audio_devs[my_dev]->dmachan1 = dsp_16bit ? dma16 : dma8;
 550       if (dsp_stereo)
 551         sb_dsp_command (dsp_16bit ? 0xac : 0xa8);
 552       else
 553         sb_dsp_command (dsp_16bit ? 0xa4 : 0xa0);
 554 #else
 555       /* 8 bit only cards use this
 556        */
 557       if (dsp_stereo)
 558         sb_dsp_command (0xa8);
 559       else
 560         sb_dsp_command (0xa0);
 561 #endif
 562       dsp_speed (dsp_current_speed);    /*
 563                                          * Speed must be recalculated if
 564                                          * #channels * changes
 565                                          */
 566     }
 567   trigger_bits = 0;
 568   sb_dsp_command (0xd0);        /* Halt DMA */
 569   return 0;
 570 }
 571 
 572 static int
 573 sb_dsp_prepare_for_output (int dev, int bsize, int bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 574 {
 575   dsp_cleanup ();
 576   dsp_speaker (ON);
 577 
 578 #ifndef EXCLUDE_SBPRO
 579   if (sbc_major == 3)           /*
 580                                  * SB Pro
 581                                  */
 582     {
 583 #ifdef JAZZ16
 584       /* 16 bit specific instructions
 585        */
 586       audio_devs[my_dev]->dmachan1 = dsp_16bit ? dma16 : dma8;
 587       if (Jazz16_detected != 2) /* SM Wave */
 588         sb_mixer_set_stereo (dsp_stereo);
 589       if (dsp_stereo)
 590         sb_dsp_command (dsp_16bit ? 0xac : 0xa8);
 591       else
 592         sb_dsp_command (dsp_16bit ? 0xa4 : 0xa0);
 593 #else
 594       sb_mixer_set_stereo (dsp_stereo);
 595 #endif
 596       dsp_speed (dsp_current_speed);    /*
 597                                          * Speed must be recalculated if
 598                                          * #channels * changes
 599                                          */
 600     }
 601 #endif
 602   trigger_bits = 0;
 603   sb_dsp_command (0xd0);        /* Halt DMA */
 604   return 0;
 605 }
 606 
 607 static void
 608 sb_dsp_halt_xfer (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 609 {
 610 }
 611 
 612 static int
 613 verify_irq (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 614 {
 615   irq_ok = 1;
 616   return irq_ok;
 617 }
 618 
 619 static int
 620 sb_dsp_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 621 {
 622   int             retval;
 623 
 624   if (!sb_dsp_ok)
 625     {
 626       printk ("SB Error: SoundBlaster board not installed\n");
 627       return -ENXIO;
 628     }
 629 
 630   if (sb_no_recording && mode & OPEN_READ)
 631     {
 632       printk ("SB Warning: Recording not supported by this device\n");
 633     }
 634 
 635   if (sb_intr_active || (sb_midi_busy && sb_midi_mode == UART_MIDI))
 636     {
 637       printk ("SB: PCM not possible during MIDI input\n");
 638       return -EBUSY;
 639     }
 640 
 641   if (!irq_verified)
 642     {
 643       verify_irq ();
 644       irq_verified = 1;
 645     }
 646   else if (!irq_ok)
 647     printk ("SB Warning: Incorrect IRQ setting %d\n",
 648             sbc_irq);
 649 
 650   retval = sb_get_irq ();
 651   if (retval)
 652     return retval;
 653 
 654   /* Allocate 8 bit dma
 655    */
 656 #ifdef JAZZ16
 657   audio_devs[my_dev]->dmachan1 = dma8;
 658 #endif
 659 #ifdef JAZZ16
 660   /* Allocate 16 bit dma
 661    */
 662   if (Jazz16_detected != 0)
 663     if (dma16 != dma8)
 664       {
 665         if (sound_open_dma (dma16, "Jazz16 16 bit"))
 666           {
 667             sb_free_irq ();
 668             /* DMAbuf_close_dma (dev); */
 669             return -EBUSY;
 670           }
 671       }
 672 #endif
 673 
 674   sb_irq_mode = IMODE_NONE;
 675 
 676   sb_dsp_busy = 1;
 677   open_mode = mode;
 678 
 679   return 0;
 680 }
 681 
 682 static void
 683 sb_dsp_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 684 {
 685 #ifdef JAZZ16
 686   /* Release 16 bit dma channel
 687    */
 688   if (Jazz16_detected)
 689     {
 690       audio_devs[my_dev]->dmachan1 = dma8;
 691 
 692       if (dma16 != dma8)
 693         sound_close_dma (dma16);
 694     }
 695 #endif
 696 
 697   /* DMAbuf_close_dma (dev); */
 698   sb_free_irq ();
 699   /* sb_dsp_command (0xd4); */
 700   dsp_cleanup ();
 701   dsp_speaker (OFF);
 702   sb_dsp_busy = 0;
 703   sb_dsp_highspeed = 0;
 704   open_mode = 0;
 705 }
 706 
 707 #ifdef JAZZ16
 708 /* Function dsp_set_bits() only required for 16 bit cards
 709  */
 710 static int
 711 dsp_set_bits (int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 712 {
 713   if (arg)
 714     if (Jazz16_detected == 0)
 715       dsp_16bit = 0;
 716     else
 717       switch (arg)
 718         {
 719         case 8:
 720           dsp_16bit = 0;
 721           break;
 722         case 16:
 723           dsp_16bit = 1;
 724           break;
 725         default:
 726           dsp_16bit = 0;
 727         }
 728   return dsp_16bit ? 16 : 8;
 729 }
 730 
 731 #endif /* ifdef JAZZ16 */
 732 
 733 static int
 734 sb_dsp_ioctl (int dev, unsigned int cmd, ioctl_arg arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
 735 {
 736   switch (cmd)
 737     {
 738     case SOUND_PCM_WRITE_RATE:
 739       if (local)
 740         return dsp_speed ((long) arg);
 741       return snd_ioctl_return ((int *) arg, dsp_speed (get_fs_long ((long *) arg)));
 742       break;
 743 
 744     case SOUND_PCM_READ_RATE:
 745       if (local)
 746         return dsp_current_speed;
 747       return snd_ioctl_return ((int *) arg, dsp_current_speed);
 748       break;
 749 
 750     case SOUND_PCM_WRITE_CHANNELS:
 751       if (local)
 752         return dsp_set_stereo ((long) arg - 1) + 1;
 753       return snd_ioctl_return ((int *) arg, dsp_set_stereo (get_fs_long ((long *) arg) - 1) + 1);
 754       break;
 755 
 756     case SOUND_PCM_READ_CHANNELS:
 757       if (local)
 758         return dsp_stereo + 1;
 759       return snd_ioctl_return ((int *) arg, dsp_stereo + 1);
 760       break;
 761 
 762     case SNDCTL_DSP_STEREO:
 763       if (local)
 764         return dsp_set_stereo ((long) arg);
 765       return snd_ioctl_return ((int *) arg, dsp_set_stereo (get_fs_long ((long *) arg)));
 766       break;
 767 
 768 #ifdef JAZZ16
 769       /* Word size specific cases here.
 770          * SNDCTL_DSP_SETFMT=SOUND_PCM_WRITE_BITS
 771        */
 772     case SNDCTL_DSP_SETFMT:
 773       if (local)
 774         return dsp_set_bits ((int) arg);
 775       return snd_ioctl_return ((int *) arg, dsp_set_bits (get_fs_long ((long *) arg)));
 776       break;
 777 
 778     case SOUND_PCM_READ_BITS:
 779       if (local)
 780         return dsp_16bit ? 16 : 8;
 781       return snd_ioctl_return ((int *) arg, dsp_16bit ? 16 : 8);
 782       break;
 783 #else
 784     case SOUND_PCM_WRITE_BITS:
 785     case SOUND_PCM_READ_BITS:
 786       if (local)
 787         return 8;
 788       return snd_ioctl_return ((int *) arg, 8); /* Only 8 bits/sample supported */
 789       break;
 790 #endif /* ifdef JAZZ16  */
 791 
 792     case SOUND_PCM_WRITE_FILTER:
 793     case SOUND_PCM_READ_FILTER:
 794       return -EINVAL;
 795       break;
 796 
 797     default:;
 798     }
 799 
 800   return -EINVAL;
 801 }
 802 
 803 static void
 804 sb_dsp_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 805 {
 806   unsigned long   flags;
 807 
 808   save_flags (flags);
 809   cli ();
 810 
 811   sb_reset_dsp ();
 812   dsp_speed (dsp_current_speed);
 813   dsp_cleanup ();
 814 
 815   restore_flags (flags);
 816 }
 817 
 818 #endif
 819 
 820 
 821 #ifdef JAZZ16
 822 
 823 /*
 824  * Initialization of a Media Vision ProSonic 16 Soundcard.
 825  * The function initializes a ProSonic 16 like PROS.EXE does for DOS. It sets
 826  * the base address, the DMA-channels, interrupts and enables the joystickport.
 827  *
 828  * Also used by Jazz 16 (same card, different name)
 829  *
 830  * written 1994 by Rainer Vranken
 831  * E-Mail: rvranken@polaris.informatik.uni-essen.de
 832  */
 833 
 834 unsigned int
 835 get_sb_byte (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 836 {
 837   int             i;
 838 
 839   for (i = 1000; i; i--)
 840     if (inb (DSP_DATA_AVAIL) & 0x80)
 841       {
 842         return inb (DSP_READ);
 843       }
 844 
 845   return 0xffff;
 846 }
 847 
 848 #ifdef SM_WAVE
 849 /*
 850  * Logitech Soundman Wave detection and initialization by Hannu Savolainen.
 851  *
 852  * There is a microcontroller (8031) in the SM Wave card for MIDI emulation.
 853  * it's located at address MPU_BASE+4.  MPU_BASE+7 is a SM Wave specific
 854  * control register for MC reset, SCSI, OPL4 and DSP (future expansion)
 855  * address decoding. Otherwise the SM Wave is just a ordinary MV Jazz16
 856  * based soundcard.
 857  */
 858 
 859 static void
 860 smw_putmem (int base, int addr, unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 861 {
 862   unsigned long   flags;
 863 
 864   save_flags (flags);
 865   cli ();
 866 
 867   outb (addr & 0xff, base + 1); /* Low address bits */
 868   outb (addr >> 8, base + 2);   /* High address bits */
 869   outb (val, base);             /* Data */
 870 
 871   restore_flags (flags);
 872 }
 873 
 874 static unsigned char
 875 smw_getmem (int base, int addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 876 {
 877   unsigned long   flags;
 878   unsigned char   val;
 879 
 880   save_flags (flags);
 881   cli ();
 882 
 883   outb (addr & 0xff, base + 1); /* Low address bits */
 884   outb (addr >> 8, base + 2);   /* High address bits */
 885   val = inb (base);             /* Data */
 886 
 887   restore_flags (flags);
 888   return val;
 889 }
 890 
 891 #ifdef SMW_MIDI0001_INCLUDED
 892 #include "smw-midi0001.h"
 893 #else
 894 unsigned char  *smw_ucode = NULL;
 895 int             smw_ucodeLen = 0;
 896 
 897 #endif
 898 
 899 static int
 900 initialize_smw (int mpu_base)
     /* [previous][next][first][last][top][bottom][index][help] */
 901 {
 902 
 903   int             mp_base = mpu_base + 4;       /* Microcontroller base */
 904   int             i;
 905   unsigned char   control;
 906 
 907 
 908   /*
 909      *  Reset the microcontroller so that the RAM can be accessed
 910    */
 911 
 912   control = inb (mpu_base + 7);
 913   outb (control | 3, mpu_base + 7);     /* Set last two bits to 1 (?) */
 914   outb ((control & 0xfe) | 2, mpu_base + 7);    /* xxxxxxx0 resets the mc */
 915 
 916   for (i = 0; i < 300; i++)     /* Wait at least 1ms */
 917     tenmicrosec ();
 918 
 919   outb (control & 0xfc, mpu_base + 7);  /* xxxxxx00 enables RAM */
 920 
 921   /*
 922      *  Detect microcontroller by probing the 8k RAM area
 923    */
 924   smw_putmem (mp_base, 0, 0x00);
 925   smw_putmem (mp_base, 1, 0xff);
 926   tenmicrosec ();
 927 
 928   if (smw_getmem (mp_base, 0) != 0x00 || smw_getmem (mp_base, 1) != 0xff)
 929     {
 930       printk ("\nSM Wave: No microcontroller RAM detected (%02x, %02x)\n",
 931               smw_getmem (mp_base, 0), smw_getmem (mp_base, 1));
 932       return 0;                 /* No RAM */
 933     }
 934 
 935   /*
 936      *  There is RAM so assume it's really a SM Wave
 937    */
 938 
 939   if (smw_ucodeLen > 0)
 940     {
 941       if (smw_ucodeLen != 8192)
 942         {
 943           printk ("\nSM Wave: Invalid microcode (MIDI0001.BIN) length\n");
 944           return 1;
 945         }
 946 
 947       /*
 948          *  Download microcode
 949        */
 950 
 951       for (i = 0; i < 8192; i++)
 952         smw_putmem (mp_base, i, smw_ucode[i]);
 953 
 954       /*
 955          *  Verify microcode
 956        */
 957 
 958       for (i = 0; i < 8192; i++)
 959         if (smw_getmem (mp_base, i) != smw_ucode[i])
 960           {
 961             printk ("SM Wave: Microcode verification failed\n");
 962             return 0;
 963           }
 964     }
 965 
 966   control = 0;
 967 #ifdef SMW_SCSI_IRQ
 968   /*
 969      * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
 970      * is disabled by default.
 971      *
 972      * Btw the Zilog 5380 SCSI controller is located at MPU base + 0x10.
 973    */
 974   {
 975     static unsigned char scsi_irq_bits[] =
 976     {0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0};
 977 
 978     control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
 979   }
 980 #endif
 981 
 982 #ifdef SMW_OPL4_ENABLE
 983   /*
 984      *  Make the OPL4 chip visible on the PC bus at 0x380.
 985      *
 986      *  There is no need to enable this feature since VoxWare
 987      *  doesn't support OPL4 yet. Also there is no RAM in SM Wave so
 988      *  enabling OPL4 is pretty useless.
 989    */
 990   control |= 0x10;              /* Uses IRQ12 if bit 0x20 == 0 */
 991   /* control |= 0x20;      Uncomment this if you want to use IRQ7 */
 992 #endif
 993 
 994   outb (control | 0x03, mpu_base + 7);  /* xxxxxx11 restarts */
 995   return 1;
 996 }
 997 
 998 #endif
 999 
1000 static int
1001 initialize_ProSonic16 (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1002 {
1003   int             x;
1004   static unsigned char int_translat[16] =
1005   {0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6}, dma_translat[8] =
1006   {0, 1, 0, 2, 0, 3, 0, 4};
1007 
1008   struct address_info *mpu_config;
1009 
1010   int             mpu_base, mpu_irq;
1011 
1012   if ((mpu_config = sound_getconf (SNDCARD_MPU401)))
1013     {
1014       mpu_base = mpu_config->io_base;
1015       mpu_irq = mpu_config->irq;
1016     }
1017   else
1018     {
1019       mpu_base = mpu_irq = 0;
1020     }
1021 
1022   outb (0xAF, 0x201);           /* ProSonic/Jazz16 wakeup */
1023   for (x = 0; x < 1000; ++x)    /* wait 10 milliseconds */
1024     tenmicrosec ();
1025   outb (0x50, 0x201);
1026   outb ((sbc_base & 0x70) | ((mpu_base & 0x30) >> 4), 0x201);
1027 
1028   if (sb_reset_dsp ())
1029     {                           /* OK. We have at least a SB */
1030 
1031       /* Check the version number of ProSonic (I guess) */
1032 
1033       if (!sb_dsp_command (0xFA))
1034         return 1;
1035       if (get_sb_byte () != 0x12)
1036         return 1;
1037 
1038       if (sb_dsp_command (0xFB) &&      /* set DMA-channels and Interrupts */
1039           sb_dsp_command ((dma_translat[JAZZ_DMA16] << 4) | dma_translat[dma8]) &&
1040       sb_dsp_command ((int_translat[mpu_irq] << 4) | int_translat[sbc_irq]))
1041         {
1042           Jazz16_detected = 1;
1043           if (mpu_base == 0)
1044             printk ("Jazz16: No MPU401 devices configured - MIDI port not initialized\n");
1045 
1046 #ifdef SM_WAVE
1047           if (mpu_base != 0)
1048             if (initialize_smw (mpu_base))
1049               Jazz16_detected = 2;
1050 #endif
1051           sb_dsp_disable_midi ();
1052         }
1053 
1054       return 1;                 /* There was at least a SB */
1055     }
1056   return 0;                     /* No SB or ProSonic16 detected */
1057 }
1058 
1059 #endif /* ifdef JAZZ16  */
1060 
1061 int
1062 sb_dsp_detect (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1063 {
1064   sbc_base = hw_config->io_base;
1065   sbc_irq = hw_config->irq;
1066   sb_osp = hw_config->osp;
1067 
1068   if (sb_dsp_ok)
1069     return 0;                   /*
1070                                  * Already initialized
1071                                  */
1072   dma8 = hw_config->dma;
1073 
1074 #ifdef JAZZ16
1075   dma16 = JAZZ_DMA16;
1076 
1077   if (!initialize_ProSonic16 ())
1078     return 0;
1079 #else
1080   if (!sb_reset_dsp ())
1081     return 0;
1082 #endif
1083 
1084   return 1;                     /*
1085                                  * Detected
1086                                  */
1087 }
1088 
1089 #ifndef EXCLUDE_AUDIO
1090 static struct audio_operations sb_dsp_operations =
1091 {
1092   "SoundBlaster",
1093   NOTHING_SPECIAL,
1094   AFMT_U8,                      /* Just 8 bits. Poor old SB */
1095   NULL,
1096   sb_dsp_open,
1097   sb_dsp_close,
1098   sb_dsp_output_block,
1099   sb_dsp_start_input,
1100   sb_dsp_ioctl,
1101   sb_dsp_prepare_for_input,
1102   sb_dsp_prepare_for_output,
1103   sb_dsp_reset,
1104   sb_dsp_halt_xfer,
1105   NULL,                         /* local_qlen */
1106   NULL,                         /* copy_from_user */
1107   NULL,
1108   NULL,
1109   sb_dsp_trigger
1110 };
1111 
1112 #endif
1113 
1114 long
1115 sb_dsp_init (long mem_start, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1116 {
1117   int             i;
1118 
1119 #ifndef EXCLUDE_SBPRO
1120   int             mixer_type = 0;
1121 
1122 #endif
1123 
1124   sb_osp = hw_config->osp;
1125   sbc_major = sbc_minor = 0;
1126   sb_dsp_command (0xe1);        /*
1127                                  * Get version
1128                                  */
1129 
1130   for (i = 1000; i; i--)
1131     {
1132       if (inb (DSP_DATA_AVAIL) & 0x80)
1133         {                       /*
1134                                  * wait for Data Ready
1135                                  */
1136           if (sbc_major == 0)
1137             sbc_major = inb (DSP_READ);
1138           else
1139             {
1140               sbc_minor = inb (DSP_READ);
1141               break;
1142             }
1143         }
1144     }
1145 
1146   if (sbc_major == 0)
1147     {
1148       printk ("\n\nFailed to get SB version (%x) - possible I/O conflict\n\n",
1149               inb (DSP_DATA_AVAIL));
1150       sbc_major = 1;
1151     }
1152 
1153   if (sbc_major == 2 || sbc_major == 3)
1154     sb_duplex_midi = 1;
1155 
1156   if (sbc_major == 4)
1157     sb16 = 1;
1158 
1159   if (sbc_major == 3 && sbc_minor == 1)
1160     {
1161       int             ess_major = 0, ess_minor = 0;
1162 
1163 /*
1164  * Try to detect ESS chips.
1165  */
1166 
1167       sb_dsp_command (0xe7);    /*
1168                                  * Return identification bytes.
1169                                  */
1170 
1171       for (i = 1000; i; i--)
1172         {
1173           if (inb (DSP_DATA_AVAIL) & 0x80)
1174             {                   /*
1175                                  * wait for Data Ready
1176                                  */
1177               if (ess_major == 0)
1178                 ess_major = inb (DSP_READ);
1179               else
1180                 {
1181                   ess_minor = inb (DSP_READ);
1182                   break;
1183                 }
1184             }
1185         }
1186 
1187       if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80)
1188         printk ("Hmm... Could this be an ESS488 based card (rev %d)\n",
1189                 ess_minor & 0x0f);
1190       else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80)
1191         printk ("Hmm... Could this be an ESS688 based card (rev %d)\n",
1192                 ess_minor & 0x0f);
1193     }
1194 
1195   if (snd_set_irq_handler (sbc_irq, sbintr, "SoundBlaster", sb_osp) < 0)
1196     printk ("sb_dsp: Can't allocate IRQ\n");;
1197 
1198 #ifndef EXCLUDE_SBPRO
1199   if (sbc_major >= 3)
1200     mixer_type = sb_mixer_init (sbc_major);
1201 #else
1202   if (sbc_major >= 3)
1203     printk ("\n\n\n\nNOTE! SB Pro support is required with your soundcard!\n\n\n");
1204 #endif
1205 
1206 
1207 #ifndef EXCLUDE_AUDIO
1208   if (sbc_major >= 3)
1209     {
1210       if (Jazz16_detected)
1211         {
1212           if (Jazz16_detected == 2)
1213             sprintf (sb_dsp_operations.name, "SoundMan Wave %d.%d", sbc_major, sbc_minor);
1214           else
1215             sprintf (sb_dsp_operations.name, "MV Jazz16 %d.%d", sbc_major, sbc_minor);
1216           sb_dsp_operations.format_mask |= AFMT_S16_LE;         /* Hurrah, 16 bits          */
1217         }
1218       else
1219 #ifdef __SGNXPRO__
1220       if (mixer_type == 2)
1221         {
1222           sprintf (sb_dsp_operations.name, "Sound Galaxy NX Pro %d.%d", sbc_major, sbc_minor);
1223         }
1224       else
1225 #endif
1226 
1227       if (sbc_major == 4)
1228         {
1229           sprintf (sb_dsp_operations.name, "SoundBlaster 16 %d.%d", sbc_major, sbc_minor);
1230         }
1231       else
1232         {
1233           sprintf (sb_dsp_operations.name, "SoundBlaster Pro %d.%d", sbc_major, sbc_minor);
1234         }
1235     }
1236   else
1237     {
1238       sprintf (sb_dsp_operations.name, "SoundBlaster %d.%d", sbc_major, sbc_minor);
1239     }
1240 
1241   printk (" <%s>", sb_dsp_operations.name);
1242 
1243 #if !defined(EXCLUDE_SB16) && !defined(EXCLUDE_SBPRO)
1244   if (!sb16)                    /*
1245                                  * There is a better driver for SB16
1246                                  */
1247 #endif
1248     if (num_audiodevs < MAX_AUDIO_DEV)
1249       {
1250         audio_devs[my_dev = num_audiodevs++] = &sb_dsp_operations;
1251         audio_devs[my_dev]->buffsize = DSP_BUFFSIZE;
1252         dma8 = audio_devs[my_dev]->dmachan1 = hw_config->dma;
1253         audio_devs[my_dev]->dmachan2 = -1;
1254         if (sound_alloc_dma (hw_config->dma, "soundblaster"))
1255           printk ("sb_dsp.c: Can't allocate DMA channel\n");
1256 #ifdef JAZZ16
1257         /* Allocate 16 bit dma
1258          */
1259         if (Jazz16_detected != 0)
1260           if (dma16 != dma8)
1261             {
1262               if (sound_alloc_dma (dma16, "Jazz16 16 bit"))
1263                 {
1264                   printk ("Jazz16: Can't allocate 16 bit DMA channel\n");
1265                 }
1266             }
1267 #endif
1268       }
1269     else
1270       printk ("SB: Too many DSP devices available\n");
1271 #else
1272   printk (" <SoundBlaster (configured without audio support)>");
1273 #endif
1274 
1275 #ifndef EXCLUDE_MIDI
1276   if (!midi_disabled && !sb16)  /*
1277                                  * Midi don't work in the SB emulation mode *
1278                                  * of PAS, SB16 has better midi interface
1279                                  */
1280     sb_midi_init (sbc_major);
1281 #endif
1282 
1283   sb_dsp_ok = 1;
1284   return mem_start;
1285 }
1286 
1287 void
1288 sb_dsp_unload (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1289 {
1290   sound_free_dma (dma8);
1291 #ifdef JAZZ16
1292   /* Allocate 16 bit dma
1293    */
1294   if (Jazz16_detected != 0)
1295     if (dma16 != dma8)
1296       {
1297         sound_free_dma (dma16);
1298       }
1299 #endif
1300   snd_release_irq (sbc_irq);
1301 }
1302 
1303 void
1304 sb_dsp_disable_midi (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1305 {
1306   midi_disabled = 1;
1307 }
1308 
1309 
1310 #endif

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