Home | History | Annotate | Line # | Download | only in isa
sbdsp.c revision 1.34
      1 /*	$NetBSD: sbdsp.c,v 1.34 1997/03/20 06:48:58 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/intr.h>
     55 #include <machine/pio.h>
     56 
     57 #include <sys/audioio.h>
     58 #include <dev/audio_if.h>
     59 
     60 #include <dev/isa/isavar.h>
     61 #include <dev/isa/isadmavar.h>
     62 #include <i386/isa/icu.h>			/* XXX BROKEN; WHY? */
     63 
     64 #include <dev/isa/sbreg.h>
     65 #include <dev/isa/sbdspvar.h>
     66 
     67 #ifdef AUDIO_DEBUG
     68 extern void Dprintf __P((const char *, ...));
     69 #define DPRINTF(x)	if (sbdspdebug) Dprintf x
     70 int	sbdspdebug = 0;
     71 #else
     72 #define DPRINTF(x)
     73 #endif
     74 
     75 #ifndef SBDSP_NPOLL
     76 #define SBDSP_NPOLL 3000
     77 #endif
     78 
     79 struct {
     80 	int wdsp;
     81 	int rdsp;
     82 	int wmidi;
     83 } sberr;
     84 
     85 int sbdsp_srtotc __P((struct sbdsp_softc *sc, int sr, int isdac,
     86 		      int *tcp, int *modep));
     87 u_int sbdsp_jazz16_probe __P((struct sbdsp_softc *));
     88 
     89 /*
     90  * Time constant routines follow.  See SBK, section 12.
     91  * Although they don't come out and say it (in the docs),
     92  * the card clearly uses a 1MHz countdown timer, as the
     93  * low-speed formula (p. 12-4) is:
     94  *	tc = 256 - 10^6 / sr
     95  * In high-speed mode, the constant is the upper byte of a 16-bit counter,
     96  * and a 256MHz clock is used:
     97  *	tc = 65536 - 256 * 10^ 6 / sr
     98  * Since we can only use the upper byte of the HS TC, the two formulae
     99  * are equivalent.  (Why didn't they say so?)  E.g.,
    100  * 	(65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
    101  *
    102  * The crossover point (from low- to high-speed modes) is different
    103  * for the SBPRO and SB20.  The table on p. 12-5 gives the following data:
    104  *
    105  *				SBPRO			SB20
    106  *				-----			--------
    107  * input ls min			4	KHz		4	KHz
    108  * input ls max			23	KHz		13	KHz
    109  * input hs max			44.1	KHz		15	KHz
    110  * output ls min		4	KHz		4	KHz
    111  * output ls max		23	KHz		23	KHz
    112  * output hs max		44.1	KHz		44.1	KHz
    113  */
    114 #define SB_LS_MIN	0x06	/* 4000 Hz */
    115 #define	SB_8K		0x83	/* 8000 Hz */
    116 #define SBPRO_ADC_LS_MAX	0xd4	/* 22727 Hz */
    117 #define SBPRO_ADC_HS_MAX	0xea	/* 45454 Hz */
    118 #define SBCLA_ADC_LS_MAX	0xb3	/* 12987 Hz */
    119 #define SBCLA_ADC_HS_MAX	0xbd	/* 14925 Hz */
    120 #define SB_DAC_LS_MAX	0xd4	/* 22727 Hz */
    121 #define SB_DAC_HS_MAX	0xea	/* 45454 Hz */
    122 
    123 int	sbdsp16_wait __P((struct sbdsp_softc *));
    124 void	sbdsp_to __P((void *));
    125 void	sbdsp_pause __P((struct sbdsp_softc *));
    126 int	sbdsp_setrate __P((struct sbdsp_softc *, int, int, int *));
    127 int	sbdsp_tctosr __P((struct sbdsp_softc *, int));
    128 int	sbdsp_set_timeconst __P((struct sbdsp_softc *, int));
    129 
    130 #ifdef AUDIO_DEBUG
    131 void sb_printsc __P((struct sbdsp_softc *));
    132 #endif
    133 
    134 #ifdef AUDIO_DEBUG
    135 void
    136 sb_printsc(sc)
    137 	struct sbdsp_softc *sc;
    138 {
    139 	int i;
    140 
    141 	printf("open %d dmachan %d iobase %x\n",
    142 	    sc->sc_open, sc->sc_drq, sc->sc_iobase);
    143 	printf("irate %d itc %d imode %d orate %d otc %d omode %d encoding %x\n",
    144 	    sc->sc_irate, sc->sc_itc, sc->sc_imode,
    145 	    sc->sc_orate, sc->sc_otc, sc->sc_omode, sc->sc_encoding);
    146 	printf("outport %d inport %d spkron %d nintr %lu\n",
    147 	    sc->out_port, sc->in_port, sc->spkr_state, sc->sc_interrupts);
    148 	printf("precision %d channels %d intr %p arg %p\n",
    149 	    sc->sc_precision, sc->sc_channels, sc->sc_intr, sc->sc_arg);
    150 	printf("gain: ");
    151 	for (i = 0; i < SB_NDEVS; i++)
    152 		printf("%d ", sc->gain[i]);
    153 	printf("\n");
    154 }
    155 #endif
    156 
    157 /*
    158  * Probe / attach routines.
    159  */
    160 
    161 /*
    162  * Probe for the soundblaster hardware.
    163  */
    164 int
    165 sbdsp_probe(sc)
    166 	struct sbdsp_softc *sc;
    167 {
    168 
    169 	if (sbdsp_reset(sc) < 0) {
    170 		DPRINTF(("sbdsp: couldn't reset card\n"));
    171 		return 0;
    172 	}
    173 	/* if flags set, go and probe the jazz16 stuff */
    174 	if (sc->sc_dev.dv_cfdata->cf_flags != 0)
    175 		sc->sc_model = sbdsp_jazz16_probe(sc);
    176 	else
    177 		sc->sc_model = sbversion(sc);
    178 
    179 	return 1;
    180 }
    181 
    182 /*
    183  * Try add-on stuff for Jazz16.
    184  */
    185 u_int
    186 sbdsp_jazz16_probe(sc)
    187 	struct sbdsp_softc *sc;
    188 {
    189 	static u_char jazz16_irq_conf[16] = {
    190 	    -1, -1, 0x02, 0x03,
    191 	    -1, 0x01, -1, 0x04,
    192 	    -1, 0x02, 0x05, -1,
    193 	    -1, -1, -1, 0x06};
    194 	static u_char jazz16_drq_conf[8] = {
    195 	    -1, 0x01, -1, 0x02,
    196 	    -1, 0x03, -1, 0x04};
    197 
    198 	u_int rval = sbversion(sc);
    199 	bus_space_tag_t iot = sc->sc_iot;
    200 	bus_space_handle_t ioh;
    201 
    202 	if (bus_space_map(iot, JAZZ16_CONFIG_PORT, 1, 0, &ioh))
    203 		return rval;
    204 
    205 	if (jazz16_drq_conf[sc->sc_drq] == (u_char)-1 ||
    206 	    jazz16_irq_conf[sc->sc_irq] == (u_char)-1)
    207 		goto done;		/* give up, we can't do it. */
    208 
    209 	bus_space_write_1(iot, ioh, 0, JAZZ16_WAKEUP);
    210 	delay(10000);			/* delay 10 ms */
    211 	bus_space_write_1(iot, ioh, 0, JAZZ16_SETBASE);
    212 	bus_space_write_1(iot, ioh, 0, sc->sc_iobase & 0x70);
    213 
    214 	if (sbdsp_reset(sc) < 0)
    215 		goto done;		/* XXX? what else could we do? */
    216 
    217 	if (sbdsp_wdsp(sc, JAZZ16_READ_VER))
    218 		goto done;
    219 
    220 	if (sbdsp_rdsp(sc) != JAZZ16_VER_JAZZ)
    221 		goto done;
    222 
    223 	if (sbdsp_wdsp(sc, JAZZ16_SET_DMAINTR) ||
    224 	    /* set both 8 & 16-bit drq to same channel, it works fine. */
    225 	    sbdsp_wdsp(sc, (jazz16_drq_conf[sc->sc_drq] << 4) |
    226 		jazz16_drq_conf[sc->sc_drq]) ||
    227 	    sbdsp_wdsp(sc, jazz16_irq_conf[sc->sc_irq]))
    228 		DPRINTF(("sbdsp: can't write jazz16 probe stuff"));
    229 	else
    230 		rval |= MODEL_JAZZ16;
    231 
    232 done:
    233 	bus_space_unmap(iot, ioh, 1);
    234 	return rval;
    235 }
    236 
    237 /*
    238  * Attach hardware to driver, attach hardware driver to audio
    239  * pseudo-device driver .
    240  */
    241 void
    242 sbdsp_attach(sc)
    243 	struct sbdsp_softc *sc;
    244 {
    245 
    246 	/* Set defaults */
    247 	if (ISSB16CLASS(sc))
    248 		sc->sc_irate = sc->sc_orate = 8000;
    249 	else if (ISSBPROCLASS(sc))
    250 		sc->sc_itc = sc->sc_otc = SB_8K;
    251   	else
    252 		sc->sc_itc = sc->sc_otc = SB_8K;
    253 	sc->sc_encoding = AUDIO_ENCODING_ULAW;
    254 	sc->sc_precision = 8;
    255 	sc->sc_channels = 1;
    256 
    257 	(void) sbdsp_set_in_port(sc, SB_MIC_PORT);
    258 	(void) sbdsp_set_out_port(sc, SB_SPEAKER);
    259 
    260 	if (ISSBPROCLASS(sc)) {
    261 		int i;
    262 
    263 		/* set mixer to default levels, by sending a mixer
    264                    reset command. */
    265 		sbdsp_mix_write(sc, SBP_MIX_RESET, SBP_MIX_RESET);
    266 		/* then some adjustments :) */
    267 		sbdsp_mix_write(sc, SBP_CD_VOL,
    268 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    269 		sbdsp_mix_write(sc, SBP_DAC_VOL,
    270 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    271 		sbdsp_mix_write(sc, SBP_MASTER_VOL,
    272 				sbdsp_stereo_vol(SBP_MAXVOL/2, SBP_MAXVOL/2));
    273 		sbdsp_mix_write(sc, SBP_LINE_VOL,
    274 				sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
    275 		for (i = 0; i < SB_NDEVS; i++)
    276 			sc->gain[i] = sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL);
    277 		sc->in_filter = 0;	/* no filters turned on, please */
    278 	}
    279 
    280 	printf(": dsp v%d.%02d%s\n",
    281 	       SBVER_MAJOR(sc->sc_model), SBVER_MINOR(sc->sc_model),
    282 	       ISJAZZ16(sc) ? ": <Jazz16>" : "");
    283 
    284 #ifdef notyet
    285 	sbdsp_mix_write(sc, SBP_SET_IRQ, 0x04);
    286 	sbdsp_mix_write(sc, SBP_SET_DRQ, 0x22);
    287 
    288 	printf("sbdsp_attach: irq=%02x, drq=%02x\n",
    289 	    sbdsp_mix_read(sc, SBP_SET_IRQ),
    290 	    sbdsp_mix_read(sc, SBP_SET_DRQ));
    291 #else
    292 	if (ISSB16CLASS(sc))
    293 		sc->sc_model = 0x0300;
    294 #endif
    295 }
    296 
    297 /*
    298  * Various routines to interface to higher level audio driver
    299  */
    300 
    301 void
    302 sbdsp_mix_write(sc, mixerport, val)
    303 	struct sbdsp_softc *sc;
    304 	int mixerport;
    305 	int val;
    306 {
    307 	bus_space_tag_t iot = sc->sc_iot;
    308 	bus_space_handle_t ioh = sc->sc_ioh;
    309 
    310 	bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
    311 	delay(10);
    312 	bus_space_write_1(iot, ioh, SBP_MIXER_DATA, val);
    313 	delay(30);
    314 }
    315 
    316 int
    317 sbdsp_mix_read(sc, mixerport)
    318 	struct sbdsp_softc *sc;
    319 	int mixerport;
    320 {
    321 	bus_space_tag_t iot = sc->sc_iot;
    322 	bus_space_handle_t ioh = sc->sc_ioh;
    323 
    324 	bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
    325 	delay(10);
    326 	return bus_space_read_1(iot, ioh, SBP_MIXER_DATA);
    327 }
    328 
    329 int
    330 sbdsp_set_in_sr(addr, sr)
    331 	void *addr;
    332 	u_long sr;
    333 {
    334 	register struct sbdsp_softc *sc = addr;
    335 
    336 	if (ISSB16CLASS(sc))
    337 		return (sbdsp_setrate(sc, sr, SB_INPUT_RATE, &sc->sc_irate));
    338 	else
    339 		return (sbdsp_srtotc(sc, sr, SB_INPUT_RATE, &sc->sc_itc, &sc->sc_imode));
    340 }
    341 
    342 u_long
    343 sbdsp_get_in_sr(addr)
    344 	void *addr;
    345 {
    346 	register struct sbdsp_softc *sc = addr;
    347 
    348 	if (ISSB16CLASS(sc))
    349 		return (sc->sc_irate);
    350 	else
    351 		return (sbdsp_tctosr(sc, sc->sc_itc));
    352 }
    353 
    354 int
    355 sbdsp_set_out_sr(addr, sr)
    356 	void *addr;
    357 	u_long sr;
    358 {
    359 	register struct sbdsp_softc *sc = addr;
    360 
    361 	if (ISSB16CLASS(sc))
    362 		return (sbdsp_setrate(sc, sr, SB_OUTPUT_RATE, &sc->sc_orate));
    363 	else
    364 		return (sbdsp_srtotc(sc, sr, SB_OUTPUT_RATE, &sc->sc_otc, &sc->sc_omode));
    365 }
    366 
    367 u_long
    368 sbdsp_get_out_sr(addr)
    369 	void *addr;
    370 {
    371 	register struct sbdsp_softc *sc = addr;
    372 
    373 	if (ISSB16CLASS(sc))
    374 		return (sc->sc_orate);
    375 	else
    376 		return (sbdsp_tctosr(sc, sc->sc_otc));
    377 }
    378 
    379 int
    380 sbdsp_query_encoding(addr, fp)
    381 	void *addr;
    382 	struct audio_encoding *fp;
    383 {
    384 	switch (fp->index) {
    385 	case 0:
    386 		strcpy(fp->name, AudioEmulaw);
    387 		fp->format_id = AUDIO_ENCODING_ULAW;
    388 		break;
    389 	case 1:
    390 		strcpy(fp->name, AudioEpcm16);
    391 		fp->format_id = AUDIO_ENCODING_PCM16;
    392 		break;
    393 	default:
    394 		return (EINVAL);
    395 	}
    396 	return (0);
    397 }
    398 
    399 int
    400 sbdsp_set_format(addr, encoding, precision)
    401 	void *addr;
    402 	u_int encoding, precision;
    403 {
    404 	register struct sbdsp_softc *sc = addr;
    405 
    406 	switch (encoding) {
    407 	case AUDIO_ENCODING_ULAW:
    408 	case AUDIO_ENCODING_PCM16:
    409 	case AUDIO_ENCODING_PCM8:
    410 		break;
    411 	default:
    412 		return (EINVAL);
    413 	}
    414 
    415 	if (precision == 16)
    416 		if (!ISSB16CLASS(sc) && !ISJAZZ16(sc))
    417 			return (EINVAL);
    418 
    419 	sc->sc_encoding = encoding;
    420 	sc->sc_precision = precision;
    421 
    422 	return (0);
    423 }
    424 
    425 int
    426 sbdsp_get_encoding(addr)
    427 	void *addr;
    428 {
    429 	register struct sbdsp_softc *sc = addr;
    430 
    431 	return (sc->sc_encoding);
    432 }
    433 
    434 int
    435 sbdsp_get_precision(addr)
    436 	void *addr;
    437 {
    438 	register struct sbdsp_softc *sc = addr;
    439 
    440 	return (sc->sc_precision);
    441 }
    442 
    443 int
    444 sbdsp_set_channels(addr, channels)
    445 	void *addr;
    446 	int channels;
    447 {
    448 	register struct sbdsp_softc *sc = addr;
    449 
    450 	if (ISSBPROCLASS(sc)) {
    451 		if (channels != 1 && channels != 2)
    452 			return (EINVAL);
    453 		sc->sc_channels = channels;
    454 		sc->sc_dmadir = SB_DMA_NONE;
    455 		/*
    456 		 * XXXX
    457 		 * With 2 channels, SBPro can't do more than 22kHz.
    458 		 * No framework to check this.
    459 		 */
    460 	} else {
    461 		if (channels != 1)
    462 			return (EINVAL);
    463 		sc->sc_channels = channels;
    464 	}
    465 
    466 	return (0);
    467 }
    468 
    469 int
    470 sbdsp_get_channels(addr)
    471 	void *addr;
    472 {
    473 	register struct sbdsp_softc *sc = addr;
    474 
    475 	return (sc->sc_channels);
    476 }
    477 
    478 int
    479 sbdsp_set_ifilter(addr, which)
    480 	void *addr;
    481 	int which;
    482 {
    483 	register struct sbdsp_softc *sc = addr;
    484 	int mixval;
    485 
    486 	if (ISSBPROCLASS(sc)) {
    487 		mixval = sbdsp_mix_read(sc, SBP_INFILTER) & ~SBP_IFILTER_MASK;
    488 		switch (which) {
    489 		case 0:
    490 			mixval |= SBP_FILTER_OFF;
    491 			break;
    492 		case SBP_TREBLE_EQ:
    493 			mixval |= SBP_FILTER_ON | SBP_IFILTER_HIGH;
    494 			break;
    495 		case SBP_BASS_EQ:
    496 			mixval |= SBP_FILTER_ON | SBP_IFILTER_LOW;
    497 			break;
    498 		default:
    499 			return (EINVAL);
    500 		}
    501 		sc->in_filter = mixval & SBP_IFILTER_MASK;
    502 		sbdsp_mix_write(sc, SBP_INFILTER, mixval);
    503 		return (0);
    504 	} else
    505 		return (EINVAL);
    506 }
    507 
    508 int
    509 sbdsp_get_ifilter(addr)
    510 	void *addr;
    511 {
    512 	register struct sbdsp_softc *sc = addr;
    513 
    514 	if (ISSBPROCLASS(sc)) {
    515 		sc->in_filter =
    516 		    sbdsp_mix_read(sc, SBP_INFILTER) & SBP_IFILTER_MASK;
    517 		switch (sc->in_filter) {
    518 		case SBP_FILTER_ON|SBP_IFILTER_HIGH:
    519 			return (SBP_TREBLE_EQ);
    520 		case SBP_FILTER_ON|SBP_IFILTER_LOW:
    521 			return (SBP_BASS_EQ);
    522 		case SBP_FILTER_OFF:
    523 		default:
    524 			return (0);
    525 		}
    526 	} else
    527 		return (0);
    528 }
    529 
    530 int
    531 sbdsp_set_out_port(addr, port)
    532 	void *addr;
    533 	int port;
    534 {
    535 	register struct sbdsp_softc *sc = addr;
    536 
    537 	sc->out_port = port; /* Just record it */
    538 
    539 	return (0);
    540 }
    541 
    542 int
    543 sbdsp_get_out_port(addr)
    544 	void *addr;
    545 {
    546 	register struct sbdsp_softc *sc = addr;
    547 
    548 	return (sc->out_port);
    549 }
    550 
    551 
    552 int
    553 sbdsp_set_in_port(addr, port)
    554 	void *addr;
    555 	int port;
    556 {
    557 	register struct sbdsp_softc *sc = addr;
    558 	int mixport, sbport;
    559 
    560 	if (ISSBPROCLASS(sc)) {
    561 		switch (port) {
    562 		case SB_MIC_PORT:
    563 			sbport = SBP_FROM_MIC;
    564 			mixport = SBP_MIC_VOL;
    565 			break;
    566 		case SB_LINE_IN_PORT:
    567 			sbport = SBP_FROM_LINE;
    568 			mixport = SBP_LINE_VOL;
    569 			break;
    570 		case SB_CD_PORT:
    571 			sbport = SBP_FROM_CD;
    572 			mixport = SBP_CD_VOL;
    573 			break;
    574 		case SB_DAC_PORT:
    575 		case SB_FM_PORT:
    576 		default:
    577 			return (EINVAL);
    578 		}
    579 	} else {
    580 		switch (port) {
    581 		case SB_MIC_PORT:
    582 			sbport = SBP_FROM_MIC;
    583 			mixport = SBP_MIC_VOL;
    584 			break;
    585 		default:
    586 			return (EINVAL);
    587 		}
    588 	}
    589 
    590 	sc->in_port = port;	/* Just record it */
    591 
    592 	if (ISSBPROCLASS(sc)) {
    593 		/* record from that port */
    594 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE,
    595 		    SBP_RECORD_FROM(sbport, SBP_FILTER_OFF, SBP_IFILTER_HIGH));
    596 		/* fetch gain from that port */
    597 		sc->gain[port] = sbdsp_mix_read(sc, mixport);
    598 	}
    599 
    600 	return (0);
    601 }
    602 
    603 int
    604 sbdsp_get_in_port(addr)
    605 	void *addr;
    606 {
    607 	register struct sbdsp_softc *sc = addr;
    608 
    609 	return (sc->in_port);
    610 }
    611 
    612 
    613 int
    614 sbdsp_speaker_ctl(addr, newstate)
    615 	void *addr;
    616 	int newstate;
    617 {
    618 	register struct sbdsp_softc *sc = addr;
    619 
    620 	if ((newstate == SPKR_ON) &&
    621 	    (sc->spkr_state == SPKR_OFF)) {
    622 		sbdsp_spkron(sc);
    623 		sc->spkr_state = SPKR_ON;
    624 	}
    625 	if ((newstate == SPKR_OFF) &&
    626 	    (sc->spkr_state == SPKR_ON)) {
    627 		sbdsp_spkroff(sc);
    628 		sc->spkr_state = SPKR_OFF;
    629 	}
    630 	return(0);
    631 }
    632 
    633 int
    634 sbdsp_round_blocksize(addr, blk)
    635 	void *addr;
    636 	int blk;
    637 {
    638 	register struct sbdsp_softc *sc = addr;
    639 
    640 	sc->sc_last_hs_size = 0;
    641 
    642 	/* Higher speeds need bigger blocks to avoid popping and silence gaps. */
    643 	if (blk < NBPG/4 || blk > NBPG/2) {
    644 		if (ISSB16CLASS(sc)) {
    645 			if (sc->sc_orate > 8000 || sc->sc_irate > 8000)
    646 				blk = NBPG/2;
    647 		} else {
    648 			if (sc->sc_otc > SB_8K || sc->sc_itc < SB_8K)
    649 				blk = NBPG/2;
    650 		}
    651 	}
    652 	/* don't try to DMA too much at once, though. */
    653 	if (blk > NBPG)
    654 		blk = NBPG;
    655 	if (sc->sc_channels == 2)
    656 		return (blk & ~1); /* must be even to preserve stereo separation */
    657 	else
    658 		return (blk);	/* Anything goes :-) */
    659 }
    660 
    661 int
    662 sbdsp_commit_settings(addr)
    663 	void *addr;
    664 {
    665 	register struct sbdsp_softc *sc = addr;
    666 
    667 	/* due to potentially unfortunate ordering in the above layers,
    668 	   re-do a few sets which may be important--input gains
    669 	   (adjust the proper channels), number of input channels (hit the
    670 	   record rate and set mode) */
    671 
    672 	if (ISSBPRO(sc)) {
    673 		/*
    674 		 * With 2 channels, SBPro can't do more than 22kHz.
    675 		 * Whack the rates down to speed if necessary.
    676 		 * Reset the time constant anyway
    677 		 * because it may have been adjusted with a different number
    678 		 * of channels, which means it might have computed the wrong
    679 		 * mode (low/high speed).
    680 		 */
    681 		if (sc->sc_channels == 2 &&
    682 		    sbdsp_tctosr(sc, sc->sc_itc) > 22727) {
    683 			sbdsp_srtotc(sc, 22727, SB_INPUT_RATE,
    684 				     &sc->sc_itc, &sc->sc_imode);
    685 		} else
    686 			sbdsp_srtotc(sc, sbdsp_tctosr(sc, sc->sc_itc),
    687 				     SB_INPUT_RATE, &sc->sc_itc,
    688 				     &sc->sc_imode);
    689 
    690 		if (sc->sc_channels == 2 &&
    691 		    sbdsp_tctosr(sc, sc->sc_otc) > 22727) {
    692 			sbdsp_srtotc(sc, 22727, SB_OUTPUT_RATE,
    693 				     &sc->sc_otc, &sc->sc_omode);
    694 		} else
    695 			sbdsp_srtotc(sc, sbdsp_tctosr(sc, sc->sc_otc),
    696 				     SB_OUTPUT_RATE, &sc->sc_otc,
    697 				     &sc->sc_omode);
    698 	}
    699 
    700 	/*
    701 	 * XXX
    702 	 * Should wait for chip to be idle.
    703 	 */
    704 	sc->sc_dmadir = SB_DMA_NONE;
    705 
    706 	return 0;
    707 }
    708 
    709 
    710 int
    711 sbdsp_open(sc, dev, flags)
    712 	register struct sbdsp_softc *sc;
    713 	dev_t dev;
    714 	int flags;
    715 {
    716         DPRINTF(("sbdsp_open: sc=0x%x\n", sc));
    717 
    718 	if (sc->sc_open != 0 || sbdsp_reset(sc) != 0)
    719 		return ENXIO;
    720 
    721 	sc->sc_open = 1;
    722 	sc->sc_mintr = 0;
    723 	if (ISSBPROCLASS(sc) &&
    724 	    sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0) {
    725 		DPRINTF(("sbdsp_open: can't set mono mode\n"));
    726 		/* we'll readjust when it's time for DMA. */
    727 	}
    728 
    729 	/*
    730 	 * Leave most things as they were; users must change things if
    731 	 * the previous process didn't leave it they way they wanted.
    732 	 * Looked at another way, it's easy to set up a configuration
    733 	 * in one program and leave it for another to inherit.
    734 	 */
    735 	DPRINTF(("sbdsp_open: opened\n"));
    736 
    737 	return 0;
    738 }
    739 
    740 void
    741 sbdsp_close(addr)
    742 	void *addr;
    743 {
    744 	struct sbdsp_softc *sc = addr;
    745 
    746         DPRINTF(("sbdsp_close: sc=0x%x\n", sc));
    747 
    748 	sc->sc_open = 0;
    749 	sbdsp_spkroff(sc);
    750 	sc->spkr_state = SPKR_OFF;
    751 	sc->sc_mintr = 0;
    752 	sbdsp_haltdma(sc);
    753 
    754 	DPRINTF(("sbdsp_close: closed\n"));
    755 }
    756 
    757 /*
    758  * Lower-level routines
    759  */
    760 
    761 /*
    762  * Reset the card.
    763  * Return non-zero if the card isn't detected.
    764  */
    765 int
    766 sbdsp_reset(sc)
    767 	register struct sbdsp_softc *sc;
    768 {
    769 	bus_space_tag_t iot = sc->sc_iot;
    770 	bus_space_handle_t ioh = sc->sc_ioh;
    771 
    772 	sc->sc_intr = 0;
    773 	if (sc->sc_dmadir != SB_DMA_NONE) {
    774 		isa_dmaabort(sc->sc_drq);
    775 		sc->sc_dmadir = SB_DMA_NONE;
    776 	}
    777 	sc->sc_last_hs_size = 0;
    778 
    779 	/*
    780 	 * See SBK, section 11.3.
    781 	 * We pulse a reset signal into the card.
    782 	 * Gee, what a brilliant hardware design.
    783 	 */
    784 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 1);
    785 	delay(10);
    786 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 0);
    787 	delay(30);
    788 	if (sbdsp_rdsp(sc) != SB_MAGIC)
    789 		return -1;
    790 
    791 	return 0;
    792 }
    793 
    794 int
    795 sbdsp16_wait(sc)
    796 	struct sbdsp_softc *sc;
    797 {
    798 	bus_space_tag_t iot = sc->sc_iot;
    799 	bus_space_handle_t ioh = sc->sc_ioh;
    800 	register int i;
    801 
    802 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    803 		register u_char x;
    804 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
    805 		delay(10);
    806 		if ((x & SB_DSP_BUSY) == 0)
    807 			continue;
    808 		return 0;
    809 	}
    810 	++sberr.wdsp;
    811 	return -1;
    812 }
    813 
    814 /*
    815  * Write a byte to the dsp.
    816  * XXX We are at the mercy of the card as we use a
    817  * polling loop and wait until it can take the byte.
    818  */
    819 int
    820 sbdsp_wdsp(sc, v)
    821 	struct sbdsp_softc *sc;
    822 	int v;
    823 {
    824 	bus_space_tag_t iot = sc->sc_iot;
    825 	bus_space_handle_t ioh = sc->sc_ioh;
    826 	register int i;
    827 
    828 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    829 		register u_char x;
    830 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
    831 		delay(10);
    832 		if ((x & SB_DSP_BUSY) != 0)
    833 			continue;
    834 		bus_space_write_1(iot, ioh, SBP_DSP_WRITE, v);
    835 		delay(10);
    836 		return 0;
    837 	}
    838 	++sberr.wdsp;
    839 	return -1;
    840 }
    841 
    842 /*
    843  * Read a byte from the DSP, using polling.
    844  */
    845 int
    846 sbdsp_rdsp(sc)
    847 	struct sbdsp_softc *sc;
    848 {
    849 	bus_space_tag_t iot = sc->sc_iot;
    850 	bus_space_handle_t ioh = sc->sc_ioh;
    851 	register int i;
    852 
    853 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    854 		register u_char x;
    855 		x = bus_space_read_1(iot, ioh, SBP_DSP_RSTAT);
    856 		delay(10);
    857 		if ((x & SB_DSP_READY) == 0)
    858 			continue;
    859 		x = bus_space_read_1(iot, ioh, SBP_DSP_READ);
    860 		delay(10);
    861 		return x;
    862 	}
    863 	++sberr.rdsp;
    864 	return -1;
    865 }
    866 
    867 /*
    868  * Doing certain things (like toggling the speaker) make
    869  * the SB hardware go away for a while, so pause a little.
    870  */
    871 void
    872 sbdsp_to(arg)
    873 	void *arg;
    874 {
    875 	wakeup(arg);
    876 }
    877 
    878 void
    879 sbdsp_pause(sc)
    880 	struct sbdsp_softc *sc;
    881 {
    882 	extern int hz;
    883 
    884 	timeout(sbdsp_to, sbdsp_to, hz/8);
    885 	(void)tsleep(sbdsp_to, PWAIT, "sbpause", 0);
    886 }
    887 
    888 /*
    889  * Turn on the speaker.  The SBK documention says this operation
    890  * can take up to 1/10 of a second.  Higher level layers should
    891  * probably let the task sleep for this amount of time after
    892  * calling here.  Otherwise, things might not work (because
    893  * sbdsp_wdsp() and sbdsp_rdsp() will probably timeout.)
    894  *
    895  * These engineers had their heads up their ass when
    896  * they designed this card.
    897  */
    898 void
    899 sbdsp_spkron(sc)
    900 	struct sbdsp_softc *sc;
    901 {
    902 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_ON);
    903 	sbdsp_pause(sc);
    904 }
    905 
    906 /*
    907  * Turn off the speaker; see comment above.
    908  */
    909 void
    910 sbdsp_spkroff(sc)
    911 	struct sbdsp_softc *sc;
    912 {
    913 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_OFF);
    914 	sbdsp_pause(sc);
    915 }
    916 
    917 /*
    918  * Read the version number out of the card.  Return major code
    919  * in high byte, and minor code in low byte.
    920  */
    921 short
    922 sbversion(sc)
    923 	struct sbdsp_softc *sc;
    924 {
    925 	short v;
    926 
    927 	if (sbdsp_wdsp(sc, SB_DSP_VERSION) < 0)
    928 		return 0;
    929 	v = sbdsp_rdsp(sc) << 8;
    930 	v |= sbdsp_rdsp(sc);
    931 	return ((v >= 0) ? v : 0);
    932 }
    933 
    934 /*
    935  * Halt a DMA in progress.  A low-speed transfer can be
    936  * resumed with sbdsp_contdma().
    937  */
    938 int
    939 sbdsp_haltdma(addr)
    940 	void *addr;
    941 {
    942 	register struct sbdsp_softc *sc = addr;
    943 
    944 	DPRINTF(("sbdsp_haltdma: sc=0x%x\n", sc));
    945 
    946 	sbdsp_reset(sc);
    947 	return 0;
    948 }
    949 
    950 int
    951 sbdsp_contdma(addr)
    952 	void *addr;
    953 {
    954 	register struct sbdsp_softc *sc = addr;
    955 
    956 	DPRINTF(("sbdsp_contdma: sc=0x%x\n", sc));
    957 
    958 	/* XXX how do we reinitialize the DMA controller state?  do we care? */
    959 	(void)sbdsp_wdsp(sc, SB_DSP_CONT);
    960 	return(0);
    961 }
    962 
    963 int
    964 sbdsp_setrate(sc, sr, isdac, ratep)
    965 	register struct sbdsp_softc *sc;
    966 	int sr;
    967 	int isdac;
    968 	int *ratep;
    969 {
    970 
    971 	/*
    972 	 * XXXX
    973 	 * More checks here?
    974 	 */
    975 	if (sr < 5000 || sr > 44100)
    976 		return (EINVAL);
    977 	*ratep = sr;
    978 	return (0);
    979 }
    980 
    981 /*
    982  * Convert a linear sampling rate into the DAC time constant.
    983  * Set *mode to indicate the high/low-speed DMA operation.
    984  * Because of limitations of the card, not all rates are possible.
    985  * We return the time constant of the closest possible rate.
    986  * The sampling rate limits are different for the DAC and ADC,
    987  * so isdac indicates output, and !isdac indicates input.
    988  */
    989 int
    990 sbdsp_srtotc(sc, sr, isdac, tcp, modep)
    991 	register struct sbdsp_softc *sc;
    992 	int sr;
    993 	int isdac;
    994 	int *tcp, *modep;
    995 {
    996 	int tc, realtc, mode;
    997 
    998 	/*
    999 	 * Don't forget to compute which mode we'll be in based on whether
   1000 	 * we need to double the rate for stereo on SBPRO.
   1001 	 */
   1002 
   1003 	if (sr == 0) {
   1004 		tc = SB_LS_MIN;
   1005 		mode = SB_ADAC_LS;
   1006 		goto out;
   1007 	}
   1008 
   1009 	tc = 256 - (1000000 / sr);
   1010 
   1011 	if (sc->sc_channels == 2 && ISSBPRO(sc))
   1012 		/* compute based on 2x sample rate when needed */
   1013 		realtc = 256 - ( 500000 / sr);
   1014 	else
   1015 		realtc = tc;
   1016 
   1017 	if (tc < SB_LS_MIN) {
   1018 		tc = SB_LS_MIN;
   1019 		mode = SB_ADAC_LS;	/* NB: 2x minimum speed is still low
   1020 					 * speed mode. */
   1021 		goto out;
   1022 	} else if (isdac) {
   1023 		if (realtc <= SB_DAC_LS_MAX)
   1024 			mode = SB_ADAC_LS;
   1025 		else {
   1026 			mode = SB_ADAC_HS;
   1027 			if (tc > SB_DAC_HS_MAX)
   1028 				tc = SB_DAC_HS_MAX;
   1029 		}
   1030 	} else {
   1031 		int adc_ls_max, adc_hs_max;
   1032 
   1033 		/* XXX use better rounding--compare distance to nearest tc on both
   1034 		   sides of requested speed */
   1035 		if (ISSBPROCLASS(sc)) {
   1036 			adc_ls_max = SBPRO_ADC_LS_MAX;
   1037 			adc_hs_max = SBPRO_ADC_HS_MAX;
   1038 		} else {
   1039 			adc_ls_max = SBCLA_ADC_LS_MAX;
   1040 			adc_hs_max = SBCLA_ADC_HS_MAX;
   1041 		}
   1042 
   1043 		if (realtc <= adc_ls_max)
   1044 			mode = SB_ADAC_LS;
   1045 		else {
   1046 			mode = SB_ADAC_HS;
   1047 			if (tc > adc_hs_max)
   1048 				tc = adc_hs_max;
   1049 		}
   1050 	}
   1051 
   1052 out:
   1053 	*tcp = tc;
   1054 	*modep = mode;
   1055 	return (0);
   1056 }
   1057 
   1058 /*
   1059  * Convert a DAC time constant to a sampling rate.
   1060  * See SBK, section 12.
   1061  */
   1062 int
   1063 sbdsp_tctosr(sc, tc)
   1064 	register struct sbdsp_softc *sc;
   1065 	int tc;
   1066 {
   1067 	int adc;
   1068 
   1069 	if (ISSBPROCLASS(sc))
   1070 		adc = SBPRO_ADC_HS_MAX;
   1071 	else
   1072 		adc = SBCLA_ADC_HS_MAX;
   1073 
   1074 	if (tc > adc)
   1075 		tc = adc;
   1076 
   1077 	return (1000000 / (256 - tc));
   1078 }
   1079 
   1080 int
   1081 sbdsp_set_timeconst(sc, tc)
   1082 	register struct sbdsp_softc *sc;
   1083 	int tc;
   1084 {
   1085 	/*
   1086 	 * A SBPro in stereo mode uses time constants at double the
   1087 	 * actual rate.
   1088 	 */
   1089 	if (ISSBPRO(sc) && sc->sc_channels == 2)
   1090 		tc = 256 - ((256 - tc) / 2);
   1091 
   1092 	DPRINTF(("sbdsp_set_timeconst: sc=%p tc=%d\n", sc, tc));
   1093 
   1094 	if (sbdsp_wdsp(sc, SB_DSP_TIMECONST) < 0 ||
   1095 	    sbdsp_wdsp(sc, tc) < 0)
   1096 		return (EIO);
   1097 
   1098 	return (0);
   1099 }
   1100 
   1101 int
   1102 sbdsp_dma_input(addr, p, cc, intr, arg)
   1103 	void *addr;
   1104 	void *p;
   1105 	int cc;
   1106 	void (*intr) __P((void *));
   1107 	void *arg;
   1108 {
   1109 	register struct sbdsp_softc *sc = addr;
   1110 
   1111 #ifdef AUDIO_DEBUG
   1112 	if (sbdspdebug > 1)
   1113 		Dprintf("sbdsp_dma_input: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
   1114 #endif
   1115 	if (sc->sc_channels == 2 && (cc & 1)) {
   1116 		DPRINTF(("sbdsp_dma_input: stereo input, odd bytecnt\n"));
   1117 		return EIO;
   1118 	}
   1119 
   1120 	if (sc->sc_dmadir != SB_DMA_IN) {
   1121 		if (ISSBPRO(sc)) {
   1122 			if (sc->sc_channels == 2) {
   1123 				if (ISJAZZ16(sc) && sc->sc_precision == 16) {
   1124 					if (sbdsp_wdsp(sc,
   1125 						       JAZZ16_RECORD_STEREO) < 0) {
   1126 						goto badmode;
   1127 					}
   1128 				} else if (sbdsp_wdsp(sc,
   1129 						      SB_DSP_RECORD_STEREO) < 0)
   1130 					goto badmode;
   1131 				sbdsp_mix_write(sc, SBP_INFILTER,
   1132 				    (sbdsp_mix_read(sc, SBP_INFILTER) &
   1133 				    ~SBP_IFILTER_MASK) | SBP_FILTER_OFF);
   1134 			} else {
   1135 				if (ISJAZZ16(sc) && sc->sc_precision == 16) {
   1136 					if (sbdsp_wdsp(sc,
   1137 						       JAZZ16_RECORD_MONO) < 0)
   1138 					{
   1139 						goto badmode;
   1140 					}
   1141 				} else if (sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0)
   1142 					goto badmode;
   1143 				sbdsp_mix_write(sc, SBP_INFILTER,
   1144 				    (sbdsp_mix_read(sc, SBP_INFILTER) &
   1145 				    ~SBP_IFILTER_MASK) | sc->in_filter);
   1146 			}
   1147 		}
   1148 
   1149 		if (ISSB16CLASS(sc)) {
   1150 			if (sbdsp_wdsp(sc, SB_DSP16_INPUTRATE) < 0 ||
   1151 			    sbdsp_wdsp(sc, sc->sc_irate >> 8) < 0 ||
   1152 			    sbdsp_wdsp(sc, sc->sc_irate) < 0)
   1153 				goto giveup;
   1154 		} else
   1155 			sbdsp_set_timeconst(sc, sc->sc_itc);
   1156 		sc->sc_dmadir = SB_DMA_IN;
   1157 	}
   1158 
   1159 	isa_dmastart(DMAMODE_READ, p, cc, sc->sc_drq);
   1160 	sc->sc_intr = intr;
   1161 	sc->sc_arg = arg;
   1162 	sc->dmaflags = DMAMODE_READ;
   1163 	sc->dmaaddr = p;
   1164 	sc->dmacnt = cc;		/* DMA controller is strange...? */
   1165 
   1166 	if ((ISSB16CLASS(sc) && sc->sc_precision == 16) ||
   1167 	    (ISJAZZ16(sc) && sc->sc_drq > 3))
   1168 		cc >>= 1;
   1169 	--cc;
   1170 	if (ISSB16CLASS(sc)) {
   1171 		if (sbdsp_wdsp(sc, sc->sc_precision == 16 ? SB_DSP16_RDMA_16 :
   1172 								SB_DSP16_RDMA_8) < 0 ||
   1173 		    sbdsp_wdsp(sc, (sc->sc_precision == 16 ? 0x10 : 0x00) |
   1174 				       (sc->sc_channels == 2 ? 0x20 : 0x00)) < 0 ||
   1175 		    sbdsp16_wait(sc) ||
   1176 		    sbdsp_wdsp(sc, cc) < 0 ||
   1177 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1178 			DPRINTF(("sbdsp_dma_input: SB16 DMA start failed\n"));
   1179 			goto giveup;
   1180 		}
   1181 	} else if (sc->sc_imode == SB_ADAC_LS) {
   1182 		if (sbdsp_wdsp(sc, SB_DSP_RDMA) < 0 ||
   1183 		    sbdsp_wdsp(sc, cc) < 0 ||
   1184 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1185 		        DPRINTF(("sbdsp_dma_input: LS DMA start failed\n"));
   1186 			goto giveup;
   1187 		}
   1188 	} else {
   1189 		if (cc != sc->sc_last_hs_size) {
   1190 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
   1191 			    sbdsp_wdsp(sc, cc) < 0 ||
   1192 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1193 				DPRINTF(("sbdsp_dma_input: HS DMA start failed\n"));
   1194 				goto giveup;
   1195 			}
   1196 			sc->sc_last_hs_size = cc;
   1197 		}
   1198 		if (sbdsp_wdsp(sc, SB_DSP_HS_INPUT) < 0) {
   1199 			DPRINTF(("sbdsp_dma_input: HS DMA restart failed\n"));
   1200 			goto giveup;
   1201 		}
   1202 	}
   1203 	return 0;
   1204 
   1205 giveup:
   1206 	sbdsp_reset(sc);
   1207 	return EIO;
   1208 
   1209 badmode:
   1210 	DPRINTF(("sbdsp_dma_input: can't set %s mode\n",
   1211 		 sc->sc_channels == 2 ? "stereo" : "mono"));
   1212 	return EIO;
   1213 }
   1214 
   1215 int
   1216 sbdsp_dma_output(addr, p, cc, intr, arg)
   1217 	void *addr;
   1218 	void *p;
   1219 	int cc;
   1220 	void (*intr) __P((void *));
   1221 	void *arg;
   1222 {
   1223 	register struct sbdsp_softc *sc = addr;
   1224 
   1225 #ifdef AUDIO_DEBUG
   1226 	if (sbdspdebug > 1)
   1227 		Dprintf("sbdsp_dma_output: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
   1228 #endif
   1229 	if (sc->sc_channels == 2 && (cc & 1)) {
   1230 		DPRINTF(("stereo playback odd bytes (%d)\n", cc));
   1231 		return EIO;
   1232 	}
   1233 
   1234 	if (sc->sc_dmadir != SB_DMA_OUT) {
   1235 		if (ISSBPRO(sc)) {
   1236 			/* make sure we re-set stereo mixer bit when we start
   1237 			   output. */
   1238 			sbdsp_mix_write(sc, SBP_STEREO,
   1239 			    (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
   1240 			    (sc->sc_channels == 2 ?  SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
   1241 			if (ISJAZZ16(sc)) {
   1242 				/* Yes, we write the record mode to set
   1243 				   16-bit playback mode. weird, huh? */
   1244 				if (sc->sc_precision == 16) {
   1245 					sbdsp_wdsp(sc,
   1246 						   sc->sc_channels == 2 ?
   1247 						   JAZZ16_RECORD_STEREO :
   1248 						   JAZZ16_RECORD_MONO);
   1249 				} else {
   1250 					sbdsp_wdsp(sc,
   1251 						   sc->sc_channels == 2 ?
   1252 						   SB_DSP_RECORD_STEREO :
   1253 						   SB_DSP_RECORD_MONO);
   1254 				}
   1255 			}
   1256 		}
   1257 
   1258 		if (ISSB16CLASS(sc)) {
   1259 			if (sbdsp_wdsp(sc, SB_DSP16_OUTPUTRATE) < 0 ||
   1260 			    sbdsp_wdsp(sc, sc->sc_orate >> 8) < 0 ||
   1261 			    sbdsp_wdsp(sc, sc->sc_orate) < 0)
   1262 				goto giveup;
   1263 		} else
   1264 			sbdsp_set_timeconst(sc, sc->sc_otc);
   1265 		sc->sc_dmadir = SB_DMA_OUT;
   1266 	}
   1267 
   1268 	isa_dmastart(DMAMODE_WRITE, p, cc, sc->sc_drq);
   1269 	sc->sc_intr = intr;
   1270 	sc->sc_arg = arg;
   1271 	sc->dmaflags = DMAMODE_WRITE;
   1272 	sc->dmaaddr = p;
   1273 	sc->dmacnt = cc;	/* a vagary of how DMA works, apparently. */
   1274 
   1275 	if ((ISSB16CLASS(sc) && sc->sc_precision == 16) ||
   1276 	    (ISJAZZ16(sc) && sc->sc_drq > 3))
   1277 		cc >>= 1;
   1278 	--cc;
   1279 	if (ISSB16CLASS(sc)) {
   1280 		if (sbdsp_wdsp(sc, sc->sc_precision == 16 ? SB_DSP16_WDMA_16 :
   1281 								SB_DSP16_WDMA_8) < 0 ||
   1282 		    sbdsp_wdsp(sc, (sc->sc_precision == 16 ? 0x10 : 0x00) |
   1283 				       (sc->sc_channels == 2 ? 0x20 : 0x00)) < 0 ||
   1284 		    sbdsp16_wait(sc) ||
   1285 		    sbdsp_wdsp(sc, cc) < 0 ||
   1286 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1287 			DPRINTF(("sbdsp_dma_output: SB16 DMA start failed\n"));
   1288 			goto giveup;
   1289 		}
   1290 	} else if (sc->sc_omode == SB_ADAC_LS) {
   1291 		if (sbdsp_wdsp(sc, SB_DSP_WDMA) < 0 ||
   1292 		    sbdsp_wdsp(sc, cc) < 0 ||
   1293 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1294 		        DPRINTF(("sbdsp_dma_output: LS DMA start failed\n"));
   1295 			goto giveup;
   1296 		}
   1297 	} else {
   1298 		if (cc != sc->sc_last_hs_size) {
   1299 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
   1300 			    sbdsp_wdsp(sc, cc) < 0 ||
   1301 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1302 				DPRINTF(("sbdsp_dma_output: HS DMA start failed\n"));
   1303 				goto giveup;
   1304 			}
   1305 			sc->sc_last_hs_size = cc;
   1306 		}
   1307 		if (sbdsp_wdsp(sc, SB_DSP_HS_OUTPUT) < 0) {
   1308 			DPRINTF(("sbdsp_dma_output: HS DMA restart failed\n"));
   1309 			goto giveup;
   1310 		}
   1311 	}
   1312 	return 0;
   1313 
   1314 giveup:
   1315 	sbdsp_reset(sc);
   1316 	return EIO;
   1317 }
   1318 
   1319 /*
   1320  * Only the DSP unit on the sound blaster generates interrupts.
   1321  * There are three cases of interrupt: reception of a midi byte
   1322  * (when mode is enabled), completion of dma transmission, or
   1323  * completion of a dma reception.  The three modes are mutually
   1324  * exclusive so we know a priori which event has occurred.
   1325  */
   1326 int
   1327 sbdsp_intr(arg)
   1328 	void *arg;
   1329 {
   1330 	register struct sbdsp_softc *sc = arg;
   1331 	u_char x;
   1332 
   1333 #ifdef AUDIO_DEBUG
   1334 	if (sbdspdebug > 1)
   1335 		Dprintf("sbdsp_intr: intr=0x%x\n", sc->sc_intr);
   1336 #endif
   1337 	if (!isa_dmafinished(sc->sc_drq)) {
   1338 #ifdef AUDIO_DEBUG
   1339 		printf("sbdsp_intr: not finished\n");
   1340 #endif
   1341 		return 0;
   1342 	}
   1343 	sc->sc_interrupts++;
   1344 	/* clear interrupt */
   1345 #ifdef notyet
   1346 	x = sbdsp_mix_read(sc, 0x82);
   1347 	x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 15);
   1348 #endif
   1349 	x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_RSTAT);
   1350 	delay(10);
   1351 #if 0
   1352 	if (sc->sc_mintr != 0) {
   1353 		x = sbdsp_rdsp(sc);
   1354 		(*sc->sc_mintr)(sc->sc_arg, x);
   1355 	} else
   1356 #endif
   1357 	if (sc->sc_intr != 0) {
   1358 		isa_dmadone(sc->dmaflags, sc->dmaaddr, sc->dmacnt, sc->sc_drq);
   1359 		(*sc->sc_intr)(sc->sc_arg);
   1360 	}
   1361 	else
   1362 		return 0;
   1363 	return 1;
   1364 }
   1365 
   1366 #if 0
   1367 /*
   1368  * Enter midi uart mode and arrange for read interrupts
   1369  * to vector to `intr'.  This puts the card in a mode
   1370  * which allows only midi I/O; the card must be reset
   1371  * to leave this mode.  Unfortunately, the card does not
   1372  * use transmit interrupts, so bytes must be output
   1373  * using polling.  To keep the polling overhead to a
   1374  * minimum, output should be driven off a timer.
   1375  * This is a little tricky since only 320us separate
   1376  * consecutive midi bytes.
   1377  */
   1378 void
   1379 sbdsp_set_midi_mode(sc, intr, arg)
   1380 	struct sbdsp_softc *sc;
   1381 	void (*intr)();
   1382 	void *arg;
   1383 {
   1384 
   1385 	sbdsp_wdsp(sc, SB_MIDI_UART_INTR);
   1386 	sc->sc_mintr = intr;
   1387 	sc->sc_intr = 0;
   1388 	sc->sc_arg = arg;
   1389 }
   1390 
   1391 /*
   1392  * Write a byte to the midi port, when in midi uart mode.
   1393  */
   1394 void
   1395 sbdsp_midi_output(sc, v)
   1396 	struct sbdsp_softc *sc;
   1397 	int v;
   1398 {
   1399 
   1400 	if (sbdsp_wdsp(sc, v) < 0)
   1401 		++sberr.wmidi;
   1402 }
   1403 #endif
   1404 
   1405 int
   1406 sbdsp_setfd(addr, flag)
   1407 	void *addr;
   1408 	int flag;
   1409 {
   1410 	/* Can't do full-duplex */
   1411 	return(ENOTTY);
   1412 }
   1413 
   1414 int
   1415 sbdsp_mixer_set_port(addr, cp)
   1416 	void *addr;
   1417 	mixer_ctrl_t *cp;
   1418 {
   1419 	register struct sbdsp_softc *sc = addr;
   1420 	int src, gain;
   1421 
   1422 	DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev,
   1423 	    cp->un.value.num_channels));
   1424 
   1425 	if (!ISSBPROCLASS(sc))
   1426 		return EINVAL;
   1427 
   1428 	/*
   1429 	 * Everything is a value except for SBPro BASS/TREBLE and
   1430 	 * RECORD_SOURCE
   1431 	 */
   1432 	switch (cp->dev) {
   1433 	case SB_SPEAKER:
   1434 		cp->dev = SB_MASTER_VOL;
   1435 	case SB_MIC_PORT:
   1436 	case SB_LINE_IN_PORT:
   1437 	case SB_DAC_PORT:
   1438 	case SB_FM_PORT:
   1439 	case SB_CD_PORT:
   1440 	case SB_MASTER_VOL:
   1441 		if (cp->type != AUDIO_MIXER_VALUE)
   1442 			return EINVAL;
   1443 
   1444 		/*
   1445 		 * All the mixer ports are stereo except for the microphone.
   1446 		 * If we get a single-channel gain value passed in, then we
   1447 		 * duplicate it to both left and right channels.
   1448 		 */
   1449 
   1450 		switch (cp->dev) {
   1451 		case SB_MIC_PORT:
   1452 			if (cp->un.value.num_channels != 1)
   1453 				return EINVAL;
   1454 
   1455 			/* handle funny microphone gain */
   1456 			gain = SBP_AGAIN_TO_MICGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1457 			break;
   1458 		case SB_LINE_IN_PORT:
   1459 		case SB_DAC_PORT:
   1460 		case SB_FM_PORT:
   1461 		case SB_CD_PORT:
   1462 		case SB_MASTER_VOL:
   1463 			switch (cp->un.value.num_channels) {
   1464 			case 1:
   1465 				gain = sbdsp_mono_vol(SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]));
   1466 				break;
   1467 			case 2:
   1468 				gain = sbdsp_stereo_vol(SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]),
   1469 							SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]));
   1470 				break;
   1471 			default:
   1472 				return EINVAL;
   1473 			}
   1474 			break;
   1475 		default:
   1476 			return EINVAL;
   1477 		}
   1478 
   1479 		switch (cp->dev) {
   1480 		case SB_MIC_PORT:
   1481 			src = SBP_MIC_VOL;
   1482 			break;
   1483 		case SB_MASTER_VOL:
   1484 			src = SBP_MASTER_VOL;
   1485 			break;
   1486 		case SB_LINE_IN_PORT:
   1487 			src = SBP_LINE_VOL;
   1488 			break;
   1489 		case SB_DAC_PORT:
   1490 			src = SBP_DAC_VOL;
   1491 			break;
   1492 		case SB_FM_PORT:
   1493 			src = SBP_FM_VOL;
   1494 			break;
   1495 		case SB_CD_PORT:
   1496 			src = SBP_CD_VOL;
   1497 			break;
   1498 		default:
   1499 			return EINVAL;
   1500 		}
   1501 
   1502 		sbdsp_mix_write(sc, src, gain);
   1503 		sc->gain[cp->dev] = gain;
   1504 		break;
   1505 
   1506 	case SB_TREBLE:
   1507 	case SB_BASS:
   1508 	case SB_RECORD_SOURCE:
   1509 		if (cp->type != AUDIO_MIXER_ENUM)
   1510 			return EINVAL;
   1511 
   1512 		switch (cp->dev) {
   1513 		case SB_TREBLE:
   1514 			return sbdsp_set_ifilter(addr, cp->un.ord ? SBP_TREBLE_EQ : 0);
   1515 		case SB_BASS:
   1516 			return sbdsp_set_ifilter(addr, cp->un.ord ? SBP_BASS_EQ : 0);
   1517 		case SB_RECORD_SOURCE:
   1518 			return sbdsp_set_in_port(addr, cp->un.ord);
   1519 		}
   1520 
   1521 		break;
   1522 
   1523 	default:
   1524 		return EINVAL;
   1525 	}
   1526 
   1527 	return (0);
   1528 }
   1529 
   1530 int
   1531 sbdsp_mixer_get_port(addr, cp)
   1532 	void *addr;
   1533 	mixer_ctrl_t *cp;
   1534 {
   1535 	register struct sbdsp_softc *sc = addr;
   1536 	int gain;
   1537 
   1538 	DPRINTF(("sbdsp_mixer_get_port: port=%d", cp->dev));
   1539 
   1540 	if (!ISSBPROCLASS(sc))
   1541 		return EINVAL;
   1542 
   1543 	switch (cp->dev) {
   1544 	case SB_SPEAKER:
   1545 		cp->dev = SB_MASTER_VOL;
   1546 	case SB_MIC_PORT:
   1547 	case SB_LINE_IN_PORT:
   1548 	case SB_DAC_PORT:
   1549 	case SB_FM_PORT:
   1550 	case SB_CD_PORT:
   1551 	case SB_MASTER_VOL:
   1552 		gain = sc->gain[cp->dev];
   1553 
   1554 		switch (cp->dev) {
   1555 		case SB_MIC_PORT:
   1556 			if (cp->un.value.num_channels != 1)
   1557 				return EINVAL;
   1558 
   1559 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = SBP_MICGAIN_TO_AGAIN(gain);
   1560 			break;
   1561 		case SB_LINE_IN_PORT:
   1562 		case SB_DAC_PORT:
   1563 		case SB_FM_PORT:
   1564 		case SB_CD_PORT:
   1565 		case SB_MASTER_VOL:
   1566 			switch (cp->un.value.num_channels) {
   1567 			case 1:
   1568 				cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = SBP_SBGAIN_TO_AGAIN(gain);
   1569 				break;
   1570 			case 2:
   1571 				cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = SBP_LEFTGAIN(gain);
   1572 				cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = SBP_RIGHTGAIN(gain);
   1573 				break;
   1574 			default:
   1575 				return EINVAL;
   1576 			}
   1577 			break;
   1578 		}
   1579 
   1580 		break;
   1581 
   1582 	case SB_TREBLE:
   1583 	case SB_BASS:
   1584 	case SB_RECORD_SOURCE:
   1585 		switch (cp->dev) {
   1586 		case SB_TREBLE:
   1587 			cp->un.ord = sbdsp_get_ifilter(addr) == SBP_TREBLE_EQ;
   1588 			return 0;
   1589 		case SB_BASS:
   1590 			cp->un.ord = sbdsp_get_ifilter(addr) == SBP_BASS_EQ;
   1591 			return 0;
   1592 		case SB_RECORD_SOURCE:
   1593 			cp->un.ord = sbdsp_get_in_port(addr);
   1594 			return 0;
   1595 		}
   1596 
   1597 		break;
   1598 
   1599 	default:
   1600 		return EINVAL;
   1601 	}
   1602 
   1603 	return (0);
   1604 }
   1605 
   1606 int
   1607 sbdsp_mixer_query_devinfo(addr, dip)
   1608 	void *addr;
   1609 	register mixer_devinfo_t *dip;
   1610 {
   1611 	register struct sbdsp_softc *sc = addr;
   1612 
   1613 	DPRINTF(("sbdsp_mixer_query_devinfo: index=%d\n", dip->index));
   1614 
   1615 	switch (dip->index) {
   1616 	case SB_MIC_PORT:
   1617 		dip->type = AUDIO_MIXER_VALUE;
   1618 		dip->mixer_class = SB_INPUT_CLASS;
   1619 		dip->prev = AUDIO_MIXER_LAST;
   1620 		dip->next = AUDIO_MIXER_LAST;
   1621 		strcpy(dip->label.name, AudioNmicrophone);
   1622 		dip->un.v.num_channels = 1;
   1623 		strcpy(dip->un.v.units.name, AudioNvolume);
   1624 		return 0;
   1625 
   1626 	case SB_SPEAKER:
   1627 		dip->type = AUDIO_MIXER_VALUE;
   1628 		dip->mixer_class = SB_OUTPUT_CLASS;
   1629 		dip->prev = AUDIO_MIXER_LAST;
   1630 		dip->next = AUDIO_MIXER_LAST;
   1631 		strcpy(dip->label.name, AudioNspeaker);
   1632 		dip->un.v.num_channels = 1;
   1633 		strcpy(dip->un.v.units.name, AudioNvolume);
   1634 		return 0;
   1635 
   1636 	case SB_INPUT_CLASS:
   1637 		dip->type = AUDIO_MIXER_CLASS;
   1638 		dip->mixer_class = SB_INPUT_CLASS;
   1639 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1640 		strcpy(dip->label.name, AudioCInputs);
   1641 		return 0;
   1642 
   1643 	case SB_OUTPUT_CLASS:
   1644 		dip->type = AUDIO_MIXER_CLASS;
   1645 		dip->mixer_class = SB_OUTPUT_CLASS;
   1646 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1647 		strcpy(dip->label.name, AudioCOutputs);
   1648 		return 0;
   1649 	}
   1650 
   1651 	if (ISSBPROCLASS(sc)) {
   1652 		switch (dip->index) {
   1653 		case SB_LINE_IN_PORT:
   1654 			dip->type = AUDIO_MIXER_VALUE;
   1655 			dip->mixer_class = SB_INPUT_CLASS;
   1656 			dip->prev = AUDIO_MIXER_LAST;
   1657 			dip->next = AUDIO_MIXER_LAST;
   1658 			strcpy(dip->label.name, AudioNline);
   1659 			dip->un.v.num_channels = 2;
   1660 			strcpy(dip->un.v.units.name, AudioNvolume);
   1661 			return 0;
   1662 
   1663 		case SB_DAC_PORT:
   1664 			dip->type = AUDIO_MIXER_VALUE;
   1665 			dip->mixer_class = SB_INPUT_CLASS;
   1666 			dip->prev = AUDIO_MIXER_LAST;
   1667 			dip->next = AUDIO_MIXER_LAST;
   1668 			strcpy(dip->label.name, AudioNdac);
   1669 			dip->un.v.num_channels = 2;
   1670 			strcpy(dip->un.v.units.name, AudioNvolume);
   1671 			return 0;
   1672 
   1673 		case SB_CD_PORT:
   1674 			dip->type = AUDIO_MIXER_VALUE;
   1675 			dip->mixer_class = SB_INPUT_CLASS;
   1676 			dip->prev = AUDIO_MIXER_LAST;
   1677 			dip->next = AUDIO_MIXER_LAST;
   1678 			strcpy(dip->label.name, AudioNcd);
   1679 			dip->un.v.num_channels = 2;
   1680 			strcpy(dip->un.v.units.name, AudioNvolume);
   1681 			return 0;
   1682 
   1683 		case SB_FM_PORT:
   1684 			dip->type = AUDIO_MIXER_VALUE;
   1685 			dip->mixer_class = SB_INPUT_CLASS;
   1686 			dip->prev = AUDIO_MIXER_LAST;
   1687 			dip->next = AUDIO_MIXER_LAST;
   1688 			strcpy(dip->label.name, AudioNfmsynth);
   1689 			dip->un.v.num_channels = 2;
   1690 			strcpy(dip->un.v.units.name, AudioNvolume);
   1691 			return 0;
   1692 
   1693 		case SB_MASTER_VOL:
   1694 			dip->type = AUDIO_MIXER_VALUE;
   1695 			dip->mixer_class = SB_OUTPUT_CLASS;
   1696 			dip->prev = AUDIO_MIXER_LAST;
   1697 			dip->next = AUDIO_MIXER_LAST;
   1698 			strcpy(dip->label.name, AudioNvolume);
   1699 			dip->un.v.num_channels = 2;
   1700 			strcpy(dip->un.v.units.name, AudioNvolume);
   1701 			return 0;
   1702 
   1703 		case SB_RECORD_SOURCE:
   1704 			dip->mixer_class = SB_RECORD_CLASS;
   1705 			dip->type = AUDIO_MIXER_ENUM;
   1706 			dip->prev = AUDIO_MIXER_LAST;
   1707 			dip->next = AUDIO_MIXER_LAST;
   1708 			strcpy(dip->label.name, AudioNsource);
   1709 			dip->un.e.num_mem = 3;
   1710 			strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
   1711 			dip->un.e.member[0].ord = SB_MIC_PORT;
   1712 			strcpy(dip->un.e.member[1].label.name, AudioNcd);
   1713 			dip->un.e.member[1].ord = SB_CD_PORT;
   1714 			strcpy(dip->un.e.member[2].label.name, AudioNline);
   1715 			dip->un.e.member[2].ord = SB_LINE_IN_PORT;
   1716 			return 0;
   1717 
   1718 		case SB_BASS:
   1719 			dip->type = AUDIO_MIXER_ENUM;
   1720 			dip->mixer_class = SB_INPUT_CLASS;
   1721 			dip->prev = AUDIO_MIXER_LAST;
   1722 			dip->next = AUDIO_MIXER_LAST;
   1723 			strcpy(dip->label.name, AudioNbass);
   1724 			dip->un.e.num_mem = 2;
   1725 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1726 			dip->un.e.member[0].ord = 0;
   1727 			strcpy(dip->un.e.member[1].label.name, AudioNon);
   1728 			dip->un.e.member[1].ord = 1;
   1729 			return 0;
   1730 
   1731 		case SB_TREBLE:
   1732 			dip->type = AUDIO_MIXER_ENUM;
   1733 			dip->mixer_class = SB_INPUT_CLASS;
   1734 			dip->prev = AUDIO_MIXER_LAST;
   1735 			dip->next = AUDIO_MIXER_LAST;
   1736 			strcpy(dip->label.name, AudioNtreble);
   1737 			dip->un.e.num_mem = 2;
   1738 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1739 			dip->un.e.member[0].ord = 0;
   1740 			strcpy(dip->un.e.member[1].label.name, AudioNon);
   1741 			dip->un.e.member[1].ord = 1;
   1742 			return 0;
   1743 
   1744 		case SB_RECORD_CLASS:			/* record source class */
   1745 			dip->type = AUDIO_MIXER_CLASS;
   1746 			dip->mixer_class = SB_RECORD_CLASS;
   1747 			dip->next = dip->prev = AUDIO_MIXER_LAST;
   1748 			strcpy(dip->label.name, AudioCRecord);
   1749 			return 0;
   1750 		}
   1751 	}
   1752 
   1753 	return ENXIO;
   1754 }
   1755