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 ((int) 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 ((int) 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 ((int) 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 *) (int) arg, 8);   /*
 789                                                            * Only 8 bits/sample supported
 790                                                          */
 791       break;
 792 #endif /* ifdef JAZZ16  */
 793 
 794     case SOUND_PCM_WRITE_FILTER:
 795     case SOUND_PCM_READ_FILTER:
 796       return -EINVAL;
 797       break;
 798 
 799     default:;
 800     }
 801 
 802   return -EINVAL;
 803 }
 804 
 805 static void
 806 sb_dsp_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 807 {
 808   unsigned long   flags;
 809 
 810   save_flags (flags);
 811   cli ();
 812 
 813   sb_reset_dsp ();
 814   dsp_speed (dsp_current_speed);
 815   dsp_cleanup ();
 816 
 817   restore_flags (flags);
 818 }
 819 
 820 #endif
 821 
 822 
 823 #ifdef JAZZ16
 824 
 825 /*
 826  * Initialization of a Media Vision ProSonic 16 Soundcard.
 827  * The function initializes a ProSonic 16 like PROS.EXE does for DOS. It sets
 828  * the base address, the DMA-channels, interrupts and enables the joystickport.
 829  *
 830  * Also used by Jazz 16 (same card, different name)
 831  *
 832  * written 1994 by Rainer Vranken
 833  * E-Mail: rvranken@polaris.informatik.uni-essen.de
 834  */
 835 
 836 unsigned int
 837 get_sb_byte (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 838 {
 839   int             i;
 840 
 841   for (i = 1000; i; i--)
 842     if (inb (DSP_DATA_AVAIL) & 0x80)
 843       {
 844         return inb (DSP_READ);
 845       }
 846 
 847   return 0xffff;
 848 }
 849 
 850 #ifdef SM_WAVE
 851 /*
 852  * Logitech Soundman Wave detection and initialization by Hannu Savolainen.
 853  *
 854  * There is a microcontroller (8031) in the SM Wave card for MIDI emulation.
 855  * it's located at address MPU_BASE+4.  MPU_BASE+7 is a SM Wave specific
 856  * control register for MC reset, SCSI, OPL4 and DSP (future expansion)
 857  * address decoding. Otherwise the SM Wave is just a ordinary MV Jazz16
 858  * based soundcard.
 859  */
 860 
 861 static void
 862 smw_putmem (int base, int addr, unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 863 {
 864   unsigned long   flags;
 865 
 866   save_flags (flags);
 867   cli ();
 868 
 869   outb (addr & 0xff, base + 1); /* Low address bits */
 870   outb (addr >> 8, base + 2);   /* High address bits */
 871   outb (val, base);             /* Data */
 872 
 873   restore_flags (flags);
 874 }
 875 
 876 static unsigned char
 877 smw_getmem (int base, int addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 878 {
 879   unsigned long   flags;
 880   unsigned char   val;
 881 
 882   save_flags (flags);
 883   cli ();
 884 
 885   outb (addr & 0xff, base + 1); /* Low address bits */
 886   outb (addr >> 8, base + 2);   /* High address bits */
 887   val = inb (base);             /* Data */
 888 
 889   restore_flags (flags);
 890   return val;
 891 }
 892 
 893 #ifdef SMW_MIDI0001_INCLUDED
 894 #include "smw-midi0001.h"
 895 #else
 896 unsigned char  *smw_ucode = NULL;
 897 int             smw_ucodeLen = 0;
 898 
 899 #endif
 900 
 901 static int
 902 initialize_smw (int mpu_base)
     /* [previous][next][first][last][top][bottom][index][help] */
 903 {
 904 
 905   int             mp_base = mpu_base + 4;       /* Microcontroller base */
 906   int             i;
 907   unsigned char   control;
 908 
 909 
 910   /*
 911      *  Reset the microcontroller so that the RAM can be accessed
 912    */
 913 
 914   control = inb (mpu_base + 7);
 915   outb (control | 3, mpu_base + 7);     /* Set last two bits to 1 (?) */
 916   outb ((control & 0xfe) | 2, mpu_base + 7);    /* xxxxxxx0 resets the mc */
 917 
 918   for (i = 0; i < 300; i++)     /* Wait at least 1ms */
 919     tenmicrosec ();
 920 
 921   outb (control & 0xfc, mpu_base + 7);  /* xxxxxx00 enables RAM */
 922 
 923   /*
 924      *  Detect microcontroller by probing the 8k RAM area
 925    */
 926   smw_putmem (mp_base, 0, 0x00);
 927   smw_putmem (mp_base, 1, 0xff);
 928   tenmicrosec ();
 929 
 930   if (smw_getmem (mp_base, 0) != 0x00 || smw_getmem (mp_base, 1) != 0xff)
 931     {
 932       printk ("\nSM Wave: No microcontroller RAM detected (%02x, %02x)\n",
 933               smw_getmem (mp_base, 0), smw_getmem (mp_base, 1));
 934       return 0;                 /* No RAM */
 935     }
 936 
 937   /*
 938      *  There is RAM so assume it's really a SM Wave
 939    */
 940 
 941   if (smw_ucodeLen > 0)
 942     {
 943       if (smw_ucodeLen != 8192)
 944         {
 945           printk ("\nSM Wave: Invalid microcode (MIDI0001.BIN) length\n");
 946           return 1;
 947         }
 948 
 949       /*
 950          *  Download microcode
 951        */
 952 
 953       for (i = 0; i < 8192; i++)
 954         smw_putmem (mp_base, i, smw_ucode[i]);
 955 
 956       /*
 957          *  Verify microcode
 958        */
 959 
 960       for (i = 0; i < 8192; i++)
 961         if (smw_getmem (mp_base, i) != smw_ucode[i])
 962           {
 963             printk ("SM Wave: Microcode verification failed\n");
 964             return 0;
 965           }
 966     }
 967 
 968   control = 0;
 969 #ifdef SMW_SCSI_IRQ
 970   /*
 971      * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
 972      * is disabled by default.
 973      *
 974      * Btw the Zilog 5380 SCSI controller is located at MPU base + 0x10.
 975    */
 976   {
 977     static unsigned char scsi_irq_bits[] =
 978     {0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0};
 979 
 980     control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
 981   }
 982 #endif
 983 
 984 #ifdef SMW_OPL4_ENABLE
 985   /*
 986      *  Make the OPL4 chip visible on the PC bus at 0x380.
 987      *
 988      *  There is no need to enable this feature since VoxWare
 989      *  doesn't support OPL4 yet. Also there is no RAM in SM Wave so
 990      *  enabling OPL4 is pretty useless.
 991    */
 992   control |= 0x10;              /* Uses IRQ12 if bit 0x20 == 0 */
 993   /* control |= 0x20;      Uncomment this if you want to use IRQ7 */
 994 #endif
 995 
 996   outb (control | 0x03, mpu_base + 7);  /* xxxxxx11 restarts */
 997   return 1;
 998 }
 999 
1000 #endif
1001 
1002 static int
1003 initialize_ProSonic16 (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1004 {
1005   int             x;
1006   static unsigned char int_translat[16] =
1007   {0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6}, dma_translat[8] =
1008   {0, 1, 0, 2, 0, 3, 0, 4};
1009 
1010   struct address_info *mpu_config;
1011 
1012   int             mpu_base, mpu_irq;
1013 
1014   if ((mpu_config = sound_getconf (SNDCARD_MPU401)))
1015     {
1016       mpu_base = mpu_config->io_base;
1017       mpu_irq = mpu_config->irq;
1018     }
1019   else
1020     {
1021       mpu_base = mpu_irq = 0;
1022     }
1023 
1024   outb (0xAF, 0x201);           /* ProSonic/Jazz16 wakeup */
1025   for (x = 0; x < 1000; ++x)    /* wait 10 milliseconds */
1026     tenmicrosec ();
1027   outb (0x50, 0x201);
1028   outb ((sbc_base & 0x70) | ((mpu_base & 0x30) >> 4), 0x201);
1029 
1030   if (sb_reset_dsp ())
1031     {                           /* OK. We have at least a SB */
1032 
1033       /* Check the version number of ProSonic (I guess) */
1034 
1035       if (!sb_dsp_command (0xFA))
1036         return 1;
1037       if (get_sb_byte () != 0x12)
1038         return 1;
1039 
1040       if (sb_dsp_command (0xFB) &&      /* set DMA-channels and Interrupts */
1041           sb_dsp_command ((dma_translat[JAZZ_DMA16] << 4) | dma_translat[dma8]) &&
1042       sb_dsp_command ((int_translat[mpu_irq] << 4) | int_translat[sbc_irq]))
1043         {
1044           Jazz16_detected = 1;
1045           if (mpu_base == 0)
1046             printk ("Jazz16: No MPU401 devices configured - MIDI port not initialized\n");
1047 
1048 #ifdef SM_WAVE
1049           if (mpu_base != 0)
1050             if (initialize_smw (mpu_base))
1051               Jazz16_detected = 2;
1052 #endif
1053           sb_dsp_disable_midi ();
1054         }
1055 
1056       return 1;                 /* There was at least a SB */
1057     }
1058   return 0;                     /* No SB or ProSonic16 detected */
1059 }
1060 
1061 #endif /* ifdef JAZZ16  */
1062 
1063 int
1064 sb_dsp_detect (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1065 {
1066   sbc_base = hw_config->io_base;
1067   sbc_irq = hw_config->irq;
1068   sb_osp = hw_config->osp;
1069 
1070   if (sb_dsp_ok)
1071     return 0;                   /*
1072                                  * Already initialized
1073                                  */
1074   dma8 = hw_config->dma;
1075 
1076 #ifdef JAZZ16
1077   dma16 = JAZZ_DMA16;
1078 
1079   if (!initialize_ProSonic16 ())
1080     return 0;
1081 #else
1082   if (!sb_reset_dsp ())
1083     return 0;
1084 #endif
1085 
1086   return 1;                     /*
1087                                  * Detected
1088                                  */
1089 }
1090 
1091 #ifndef EXCLUDE_AUDIO
1092 static struct audio_operations sb_dsp_operations =
1093 {
1094   "SoundBlaster",
1095   NOTHING_SPECIAL,
1096   AFMT_U8,                      /* Just 8 bits. Poor old SB */
1097   NULL,
1098   sb_dsp_open,
1099   sb_dsp_close,
1100   sb_dsp_output_block,
1101   sb_dsp_start_input,
1102   sb_dsp_ioctl,
1103   sb_dsp_prepare_for_input,
1104   sb_dsp_prepare_for_output,
1105   sb_dsp_reset,
1106   sb_dsp_halt_xfer,
1107   NULL,                         /* local_qlen */
1108   NULL,                         /* copy_from_user */
1109   NULL,
1110   NULL,
1111   sb_dsp_trigger
1112 };
1113 
1114 #endif
1115 
1116 long
1117 sb_dsp_init (long mem_start, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1118 {
1119   int             i;
1120 
1121 #ifndef EXCLUDE_SBPRO
1122   int             mixer_type = 0;
1123 
1124 #endif
1125 
1126   sb_osp = hw_config->osp;
1127   sbc_major = sbc_minor = 0;
1128   sb_dsp_command (0xe1);        /*
1129                                  * Get version
1130                                  */
1131 
1132   for (i = 1000; i; i--)
1133     {
1134       if (inb (DSP_DATA_AVAIL) & 0x80)
1135         {                       /*
1136                                  * wait for Data Ready
1137                                  */
1138           if (sbc_major == 0)
1139             sbc_major = inb (DSP_READ);
1140           else
1141             {
1142               sbc_minor = inb (DSP_READ);
1143               break;
1144             }
1145         }
1146     }
1147 
1148   if (sbc_major == 0)
1149     {
1150       printk ("\n\nFailed to get SB version (%x) - possible I/O conflict\n\n",
1151               inb (DSP_DATA_AVAIL));
1152       sbc_major = 1;
1153     }
1154 
1155   if (sbc_major == 2 || sbc_major == 3)
1156     sb_duplex_midi = 1;
1157 
1158   if (sbc_major == 4)
1159     sb16 = 1;
1160 
1161   if (sbc_major == 3 && sbc_minor == 1)
1162     {
1163       int             ess_major = 0, ess_minor = 0;
1164 
1165 /*
1166  * Try to detect ESS chips.
1167  */
1168 
1169       sb_dsp_command (0xe7);    /*
1170                                  * Return identification bytes.
1171                                  */
1172 
1173       for (i = 1000; i; i--)
1174         {
1175           if (inb (DSP_DATA_AVAIL) & 0x80)
1176             {                   /*
1177                                  * wait for Data Ready
1178                                  */
1179               if (ess_major == 0)
1180                 ess_major = inb (DSP_READ);
1181               else
1182                 {
1183                   ess_minor = inb (DSP_READ);
1184                   break;
1185                 }
1186             }
1187         }
1188 
1189       if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80)
1190         printk ("Hmm... Could this be an ESS488 based card (rev %d)\n",
1191                 ess_minor & 0x0f);
1192       else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80)
1193         printk ("Hmm... Could this be an ESS688 based card (rev %d)\n",
1194                 ess_minor & 0x0f);
1195     }
1196 
1197   if (snd_set_irq_handler (sbc_irq, sbintr, "SoundBlaster", sb_osp) < 0)
1198     printk ("sb_dsp: Can't allocate IRQ\n");;
1199 
1200 #ifndef EXCLUDE_SBPRO
1201   if (sbc_major >= 3)
1202     mixer_type = sb_mixer_init (sbc_major);
1203 #else
1204   if (sbc_major >= 3)
1205     printk ("\n\n\n\nNOTE! SB Pro support is required with your soundcard!\n\n\n");
1206 #endif
1207 
1208 
1209 #ifndef EXCLUDE_AUDIO
1210   if (sbc_major >= 3)
1211     {
1212       if (Jazz16_detected)
1213         {
1214           if (Jazz16_detected == 2)
1215             sprintf (sb_dsp_operations.name, "SoundMan Wave %d.%d", sbc_major, sbc_minor);
1216           else
1217             sprintf (sb_dsp_operations.name, "MV Jazz16 %d.%d", sbc_major, sbc_minor);
1218           sb_dsp_operations.format_mask |= AFMT_S16_LE;         /* Hurrah, 16 bits          */
1219         }
1220       else
1221 #ifdef __SGNXPRO__
1222       if (mixer_type == 2)
1223         {
1224           sprintf (sb_dsp_operations.name, "Sound Galaxy NX Pro %d.%d", sbc_major, sbc_minor);
1225         }
1226       else
1227 #endif
1228 
1229       if (sbc_major == 4)
1230         {
1231           sprintf (sb_dsp_operations.name, "SoundBlaster 16 %d.%d", sbc_major, sbc_minor);
1232         }
1233       else
1234         {
1235           sprintf (sb_dsp_operations.name, "SoundBlaster Pro %d.%d", sbc_major, sbc_minor);
1236         }
1237     }
1238   else
1239     {
1240       sprintf (sb_dsp_operations.name, "SoundBlaster %d.%d", sbc_major, sbc_minor);
1241     }
1242 
1243   printk (" <%s>", sb_dsp_operations.name);
1244 
1245 #if !defined(EXCLUDE_SB16) && !defined(EXCLUDE_SBPRO)
1246   if (!sb16)                    /*
1247                                  * There is a better driver for SB16
1248                                  */
1249 #endif
1250     if (num_audiodevs < MAX_AUDIO_DEV)
1251       {
1252         audio_devs[my_dev = num_audiodevs++] = &sb_dsp_operations;
1253         audio_devs[my_dev]->buffsize = DSP_BUFFSIZE;
1254         dma8 = audio_devs[my_dev]->dmachan1 = hw_config->dma;
1255         audio_devs[my_dev]->dmachan2 = -1;
1256         if (sound_alloc_dma (hw_config->dma, "soundblaster"))
1257           printk ("sb_dsp.c: Can't allocate DMA channel\n");
1258 #ifdef JAZZ16
1259         /* Allocate 16 bit dma
1260          */
1261         if (Jazz16_detected != 0)
1262           if (dma16 != dma8)
1263             {
1264               if (sound_alloc_dma (dma16, "Jazz16 16 bit"))
1265                 {
1266                   printk ("Jazz16: Can't allocate 16 bit DMA channel\n");
1267                 }
1268             }
1269 #endif
1270       }
1271     else
1272       printk ("SB: Too many DSP devices available\n");
1273 #else
1274   printk (" <SoundBlaster (configured without audio support)>");
1275 #endif
1276 
1277 #ifndef EXCLUDE_MIDI
1278   if (!midi_disabled && !sb16)  /*
1279                                  * Midi don't work in the SB emulation mode *
1280                                  * of PAS, SB16 has better midi interface
1281                                  */
1282     sb_midi_init (sbc_major);
1283 #endif
1284 
1285   sb_dsp_ok = 1;
1286   return mem_start;
1287 }
1288 
1289 void
1290 sb_dsp_unload (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1291 {
1292   sound_free_dma (dma8);
1293 #ifdef JAZZ16
1294   /* Allocate 16 bit dma
1295    */
1296   if (Jazz16_detected != 0)
1297     if (dma16 != dma8)
1298       {
1299         sound_free_dma (dma16);
1300       }
1301 #endif
1302   snd_release_irq (sbc_irq);
1303 }
1304 
1305 void
1306 sb_dsp_disable_midi (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1307 {
1308   midi_disabled = 1;
1309 }
1310 
1311 
1312 #endif

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