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

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