Home | History | Annotate | Line # | Download | only in isa
sbdsp.c revision 1.5
      1 /*	$NetBSD: sbdsp.c,v 1.5 1995/03/25 00:01:00 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1991-1993 Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the Computer Systems
     18  *	Engineering Group at Lawrence Berkeley Laboratory.
     19  * 4. Neither the name of the University nor of the Laboratory may be used
     20  *    to endorse or promote products derived from this software without
     21  *    specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	$Id: sbdsp.c,v 1.5 1995/03/25 00:01:00 mycroft Exp $
     36  */
     37 /*
     38  * SoundBlaster Pro code provided by John Kohl, based on lots of
     39  * information he gleaned from Steve Haehnichen <steve (at) vigra.com>'s
     40  * SBlast driver for 386BSD and DOS driver code from Daniel Sachs
     41  * <sachs (at) meibm15.cen.uiuc.edu>.
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/errno.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/syslog.h>
     49 #include <sys/device.h>
     50 #include <sys/proc.h>
     51 #include <sys/buf.h>
     52 #include <vm/vm.h>
     53 
     54 #include <machine/cpu.h>
     55 #include <machine/pio.h>
     56 
     57 #include <sys/audioio.h>
     58 #include <dev/audio_if.h>
     59 
     60 #include <i386/isa/isavar.h>
     61 #include <i386/isa/dmavar.h>
     62 #include <i386/isa/icu.h>
     63 
     64 #include <i386/isa/sbreg.h>
     65 #include <i386/isa/sbdspvar.h>
     66 
     67 #ifdef DEBUG
     68 extern void Dprintf __P((const char *, ...));
     69 
     70 #define DPRINTF(x)	if (sbdspdebug) printf x
     71 int	sbdspdebug = 0;
     72 #else
     73 #define DPRINTF(x)
     74 #endif
     75 
     76 #ifndef NEWCONFIG
     77 #define at_dma(flags, ptr, cc, chan)	isa_dmastart(flags, ptr, cc, chan)
     78 #endif
     79 
     80 #ifndef SBDSP_NPOLL
     81 #define SBDSP_NPOLL 3000
     82 #endif
     83 
     84 struct {
     85 	int wdsp;
     86 	int rdsp;
     87 	int wmidi;
     88 } sberr;
     89 
     90 #ifdef DEBUG
     91 void
     92 sb_printsc(struct sbdsp_softc *sc)
     93 {
     94 	int i;
     95 
     96 	printf("open %d dmachan %d iobase %x locked %d\n", sc->sc_open, sc->sc_drq,
     97 		sc->sc_iobase, sc->sc_locked);
     98 	printf("hispeed %d irate %d orate %d encoding %x\n",
     99 		sc->sc_adacmode, sc->sc_irate, sc->sc_orate, sc->encoding);
    100 	printf("outport %d inport %d spkron %d nintr %d\n",
    101 		sc->out_port, sc->in_port, sc->spkr_state, sc->sc_interrupts);
    102 	printf("tc %x chans %x scintr %x arg %x\n", sc->sc_adactc, sc->sc_chans,
    103 		sc->sc_intr, sc->sc_arg);
    104 	printf("gain: ");
    105 	for (i = 0; i < SB_NDEVS; i++)
    106 	    printf("%d ", sc->gain[i]);
    107 	printf("\n");
    108 }
    109 #endif
    110 
    111 /*
    112  * Probe / attach routines.
    113  */
    114 
    115 /*
    116  * Probe for the soundblaster hardware.
    117  */
    118 int
    119 sbdsp_probe(sc)
    120 	struct sbdsp_softc *sc;
    121 {
    122 	register u_short iobase = sc->sc_iobase;
    123 
    124 	if (sbdsp_reset(sc) < 0) {
    125 		DPRINTF(("sbdsp: couldn't reset card\n"));
    126 		return 0;
    127 	}
    128 	sc->sc_model = sbversion(sc);
    129 
    130 	return 1;
    131 }
    132 
    133 /*
    134  * Attach hardware to driver, attach hardware driver to audio
    135  * pseudo-device driver .
    136  */
    137 void
    138 sbdsp_attach(sc)
    139 	struct sbdsp_softc *sc;
    140 {
    141 	register u_short iobase = sc->sc_iobase;
    142 
    143 	sc->sc_locked = 0;
    144 
    145 #ifdef NEWCONFIG
    146 	/*
    147 	 * We limit DMA transfers to a page, and use the generic DMA handling
    148 	 * code in isa.c.  This code can end up copying a buffer, but since
    149 	 * the audio driver uses relative small buffers this isn't likely.
    150 	 *
    151 	 * This allocation scheme means that the maximum transfer is limited
    152 	 * by the page size (rather than 64k).  This is reasonable.  For 4K
    153 	 * pages, the transfer time at 48KHz is 4096 / 48000 = 85ms.  This
    154 	 * is plenty long enough to amortize any fixed time overhead.
    155 	 */
    156 	at_setup_dmachan(sc->sc_drq, NBPG);
    157 #endif
    158 
    159 	/* Set defaults */
    160 	if (ISSBPROCLASS(sc))
    161 	    sc->sc_irate = sc->sc_orate = 45454;
    162   	else
    163 	    sc->sc_irate = sc->sc_orate = 14925;
    164 	sc->sc_chans = 1;
    165 	sc->encoding = AUDIO_ENCODING_LINEAR;
    166 
    167 	(void) sbdsp_set_in_sr_real(sc, sc->sc_irate);
    168 	(void) sbdsp_set_out_sr_real(sc, sc->sc_orate);
    169 
    170 	(void) sbdsp_set_in_port(sc, SB_MIC_PORT);
    171 	(void) sbdsp_set_out_port(sc, SB_SPEAKER);
    172 
    173 	if (ISSBPROCLASS(sc)) {
    174 		int i;
    175 
    176 		/* set mixer to default levels, by sending a mixer
    177                    reset command. */
    178 		sbdsp_mix_write(sc, SBP_MIX_RESET, SBP_MIX_RESET);
    179 		/* then some adjustments :) */
    180 		sbdsp_mix_write(sc, SBP_CD_VOL,
    181 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    182 		sbdsp_mix_write(sc, SBP_DAC_VOL,
    183 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    184 		sbdsp_mix_write(sc, SBP_MASTER_VOL,
    185 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    186 		sbdsp_mix_write(sc, SBP_LINE_VOL,
    187 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    188 		for (i = 0; i < SB_NDEVS; i++)
    189 		    sc->gain[i] = sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL);
    190 	}
    191 	printf(": dsp v%d.%d\n",
    192 	       SBVER_MAJOR(sc->sc_model), SBVER_MINOR(sc->sc_model));
    193 }
    194 
    195 /*
    196  * Various routines to interface to higher level audio driver
    197  */
    198 
    199 void
    200 sbdsp_mix_write(sc, mixerport, val)
    201 	struct sbdsp_softc *sc;
    202 	int mixerport;
    203 	int val;
    204 {
    205 	int iobase = sc->sc_iobase;
    206 	outb(iobase + SBP_MIXER_ADDR, mixerport);
    207 	delay(10);
    208 	outb(iobase + SBP_MIXER_DATA, val);
    209 	delay(30);
    210 }
    211 
    212 int
    213 sbdsp_mix_read(sc, mixerport)
    214 	struct sbdsp_softc *sc;
    215 	int mixerport;
    216 {
    217 	int iobase = sc->sc_iobase;
    218 	outb(iobase + SBP_MIXER_ADDR, mixerport);
    219 	delay(10);
    220 	return inb(iobase + SBP_MIXER_DATA);
    221 }
    222 
    223 int
    224 sbdsp_set_in_sr(addr, sr)
    225 	void *addr;
    226 	u_long sr;
    227 {
    228 	register struct sbdsp_softc *sc = addr;
    229 
    230 	sc->sc_irate = sr;
    231 
    232 	return 0;
    233 }
    234 
    235 int
    236 sbdsp_set_in_sr_real(addr, sr)
    237 	void *addr;
    238 	u_long sr;
    239 {
    240 	register struct sbdsp_softc *sc = addr;
    241 	int rval;
    242 
    243 	if (rval = sbdsp_set_sr(sc, &sr, SB_INPUT_RATE))
    244 		return rval;
    245 	sc->sc_irate = sr;
    246 	sc->sc_dmain_inprogress = 0;		/* do it again on next DMA out */
    247 	sc->sc_dmaout_inprogress = 0;
    248 	return(0);
    249 }
    250 
    251 u_long
    252 sbdsp_get_in_sr(addr)
    253 	void *addr;
    254 {
    255 	register struct sbdsp_softc *sc = addr;
    256 
    257 	return(sc->sc_irate);
    258 }
    259 
    260 int
    261 sbdsp_set_out_sr(addr, sr)
    262 	void *addr;
    263 	u_long sr;
    264 {
    265 	register struct sbdsp_softc *sc = addr;
    266 
    267 	sc->sc_orate = sr;
    268 	return(0);
    269 }
    270 
    271 int
    272 sbdsp_set_out_sr_real(addr, sr)
    273 	void *addr;
    274 	u_long sr;
    275 {
    276 	register struct sbdsp_softc *sc = addr;
    277 	int rval;
    278 
    279 	if (rval = sbdsp_set_sr(sc, &sr, SB_OUTPUT_RATE))
    280 		return rval;
    281 	sc->sc_orate = sr;
    282 	sc->sc_dmain_inprogress = 0;		/* do it again on next DMA out */
    283 	return(0);
    284 }
    285 
    286 u_long
    287 sbdsp_get_out_sr(addr)
    288 	void *addr;
    289 {
    290 	register struct sbdsp_softc *sc = addr;
    291 
    292 	return(sc->sc_orate);
    293 }
    294 
    295 int
    296 sbdsp_query_encoding(addr, fp)
    297     void *addr;
    298     struct audio_encoding *fp;
    299 {
    300     register struct sbdsp_softc *sc = addr;
    301 
    302     switch (fp->index) {
    303     case 0:
    304 	strcpy(fp->name, "MU-Law");
    305 	fp->format_id = AUDIO_ENCODING_ULAW;
    306 	break;
    307     case 2:
    308 	strcpy(fp->name, "pcm16");
    309 	fp->format_id = AUDIO_ENCODING_PCM16;
    310 	break;
    311     default:
    312 	return(EINVAL);
    313 	/*NOTREACHED*/
    314     }
    315     return (0);
    316 }
    317 
    318 int
    319 sbdsp_set_encoding(addr, enc)
    320 	void *addr;
    321 	u_int enc;
    322 {
    323 	register struct sbdsp_softc *sc = addr;
    324 
    325 	switch(enc){
    326 	case AUDIO_ENCODING_ULAW:
    327 		sc->encoding = AUDIO_ENCODING_ULAW;
    328 		break;
    329 	case AUDIO_ENCODING_LINEAR:
    330 		sc->encoding = AUDIO_ENCODING_LINEAR;
    331 		break;
    332 	default:
    333 		return (EINVAL);
    334 	}
    335 	return (0);
    336 }
    337 
    338 int
    339 sbdsp_get_encoding(addr)
    340 	void *addr;
    341 {
    342 	register struct sbdsp_softc *sc = addr;
    343 
    344 	return(sc->encoding);
    345 }
    346 
    347 int
    348 sbdsp_set_precision(addr, prec)
    349 	void *addr;
    350 	u_int prec;
    351 {
    352 
    353 	if (prec != 8)
    354 		return(EINVAL);
    355 	return(0);
    356 }
    357 
    358 int
    359 sbdsp_get_precision(addr)
    360 	void *addr;
    361 {
    362 	return(8);
    363 }
    364 
    365 int
    366 sbdsp_set_channels(addr, chans)
    367 	void *addr;
    368 	int chans;
    369 {
    370 	register struct sbdsp_softc *sc = addr;
    371 	int rval;
    372 
    373 	if (ISSBPROCLASS(sc)) {
    374 		if (chans != 1 && chans != 2)
    375 			return(EINVAL);
    376 
    377 		sc->sc_chans = chans;
    378 		if (rval = sbdsp_set_in_sr_real(addr, sc->sc_irate))
    379 		    return rval;
    380 		sbdsp_mix_write(sc, SBP_STEREO,
    381 				(sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
    382 				(chans == 2 ? SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
    383 		/* recording channels needs to be done right when we start
    384 		   DMA recording.  Just record number of channels for now
    385 		   and set stereo when ready. */
    386 	}
    387 	else {
    388 		if (chans != 1)
    389 			return(EINVAL);
    390 		sc->sc_chans = 1;
    391 	}
    392 
    393 	return(0);
    394 }
    395 
    396 int
    397 sbdsp_get_channels(addr)
    398 	void *addr;
    399 {
    400 	register struct sbdsp_softc *sc = addr;
    401 
    402 #if 0
    403 	/* recording stereo may frob the mixer output */
    404 	if (ISSBPROCLASS(sc)) {
    405 		if ((sbdsp_mix_read(sc, SBP_STEREO) & SBP_PLAYMODE_MASK) == SBP_PLAYMODE_STEREO) {
    406 			sc->sc_chans = 2;
    407 		}
    408 		else {
    409 			sc->sc_chans = 1;
    410 		}
    411 	}
    412 	else {
    413 		sc->sc_chans = 1;
    414 	}
    415 #endif
    416 
    417 	return(sc->sc_chans);
    418 }
    419 
    420 int
    421 sbdsp_set_out_port(addr, port)
    422 	void *addr;
    423 	int port;
    424 {
    425 	register struct sbdsp_softc *sc = addr;
    426 
    427 	sc->out_port = port; /* Just record it */
    428 
    429 	return(0);
    430 }
    431 
    432 int
    433 sbdsp_get_out_port(addr)
    434 	void *addr;
    435 {
    436 	register struct sbdsp_softc *sc = addr;
    437 
    438 	return(sc->out_port);
    439 }
    440 
    441 
    442 int
    443 sbdsp_set_in_port(addr, port)
    444 	void *addr;
    445 	int port;
    446 {
    447 	register struct sbdsp_softc *sc = addr;
    448 	int mixport, sbport;
    449 
    450 	switch (port) {
    451 	case SB_MIC_PORT:
    452 	    sbport = SBP_FROM_MIC;
    453 	    mixport = SBP_MIC_VOL;
    454 	    break;
    455 	}
    456 
    457 	if (ISSBPROCLASS(sc)) {
    458 	    switch (port) {
    459 	    case SB_LINE_IN_PORT:
    460 		sbport = SBP_FROM_LINE;
    461 		mixport = SBP_LINE_VOL;
    462 		break;
    463 	    case SB_CD_PORT:
    464 		sbport = SBP_FROM_CD;
    465 		mixport = SBP_CD_VOL;
    466 		break;
    467 	    case SB_DAC_PORT:
    468 	    case SB_FM_PORT:
    469 	    default:
    470 		return(EINVAL);
    471 		/*NOTREACHED*/
    472 	    }
    473 	}
    474 	else {
    475 	    return(EINVAL);
    476 	    /*NOTREACHED*/
    477 	}
    478 
    479 	sc->in_port = port;	/* Just record it */
    480 
    481 	if (ISSBPROCLASS(sc)) {
    482 		/* record from that port */
    483 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE,
    484 				SBP_RECORD_FROM(sbport, SBP_FILTER_OFF,
    485 						SBP_FILTER_HIGH));
    486 		/* fetch gain from that port */
    487 		sc->gain[port] = sbdsp_mix_read(sc, mixport);
    488 	}
    489 
    490 	return(0);
    491 }
    492 
    493 int
    494 sbdsp_get_in_port(addr)
    495 	void *addr;
    496 {
    497 	register struct sbdsp_softc *sc = addr;
    498 
    499 	return(sc->in_port);
    500 }
    501 
    502 
    503 int
    504 sbdsp_speaker_ctl(addr, newstate)
    505 	void *addr;
    506 	int newstate;
    507 {
    508 	register struct sbdsp_softc *sc = addr;
    509 
    510 	if ((newstate == SPKR_ON) &&
    511 	    (sc->spkr_state == SPKR_OFF)) {
    512 		sbdsp_spkron(sc);
    513 		sc->spkr_state = SPKR_ON;
    514 	}
    515 	if ((newstate == SPKR_OFF) &&
    516 	    (sc->spkr_state == SPKR_ON)) {
    517 		sbdsp_spkroff(sc);
    518 		sc->spkr_state = SPKR_OFF;
    519 	}
    520 	return(0);
    521 }
    522 
    523 int
    524 sbdsp_round_blocksize(addr, blk)
    525 	void *addr;
    526 	int blk;
    527 {
    528 	register struct sbdsp_softc *sc = addr;
    529 
    530 	sc->sc_last_hsr_size = sc->sc_last_hsw_size = 0;
    531 
    532 	/* Higher speeds need bigger blocks to avoid popping and silence gaps. */
    533 	if ((sc->sc_orate > 8000 || sc->sc_irate > 8000) &&
    534 	    (blk > NBPG/2 || blk < NBPG/4))
    535 		blk = NBPG/2;
    536 	/* don't try to DMA too much at once, though. */
    537 	if (blk > NBPG) blk = NBPG;
    538 	if (sc->sc_chans == 2)
    539 		return (blk & ~1); /* must be even to preserve stereo separation */
    540 	else
    541 		return(blk);	/* Anything goes :-) */
    542 }
    543 
    544 int
    545 sbdsp_commit_settings(addr)
    546 	void *addr;
    547 {
    548 	/* due to potentially unfortunate ordering in the above layers,
    549 	   re-do a few sets which may be important--input gains
    550 	   (adjust the proper channels), number of input channels (hit the
    551 	   record rate and set mode) */
    552 
    553 	register struct sbdsp_softc *sc = addr;
    554 
    555 	sbdsp_set_out_sr_real(addr, sc->sc_orate);
    556 	sbdsp_set_in_sr_real(addr, sc->sc_irate);
    557 
    558 	sc->sc_last_hsw_size = sc->sc_last_hsr_size = 0;
    559 	return(0);
    560 }
    561 
    562 
    563 int
    564 sbdsp_open(sc, dev, flags)
    565 	register struct sbdsp_softc *sc;
    566 	dev_t dev;
    567 	int flags;
    568 {
    569         DPRINTF(("sbdsp_open: sc=0x%x\n", sc));
    570 
    571 	if (sc->sc_open != 0 || sbdsp_reset(sc) != 0)
    572 		return ENXIO;
    573 
    574 	sc->sc_open = 1;
    575 	sc->sc_mintr = 0;
    576 	sc->sc_intr = 0;
    577 	sc->sc_arg = 0;
    578 	sc->sc_locked = 0;
    579 	if (ISSBPROCLASS(sc) &&
    580 	    sbdsp_wdsp(sc->sc_iobase, SB_DSP_RECORD_MONO) < 0) {
    581 		DPRINTF(("sbdsp_open: can't set mono mode\n"));
    582 		/* we'll readjust when it's time for DMA. */
    583 	}
    584 	sc->sc_dmain_inprogress = 0;
    585 	sc->sc_dmaout_inprogress = 0;
    586 
    587 	/*
    588 	 * Leave most things as they were; users must change things if
    589 	 * the previous process didn't leave it they way they wanted.
    590 	 * Looked at another way, it's easy to set up a configuration
    591 	 * in one program and leave it for another to inherit.
    592 	 */
    593 	DPRINTF(("sbdsp_open: opened\n"));
    594 
    595 	return 0;
    596 }
    597 
    598 void
    599 sbdsp_close(addr)
    600 	void *addr;
    601 {
    602 	struct sbdsp_softc *sc = addr;
    603 
    604         DPRINTF(("sbdsp_close: sc=0x%x\n", sc));
    605 
    606 	sc->sc_open = 0;
    607 	sbdsp_spkroff(sc);
    608 	sc->spkr_state = SPKR_OFF;
    609 	sc->sc_intr = 0;
    610 	sc->sc_mintr = 0;
    611 	/* XXX this will turn off any dma */
    612 	sbdsp_reset(sc);
    613 
    614 	DPRINTF(("sbdsp_close: closed\n"));
    615 }
    616 
    617 /*
    618  * Lower-level routines
    619  */
    620 
    621 /*
    622  * Reset the card.
    623  * Return non-zero if the card isn't detected.
    624  */
    625 int
    626 sbdsp_reset(sc)
    627 	register struct sbdsp_softc *sc;
    628 {
    629 	register u_short iobase = sc->sc_iobase;
    630 
    631 	/*
    632 	 * erase any memory of last transfer size.
    633 	 */
    634 	sc->sc_last_hsr_size = sc->sc_last_hsw_size = 0;
    635 	/*
    636 	 * See SBK, section 11.3.
    637 	 * We pulse a reset signal into the card.
    638 	 * Gee, what a brilliant hardware design.
    639 	 */
    640 	outb(iobase + SBP_DSP_RESET, 1);
    641 	delay(3);
    642 	outb(iobase + SBP_DSP_RESET, 0);
    643 	if (sbdsp_rdsp(iobase) != SB_MAGIC)
    644 		return -1;
    645 	return 0;
    646 }
    647 
    648 /*
    649  * Write a byte to the dsp.
    650  * XXX We are at the mercy of the card as we use a
    651  * polling loop and wait until it can take the byte.
    652  */
    653 int
    654 sbdsp_wdsp(u_short iobase, int v)
    655 {
    656 	register int i;
    657 
    658 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    659 		if ((inb(iobase + SBP_DSP_WSTAT) & SB_DSP_BUSY) != 0) {
    660 			delay(10); continue;
    661 		}
    662 		outb(iobase + SBP_DSP_WRITE, v);
    663 		return 0;
    664 	}
    665 	++sberr.wdsp;
    666 	return -1;
    667 }
    668 
    669 /*
    670  * Read a byte from the DSP, using polling.
    671  */
    672 int
    673 sbdsp_rdsp(u_short iobase)
    674 {
    675 	register int i;
    676 
    677 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    678 		if ((inb(iobase + SBP_DSP_RSTAT) & SB_DSP_READY) == 0)
    679 			continue;
    680 		return inb(iobase + SBP_DSP_READ);
    681 	}
    682 	++sberr.rdsp;
    683 	return -1;
    684 }
    685 
    686 /*
    687  * Doing certain things (like toggling the speaker) make
    688  * the SB hardware go away for a while, so pause a little.
    689  */
    690 void
    691 sbdsp_to(arg)
    692 	void *arg;
    693 {
    694 	wakeup(arg);
    695 }
    696 
    697 void
    698 sbdsp_pause(sc)
    699 	struct sbdsp_softc *sc;
    700 {
    701 	extern int hz;
    702 
    703 	timeout(sbdsp_to, sbdsp_to, hz/8);
    704 	(void)tsleep(sbdsp_to, PWAIT, "sbpause", 0);
    705 }
    706 
    707 /*
    708  * Turn on the speaker.  The SBK documention says this operation
    709  * can take up to 1/10 of a second.  Higher level layers should
    710  * probably let the task sleep for this amount of time after
    711  * calling here.  Otherwise, things might not work (because
    712  * sbdsp_wdsp() and sbdsp_rdsp() will probably timeout.)
    713  *
    714  * These engineers had their heads up their ass when
    715  * they designed this card.
    716  */
    717 void
    718 sbdsp_spkron(sc)
    719 	struct sbdsp_softc *sc;
    720 {
    721 	(void)sbdsp_wdsp(sc->sc_iobase, SB_DSP_SPKR_ON);
    722 	sbdsp_pause(sc);
    723 }
    724 
    725 /*
    726  * Turn off the speaker; see comment above.
    727  */
    728 void
    729 sbdsp_spkroff(sc)
    730 	struct sbdsp_softc *sc;
    731 {
    732 	(void)sbdsp_wdsp(sc->sc_iobase, SB_DSP_SPKR_OFF);
    733 	sbdsp_pause(sc);
    734 }
    735 
    736 /*
    737  * Read the version number out of the card.  Return major code
    738  * in high byte, and minor code in low byte.
    739  */
    740 short
    741 sbversion(sc)
    742 	struct sbdsp_softc *sc;
    743 {
    744 	register u_short iobase = sc->sc_iobase;
    745 	short v;
    746 
    747 	if (sbdsp_wdsp(iobase, SB_DSP_VERSION) < 0)
    748 		return 0;
    749 	v = sbdsp_rdsp(iobase) << 8;
    750 	v |= sbdsp_rdsp(iobase);
    751 	return ((v >= 0) ? v : 0);
    752 }
    753 
    754 /*
    755  * Halt a DMA in progress.  A low-speed transfer can be
    756  * resumed with sbdsp_contdma().
    757  */
    758 int
    759 sbdsp_haltdma(addr)
    760 	void *addr;
    761 {
    762 	register struct sbdsp_softc *sc = addr;
    763 
    764 	DPRINTF(("sbdsp_haltdma: sc=0x%x\n", sc));
    765 
    766 	if (sc->sc_locked)
    767 		sbdsp_reset(sc);
    768 	else
    769 		(void)sbdsp_wdsp(sc->sc_iobase, SB_DSP_HALT);
    770 
    771 	isa_dmaabort(sc->sc_drq);
    772 	sc->dmaaddr = 0;
    773 	sc->dmacnt = 0;
    774 	sc->sc_locked = 0;
    775 	sc->dmaflags = 0;
    776 	sc->sc_dmain_inprogress = sc->sc_dmaout_inprogress = 0;
    777 	return(0);
    778 }
    779 
    780 int
    781 sbdsp_contdma(addr)
    782 	void *addr;
    783 {
    784 	register struct sbdsp_softc *sc = addr;
    785 
    786 	DPRINTF(("sbdsp_contdma: sc=0x%x\n", sc));
    787 
    788 	/* XXX how do we reinitialize the DMA controller state?  do we care? */
    789 	(void)sbdsp_wdsp(sc->sc_iobase, SB_DSP_CONT);
    790 	return(0);
    791 }
    792 
    793 /*
    794  * Time constant routines follow.  See SBK, section 12.
    795  * Although they don't come out and say it (in the docs),
    796  * the card clearly uses a 1MHz countdown timer, as the
    797  * low-speed formula (p. 12-4) is:
    798  *	tc = 256 - 10^6 / sr
    799  * In high-speed mode, the constant is the upper byte of a 16-bit counter,
    800  * and a 256MHz clock is used:
    801  *	tc = 65536 - 256 * 10^ 6 / sr
    802  * Since we can only use the upper byte of the HS TC, the two formulae
    803  * are equivalent.  (Why didn't they say so?)  E.g.,
    804  * 	(65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
    805  *
    806  * The crossover point (from low- to high-speed modes) is different
    807  * for the SBPRO and SB20.  The table on p. 12-5 gives the following data:
    808  *
    809  *				SBPRO			SB20
    810  *				-----			--------
    811  * input ls min			4	KHz		4	KHz
    812  * input ls max			23	KHz		13	KHz
    813  * input hs max			44.1	KHz		15	KHz
    814  * output ls min		4	KHz		4	KHz
    815  * output ls max		23	KHz		23	KHz
    816  * output hs max		44.1	KHz		44.1	KHz
    817  */
    818 #define SB_LS_MIN	0x06	/* 4000 Hz */
    819 #define SBPRO_ADC_LS_MAX	0xd4	/* 22727 Hz */
    820 #define SBPRO_ADC_HS_MAX	0xea	/* 45454 Hz */
    821 #define SBCLA_ADC_LS_MAX	0xb3	/* 12987 Hz */
    822 #define SBCLA_ADC_HS_MAX	0xbd	/* 14925 Hz */
    823 #define SB_DAC_LS_MAX	0xd4	/* 22727 Hz */
    824 #define SB_DAC_HS_MAX	0xea	/* 45454 Hz */
    825 
    826 /*
    827  * Convert a linear sampling rate into the DAC time constant.
    828  * Set *mode to indicate the high/low-speed DMA operation.
    829  * Because of limitations of the card, not all rates are possible.
    830  * We return the time constant of the closest possible rate.
    831  * The sampling rate limits are different for the DAC and ADC,
    832  * so isdac indicates output, and !isdac indicates input.
    833  */
    834 int
    835 sbdsp_srtotc(sc, sr, mode, isdac)
    836 	register struct sbdsp_softc *sc;
    837 	int sr;
    838 	int *mode;
    839 	int isdac;
    840 {
    841 	int adc_ls_max, adc_hs_max;
    842 	register int tc;
    843 
    844 	if (sr == 0) {
    845 		*mode = SB_ADAC_LS;
    846 		return SB_LS_MIN;
    847 	}
    848 	tc = 256 - 1000000 / sr;
    849 
    850 	/* XXX use better rounding--compare distance to nearest tc on both
    851 	   sides of requested speed */
    852 	if (ISSBPROCLASS(sc)) {
    853 		adc_ls_max = SBPRO_ADC_LS_MAX;
    854 		adc_hs_max = SBPRO_ADC_HS_MAX;
    855 	}
    856 	else {
    857 		adc_ls_max = SBCLA_ADC_LS_MAX;
    858 		adc_hs_max = SBCLA_ADC_HS_MAX;
    859 	}
    860 
    861 	if (tc < SB_LS_MIN) {
    862 		tc = SB_LS_MIN;
    863 		*mode = SB_ADAC_LS;
    864 	} else if (isdac) {
    865 		if (tc <= SB_DAC_LS_MAX)
    866 			*mode = SB_ADAC_LS;
    867 		else {
    868 			*mode = SB_ADAC_HS;
    869 			if (tc > SB_DAC_HS_MAX)
    870 				tc = SB_DAC_HS_MAX;
    871 		}
    872 	} else {
    873 		if (tc <= adc_ls_max)
    874 			*mode = SB_ADAC_LS;
    875 		else {
    876 			*mode = SB_ADAC_HS;
    877 			if (tc > adc_hs_max)
    878 				tc = adc_hs_max;
    879 		}
    880 	}
    881 	return tc;
    882 }
    883 
    884 /*
    885  * Convert a DAC time constant to a sampling rate.
    886  * See SBK, section 12.
    887  */
    888 int
    889 sbdsp_tctosr(sc, tc)
    890 	register struct sbdsp_softc *sc;
    891 	int tc;
    892 {
    893 	int adc;
    894 
    895 	if (ISSBPROCLASS(sc))
    896 		adc = SBPRO_ADC_HS_MAX;
    897 	else
    898 		adc = SBCLA_ADC_HS_MAX;
    899 
    900 	if (tc > adc)
    901 		tc = adc;
    902 
    903 	return (1000000 / (256 - tc));
    904 }
    905 
    906 int
    907 sbdsp_set_sr(sc, srp, isdac)
    908 	register struct sbdsp_softc *sc;
    909 	u_long *srp;
    910 	int isdac;
    911 {
    912 	register int tc;
    913 	int mode;
    914 	int sr = *srp;
    915 	register u_short iobase;
    916 
    917 	/*
    918 	 * A SBPro in stereo mode uses time constants at double the
    919 	 * actual rate.
    920 	 */
    921 	if (ISSBPRO(sc) && sc->sc_chans == 2) {
    922 		if (sr > 22727)
    923 			sr = 22727;	/* Can't bounce it...order of
    924 					   operations may yield bogus
    925 					   sr here. */
    926 		sr *= 2;
    927 	}
    928 	else if (!ISSBPROCLASS(sc) && sc->sc_chans != 1)
    929 		return EINVAL;
    930 
    931 	tc = sbdsp_srtotc(sc, sr, &mode, isdac);
    932 	DPRINTF(("sbdsp_set_sr: sc=0x%x sr=%d mode=0x%x\n", sc, sr, mode));
    933 
    934 	iobase = sc->sc_iobase;
    935 	if (sbdsp_wdsp(iobase, SB_DSP_TIMECONST) < 0 ||
    936 	    sbdsp_wdsp(iobase, tc) < 0)
    937 		return EIO;
    938 
    939 	sr = sbdsp_tctosr(sc, tc);
    940 	if (ISSBPRO(sc) && sc->sc_chans == 2)
    941 		*srp = sr / 2;
    942 	else
    943 		*srp = sr;
    944 
    945 	sc->sc_adacmode = mode;
    946 	sc->sc_adactc = tc;
    947 	return 0;
    948 }
    949 
    950 int
    951 sbdsp_dma_input(addr, p, cc, intr, arg)
    952 	void *addr;
    953 	void *p;
    954 	int cc;
    955 	void (*intr)();
    956 	void *arg;
    957 {
    958 	register struct sbdsp_softc *sc = addr;
    959 	register u_short iobase;
    960 	u_int phys;
    961 
    962 #ifdef DEBUG
    963 	if (sbdspdebug > 1)
    964 		Dprintf("sbdsp_dma_input: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
    965 #endif
    966 	if (sc->sc_chans == 2 && (cc & 1)) {
    967 		DPRINTF(("sbdsp_dma_input: stereo input, odd bytecnt\n"));
    968 		return EIO;
    969 	}
    970 	iobase = sc->sc_iobase;
    971 	if (ISSBPROCLASS(sc) && !sc->sc_dmain_inprogress) {
    972 		if (sc->sc_chans == 2) {
    973 			if (sbdsp_wdsp(iobase, SB_DSP_RECORD_STEREO) < 0)
    974 				goto badmode;
    975 			sbdsp_mix_write(sc, SBP_STEREO,
    976 					sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK);
    977 			sbdsp_mix_write(sc, SBP_INFILTER,
    978 					sbdsp_mix_read(sc, SBP_INFILTER) | SBP_FILTER_OFF);
    979 		}
    980 		else {
    981 			if (sbdsp_wdsp(iobase, SB_DSP_RECORD_MONO) < 0)
    982 				goto badmode;
    983 			sbdsp_mix_write(sc, SBP_STEREO,
    984 					sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK);
    985 			sbdsp_mix_write(sc, SBP_INFILTER,
    986 					sc->sc_irate <= 8000 ?
    987 					sbdsp_mix_read(sc, SBP_INFILTER) & ~SBP_FILTER_MASK :
    988 					sbdsp_mix_read(sc, SBP_INFILTER) | SBP_FILTER_OFF);
    989 		}
    990 		sc->sc_dmain_inprogress = 1;
    991 		sc->sc_last_hsr_size = 0;	/* restarting */
    992 	}
    993 	sc->sc_dmaout_inprogress = 0;
    994 
    995 	at_dma(B_READ, p, cc, sc->sc_drq);
    996 	sc->sc_intr = intr;
    997 	sc->sc_arg = arg;
    998 	sc->dmaflags = B_READ;
    999 	sc->dmaaddr = p;
   1000 	sc->dmacnt = --cc;		/* DMA controller is strange...? */
   1001 	if (sc->sc_adacmode == SB_ADAC_LS) {
   1002 		if (sbdsp_wdsp(iobase, SB_DSP_RDMA) < 0 ||
   1003 		    sbdsp_wdsp(iobase, cc) < 0 ||
   1004 		    sbdsp_wdsp(iobase, cc >> 8) < 0) {
   1005 			goto giveup;
   1006 		}
   1007 	}
   1008 	else {
   1009 		if (cc != sc->sc_last_hsr_size) {
   1010 			if (sbdsp_wdsp(iobase, SB_DSP_BLOCKSIZE) < 0 ||
   1011 			    sbdsp_wdsp(iobase, cc) < 0 ||
   1012 			    sbdsp_wdsp(iobase, cc >> 8) < 0)
   1013 				goto giveup;
   1014 		}
   1015 		if (sbdsp_wdsp(iobase, SB_DSP_HS_INPUT) < 0)
   1016 			goto giveup;
   1017 		sc->sc_last_hsr_size = cc;
   1018 		sc->sc_locked = 1;
   1019 	}
   1020 	return 0;
   1021 
   1022 giveup:
   1023 	isa_dmaabort(sc->sc_drq);
   1024 	sbdsp_reset(sc);
   1025 	sc->sc_intr = 0;
   1026 	sc->sc_arg = 0;
   1027 	return EIO;
   1028 badmode:
   1029 	DPRINTF(("sbdsp_dma_input: can't set %s mode\n",
   1030 		 sc->sc_chans == 2 ? "stereo" : "mono"));
   1031 	return EIO;
   1032 }
   1033 
   1034 int
   1035 sbdsp_dma_output(addr, p, cc, intr, arg)
   1036 	void *addr;
   1037 	void *p;
   1038 	int cc;
   1039 	void (*intr)();
   1040 	void *arg;
   1041 {
   1042 	register struct sbdsp_softc *sc = addr;
   1043 	register u_short iobase;
   1044 
   1045 #ifdef DEBUG
   1046 	if (sbdspdebug > 1)
   1047 		Dprintf("sbdsp_dma_output: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
   1048 #endif
   1049 	if (sc->sc_chans == 2 && cc & 1) {
   1050 		DPRINTF(("stereo playback odd bytes (%d)\n", cc));
   1051 		return EIO;
   1052 	}
   1053 
   1054 	if (ISSBPROCLASS(sc) && !sc->sc_dmaout_inprogress) {
   1055 		/* make sure we re-set stereo mixer bit when we start
   1056 		   output. */
   1057 		sbdsp_mix_write(sc, SBP_STEREO,
   1058 				(sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
   1059 				(sc->sc_chans == 2 ?
   1060 				 SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
   1061 		sc->sc_dmaout_inprogress = 1;
   1062 		sc->sc_last_hsw_size = 0;	/* restarting */
   1063 	}
   1064 	sc->sc_dmain_inprogress = 0;
   1065 	at_dma(B_WRITE, p, cc, sc->sc_drq);
   1066 	sc->sc_intr = intr;
   1067 	sc->sc_arg = arg;
   1068 	sc->dmaflags = B_WRITE;
   1069 	sc->dmaaddr = p;
   1070 	sc->dmacnt = --cc;	/* a vagary of how DMA works, apparently. */
   1071 
   1072 	iobase = sc->sc_iobase;
   1073 	if (sc->sc_adacmode == SB_ADAC_LS) {
   1074 		if (sbdsp_wdsp(iobase, SB_DSP_WDMA) < 0 ||
   1075 		    sbdsp_wdsp(iobase, cc) < 0 ||
   1076 		    sbdsp_wdsp(iobase, cc >> 8) < 0) {
   1077 		        DPRINTF(("sbdsp_dma_output: LS DMA start failed\n"));
   1078 			goto giveup;
   1079 		}
   1080 	}
   1081 	else {
   1082 		if (cc != sc->sc_last_hsw_size) {
   1083 			if (sbdsp_wdsp(iobase, SB_DSP_BLOCKSIZE) < 0) {
   1084 				/* sometimes fails initial startup?? */
   1085 				delay(100);
   1086 				if (sbdsp_wdsp(iobase, SB_DSP_BLOCKSIZE) < 0) {
   1087 					DPRINTF(("sbdsp_dma_output: BLOCKSIZE failed\n"));
   1088 					goto giveup;
   1089 				}
   1090 			}
   1091 			if (sbdsp_wdsp(iobase, cc) < 0 ||
   1092 			    sbdsp_wdsp(iobase, cc >> 8) < 0) {
   1093 				DPRINTF(("sbdsp_dma_output: HS DMA start failed\n"));
   1094 				goto giveup;
   1095 			}
   1096 			sc->sc_last_hsw_size = cc;
   1097 		}
   1098 		if (sbdsp_wdsp(iobase, SB_DSP_HS_OUTPUT) < 0) {
   1099 			delay(100);
   1100 			if (sbdsp_wdsp(iobase, SB_DSP_HS_OUTPUT) < 0) {
   1101 				DPRINTF(("sbdsp_dma_output: HS DMA restart failed\n"));
   1102 				goto giveup;
   1103 			}
   1104 		}
   1105 		sc->sc_locked = 1;
   1106 	}
   1107 
   1108 	return 0;
   1109 
   1110  giveup:
   1111 	isa_dmaabort(sc->sc_drq);
   1112 	sbdsp_reset(sc);
   1113 	sc->sc_intr = 0;
   1114 	sc->sc_arg = 0;
   1115 	return EIO;
   1116 }
   1117 
   1118 /*
   1119  * Only the DSP unit on the sound blaster generates interrupts.
   1120  * There are three cases of interrupt: reception of a midi byte
   1121  * (when mode is enabled), completion of dma transmission, or
   1122  * completion of a dma reception.  The three modes are mutually
   1123  * exclusive so we know a priori which event has occurred.
   1124  */
   1125 int
   1126 sbdsp_intr(sc)
   1127 	register struct sbdsp_softc *sc;
   1128 {
   1129 
   1130 #ifdef DEBUG
   1131 	if (sbdspdebug > 1)
   1132 		Dprintf("sbdsp_intr: intr=0x%x\n", sc->sc_intr);
   1133 #endif
   1134 	sc->sc_interrupts++;
   1135 	sc->sc_locked = 0;
   1136 	/* clear interrupt */
   1137 	inb(sc->sc_iobase + SBP_DSP_RSTAT);
   1138 #if 0
   1139 	if (sc->sc_mintr != 0) {
   1140 		int c = sbdsp_rdsp(sc->sc_iobase);
   1141 		(*sc->sc_mintr)(sc->sc_arg, c);
   1142 	} else
   1143 #endif
   1144 	if (sc->sc_intr != 0) {
   1145 	    /*
   1146 	     * The SBPro used to develop and test this driver often
   1147 	     * generated dma underruns--it interrupted to signal
   1148 	     * completion of the DMA input recording block, but the
   1149 	     * ISA DMA controller didn't think the channel was
   1150 	     * finished.  Maybe this is just a bus speed issue, I dunno,
   1151 	     * but it seems strange and leads to channel-flipping with stereo
   1152 	     * recording.  Sigh.
   1153 	     */
   1154 		isa_dmadone(sc->dmaflags, sc->dmaaddr, sc->dmacnt,
   1155 			    sc->sc_drq);
   1156 		sc->dmaflags = 0;
   1157 		sc->dmaaddr = 0;
   1158 		sc->dmacnt = 0;
   1159 		(*sc->sc_intr)(sc->sc_arg);
   1160 	}
   1161 	else
   1162 		return 0;
   1163 	return 1;
   1164 }
   1165 
   1166 #if 0
   1167 /*
   1168  * Enter midi uart mode and arrange for read interrupts
   1169  * to vector to `intr'.  This puts the card in a mode
   1170  * which allows only midi I/O; the card must be reset
   1171  * to leave this mode.  Unfortunately, the card does not
   1172  * use transmit interrupts, so bytes must be output
   1173  * using polling.  To keep the polling overhead to a
   1174  * minimum, output should be driven off a timer.
   1175  * This is a little tricky since only 320us separate
   1176  * consecutive midi bytes.
   1177  */
   1178 void
   1179 sbdsp_set_midi_mode(sc, intr, arg)
   1180 	struct sbdsp_softc *sc;
   1181 	void (*intr)();
   1182 	void *arg;
   1183 {
   1184 
   1185 	sbdsp_wdsp(sc->sc_iobase, SB_MIDI_UART_INTR);
   1186 	sc->sc_mintr = intr;
   1187 	sc->sc_intr = 0;
   1188 	sc->sc_arg = arg;
   1189 }
   1190 
   1191 /*
   1192  * Write a byte to the midi port, when in midi uart mode.
   1193  */
   1194 void
   1195 sbdsp_midi_output(sc, v)
   1196 	struct sbdsp_softc *sc;
   1197 	int v;
   1198 {
   1199 
   1200 	if (sbdsp_wdsp(sc->sc_iobase, v) < 0)
   1201 		++sberr.wmidi;
   1202 }
   1203 #endif
   1204 
   1205 u_int
   1206 sbdsp_get_silence(enc)
   1207     int enc;
   1208 {
   1209 #define ULAW_SILENCE	0x7f
   1210 #define LINEAR_SILENCE	0
   1211     u_int auzero;
   1212 
   1213     switch (enc) {
   1214     case AUDIO_ENCODING_ULAW:
   1215 	auzero = ULAW_SILENCE;
   1216 	break;
   1217     case AUDIO_ENCODING_PCM16:
   1218     default:
   1219 	auzero = LINEAR_SILENCE;
   1220 	break;
   1221     }
   1222 
   1223     return(auzero);
   1224 }
   1225 
   1226 static u_char mulawtolin[256] = {
   1227 	128, 4, 8, 12, 16, 20, 24, 28,
   1228 	32, 36, 40, 44, 48, 52, 56, 60,
   1229 	64, 66, 68, 70, 72, 74, 76, 78,
   1230 	80, 82, 84, 86, 88, 90, 92, 94,
   1231 	96, 97, 98, 99, 100, 101, 102, 103,
   1232 	104, 105, 106, 107, 108, 109, 110, 111,
   1233 	112, 112, 113, 113, 114, 114, 115, 115,
   1234 	116, 116, 117, 117, 118, 118, 119, 119,
   1235 	120, 120, 120, 121, 121, 121, 121, 122,
   1236 	122, 122, 122, 123, 123, 123, 123, 124,
   1237 	124, 124, 124, 124, 125, 125, 125, 125,
   1238 	125, 125, 125, 125, 126, 126, 126, 126,
   1239 	126, 126, 126, 126, 126, 126, 126, 126,
   1240 	127, 127, 127, 127, 127, 127, 127, 127,
   1241 	127, 127, 127, 127, 127, 127, 127, 127,
   1242 	127, 127, 127, 127, 127, 127, 127, 127,
   1243 	255, 251, 247, 243, 239, 235, 231, 227,
   1244 	223, 219, 215, 211, 207, 203, 199, 195,
   1245 	191, 189, 187, 185, 183, 181, 179, 177,
   1246 	175, 173, 171, 169, 167, 165, 163, 161,
   1247 	159, 158, 157, 156, 155, 154, 153, 152,
   1248 	151, 150, 149, 148, 147, 146, 145, 144,
   1249 	143, 143, 142, 142, 141, 141, 140, 140,
   1250 	139, 139, 138, 138, 137, 137, 136, 136,
   1251 	135, 135, 135, 134, 134, 134, 134, 133,
   1252 	133, 133, 133, 132, 132, 132, 132, 131,
   1253 	131, 131, 131, 131, 130, 130, 130, 130,
   1254 	130, 130, 130, 130, 129, 129, 129, 129,
   1255 	129, 129, 129, 129, 129, 129, 129, 129,
   1256 	128, 128, 128, 128, 128, 128, 128, 128,
   1257 	128, 128, 128, 128, 128, 128, 128, 128,
   1258 	128, 128, 128, 128, 128, 128, 128, 128,
   1259 };
   1260 
   1261 static u_char lintomulaw[256] = {
   1262 	0, 0, 0, 0, 0, 1, 1, 1,
   1263 	1, 2, 2, 2, 2, 3, 3, 3,
   1264 	3, 4, 4, 4, 4, 5, 5, 5,
   1265 	5, 6, 6, 6, 6, 7, 7, 7,
   1266 	7, 8, 8, 8, 8, 9, 9, 9,
   1267 	9, 10, 10, 10, 10, 11, 11, 11,
   1268 	11, 12, 12, 12, 12, 13, 13, 13,
   1269 	13, 14, 14, 14, 14, 15, 15, 15,
   1270 	15, 16, 16, 17, 17, 18, 18, 19,
   1271 	19, 20, 20, 21, 21, 22, 22, 23,
   1272 	23, 24, 24, 25, 25, 26, 26, 27,
   1273 	27, 28, 28, 29, 29, 30, 30, 31,
   1274 	31, 32, 33, 34, 35, 36, 37, 38,
   1275 	39, 40, 41, 42, 43, 44, 45, 46,
   1276 	47, 48, 50, 52, 54, 56, 58, 60,
   1277 	62, 65, 69, 73, 77, 83, 91, 103,
   1278 	255, 231, 219, 211, 205, 201, 197, 193,
   1279 	190, 188, 186, 184, 182, 180, 178, 176,
   1280 	175, 174, 173, 172, 171, 170, 169, 168,
   1281 	167, 166, 165, 164, 163, 162, 161, 160,
   1282 	159, 159, 158, 158, 157, 157, 156, 156,
   1283 	155, 155, 154, 154, 153, 153, 152, 152,
   1284 	151, 151, 150, 150, 149, 149, 148, 148,
   1285 	147, 147, 146, 146, 145, 145, 144, 144,
   1286 	143, 143, 143, 143, 142, 142, 142, 142,
   1287 	141, 141, 141, 141, 140, 140, 140, 140,
   1288 	139, 139, 139, 139, 138, 138, 138, 138,
   1289 	137, 137, 137, 137, 136, 136, 136, 136,
   1290 	135, 135, 135, 135, 134, 134, 134, 134,
   1291 	133, 133, 133, 133, 132, 132, 132, 132,
   1292 	131, 131, 131, 131, 130, 130, 130, 130,
   1293 	129, 129, 129, 129, 128, 128, 128, 128,
   1294 };
   1295 
   1296 void
   1297 sbdsp_compress(e, p, cc)
   1298 	int e;
   1299 	u_char *p;
   1300 	int cc;
   1301 {
   1302 	u_char *tab;
   1303 
   1304 	switch (e) {
   1305 	case AUDIO_ENCODING_ULAW:
   1306 		tab = lintomulaw;
   1307 		break;
   1308 	default:
   1309 		return;
   1310 	}
   1311 
   1312 	while (--cc >= 0) {
   1313 		*p = tab[*p];
   1314 		++p;
   1315 	}
   1316 }
   1317 
   1318 void
   1319 sbdsp_expand(e, p, cc)
   1320 	int e;
   1321 	u_char *p;
   1322 	int cc;
   1323 {
   1324 	u_char *tab;
   1325 
   1326 	switch (e) {
   1327 	case AUDIO_ENCODING_ULAW:
   1328 		tab = mulawtolin;
   1329 		break;
   1330 	default:
   1331 		return;
   1332 	}
   1333 
   1334 	while (--cc >= 0) {
   1335 		*p = tab[*p];
   1336 		++p;
   1337 	}
   1338 }
   1339 
   1340 int
   1341 sbdsp_setfd(addr, flag)
   1342 	void *addr;
   1343 	int flag;
   1344 {
   1345 	/* Can't do full-duplex */
   1346 	return(ENOTTY);
   1347 }
   1348 
   1349 int
   1350 sbdsp_mixer_set_port(addr, cp)
   1351     void *addr;
   1352     mixer_ctrl_t *cp;
   1353 {
   1354     register struct sbdsp_softc *sc = addr;
   1355     int error = 0;
   1356     int src, gain;
   1357     int left, right;
   1358 
   1359     DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev, cp->un.value.num_channels));
   1360 
   1361     /*
   1362      * Everything is a value except for SBPro special OUTPUT_MODE and
   1363      * RECORD_SOURCE
   1364      */
   1365     if (cp->type != AUDIO_MIXER_VALUE) {
   1366 	if (!ISSBPROCLASS(sc) || (cp->dev != SB_OUTPUT_MODE &&
   1367 				  cp->dev != SB_RECORD_SOURCE))
   1368 	    return EINVAL;
   1369     }
   1370     else {
   1371 	/*
   1372 	 * All the mixer ports are stereo except for the microphone.
   1373 	 * If we get a single-channel gain value passed in, then we
   1374 	 * duplicate it to both left and right channels.
   1375 	 */
   1376     if (cp->un.value.num_channels == 2) {
   1377 	left  = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1378 	right = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1379     }
   1380     else
   1381 	    left = right = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1382     }
   1383 
   1384     if (ISSBPROCLASS(sc)) {
   1385 	/* The _PORT things are all signal inputs to the mixer.
   1386 	 * Here we are tweaking their mixing level.
   1387 	 *
   1388 	 * We can also tweak the output stage volume (MASTER_VOL)
   1389 	 */
   1390 	gain = sbdsp_stereo_vol(SBP_AGAIN_TO_SBGAIN(left),
   1391 				SBP_AGAIN_TO_SBGAIN(right));
   1392 	switch(cp->dev) {
   1393         case SB_MIC_PORT:
   1394 	    src = SBP_MIC_VOL;
   1395 	    if (cp->un.value.num_channels != 1)
   1396 		error = EINVAL;
   1397 	    else
   1398 		/* handle funny microphone gain */
   1399 		gain = SBP_AGAIN_TO_MICGAIN(left);
   1400 	    break;
   1401         case SB_LINE_IN_PORT:
   1402 	    src = SBP_LINE_VOL;
   1403 	    break;
   1404         case SB_DAC_PORT:
   1405 	    src = SBP_DAC_VOL;
   1406 	    break;
   1407         case SB_FM_PORT:
   1408 	    src = SBP_FM_VOL;
   1409 	    break;
   1410         case SB_CD_PORT:
   1411 	    src = SBP_CD_VOL;
   1412 	    break;
   1413 	case SB_SPEAKER:
   1414 	    cp->dev = SB_MASTER_VOL;
   1415         case SB_MASTER_VOL:
   1416 	    src = SBP_MASTER_VOL;
   1417 	    break;
   1418 #if 0
   1419 	case SB_OUTPUT_MODE:
   1420 	    if (cp->type == AUDIO_MIXER_ENUM)
   1421 		return sbdsp_set_channels(addr, cp->un.ord);
   1422 	    /* fall through...carefully! */
   1423 #endif
   1424 	case SB_RECORD_SOURCE:
   1425 	    if (cp->type == AUDIO_MIXER_ENUM)
   1426 		return sbdsp_set_in_port(addr, cp->un.ord);
   1427 	    /* else fall through: bad input */
   1428         case SB_TREBLE:
   1429         case SB_BASS:
   1430         default:
   1431 	    error =  EINVAL;
   1432 	    break;
   1433 	}
   1434 	if (!error)
   1435 	sbdsp_mix_write(sc, src, gain);
   1436     }
   1437     else if (cp->dev != SB_MIC_PORT &&
   1438 	     cp->dev != SB_SPEAKER)
   1439 	error = EINVAL;
   1440 
   1441     if (!error)
   1442 	sc->gain[cp->dev] = gain;
   1443 
   1444     return(error);
   1445 }
   1446 
   1447 int
   1448 sbdsp_mixer_get_port(addr, cp)
   1449     void *addr;
   1450     mixer_ctrl_t *cp;
   1451 {
   1452     register struct sbdsp_softc *sc = addr;
   1453     int error = 0;
   1454     int done = 0;
   1455 
   1456     DPRINTF(("sbdsp_mixer_get_port: port=%d", cp->dev));
   1457 
   1458     if (ISSBPROCLASS(sc))
   1459     switch(cp->dev) {
   1460     case SB_MIC_PORT:
   1461 	    if (cp->un.value.num_channels == 1) {
   1462 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1463 		    SBP_MICGAIN_TO_AGAIN(sc->gain[cp->dev]);
   1464 		return 0;
   1465     }
   1466 	    else
   1467 		return EINVAL;
   1468 	    break;
   1469 	case SB_LINE_IN_PORT:
   1470         case SB_DAC_PORT:
   1471         case SB_FM_PORT:
   1472         case SB_CD_PORT:
   1473         case SB_MASTER_VOL:
   1474 	    break;
   1475 	case SB_SPEAKER:
   1476 	    cp->dev = SB_MASTER_VOL;
   1477 	    break;
   1478         default:
   1479 	    error =  EINVAL;
   1480 	    break;
   1481 	}
   1482     else {
   1483 	if (cp->un.value.num_channels != 1) /* no stereo on SB classic */
   1484 	    error = EINVAL;
   1485     else
   1486 	    switch(cp->dev) {
   1487 	    case SB_MIC_PORT:
   1488 		break;
   1489 	    case SB_SPEAKER:
   1490 		break;
   1491 	    default:
   1492 	error = EINVAL;
   1493 		break;
   1494 	    }
   1495     }
   1496     if (error == 0) {
   1497 	if (cp->un.value.num_channels == 1) {
   1498 	    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1499 		SBP_SBGAIN_TO_AGAIN(sc->gain[cp->dev]);
   1500 	}
   1501 	else if (cp->un.value.num_channels == 2) {
   1502 	    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
   1503 		SBP_LEFTGAIN(sc->gain[cp->dev]);
   1504 	    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
   1505 		SBP_RIGHTGAIN(sc->gain[cp->dev]);
   1506 	} else
   1507 	    return EINVAL;
   1508     }
   1509     return(error);
   1510 }
   1511 
   1512 int
   1513 sbdsp_mixer_query_devinfo(addr, dip)
   1514     void *addr;
   1515     register mixer_devinfo_t *dip;
   1516 {
   1517     register struct sbdsp_softc *sc = addr;
   1518     int done = 0;
   1519 
   1520     DPRINTF(("sbdsp_mixer_query_devinfo: index=%d\n", dip->index));
   1521 
   1522     switch (dip->index) {
   1523     case SB_MIC_PORT:
   1524 	dip->type = AUDIO_MIXER_VALUE;
   1525 	dip->mixer_class = SB_INPUT_CLASS;
   1526 	dip->prev = AUDIO_MIXER_LAST;
   1527 	dip->next = AUDIO_MIXER_LAST;
   1528 	strcpy(dip->label.name, AudioNmicrophone);
   1529 	dip->un.v.num_channels = 1;
   1530 	strcpy(dip->un.v.units.name, AudioNvolume);
   1531 	done = 1;
   1532 	break;
   1533     case SB_SPEAKER:
   1534 	dip->type = AUDIO_MIXER_VALUE;
   1535 	dip->mixer_class = SB_OUTPUT_CLASS;
   1536 	dip->prev = AUDIO_MIXER_LAST;
   1537 	dip->next = AUDIO_MIXER_LAST;
   1538 	strcpy(dip->label.name, AudioNspeaker);
   1539 	dip->un.v.num_channels = 1;
   1540 	strcpy(dip->un.v.units.name, AudioNvolume);
   1541 	done = 1;
   1542 	break;
   1543     case SB_INPUT_CLASS:
   1544 	dip->type = AUDIO_MIXER_CLASS;
   1545 	dip->mixer_class = SB_INPUT_CLASS;
   1546 	dip->next = dip->prev = AUDIO_MIXER_LAST;
   1547 	strcpy(dip->label.name, AudioCInputs);
   1548 	done = 1;
   1549 	break;
   1550     case SB_OUTPUT_CLASS:
   1551 	dip->type = AUDIO_MIXER_CLASS;
   1552 	dip->mixer_class = SB_OUTPUT_CLASS;
   1553 	dip->next = dip->prev = AUDIO_MIXER_LAST;
   1554 	strcpy(dip->label.name, AudioCOutputs);
   1555 	done = 1;
   1556 	break;
   1557     }
   1558 
   1559     if (!done) {
   1560     if (ISSBPROCLASS(sc))
   1561 	switch(dip->index) {
   1562 	case SB_LINE_IN_PORT:
   1563 	    dip->type = AUDIO_MIXER_VALUE;
   1564 	    dip->mixer_class = SB_INPUT_CLASS;
   1565 	    dip->prev = AUDIO_MIXER_LAST;
   1566 	    dip->next = AUDIO_MIXER_LAST;
   1567 	    strcpy(dip->label.name, AudioNline);
   1568 	    dip->un.v.num_channels = 2;
   1569 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1570 	    break;
   1571 	case SB_DAC_PORT:
   1572 	    dip->type = AUDIO_MIXER_VALUE;
   1573 	    dip->mixer_class = SB_INPUT_CLASS;
   1574 	    dip->prev = AUDIO_MIXER_LAST;
   1575 	    dip->next = AUDIO_MIXER_LAST;
   1576 	    strcpy(dip->label.name, AudioNdac);
   1577 	    dip->un.v.num_channels = 2;
   1578 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1579 	    break;
   1580 	case SB_CD_PORT:
   1581 	    dip->type = AUDIO_MIXER_VALUE;
   1582 	    dip->mixer_class = SB_INPUT_CLASS;
   1583 	    dip->prev = AUDIO_MIXER_LAST;
   1584 	    dip->next = AUDIO_MIXER_LAST;
   1585 	    strcpy(dip->label.name, AudioNcd);
   1586 	    dip->un.v.num_channels = 2;
   1587 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1588 	    break;
   1589 	case SB_FM_PORT:
   1590 	    dip->type = AUDIO_MIXER_VALUE;
   1591 	    dip->mixer_class = SB_INPUT_CLASS;
   1592 	    dip->prev = AUDIO_MIXER_LAST;
   1593 	    dip->next = AUDIO_MIXER_LAST;
   1594 	    strcpy(dip->label.name, "fmsynth");	/* XXX move to audioio.h */
   1595 	    dip->un.v.num_channels = 2;
   1596 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1597 	    break;
   1598 	case SB_MASTER_VOL:
   1599 	    dip->type = AUDIO_MIXER_VALUE;
   1600 	    dip->mixer_class = SB_OUTPUT_CLASS;
   1601 	    dip->prev = AUDIO_MIXER_LAST;
   1602 	    dip->next = SB_OUTPUT_MODE;
   1603 	    strcpy(dip->label.name, AudioNvolume);
   1604 	    dip->un.v.num_channels = 2;
   1605 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1606 	    break;
   1607 #if 0
   1608 	case SB_OUTPUT_MODE:
   1609 	    dip->mixer_class = SB_OUTPUT_CLASS;
   1610 	    dip->type = AUDIO_MIXER_ENUM;
   1611 	    dip->prev = SB_MASTER_VOL;
   1612 	    dip->next = AUDIO_MIXER_LAST;
   1613 	    strcpy(dip->label.name, AudioNmode);
   1614 	    dip->un.e.num_mem = 2;
   1615 	    strcpy(dip->un.e.member[0].label.name, AudioNmono);
   1616 	    dip->un.e.member[0].ord = 1; /* nchans */
   1617 	    strcpy(dip->un.e.member[1].label.name, AudioNstereo);
   1618 	    dip->un.e.member[1].ord = 2; /* nchans */
   1619 	    break;
   1620 #endif
   1621 	case SB_RECORD_SOURCE:
   1622 	    dip->mixer_class = SB_RECORD_CLASS;
   1623 	    dip->type = AUDIO_MIXER_ENUM;
   1624 	    dip->prev = AUDIO_MIXER_LAST;
   1625 	    dip->next = AUDIO_MIXER_LAST;
   1626 	    strcpy(dip->label.name, AudioNsource);
   1627 	    dip->un.e.num_mem = 3;
   1628 	    strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
   1629 	    dip->un.e.member[0].ord = SB_MIC_PORT;
   1630 	    strcpy(dip->un.e.member[1].label.name, AudioNcd);
   1631 	    dip->un.e.member[1].ord = SB_CD_PORT;
   1632 	    strcpy(dip->un.e.member[2].label.name, AudioNline);
   1633 	    dip->un.e.member[2].ord = SB_LINE_IN_PORT;
   1634 	    break;
   1635 	case SB_BASS:
   1636 	case SB_TREBLE:
   1637 	default:
   1638 	    return ENXIO;
   1639 	    /*NOTREACHED*/
   1640 	}
   1641     else
   1642 	return ENXIO;
   1643     }
   1644 
   1645     DPRINTF(("AUDIO_MIXER_DEVINFO: name=%s\n", dip->label.name));
   1646 
   1647     return 0;
   1648 }
   1649