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