sb16.c revision 39899
1/* 2 * sound/sb_dsp.c 3 * 4 * driver for the SoundBlaster and clones. 5 * 6 * Copyright 1997,1998 Luigi Rizzo. 7 * 8 * Derived from files in the Voxware 3.5 distribution, 9 * Copyright by Hannu Savolainen 1994, under the same copyright 10 * conditions. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in 19 * the documentation and/or other materials provided with the 20 * distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 * 35 */ 36 37/* 38 * use this as a template file for board-specific drivers. 39 * The next two lines (and the final #endif) are in all drivers: 40 */ 41 42#include <i386/isa/snd/sound.h> 43#if NPCM > 0 44 45/* 46 * Begin with the board-specific include files... 47 */ 48 49#define __SB_MIXER_C__ /* XXX warning... */ 50#include <i386/isa/snd/sbcard.h> 51 52/* 53 * then prototypes of functions which go in the snddev_info 54 * (usually static, unless they are shared by other modules)... 55 */ 56 57static int sb_probe(struct isa_device *dev); 58static int sb_attach(struct isa_device *dev); 59 60static d_open_t sb_dsp_open; 61static d_close_t sb_dsp_close; 62static d_ioctl_t sb_dsp_ioctl; 63static irq_proc_t sb_intr; 64static snd_callback_t sb_callback; 65 66/* 67 * and prototypes for other private functions defined in this module. 68 */ 69 70static void sb_dsp_init(snddev_info *d, struct isa_device *dev); 71static void sb_mix_init(snddev_info *d); 72static int sb_mixer_set(snddev_info *d, int dev, int value); 73static int dsp_speed(snddev_info *d); 74static void sb_mixer_reset(snddev_info *d); 75 76u_int sb_get_byte(int io_base); 77int ess_write(int io_base, u_char reg, int val); 78int ess_read(int io_base, u_char reg); 79 80/* 81 * Then put here the descriptors for the various boards supported 82 * by this module, properly initialized. 83 */ 84 85snddev_info sb_op_desc = { 86 "basic soundblaster", 87 88 SNDCARD_SB, 89 sb_probe, 90 sb_attach, 91 92 sb_dsp_open, 93 sb_dsp_close /* sb_close */, 94 NULL /* use generic sndread */, 95 NULL /* use generic sndwrite */, 96 sb_dsp_ioctl, 97 sndselect, 98 99 sb_intr, 100 sb_callback, 101 102 DSP_BUFFSIZE, /* bufsize */ 103 104 AFMT_STEREO | AFMT_U8, /* audio format */ 105 106} ; 107 108/* 109 * Then the file continues with the body of all functions 110 * directly referenced in the descriptor. 111 */ 112 113/* 114 * the probe routine for the SoundBlaster only consists in 115 * resetting the dsp and testing if it is there. 116 * Version detection etc. will be done at attach time. 117 * 118 * Remember, ISA probe routines are supposed to return the 119 * size of io space used. 120 */ 121 122static int 123sb_probe(struct isa_device *dev) 124{ 125 bzero(&pcm_info[dev->id_unit], sizeof(pcm_info[dev->id_unit]) ); 126 if (dev->id_iobase == -1) { 127 dev->id_iobase = 0x220; 128 BVDDB(printf("sb_probe: no address supplied, try defaults (0x220,0x240)\n");) 129 if (snd_conflict(dev->id_iobase)) 130 dev->id_iobase = 0x240; 131 } 132 if (snd_conflict(dev->id_iobase)) 133 return 0 ; 134 135 if (sb_reset_dsp(dev->id_iobase)) 136 return 16 ; /* the SB uses 16 registers... */ 137 else 138 return 0; 139} 140 141static int 142sb_attach(struct isa_device *dev) 143{ 144 snddev_info *d = &pcm_info[dev->id_unit] ; 145 146 dev->id_alive = 16 ; /* number of io ports */ 147 /* should be already set but just in case... */ 148 sb_dsp_init(d, dev); 149 return 0 ; 150} 151 152/* 153 * here are the main routines from the switches. 154 */ 155 156/* 157 * Unlike MSS, the sb only supports a single open (does not mean 158 * that only a single process is using it, since it can fork 159 * afterwards, or pass the descriptor to another process). 160 * 161 */ 162static int 163sb_dsp_open(dev_t dev, int flags, int mode, struct proc * p) 164{ 165 snddev_info *d; 166 int unit ; 167 168 dev = minor(dev); 169 unit = dev >> 4 ; 170 d = &pcm_info[unit] ; 171 172 DEB(printf("<%s>%d : open\n", d->name, unit)); 173 174 if (d->flags & SND_F_BUSY) { 175 printf("<%s>%d open: device busy\n", d->name, unit); 176 return EBUSY ; 177 } 178 179 d->wsel.si_pid = 0; 180 d->wsel.si_flags = 0; 181 182 d->rsel.si_pid = 0; 183 d->rsel.si_flags = 0; 184 185 d->dbuf_out.total = d->dbuf_out.prev_total = 0 ; 186 d->dbuf_in.total = d->dbuf_in.prev_total = 0 ; 187 188 d->flags = 0 ; 189 d->bd_flags &= ~BD_F_HISPEED ; 190 191 switch ( dev & 0xf ) { 192 case SND_DEV_DSP16 : 193 if ((d->audio_fmt & AFMT_S16_LE) == 0) { 194 printf("sorry, 16-bit not supported on SB %d.%02d\n", 195 (d->bd_id >>8) & 0xff, d->bd_id & 0xff); 196 return ENXIO; 197 } 198 d->play_fmt = d->rec_fmt = AFMT_S16_LE ; 199 break; 200 case SND_DEV_AUDIO : 201 d->play_fmt = d->rec_fmt = AFMT_MU_LAW ; 202 break ; 203 case SND_DEV_DSP : 204 d->play_fmt = d->rec_fmt = AFMT_U8 ; 205 break ; 206 } 207 /* 208 * since the SB is not simmetric, I use the open mode to select 209 * which channel should be privileged, and disable I/O in the 210 * other direction. 211 * In case the board is opened RW, we don't have enough 212 * information on what to do. Temporarily, privilege the 213 * playback channel, which is used more often, and set the other 214 * one to U8. 215 */ 216 if ( (flags & FREAD) == 0) /* opened write only */ 217 d->rec_fmt = 0 ; 218 else if ( (flags & FWRITE) == 0) /* opened read only */ 219 d->play_fmt = 0 ; 220 else /* opened read/write */ 221 d->rec_fmt = (d->play_fmt == AFMT_S16_LE) ? AFMT_U8 : AFMT_S16_LE ; 222 223 d->flags |= SND_F_BUSY ; 224 d->play_speed = d->rec_speed = DSP_DEFAULT_SPEED ; 225 226 if (flags & O_NONBLOCK) 227 d->flags |= SND_F_NBIO ; 228 229 sb_reset_dsp(d->io_base); 230 if (d->bd_flags & BD_F_ESS) 231 sb_cmd(d->io_base, 0xc6 ); /* enable extended ESS mode */ 232 ask_init(d); 233 234 return 0; 235} 236 237static int 238sb_dsp_close(dev_t dev, int flags, int mode, struct proc * p) 239{ 240 int unit; 241 snddev_info *d; 242 u_long s; 243 244 dev = minor(dev); 245 unit = dev >> 4 ; 246 d = &pcm_info[unit] ; 247 248 s = spltty(); 249 d->flags |= SND_F_CLOSING ; 250 splx(s); 251 snd_flush(d); 252 253 sb_cmd(d->io_base, DSP_CMD_SPKOFF ); /* XXX useless ? */ 254 255 d->flags = 0 ; 256 return 0 ; 257} 258 259static int 260sb_dsp_ioctl(dev_t dev, u_long cmd, caddr_t arg, int mode, struct proc * p) 261{ 262 int unit; 263 snddev_info *d; 264 265 dev = minor(dev); 266 unit = dev >> 4 ; 267 d = &pcm_info[unit] ; 268 269 /* 270 * handle mixer calls first. Reads are in the default handler, 271 * so do not bother about them. 272 */ 273 if ( (cmd & MIXER_WRITE(0)) == MIXER_WRITE(0) ) 274 return sb_mixer_set(d, cmd & 0xff, *(int *)arg) ; 275 276 /* 277 * for the remaining functions, use the default handler. 278 * ENOSYS means that the default handler should take care 279 * of implementing the ioctl. 280 */ 281 282 return ENOSYS ; 283} 284 285static void 286sb_intr(int unit) 287{ 288 snddev_info *d = &pcm_info[unit]; 289 int reason = 3, c=1, io_base = d->io_base; 290 291 DEB(printf("got sb_intr for unit %d, flags 0x%08lx\n", unit, d->flags)); 292 293 /* 294 * SB < 4.0 is half duplex and has only 1 bit for int source, 295 * so we fake it. SB 4.x (SB16) has the int source in a separate 296 * register. 297 * The Vibra16X has separate flags for 8 and 16 bit transfers, but 298 * I have no idea how to tell capture from playback interrupts... 299 */ 300#define PLAIN_SB16(x) ( ( (x) & (BD_F_SB16|BD_F_SB16X) ) == BD_F_SB16) 301again: 302 if (d->bd_flags & BD_F_SB16) { 303 c = sb_getmixer(io_base, IRQ_STAT); 304 /* this tells us if the source is 8-bit or 16-bit dma. We 305 * have to check the io channel to map it to read or write... 306 */ 307 reason = 0 ; 308 if ( c & 1 ) { /* 8-bit dma */ 309 if (d->play_fmt == AFMT_U8 || d->play_fmt == AFMT_MU_LAW ) 310 reason |= 1; 311 if (d->rec_fmt == AFMT_U8 || d->rec_fmt == AFMT_MU_LAW ) 312 reason |= 2; 313 } 314 if ( c & 2 ) { /* 16-bit dma */ 315 if (d->play_fmt == AFMT_S16_LE) 316 reason |= 1; 317 if (d->rec_fmt == AFMT_S16_LE) 318 reason |= 2; 319 } 320 } 321 /* XXX previous location of ack... */ 322 DEB(printf("sb_intr, flags 0x%08lx reason %d c 0x%x\n", 323 d->flags, reason, c)); 324 if ( reason & 1 ) { /* possibly a write interrupt */ 325 if ( d->dbuf_out.dl ) 326 dsp_wrintr(d); 327 else { 328 if (PLAIN_SB16(d->bd_flags)) 329 printf("WARNING: wrintr but write DMA inactive!\n"); 330 } 331 } 332 if ( reason & 2 ) { 333 if ( d->dbuf_in.dl ) 334 dsp_rdintr(d); 335 else { 336 if (PLAIN_SB16(d->bd_flags)) 337 printf("WARNING: rdintr but read DMA inactive!\n"); 338 } 339 } 340 if ( c & 2 ) 341 inb(DSP_DATA_AVL16); /* 16-bit int ack */ 342 if (c & 1) 343 inb(DSP_DATA_AVAIL); /* 8-bit int ack */ 344 345 /* 346 * the sb16 might have multiple sources etc. 347 */ 348 if (d->bd_flags & BD_F_SB16 && (c & 3) ) 349 goto again; 350} 351 352/* 353 * device-specific function called back from the dma module. 354 * The reason of the callback is the second argument. 355 * NOTE: during operations, some ioctl can be called to change 356 * settings (e.g. speed, channels, format), and the default 357 * ioctl handler will just record the change and set the 358 * flag SND_F_INIT. The callback routine is in charge of applying 359 * the changes at the next convenient time (typically, at the 360 * start of operations). For full duplex devices, in some cases the 361 * init requires both channels to be idle. 362 */ 363static int 364sb_callback(snddev_info *d, int reason) 365{ 366 int rd = reason & SND_CB_RD ; 367 snd_dbuf *b = (rd) ? & (d->dbuf_in) : & (d->dbuf_out) ; 368 int l = b->dl ; 369 370 switch (reason & SND_CB_REASON_MASK) { 371 case SND_CB_INIT : /* called with int enabled and no pending io */ 372 /* 373 * set the speed 374 */ 375 dsp_speed(d); 376 /* 377 * set the desired DMA blocksize (influences select behaviour) 378 */ 379 snd_set_blocksize(d); 380 /* 381 * since native mulaw is not present, emulate it. 382 */ 383 if ( (d->play_fmt & AFMT_MU_LAW) || (d->rec_fmt & AFMT_MU_LAW) ) 384 d->flags |= SND_F_XLAT8 ; 385 else 386 d->flags &= ~SND_F_XLAT8 ; 387 388 /* 389 * there are too many flavours of SB for my taste... here i try to do 390 * the proper initialization for each one. 391 */ 392 if (PLAIN_SB16(d->bd_flags)) { 393 u_char c, c1 ; 394 395 /* the original SB16 (non-PnP, or PnP, or Vibra16C) 396 * can do full duplex using one 16-bit channel 397 * and one 8-bit channel. It needs to be programmed to 398 * use split format though. 399 * I DON'T do this for the Vibra16X because I have no idea 400 * of what needs to be done there... 401 * 402 * I use the following algorithm: 403 * 1. check which direction(s) are active; 404 * 2. check if we should swap dma channels 405 * 3. check if we can do the swap. 406 */ 407 int swap = 1 ; /* default... */ 408 409 if (d->play_fmt == 0) { 410 /* do whatever the read channel wants */ 411 if ( d->rec_fmt == AFMT_S16_LE && d->dbuf_in.chan > 4 ) 412 swap = 0; 413 if ( d->rec_fmt != AFMT_S16_LE && d->dbuf_in.chan < 4 ) 414 swap = 0; 415 } else { 416 /* privilege the write channel */ 417 if ( d->play_fmt == AFMT_S16_LE && d->dbuf_out.chan > 4 ) 418 swap = 0; 419 if ( d->play_fmt != AFMT_S16_LE && d->dbuf_out.chan < 4 ) 420 swap = 0; 421 if ( d->rec_fmt ) { 422 /* check for possible config errors. 423 * This cannot happen at open time since even in 424 * case of opening rw we privilege the play 425 * channel. 426 */ 427 if (d->rec_fmt == d->play_fmt) { 428 DDB(printf("sorry, read DMA channel unavailable\n")); 429 } 430 } 431 } 432 DEB(printf("sb16: play_fmt %d, rec_fmt %x, swap %d\n", 433 d->play_fmt, d->rec_fmt, swap);) 434 if (swap) { 435 int c = d->dbuf_in.chan ; 436 d->dbuf_in.chan = d->dbuf_out.chan; 437 d->dbuf_out.chan = c ; 438 } 439 } else if (d->bd_flags & BD_F_ESS) { 440 u_char c ; 441 if (d->play_fmt == 0) { 442 /* initialize for record */ 443 static u_char cmd[] = { 444 0x51,0xd0,0x71,0xf4,0x51,0x98,0x71,0xbc 445 }; 446 ess_write(d->io_base, 0xb8, 0x0e); 447 c = ( ess_read(d->io_base, 0xa8) & 0xfc ) | 1 ; 448 if (d->flags & SND_F_STEREO) 449 c++ ; 450 ess_write(d->io_base, 0xa8, c); 451 ess_write(d->io_base, 0xb9, 2); /* 4bytes/transfer */ 452 /* 453 * set format in b6, b7 454 */ 455 } else { 456 /* initialize for play */ 457 static u_char cmd[] = { 458 0x80,0x51,0xd0,0x00,0x71,0xf4, 459 0x80,0x51,0x98,0x00,0x71,0xbc 460 }; 461 } 462 } 463 reset_dbuf(& (d->dbuf_in), SND_CHAN_RD ); 464 reset_dbuf(& (d->dbuf_out), SND_CHAN_WR ); 465 break ; 466 467 case SND_CB_START : /* called with int disabled */ 468 if (d->bd_flags & BD_F_SB16) { 469 u_char c, c1 ; 470 471 if (d->bd_flags & BD_F_SB16X) { 472 /* just a guess: on the Vibra16X, the first 473 * op started takes the first dma channel, 474 * the second one takes the next... 475 * The default is to be ready for play. 476 */ 477 int swap = 0 ; 478 DEB(printf("start %s -- now dma %d:%d\n", 479 rd ? "rd" : "wr", 480 d->dbuf_out.chan, d->dbuf_in.chan);); 481 /* swap only if both channels are idle 482 * play: dl=0, since there is no pause; 483 * rec: rl=0 484 */ 485 if ( rd && d->dbuf_out.dl == 0 && d->dbuf_in.rl == 0 ) { 486 /* must swap channels, but also save dl */ 487 int c = d->dbuf_in.chan ; 488 int dl = d->dbuf_in.dl ; 489 d->dbuf_in.chan = d->dbuf_out.chan; 490 d->dbuf_out.chan = c ; 491 reset_dbuf(& (d->dbuf_in), SND_CHAN_RD ); 492 reset_dbuf(& (d->dbuf_out), SND_CHAN_WR ); 493 d->dbuf_in.dl = dl ; 494 printf("swapped -- now dma %d:%d\n", 495 d->dbuf_out.chan, d->dbuf_in.chan); 496 } 497 } 498 499 /* 500 * XXX note: c1 and l should be set basing on d->rec_fmt, 501 * but there is no choice once a 16 or 8-bit channel 502 * is assigned. This means that if the application 503 * tries to use a bad format, the sound will not be nice. 504 */ 505 if ( b->chan > 4 506 || (rd && d->rec_fmt == AFMT_S16_LE) 507 || (!rd && d->play_fmt == AFMT_S16_LE) 508 ) { 509 c = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_DMA16 ; 510 c1 = DSP_F16_SIGNED ; 511 l /= 2 ; 512 } else { 513 c = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_DMA8 ; 514 c1 = 0 ; 515 } 516 c |= (rd) ? DSP_F16_ADC : DSP_F16_DAC ; 517 if (d->flags & SND_F_STEREO) 518 c1 |= DSP_F16_STEREO ; 519 520 sb_cmd(d->io_base, c ); 521 sb_cmd3(d->io_base, c1 , l - 1) ; 522 } else if (d->bd_flags & BD_F_ESS) { 523 /* XXX this code is still incomplete */ 524 sb_cmd2(d->io_base, 0xb8, rd ? 4 : 0xe ) ; /* auto dma */ 525 sb_cmd2(d->io_base, 0xa8, d->flags & SND_F_STEREO ? 1 : 2) ; 526 sb_cmd2(d->io_base, 0xb9, 2) ; /* demand dma */ 527 } else { /* SBPro -- stereo not supported */ 528 u_char c ; 529 if (!rd) 530 sb_cmd(d->io_base, DSP_CMD_SPKON); 531 /* code for the SB2 and SB3, only MONO */ 532 if (d->bd_flags & BD_F_HISPEED) 533 c = (rd) ? 0x98 : 0x90 ; 534 else 535 c = (rd) ? 0x2c : 0x1c ; 536 if (d->flags & SND_F_STEREO) 537 sb_setmixer(d->io_base, 0xe, 2 ); 538 else 539 sb_setmixer(d->io_base, 0xe, 0 ); 540 /* 541 * some ESS extensions -- they can do 16 bits 542 */ 543 if ( (rd && d->rec_fmt == AFMT_S16_LE) || 544 (!rd && d->play_fmt == AFMT_S16_LE) ) { 545 c |= 1; 546 l /= 2 ; 547 } 548 sb_cmd3(d->io_base, 0x48 , l - 1) ; 549 sb_cmd(d->io_base, c ) ; 550 } 551 break; 552 553 case SND_CB_ABORT : /* XXX */ 554 case SND_CB_STOP : 555 { 556 int cmd = DSP_CMD_DMAPAUSE_8 ; /* default: halt 8 bit chan */ 557 if ( b->chan > 4 558 || (rd && d->rec_fmt == AFMT_S16_LE) 559 || (!rd && d->play_fmt == AFMT_S16_LE) 560 ) 561 cmd = DSP_CMD_DMAPAUSE_16 ; 562 if (d->bd_flags & BD_F_HISPEED) { 563 sb_reset_dsp(d->io_base); 564 if (d->bd_flags & BD_F_ESS) 565 sb_cmd(d->io_base, 0xc6 ); /* enable extended ESS mode */ 566 d->flags |= SND_F_INIT ; 567 } else { 568 sb_cmd(d->io_base, cmd); /* pause dma. */ 569 /* 570 * The above seems to have the undocumented side effect of 571 * blocking the other side as well. If the other 572 * channel was active (SB16) I have to re-enable it :( 573 */ 574 if ( (rd && d->dbuf_out.dl) || 575 (!rd && d->dbuf_in.dl) ) 576 sb_cmd(d->io_base, cmd == DSP_CMD_DMAPAUSE_8 ? 577 0xd6 : 0xd4); /* continue other dma */ 578 } 579 if (d->bd_flags & BD_F_SB16X) { 580 /* restore possible swapped channels. 581 * The default is to be ready for play. 582 * XXX right now, it kills all input on overflow 583 */ 584 if ( rd && d->dbuf_out.dl == 0 ) { 585 /* must swap channels ? */ 586 int c = d->dbuf_in.chan ; 587 d->dbuf_in.chan = d->dbuf_out.chan; 588 d->dbuf_out.chan = c ; 589 reset_dbuf(& (d->dbuf_in), SND_CHAN_RD ); 590 reset_dbuf(& (d->dbuf_out), SND_CHAN_WR ); 591 printf("restored -- now dma %d:%d\n", 592 d->dbuf_out.chan, d->dbuf_in.chan); 593 } 594 } 595 } 596 DEB( sb_cmd(d->io_base, DSP_CMD_SPKOFF) ); /* speaker off */ 597 break ; 598 599 } 600 return 0 ; 601} 602 603/* 604 * The second part of the file contains all functions specific to 605 * the board and (usually) not exported to other modules. 606 */ 607 608int 609sb_reset_dsp(int io_base) 610{ 611 int loopc; 612 613 outb(io_base + SBDSP_RST, 3); 614 DELAY(100); 615 outb(io_base + SBDSP_RST, 0); 616 for (loopc = 0; loopc<100 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++) 617 DELAY(30); 618 619 if (inb(DSP_READ) != 0xAA) { 620 DEB(printf("sb_reset_dsp 0x%x failed\n", io_base)); 621 return 0; /* Sorry */ 622 } 623 return 1; 624} 625 626/* 627 * only used in sb_attach from here. 628 */ 629 630static void 631sb_dsp_init(snddev_info *d, struct isa_device *dev) 632{ 633 int i, x; 634 char *fmt = NULL ; 635 int io_base = dev->id_iobase ; 636 637 d->bd_id = 0 ; 638 639 sb_reset_dsp(io_base); 640 sb_cmd(io_base, DSP_CMD_GETVER); /* Get version */ 641 642 for (i = 10000; i; i--) { /* perhaps wait longer on a fast machine ? */ 643 if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */ 644 if ( (d->bd_id & 0xff00) == 0) 645 d->bd_id = inb(DSP_READ) << 8; /* major */ 646 else { 647 d->bd_id |= inb(DSP_READ); /* minor */ 648 break; 649 } 650 } else 651 DELAY(20); 652 } 653 654 /* 655 * now do various initializations depending on board id. 656 */ 657 658 fmt = "SoundBlaster %d.%d" ; /* default */ 659 660 switch ( d->bd_id >> 8 ) { 661 case 0 : 662 printf("\n\nFailed to get SB version (%x) - possible I/O conflict\n\n", 663 inb(DSP_DATA_AVAIL)); 664 d->bd_id = 0x100; 665 case 1 : /* old sound blaster has nothing... */ 666 break ; 667 668 case 2 : 669 d->dbuf_in.chan = d->dbuf_out.chan ; /* half duplex */ 670 d->bd_flags |= BD_F_DUP_MIDI ; 671 672 if (d->bd_id == 0x200) 673 break ; /* no mixer on the 2.0 */ 674 d->bd_flags &= ~BD_F_MIX_MASK ; 675 d->bd_flags |= BD_F_MIX_CT1335 ; 676 677 break ; 678 case 4 : 679 fmt = "SoundBlaster 16 %d.%d"; 680 d->audio_fmt |= AFMT_FULLDUPLEX | AFMT_WEIRD | AFMT_S8 | AFMT_S16_LE; 681 d->bd_flags |= BD_F_SB16; 682 d->bd_flags &= ~BD_F_MIX_MASK ; 683 d->bd_flags |= BD_F_MIX_CT1745 ; 684 685 /* soft irq/dma configuration */ 686 x = -1 ; 687 if (d->irq == 5) x = 2; 688 else if (d->irq == 7) x = 4; 689 else if (d->irq == 9) x = 1; 690 else if (d->irq == 10) x = 8; 691 if (x == -1) 692 printf("<%s>%d: bad irq %d (only 5,7,9,10 allowed)\n", 693 d->name, dev->id_unit, d->irq); 694 else 695 sb_setmixer(io_base, IRQ_NR, x); 696 if (d->dbuf_out.chan == d->dbuf_in.chan) { 697 printf("WARNING: sb: misconfigured secondary DMA channel\n"); 698 } 699 sb_setmixer(io_base, DMA_NR, (1 << d->dbuf_out.chan) | (1 << d->dbuf_in.chan)); 700 break ; 701 702 case 3 : 703 d->dbuf_in.chan = d->dbuf_out.chan ; /* half duplex */ 704 fmt = "SoundBlaster Pro %d.%d"; 705 d->bd_flags |= BD_F_DUP_MIDI ; 706 d->bd_flags &= ~BD_F_MIX_MASK ; 707 d->bd_flags |= BD_F_MIX_CT1345 ; 708 if (d->bd_id == 0x301) { 709 int ess_major = 0, ess_minor = 0; 710 711 /* 712 * Try to detect ESS chips. 713 */ 714 715 sb_cmd(io_base, DSP_CMD_GETID); /* Return ident. bytes. */ 716 717 for (i = 1000; i; i--) { 718 if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */ 719 if (ess_major == 0) 720 ess_major = inb(DSP_READ); 721 else { 722 ess_minor = inb(DSP_READ); 723 break; 724 } 725 } else 726 DELAY(20); 727 } 728 729 if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80) { 730 /* the ESS488 can be treated as an SBPRO */ 731 printf("ESS488 (rev %d)\n", ess_minor & 0x0f); 732 break ; 733 } else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80) { 734 int rev = ess_minor & 0xf ; 735 if ( rev >= 8 ) 736 printf("ESS1868 (rev %d)\n", rev); 737 else 738 printf("ESS688 (rev %d)\n", rev); 739 /* d->audio_fmt |= AFMT_S16_LE; */ /* not yet... */ 740 break ; /* XXX */ 741 } else { 742 printf("Unknown card 0x%x 0x%x -- hope it is SBPRO\n", 743 ess_major, ess_minor); 744 break ; 745 } 746 } 747 748 } 749 750 sprintf(d->name, fmt, (d->bd_id >> 8) &0xff, d->bd_id & 0xff); 751 752 sb_mix_init(d); 753} 754 755static void 756sb_mix_init(snddev_info *d) 757{ 758 switch (d->bd_flags & BD_F_MIX_MASK) { 759 case BD_F_MIX_CT1345 : /* SB 3.0 has 1345 mixer */ 760 761 d->mix_devs = SBPRO_MIXER_DEVICES ; 762 d->mix_rec_devs = SBPRO_RECORDING_DEVICES ; 763 d->mix_recsrc = SOUND_MASK_MIC ; 764 765 sb_setmixer(d->io_base, 0, 1 ); /* reset mixer */ 766 sb_setmixer(d->io_base, MIC_VOL , 0x6 ); /* mic volume max */ 767 sb_setmixer(d->io_base, RECORD_SRC , 0x0 ); /* mic source */ 768 sb_setmixer(d->io_base, FM_VOL , 0x0 ); /* no midi */ 769 break ; 770 771 case BD_F_MIX_CT1745 : /* SB16 mixer ... */ 772 773 d->mix_devs = SB16_MIXER_DEVICES ; 774 d->mix_rec_devs = SB16_RECORDING_DEVICES ; 775 d->mix_recsrc = SOUND_MASK_MIC ; 776 } 777 sb_mixer_reset(d); 778} 779 780/* 781 * Common code for the midi and pcm functions 782 * 783 * sb_cmd write a single byte to the CMD port. 784 * sb_cmd2 write a CMD + 1 byte arg 785 * sb_cmd3 write a CMD + 2 byte arg 786 * sb_get_byte returns a single byte from the DSP data port 787 * 788 * ess_write is actually sb_cmd2 789 * ess_read access ext. regs via sb_cmd(0xc0, reg) followed by sb_get_byte 790 */ 791 792int 793sb_cmd(int io_base, u_char val) 794{ 795 int i; 796 797 for (i = 0; i < 1000 ; i++) { 798 if ((inb(io_base + SBDSP_STATUS) & 0x80) == 0) { 799 outb(io_base + SBDSP_CMD, val); 800 return 1; 801 } 802 if (i > 10) 803 DELAY (i > 100 ? 1000 : 10 ); 804 } 805 806 printf("SoundBlaster: DSP Command(0x%02x) timeout. IRQ conflict ?\n", val); 807 return 0; 808} 809 810int 811sb_cmd3(int io_base, u_char cmd, int val) 812{ 813 if (sb_cmd(io_base, cmd)) { 814 sb_cmd(io_base, val & 0xff ); 815 sb_cmd(io_base, (val>>8) & 0xff ); 816 return 1 ; 817 } else 818 return 0; 819} 820 821int 822sb_cmd2(int io_base, u_char cmd, int val) 823{ 824 if (sb_cmd(io_base, cmd)) { 825 sb_cmd(io_base, val & 0xff ); 826 return 1 ; 827 } else 828 return 0; 829} 830 831/* 832 * in the SB, there is a set of indirect "mixer" registers with 833 * address at offset 4, data at offset 5 834 */ 835void 836sb_setmixer(int io_base, u_int port, u_int value) 837{ 838 u_long flags; 839 840 flags = spltty(); 841 outb(io_base + 4, (u_char) (port & 0xff)); /* Select register */ 842 DELAY(10); 843 outb(io_base + 5, (u_char) (value & 0xff)); 844 DELAY(10); 845 splx(flags); 846} 847 848int 849sb_getmixer(int io_base, u_int port) 850{ 851 int val; 852 u_long flags; 853 854 flags = spltty(); 855 outb(io_base + SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */ 856 DELAY(10); 857 val = inb(io_base + SB_MIX_DATA); 858 DELAY(10); 859 splx(flags); 860 861 return val; 862} 863 864u_int 865sb_get_byte(int io_base) 866{ 867 int i; 868 869 for (i = 1000; i; i--) 870 if (inb(DSP_DATA_AVAIL) & 0x80) 871 return inb(DSP_READ); 872 else 873 DELAY(20); 874 return 0xffff; 875} 876 877int 878ess_write(int io_base, u_char reg, int val) 879{ 880 return sb_cmd2(io_base, reg, val); 881} 882 883int 884ess_read(int io_base, u_char reg) 885{ 886 if (!sb_cmd(io_base, 0xc0) || !sb_cmd(io_base, reg) ) 887 return 0xffff ; 888 return sb_get_byte(io_base); 889} 890 891 892/* 893 * various utility functions for the DSP 894 */ 895 896/* 897 * dsp_speed updates the speed setting from the descriptor. make sure 898 * it is called at spltty(). 899 * Besides, it takes care of stereo setting. 900 */ 901static int 902dsp_speed(snddev_info *d) 903{ 904 u_char tconst; 905 u_long flags; 906 int max_speed = 44100, speed = d->play_speed ; 907 908 /* 909 * special code for the SB16 910 */ 911 if (d->bd_flags & BD_F_SB16) { 912 RANGE (speed, 5000, 45000); 913 d->play_speed = d->rec_speed = speed ; 914 sb_cmd(d->io_base, 0x41); 915 sb_cmd(d->io_base, d->play_speed >> 8 ); 916 sb_cmd(d->io_base, d->play_speed & 0xff ); 917 sb_cmd(d->io_base, 0x42); 918 sb_cmd(d->io_base, d->rec_speed >> 8 ); 919 sb_cmd(d->io_base, d->rec_speed & 0xff ); 920 return speed ; 921 } 922 923 /* 924 * special code for the ESS ... 925 */ 926 if (d->bd_flags & BD_F_ESS) { 927 int t; 928 RANGE (speed, 5000, 49000); 929 if (speed > 22000) { 930 t = (795500 + speed / 2) / speed; 931 speed = (795500 + t / 2) / t ; 932 t = (256 - t ) | 0x80 ; 933 } else { 934 t = (397700 + speed / 2) / speed; 935 speed = (397700 + t / 2) / t ; 936 t = 128 - t ; 937 } 938 ess_write(d->io_base, 0xa1, t); /* set time constant */ 939 d->play_speed = d->rec_speed = speed ; 940 speed = (speed * 9 ) / 20 ; 941 t = 256-7160000/(speed*82); 942 ess_write(d->io_base,0xa2,t); 943 return speed ; 944 } 945 946 /* 947 * This is code for the SB3.x and lower. 948 * Only some models can do stereo, and only if not 949 * simultaneously using midi. 950 * At the moment we do not support either... 951 */ 952#if 0 953 d->flags &= ~SND_F_STEREO; 954#endif 955 956 /* 957 * here enforce speed limitations. 958 */ 959 if (d->bd_id <= 0x200) 960 max_speed = 22050; /* max 22050 on SB 1.X */ 961 962 /* 963 * SB models earlier than SB Pro have low limit for the 964 * input rate. Note that this is only for input, but since 965 * we do not support separate values for rec & play.... 966 */ 967 if (d->bd_id <= 0x200) 968 max_speed = 13000; 969 else if (d->bd_id < 0x300) 970 max_speed = 15000; 971 972 RANGE(speed, 4000, max_speed); 973 974 if (d->flags & SND_F_STEREO) /* really unused right now... */ 975 speed *= 2; 976 977 /* 978 * Now the speed should be valid. Compute the value to be 979 * programmed into the board. 980 */ 981 982 if (speed > 22050) { /* High speed mode on 2.01/3.xx */ 983 int tmp; 984 985 tconst = (u_char) ((65536 - ((256000000 + speed / 2) / speed)) >> 8) ; 986 d->bd_flags |= BD_F_HISPEED ; 987 988 flags = spltty(); 989 sb_cmd2(d->io_base, 0x40, tconst); /* set time constant */ 990 splx(flags); 991 992 tmp = 65536 - (tconst << 8); 993 speed = (256000000 + tmp / 2) / tmp; 994 } else { 995 int tmp; 996 997 d->bd_flags &= ~BD_F_HISPEED ; 998 tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff; 999 1000 flags = spltty(); 1001 sb_cmd2(d->io_base, 0x40, tconst); /* set time constant */ 1002 splx(flags); 1003 1004 tmp = 256 - tconst; 1005 speed = (1000000 + tmp / 2) / tmp; 1006 } 1007 1008 if (d->flags & SND_F_STEREO) /* really unused right now... */ 1009 speed /= 2; 1010 1011 d->play_speed = d->rec_speed = speed; 1012 return speed; 1013} 1014 1015/* 1016 * mixer support, originally in sb_mixer.c 1017 */ 1018 1019static void 1020sb_set_recsrc(snddev_info *d, int mask) 1021{ 1022 u_char recdev ; 1023 1024 mask &= d->mix_rec_devs; 1025 switch (d->bd_flags & BD_F_MIX_MASK) { 1026 case BD_F_MIX_CT1345 : 1027 if (mask == SOUND_MASK_LINE) 1028 recdev = 6 ; 1029 else if (mask == SOUND_MASK_CD) 1030 recdev = 2 ; 1031 else { /* default: mic */ 1032 mask = SOUND_MASK_MIC ; 1033 recdev = 0 ; 1034 } 1035 sb_setmixer(d->io_base, RECORD_SRC, 1036 recdev | (sb_getmixer(d->io_base, RECORD_SRC) & ~7 )); 1037 break ; 1038 case BD_F_MIX_CT1745 : /* sb16 */ 1039 if (mask == 0) 1040 mask = SOUND_MASK_MIC ; /* XXX For compatibility. Bug ? */ 1041 recdev = 0 ; 1042 if (mask & SOUND_MASK_MIC) 1043 recdev |= 1 ; 1044 if (mask & SOUND_MASK_CD) 1045 recdev |= 6 ; /* l+r cd */ 1046 if (mask & SOUND_MASK_LINE) 1047 recdev |= 0x18 ; /* l+r line */ 1048 if (mask & SOUND_MASK_SYNTH) 1049 recdev |= 0x60 ; /* l+r midi */ 1050 sb_setmixer(d->io_base, SB16_IMASK_L, recdev); 1051 sb_setmixer(d->io_base, SB16_IMASK_R, recdev); 1052 /* 1053 * since the same volume controls apply to the input and 1054 * output sections, the best approach to have a consistent 1055 * behaviour among cards would be to disable the output path 1056 * on devices which are used to record. 1057 * However, since users like to have feedback, we only disable 1058 * the mike -- permanently. 1059 */ 1060 sb_setmixer(d->io_base, SB16_OMASK, 0x1f & ~1); 1061 break ; 1062 } 1063 d->mix_recsrc = mask; 1064} 1065 1066static void 1067sb_mixer_reset(snddev_info *d) 1068{ 1069 int i; 1070 1071 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) 1072 sb_mixer_set(d, i, levels[i]); 1073 if (d->bd_flags & BD_F_SB16) { 1074 sb_setmixer(d->io_base, 0x3c, 0x1f); /* make all output active */ 1075 sb_setmixer(d->io_base, 0x3d, 0); /* make all inputs-l off */ 1076 sb_setmixer(d->io_base, 0x3e, 0); /* make all inputs-r off */ 1077 } 1078 sb_set_recsrc(d, SOUND_MASK_MIC); 1079} 1080 1081static int 1082sb_mixer_set(snddev_info *d, int dev, int value) 1083{ 1084 int left = value & 0x000000ff; 1085 int right = (value & 0x0000ff00) >> 8; 1086 int regoffs; 1087 u_char val; 1088 mixer_tab *iomap; 1089 1090#ifdef JAZZ16 1091 if (d->bd_flags & BD_F_JAZZ16 && d->bd_flags & BD_F_JAZZ16_2) 1092 return smw_mixer_set(dev, value); 1093#endif 1094 1095 if (dev == SOUND_MIXER_RECSRC) { 1096 sb_set_recsrc(d, value); 1097 return 0 ; 1098 } 1099 if (left > 100) 1100 left = 100; 1101 if (right > 100) 1102 right = 100; 1103 1104 if (dev > 31) 1105 return EINVAL ; 1106 1107 if (!(d->mix_devs & (1 << dev))) /* Not supported */ 1108 return EINVAL; 1109 1110 switch ( d->bd_flags & BD_F_MIX_MASK ) { 1111 default: 1112 /* mixer unknown, fail... */ 1113 return EINVAL ;/* XXX change this */ 1114 case BD_F_MIX_CT1345 : 1115 iomap = &sbpro_mix ; 1116 break; 1117 case BD_F_MIX_CT1745 : 1118 iomap = &sb16_mix ; 1119 break; 1120 /* XXX how about the SG NX Pro, iomap = sgnxpro_mix */ 1121 } 1122 regoffs = (*iomap)[dev][LEFT_CHN].regno; 1123 if (regoffs == 0) 1124 return EINVAL; 1125 1126 val = sb_getmixer(d->io_base, regoffs); 1127 1128 change_bits(iomap, &val, dev, LEFT_CHN, left); 1129 1130 d->mix_levels[dev] = left | (left << 8); 1131 1132 if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) { /* Change register */ 1133 sb_setmixer(d->io_base, regoffs, val); /* Save the old one */ 1134 regoffs = (*iomap)[dev][RIGHT_CHN].regno; 1135 1136 if (regoffs == 0) 1137 return 0 ; /* Just left channel present */ 1138 1139 val = sb_getmixer(d->io_base, regoffs); /* Read the new one */ 1140 } 1141 change_bits(iomap, &val, dev, RIGHT_CHN, right); 1142 1143 sb_setmixer(d->io_base, regoffs, val); 1144 1145 d->mix_levels[dev] = left | (right << 8); 1146 return 0 ; /* ok */ 1147} 1148 1149/* 1150 * now support for some PnP boards. 1151 */ 1152 1153#if NPNP > 0 1154static char *ess1868_probe(u_long csn, u_long vend_id); 1155static void ess1868_attach(u_long csn, u_long vend_id, char *name, 1156 struct isa_device *dev); 1157 1158static struct pnp_device ess1868 = { 1159 "ESS1868", 1160 ess1868_probe, 1161 ess1868_attach, 1162 &nsnd, /* use this for all sound cards */ 1163 &tty_imask /* imask */ 1164}; 1165DATA_SET (pnpdevice_set, ess1868); 1166 1167static char * 1168ess1868_probe(u_long csn, u_long vend_id) 1169{ 1170 /* 1171 * pnp X 1 os enable drq0 3 irq0 12 port0 0x240 1172 */ 1173 if (vend_id == 0x68187316) { 1174 struct pnp_cinfo d ; 1175 read_pnp_parms ( &d , 1 ) ; 1176 if (d.enable == 0) { 1177 printf("This is an ESS1868, but LDN 1 is disabled\n"); 1178 return NULL; 1179 } 1180 return "ESS1868" ; 1181 } 1182 return NULL ; 1183} 1184 1185static void 1186ess1868_attach(u_long csn, u_long vend_id, char *name, 1187 struct isa_device *dev) 1188{ 1189 struct pnp_cinfo d ; 1190 snddev_info tmp_d ; /* patched copy of the basic snddev_info */ 1191 int the_irq = 0 ; 1192 1193 tmp_d = sb_op_desc; 1194 snddev_last_probed = &tmp_d; 1195 1196#if 0 1197 read_pnp_parms ( &d , 3 ); /* disable LDN 3 */ 1198 d.port[0] = 0 ; 1199 d.enable = 0 ; 1200 write_pnp_parms ( &d , 3 ); 1201 1202 read_pnp_parms ( &d , 2 ); /* disable LDN 2 */ 1203 d.port[0] = 0 ; 1204 d.enable = 0 ; 1205 write_pnp_parms ( &d , 2 ); 1206 read_pnp_parms ( &d , 0 ); /* read config base */ 1207 tmp_d.conf_base = d.port[0]; 1208 write_pnp_parms ( &d , 0 ); 1209#endif 1210 1211 read_pnp_parms ( &d , 1 ) ; 1212 dev->id_iobase = d.port[0]; 1213 d.port[1] = 0 ; 1214 d.port[2] = 0 ; 1215 write_pnp_parms ( &d , 1 ); 1216 enable_pnp_card(); 1217 1218 dev->id_drq = d.drq[0] ; /* primary dma */ 1219 dev->id_irq = (1 << d.irq[0] ) ; 1220 dev->id_intr = pcmintr ; 1221 dev->id_flags = 0 /* DV_F_DUAL_DMA | (d.drq[1] ) */; 1222 1223 snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */ 1224 pcmattach(dev); 1225} 1226 1227/* 1228 * A driver for some SB16pnp and compatibles... 1229 * 1230 * Avance Asound 100 -- 0x01009305 1231 * Avance Logic ALS100+ -- 0x10019305 1232 * xxx -- 0x2b008c0e 1233 * 1234 */ 1235 1236static char *sb16pnp_probe(u_long csn, u_long vend_id); 1237static void sb16pnp_attach(u_long csn, u_long vend_id, char *name, 1238 struct isa_device *dev); 1239 1240static struct pnp_device sb16pnp = { 1241 "SB16pnp", 1242 sb16pnp_probe, 1243 sb16pnp_attach, 1244 &nsnd, /* use this for all sound cards */ 1245 &tty_imask /* imask */ 1246}; 1247DATA_SET (pnpdevice_set, sb16pnp); 1248 1249static char * 1250sb16pnp_probe(u_long csn, u_long vend_id) 1251{ 1252 char *s = NULL ; 1253 1254 /* 1255 * The SB16/AWExx cards seem to differ in the fourth byte of 1256 * the vendor id, so I have just masked it for the time being... 1257 * Reported values are: 1258 * SB16 Value PnP: 0x2b008c0e 1259 * SB AWExx PnP: 0x39008c0e 0x9d008c0e 0xc3008c0e 1260 * Vibra16X: 0xf0008c0e 1261 */ 1262 if (vend_id == 0xf0008c0e) 1263 s = "Vibra16X" ; 1264 else if ( (vend_id & 0xffffff) == (0x9d008c0e & 0xffffff) ) 1265 s = "SB16 PnP"; 1266 else if (vend_id == 0x01009305) 1267 s = "Avance Asound 100" ; 1268 else if (vend_id == 0x10019305) 1269 s = "Avance Logic 100+" ; /* Vibra16X-class */ 1270 if (s) { 1271 struct pnp_cinfo d; 1272 read_pnp_parms(&d, 0); 1273 if (d.enable == 0) { 1274 printf("This is a %s, but LDN 0 is disabled\n", s); 1275 return NULL ; 1276 } 1277 return s ; 1278 } 1279 return NULL ; 1280} 1281 1282static void 1283sb16pnp_attach(u_long csn, u_long vend_id, char *name, 1284 struct isa_device *dev) 1285{ 1286 struct pnp_cinfo d ; 1287 snddev_info tmp_d ; /* patched copy of the basic snddev_info */ 1288 1289 tmp_d = sb_op_desc; 1290 snddev_last_probed = &tmp_d; 1291 1292 read_pnp_parms ( &d , 0 ) ; 1293 d.port[1] = 0 ; /* only the first address is used */ 1294 dev->id_iobase = d.port[0]; 1295 tmp_d.synth_base = d.port[2]; 1296 write_pnp_parms ( &d , 0 ); 1297 enable_pnp_card(); 1298 1299 dev->id_drq = d.drq[0] ; /* primary dma */ 1300 dev->id_irq = (1 << d.irq[0] ) ; 1301 dev->id_intr = pcmintr ; 1302 dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ; 1303 1304 pcm_info[dev->id_unit] = tmp_d; /* pcm_info[] will be reinitialized after */ 1305 snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */ 1306 1307 if (vend_id == 0x10019305 || vend_id == 0xf0008c0e) { 1308 /* 1309 * XXX please add here the vend_id for other vibra16X cards... 1310 * And remember, must change tmp_d, not 1311 */ 1312 tmp_d.bd_flags |= BD_F_SB16X ; 1313 } 1314 pcmattach(dev); 1315} 1316#endif /* NPNP */ 1317 1318#endif 1319