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