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