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