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