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