Home | History | Annotate | Line # | Download | only in isa
sbdsp.c revision 1.7
      1 /*	$NetBSD: sbdsp.c,v 1.7 1995/04/17 15:48:34 cgd 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.7 1995/04/17 15:48:34 cgd 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 <dev/isa/isavar.h>
     61 #include <dev/isa/isadmavar.h>
     62 #include <i386/isa/icu.h>			/* XXX BROKEN; WHY? */
     63 
     64 #include <dev/isa/sbreg.h>
     65 #include <dev/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(arg)
   1127 	void *arg;
   1128 {
   1129 	register struct sbdsp_softc *sc = arg;
   1130 
   1131 #ifdef DEBUG
   1132 	if (sbdspdebug > 1)
   1133 		Dprintf("sbdsp_intr: intr=0x%x\n", sc->sc_intr);
   1134 #endif
   1135 	sc->sc_interrupts++;
   1136 	sc->sc_locked = 0;
   1137 	/* clear interrupt */
   1138 	inb(sc->sc_iobase + SBP_DSP_RSTAT);
   1139 #if 0
   1140 	if (sc->sc_mintr != 0) {
   1141 		int c = sbdsp_rdsp(sc->sc_iobase);
   1142 		(*sc->sc_mintr)(sc->sc_arg, c);
   1143 	} else
   1144 #endif
   1145 	if (sc->sc_intr != 0) {
   1146 	    /*
   1147 	     * The SBPro used to develop and test this driver often
   1148 	     * generated dma underruns--it interrupted to signal
   1149 	     * completion of the DMA input recording block, but the
   1150 	     * ISA DMA controller didn't think the channel was
   1151 	     * finished.  Maybe this is just a bus speed issue, I dunno,
   1152 	     * but it seems strange and leads to channel-flipping with stereo
   1153 	     * recording.  Sigh.
   1154 	     */
   1155 		isa_dmadone(sc->dmaflags, sc->dmaaddr, sc->dmacnt,
   1156 			    sc->sc_drq);
   1157 		sc->dmaflags = 0;
   1158 		sc->dmaaddr = 0;
   1159 		sc->dmacnt = 0;
   1160 		(*sc->sc_intr)(sc->sc_arg);
   1161 	}
   1162 	else
   1163 		return 0;
   1164 	return 1;
   1165 }
   1166 
   1167 #if 0
   1168 /*
   1169  * Enter midi uart mode and arrange for read interrupts
   1170  * to vector to `intr'.  This puts the card in a mode
   1171  * which allows only midi I/O; the card must be reset
   1172  * to leave this mode.  Unfortunately, the card does not
   1173  * use transmit interrupts, so bytes must be output
   1174  * using polling.  To keep the polling overhead to a
   1175  * minimum, output should be driven off a timer.
   1176  * This is a little tricky since only 320us separate
   1177  * consecutive midi bytes.
   1178  */
   1179 void
   1180 sbdsp_set_midi_mode(sc, intr, arg)
   1181 	struct sbdsp_softc *sc;
   1182 	void (*intr)();
   1183 	void *arg;
   1184 {
   1185 
   1186 	sbdsp_wdsp(sc->sc_iobase, SB_MIDI_UART_INTR);
   1187 	sc->sc_mintr = intr;
   1188 	sc->sc_intr = 0;
   1189 	sc->sc_arg = arg;
   1190 }
   1191 
   1192 /*
   1193  * Write a byte to the midi port, when in midi uart mode.
   1194  */
   1195 void
   1196 sbdsp_midi_output(sc, v)
   1197 	struct sbdsp_softc *sc;
   1198 	int v;
   1199 {
   1200 
   1201 	if (sbdsp_wdsp(sc->sc_iobase, v) < 0)
   1202 		++sberr.wmidi;
   1203 }
   1204 #endif
   1205 
   1206 u_int
   1207 sbdsp_get_silence(enc)
   1208     int enc;
   1209 {
   1210 #define ULAW_SILENCE	0x7f
   1211 #define LINEAR_SILENCE	0
   1212     u_int auzero;
   1213 
   1214     switch (enc) {
   1215     case AUDIO_ENCODING_ULAW:
   1216 	auzero = ULAW_SILENCE;
   1217 	break;
   1218     case AUDIO_ENCODING_PCM16:
   1219     default:
   1220 	auzero = LINEAR_SILENCE;
   1221 	break;
   1222     }
   1223 
   1224     return(auzero);
   1225 }
   1226 
   1227 static u_char mulawtolin[256] = {
   1228 	128, 4, 8, 12, 16, 20, 24, 28,
   1229 	32, 36, 40, 44, 48, 52, 56, 60,
   1230 	64, 66, 68, 70, 72, 74, 76, 78,
   1231 	80, 82, 84, 86, 88, 90, 92, 94,
   1232 	96, 97, 98, 99, 100, 101, 102, 103,
   1233 	104, 105, 106, 107, 108, 109, 110, 111,
   1234 	112, 112, 113, 113, 114, 114, 115, 115,
   1235 	116, 116, 117, 117, 118, 118, 119, 119,
   1236 	120, 120, 120, 121, 121, 121, 121, 122,
   1237 	122, 122, 122, 123, 123, 123, 123, 124,
   1238 	124, 124, 124, 124, 125, 125, 125, 125,
   1239 	125, 125, 125, 125, 126, 126, 126, 126,
   1240 	126, 126, 126, 126, 126, 126, 126, 126,
   1241 	127, 127, 127, 127, 127, 127, 127, 127,
   1242 	127, 127, 127, 127, 127, 127, 127, 127,
   1243 	127, 127, 127, 127, 127, 127, 127, 127,
   1244 	255, 251, 247, 243, 239, 235, 231, 227,
   1245 	223, 219, 215, 211, 207, 203, 199, 195,
   1246 	191, 189, 187, 185, 183, 181, 179, 177,
   1247 	175, 173, 171, 169, 167, 165, 163, 161,
   1248 	159, 158, 157, 156, 155, 154, 153, 152,
   1249 	151, 150, 149, 148, 147, 146, 145, 144,
   1250 	143, 143, 142, 142, 141, 141, 140, 140,
   1251 	139, 139, 138, 138, 137, 137, 136, 136,
   1252 	135, 135, 135, 134, 134, 134, 134, 133,
   1253 	133, 133, 133, 132, 132, 132, 132, 131,
   1254 	131, 131, 131, 131, 130, 130, 130, 130,
   1255 	130, 130, 130, 130, 129, 129, 129, 129,
   1256 	129, 129, 129, 129, 129, 129, 129, 129,
   1257 	128, 128, 128, 128, 128, 128, 128, 128,
   1258 	128, 128, 128, 128, 128, 128, 128, 128,
   1259 	128, 128, 128, 128, 128, 128, 128, 128,
   1260 };
   1261 
   1262 static u_char lintomulaw[256] = {
   1263 	0, 0, 0, 0, 0, 1, 1, 1,
   1264 	1, 2, 2, 2, 2, 3, 3, 3,
   1265 	3, 4, 4, 4, 4, 5, 5, 5,
   1266 	5, 6, 6, 6, 6, 7, 7, 7,
   1267 	7, 8, 8, 8, 8, 9, 9, 9,
   1268 	9, 10, 10, 10, 10, 11, 11, 11,
   1269 	11, 12, 12, 12, 12, 13, 13, 13,
   1270 	13, 14, 14, 14, 14, 15, 15, 15,
   1271 	15, 16, 16, 17, 17, 18, 18, 19,
   1272 	19, 20, 20, 21, 21, 22, 22, 23,
   1273 	23, 24, 24, 25, 25, 26, 26, 27,
   1274 	27, 28, 28, 29, 29, 30, 30, 31,
   1275 	31, 32, 33, 34, 35, 36, 37, 38,
   1276 	39, 40, 41, 42, 43, 44, 45, 46,
   1277 	47, 48, 50, 52, 54, 56, 58, 60,
   1278 	62, 65, 69, 73, 77, 83, 91, 103,
   1279 	255, 231, 219, 211, 205, 201, 197, 193,
   1280 	190, 188, 186, 184, 182, 180, 178, 176,
   1281 	175, 174, 173, 172, 171, 170, 169, 168,
   1282 	167, 166, 165, 164, 163, 162, 161, 160,
   1283 	159, 159, 158, 158, 157, 157, 156, 156,
   1284 	155, 155, 154, 154, 153, 153, 152, 152,
   1285 	151, 151, 150, 150, 149, 149, 148, 148,
   1286 	147, 147, 146, 146, 145, 145, 144, 144,
   1287 	143, 143, 143, 143, 142, 142, 142, 142,
   1288 	141, 141, 141, 141, 140, 140, 140, 140,
   1289 	139, 139, 139, 139, 138, 138, 138, 138,
   1290 	137, 137, 137, 137, 136, 136, 136, 136,
   1291 	135, 135, 135, 135, 134, 134, 134, 134,
   1292 	133, 133, 133, 133, 132, 132, 132, 132,
   1293 	131, 131, 131, 131, 130, 130, 130, 130,
   1294 	129, 129, 129, 129, 128, 128, 128, 128,
   1295 };
   1296 
   1297 void
   1298 sbdsp_compress(e, p, cc)
   1299 	int e;
   1300 	u_char *p;
   1301 	int cc;
   1302 {
   1303 	u_char *tab;
   1304 
   1305 	switch (e) {
   1306 	case AUDIO_ENCODING_ULAW:
   1307 		tab = lintomulaw;
   1308 		break;
   1309 	default:
   1310 		return;
   1311 	}
   1312 
   1313 	while (--cc >= 0) {
   1314 		*p = tab[*p];
   1315 		++p;
   1316 	}
   1317 }
   1318 
   1319 void
   1320 sbdsp_expand(e, p, cc)
   1321 	int e;
   1322 	u_char *p;
   1323 	int cc;
   1324 {
   1325 	u_char *tab;
   1326 
   1327 	switch (e) {
   1328 	case AUDIO_ENCODING_ULAW:
   1329 		tab = mulawtolin;
   1330 		break;
   1331 	default:
   1332 		return;
   1333 	}
   1334 
   1335 	while (--cc >= 0) {
   1336 		*p = tab[*p];
   1337 		++p;
   1338 	}
   1339 }
   1340 
   1341 int
   1342 sbdsp_setfd(addr, flag)
   1343 	void *addr;
   1344 	int flag;
   1345 {
   1346 	/* Can't do full-duplex */
   1347 	return(ENOTTY);
   1348 }
   1349 
   1350 int
   1351 sbdsp_mixer_set_port(addr, cp)
   1352     void *addr;
   1353     mixer_ctrl_t *cp;
   1354 {
   1355     register struct sbdsp_softc *sc = addr;
   1356     int error = 0;
   1357     int src, gain;
   1358     int left, right;
   1359 
   1360     DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev, cp->un.value.num_channels));
   1361 
   1362     /*
   1363      * Everything is a value except for SBPro special OUTPUT_MODE and
   1364      * RECORD_SOURCE
   1365      */
   1366     if (cp->type != AUDIO_MIXER_VALUE) {
   1367 	if (!ISSBPROCLASS(sc) || (cp->dev != SB_OUTPUT_MODE &&
   1368 				  cp->dev != SB_RECORD_SOURCE))
   1369 	    return EINVAL;
   1370     }
   1371     else {
   1372 	/*
   1373 	 * All the mixer ports are stereo except for the microphone.
   1374 	 * If we get a single-channel gain value passed in, then we
   1375 	 * duplicate it to both left and right channels.
   1376 	 */
   1377     if (cp->un.value.num_channels == 2) {
   1378 	left  = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1379 	right = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1380     }
   1381     else
   1382 	    left = right = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1383     }
   1384 
   1385     if (ISSBPROCLASS(sc)) {
   1386 	/* The _PORT things are all signal inputs to the mixer.
   1387 	 * Here we are tweaking their mixing level.
   1388 	 *
   1389 	 * We can also tweak the output stage volume (MASTER_VOL)
   1390 	 */
   1391 	gain = sbdsp_stereo_vol(SBP_AGAIN_TO_SBGAIN(left),
   1392 				SBP_AGAIN_TO_SBGAIN(right));
   1393 	switch(cp->dev) {
   1394         case SB_MIC_PORT:
   1395 	    src = SBP_MIC_VOL;
   1396 	    if (cp->un.value.num_channels != 1)
   1397 		error = EINVAL;
   1398 	    else
   1399 		/* handle funny microphone gain */
   1400 		gain = SBP_AGAIN_TO_MICGAIN(left);
   1401 	    break;
   1402         case SB_LINE_IN_PORT:
   1403 	    src = SBP_LINE_VOL;
   1404 	    break;
   1405         case SB_DAC_PORT:
   1406 	    src = SBP_DAC_VOL;
   1407 	    break;
   1408         case SB_FM_PORT:
   1409 	    src = SBP_FM_VOL;
   1410 	    break;
   1411         case SB_CD_PORT:
   1412 	    src = SBP_CD_VOL;
   1413 	    break;
   1414 	case SB_SPEAKER:
   1415 	    cp->dev = SB_MASTER_VOL;
   1416         case SB_MASTER_VOL:
   1417 	    src = SBP_MASTER_VOL;
   1418 	    break;
   1419 #if 0
   1420 	case SB_OUTPUT_MODE:
   1421 	    if (cp->type == AUDIO_MIXER_ENUM)
   1422 		return sbdsp_set_channels(addr, cp->un.ord);
   1423 	    /* fall through...carefully! */
   1424 #endif
   1425 	case SB_RECORD_SOURCE:
   1426 	    if (cp->type == AUDIO_MIXER_ENUM)
   1427 		return sbdsp_set_in_port(addr, cp->un.ord);
   1428 	    /* else fall through: bad input */
   1429         case SB_TREBLE:
   1430         case SB_BASS:
   1431         default:
   1432 	    error =  EINVAL;
   1433 	    break;
   1434 	}
   1435 	if (!error)
   1436 	sbdsp_mix_write(sc, src, gain);
   1437     }
   1438     else if (cp->dev != SB_MIC_PORT &&
   1439 	     cp->dev != SB_SPEAKER)
   1440 	error = EINVAL;
   1441 
   1442     if (!error)
   1443 	sc->gain[cp->dev] = gain;
   1444 
   1445     return(error);
   1446 }
   1447 
   1448 int
   1449 sbdsp_mixer_get_port(addr, cp)
   1450     void *addr;
   1451     mixer_ctrl_t *cp;
   1452 {
   1453     register struct sbdsp_softc *sc = addr;
   1454     int error = 0;
   1455     int done = 0;
   1456 
   1457     DPRINTF(("sbdsp_mixer_get_port: port=%d", cp->dev));
   1458 
   1459     if (ISSBPROCLASS(sc))
   1460     switch(cp->dev) {
   1461     case SB_MIC_PORT:
   1462 	    if (cp->un.value.num_channels == 1) {
   1463 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1464 		    SBP_MICGAIN_TO_AGAIN(sc->gain[cp->dev]);
   1465 		return 0;
   1466     }
   1467 	    else
   1468 		return EINVAL;
   1469 	    break;
   1470 	case SB_LINE_IN_PORT:
   1471         case SB_DAC_PORT:
   1472         case SB_FM_PORT:
   1473         case SB_CD_PORT:
   1474         case SB_MASTER_VOL:
   1475 	    break;
   1476 	case SB_SPEAKER:
   1477 	    cp->dev = SB_MASTER_VOL;
   1478 	    break;
   1479         default:
   1480 	    error =  EINVAL;
   1481 	    break;
   1482 	}
   1483     else {
   1484 	if (cp->un.value.num_channels != 1) /* no stereo on SB classic */
   1485 	    error = EINVAL;
   1486     else
   1487 	    switch(cp->dev) {
   1488 	    case SB_MIC_PORT:
   1489 		break;
   1490 	    case SB_SPEAKER:
   1491 		break;
   1492 	    default:
   1493 	error = EINVAL;
   1494 		break;
   1495 	    }
   1496     }
   1497     if (error == 0) {
   1498 	if (cp->un.value.num_channels == 1) {
   1499 	    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1500 		SBP_SBGAIN_TO_AGAIN(sc->gain[cp->dev]);
   1501 	}
   1502 	else if (cp->un.value.num_channels == 2) {
   1503 	    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
   1504 		SBP_LEFTGAIN(sc->gain[cp->dev]);
   1505 	    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
   1506 		SBP_RIGHTGAIN(sc->gain[cp->dev]);
   1507 	} else
   1508 	    return EINVAL;
   1509     }
   1510     return(error);
   1511 }
   1512 
   1513 int
   1514 sbdsp_mixer_query_devinfo(addr, dip)
   1515     void *addr;
   1516     register mixer_devinfo_t *dip;
   1517 {
   1518     register struct sbdsp_softc *sc = addr;
   1519     int done = 0;
   1520 
   1521     DPRINTF(("sbdsp_mixer_query_devinfo: index=%d\n", dip->index));
   1522 
   1523     switch (dip->index) {
   1524     case SB_MIC_PORT:
   1525 	dip->type = AUDIO_MIXER_VALUE;
   1526 	dip->mixer_class = SB_INPUT_CLASS;
   1527 	dip->prev = AUDIO_MIXER_LAST;
   1528 	dip->next = AUDIO_MIXER_LAST;
   1529 	strcpy(dip->label.name, AudioNmicrophone);
   1530 	dip->un.v.num_channels = 1;
   1531 	strcpy(dip->un.v.units.name, AudioNvolume);
   1532 	done = 1;
   1533 	break;
   1534     case SB_SPEAKER:
   1535 	dip->type = AUDIO_MIXER_VALUE;
   1536 	dip->mixer_class = SB_OUTPUT_CLASS;
   1537 	dip->prev = AUDIO_MIXER_LAST;
   1538 	dip->next = AUDIO_MIXER_LAST;
   1539 	strcpy(dip->label.name, AudioNspeaker);
   1540 	dip->un.v.num_channels = 1;
   1541 	strcpy(dip->un.v.units.name, AudioNvolume);
   1542 	done = 1;
   1543 	break;
   1544     case SB_INPUT_CLASS:
   1545 	dip->type = AUDIO_MIXER_CLASS;
   1546 	dip->mixer_class = SB_INPUT_CLASS;
   1547 	dip->next = dip->prev = AUDIO_MIXER_LAST;
   1548 	strcpy(dip->label.name, AudioCInputs);
   1549 	done = 1;
   1550 	break;
   1551     case SB_OUTPUT_CLASS:
   1552 	dip->type = AUDIO_MIXER_CLASS;
   1553 	dip->mixer_class = SB_OUTPUT_CLASS;
   1554 	dip->next = dip->prev = AUDIO_MIXER_LAST;
   1555 	strcpy(dip->label.name, AudioCOutputs);
   1556 	done = 1;
   1557 	break;
   1558     }
   1559 
   1560     if (!done) {
   1561     if (ISSBPROCLASS(sc))
   1562 	switch(dip->index) {
   1563 	case SB_LINE_IN_PORT:
   1564 	    dip->type = AUDIO_MIXER_VALUE;
   1565 	    dip->mixer_class = SB_INPUT_CLASS;
   1566 	    dip->prev = AUDIO_MIXER_LAST;
   1567 	    dip->next = AUDIO_MIXER_LAST;
   1568 	    strcpy(dip->label.name, AudioNline);
   1569 	    dip->un.v.num_channels = 2;
   1570 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1571 	    break;
   1572 	case SB_DAC_PORT:
   1573 	    dip->type = AUDIO_MIXER_VALUE;
   1574 	    dip->mixer_class = SB_INPUT_CLASS;
   1575 	    dip->prev = AUDIO_MIXER_LAST;
   1576 	    dip->next = AUDIO_MIXER_LAST;
   1577 	    strcpy(dip->label.name, AudioNdac);
   1578 	    dip->un.v.num_channels = 2;
   1579 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1580 	    break;
   1581 	case SB_CD_PORT:
   1582 	    dip->type = AUDIO_MIXER_VALUE;
   1583 	    dip->mixer_class = SB_INPUT_CLASS;
   1584 	    dip->prev = AUDIO_MIXER_LAST;
   1585 	    dip->next = AUDIO_MIXER_LAST;
   1586 	    strcpy(dip->label.name, AudioNcd);
   1587 	    dip->un.v.num_channels = 2;
   1588 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1589 	    break;
   1590 	case SB_FM_PORT:
   1591 	    dip->type = AUDIO_MIXER_VALUE;
   1592 	    dip->mixer_class = SB_INPUT_CLASS;
   1593 	    dip->prev = AUDIO_MIXER_LAST;
   1594 	    dip->next = AUDIO_MIXER_LAST;
   1595 	    strcpy(dip->label.name, "fmsynth");	/* XXX move to audioio.h */
   1596 	    dip->un.v.num_channels = 2;
   1597 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1598 	    break;
   1599 	case SB_MASTER_VOL:
   1600 	    dip->type = AUDIO_MIXER_VALUE;
   1601 	    dip->mixer_class = SB_OUTPUT_CLASS;
   1602 	    dip->prev = AUDIO_MIXER_LAST;
   1603 	    dip->next = SB_OUTPUT_MODE;
   1604 	    strcpy(dip->label.name, AudioNvolume);
   1605 	    dip->un.v.num_channels = 2;
   1606 	    strcpy(dip->un.v.units.name, AudioNvolume);
   1607 	    break;
   1608 #if 0
   1609 	case SB_OUTPUT_MODE:
   1610 	    dip->mixer_class = SB_OUTPUT_CLASS;
   1611 	    dip->type = AUDIO_MIXER_ENUM;
   1612 	    dip->prev = SB_MASTER_VOL;
   1613 	    dip->next = AUDIO_MIXER_LAST;
   1614 	    strcpy(dip->label.name, AudioNmode);
   1615 	    dip->un.e.num_mem = 2;
   1616 	    strcpy(dip->un.e.member[0].label.name, AudioNmono);
   1617 	    dip->un.e.member[0].ord = 1; /* nchans */
   1618 	    strcpy(dip->un.e.member[1].label.name, AudioNstereo);
   1619 	    dip->un.e.member[1].ord = 2; /* nchans */
   1620 	    break;
   1621 #endif
   1622 	case SB_RECORD_SOURCE:
   1623 	    dip->mixer_class = SB_RECORD_CLASS;
   1624 	    dip->type = AUDIO_MIXER_ENUM;
   1625 	    dip->prev = AUDIO_MIXER_LAST;
   1626 	    dip->next = AUDIO_MIXER_LAST;
   1627 	    strcpy(dip->label.name, AudioNsource);
   1628 	    dip->un.e.num_mem = 3;
   1629 	    strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
   1630 	    dip->un.e.member[0].ord = SB_MIC_PORT;
   1631 	    strcpy(dip->un.e.member[1].label.name, AudioNcd);
   1632 	    dip->un.e.member[1].ord = SB_CD_PORT;
   1633 	    strcpy(dip->un.e.member[2].label.name, AudioNline);
   1634 	    dip->un.e.member[2].ord = SB_LINE_IN_PORT;
   1635 	    break;
   1636 	case SB_BASS:
   1637 	case SB_TREBLE:
   1638 	default:
   1639 	    return ENXIO;
   1640 	    /*NOTREACHED*/
   1641 	}
   1642     else
   1643 	return ENXIO;
   1644     }
   1645 
   1646     DPRINTF(("AUDIO_MIXER_DEVINFO: name=%s\n", dip->label.name));
   1647 
   1648     return 0;
   1649 }
   1650