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