Home | History | Annotate | Line # | Download | only in isa
sbdsp.c revision 1.38
      1 /*	$NetBSD: sbdsp.c,v 1.38 1997/03/20 20:15:25 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. */
    635 	if (blk > NBPG)
    636 		blk = NBPG;
    637 
    638 	/* Round to a multiple of the sample size. */
    639 	blk &= -(sc->sc_channels * sc->sc_precision / 8);
    640 
    641 	return (blk);
    642 }
    643 
    644 int
    645 sbdsp_commit_settings(addr)
    646 	void *addr;
    647 {
    648 	register struct sbdsp_softc *sc = addr;
    649 
    650 	/* due to potentially unfortunate ordering in the above layers,
    651 	   re-do a few sets which may be important--input gains
    652 	   (adjust the proper channels), number of input channels (hit the
    653 	   record rate and set mode) */
    654 
    655 	if (ISSBPRO(sc)) {
    656 		/*
    657 		 * With 2 channels, SBPro can't do more than 22kHz.
    658 		 * Whack the rates down to speed if necessary.
    659 		 * Reset the time constant anyway
    660 		 * because it may have been adjusted with a different number
    661 		 * of channels, which means it might have computed the wrong
    662 		 * mode (low/high speed).
    663 		 */
    664 		if (sc->sc_channels == 2 &&
    665 		    sbdsp_tctosr(sc, sc->sc_itc) > 22727) {
    666 			sbdsp_srtotc(sc, 22727, SB_INPUT_RATE,
    667 				     &sc->sc_itc, &sc->sc_imode);
    668 		} else
    669 			sbdsp_srtotc(sc, sbdsp_tctosr(sc, sc->sc_itc),
    670 				     SB_INPUT_RATE, &sc->sc_itc,
    671 				     &sc->sc_imode);
    672 
    673 		if (sc->sc_channels == 2 &&
    674 		    sbdsp_tctosr(sc, sc->sc_otc) > 22727) {
    675 			sbdsp_srtotc(sc, 22727, SB_OUTPUT_RATE,
    676 				     &sc->sc_otc, &sc->sc_omode);
    677 		} else
    678 			sbdsp_srtotc(sc, sbdsp_tctosr(sc, sc->sc_otc),
    679 				     SB_OUTPUT_RATE, &sc->sc_otc,
    680 				     &sc->sc_omode);
    681 	}
    682 
    683 	/*
    684 	 * XXX
    685 	 * Should wait for chip to be idle.
    686 	 */
    687 	sc->sc_dmadir = SB_DMA_NONE;
    688 
    689 	return 0;
    690 }
    691 
    692 
    693 int
    694 sbdsp_open(sc, dev, flags)
    695 	register struct sbdsp_softc *sc;
    696 	dev_t dev;
    697 	int flags;
    698 {
    699         DPRINTF(("sbdsp_open: sc=0x%x\n", sc));
    700 
    701 	if (sc->sc_open != 0 || sbdsp_reset(sc) != 0)
    702 		return ENXIO;
    703 
    704 	sc->sc_open = 1;
    705 	sc->sc_mintr = 0;
    706 	if (ISSBPROCLASS(sc) &&
    707 	    sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0) {
    708 		DPRINTF(("sbdsp_open: can't set mono mode\n"));
    709 		/* we'll readjust when it's time for DMA. */
    710 	}
    711 
    712 	/*
    713 	 * Leave most things as they were; users must change things if
    714 	 * the previous process didn't leave it they way they wanted.
    715 	 * Looked at another way, it's easy to set up a configuration
    716 	 * in one program and leave it for another to inherit.
    717 	 */
    718 	DPRINTF(("sbdsp_open: opened\n"));
    719 
    720 	return 0;
    721 }
    722 
    723 void
    724 sbdsp_close(addr)
    725 	void *addr;
    726 {
    727 	struct sbdsp_softc *sc = addr;
    728 
    729         DPRINTF(("sbdsp_close: sc=0x%x\n", sc));
    730 
    731 	sc->sc_open = 0;
    732 	sbdsp_spkroff(sc);
    733 	sc->spkr_state = SPKR_OFF;
    734 	sc->sc_mintr = 0;
    735 	sbdsp_haltdma(sc);
    736 
    737 	DPRINTF(("sbdsp_close: closed\n"));
    738 }
    739 
    740 /*
    741  * Lower-level routines
    742  */
    743 
    744 /*
    745  * Reset the card.
    746  * Return non-zero if the card isn't detected.
    747  */
    748 int
    749 sbdsp_reset(sc)
    750 	register struct sbdsp_softc *sc;
    751 {
    752 	bus_space_tag_t iot = sc->sc_iot;
    753 	bus_space_handle_t ioh = sc->sc_ioh;
    754 
    755 	sc->sc_intr = 0;
    756 	if (sc->sc_dmadir != SB_DMA_NONE) {
    757 		isa_dmaabort(sc->sc_drq);
    758 		sc->sc_dmadir = SB_DMA_NONE;
    759 	}
    760 	sc->sc_last_hs_size = 0;
    761 
    762 	/*
    763 	 * See SBK, section 11.3.
    764 	 * We pulse a reset signal into the card.
    765 	 * Gee, what a brilliant hardware design.
    766 	 */
    767 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 1);
    768 	delay(10);
    769 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 0);
    770 	delay(30);
    771 	if (sbdsp_rdsp(sc) != SB_MAGIC)
    772 		return -1;
    773 
    774 	return 0;
    775 }
    776 
    777 int
    778 sbdsp16_wait(sc)
    779 	struct sbdsp_softc *sc;
    780 {
    781 	bus_space_tag_t iot = sc->sc_iot;
    782 	bus_space_handle_t ioh = sc->sc_ioh;
    783 	register int i;
    784 
    785 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    786 		register u_char x;
    787 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
    788 		delay(10);
    789 		if ((x & SB_DSP_BUSY) == 0)
    790 			continue;
    791 		return 0;
    792 	}
    793 	++sberr.wdsp;
    794 	return -1;
    795 }
    796 
    797 /*
    798  * Write a byte to the dsp.
    799  * XXX We are at the mercy of the card as we use a
    800  * polling loop and wait until it can take the byte.
    801  */
    802 int
    803 sbdsp_wdsp(sc, v)
    804 	struct sbdsp_softc *sc;
    805 	int v;
    806 {
    807 	bus_space_tag_t iot = sc->sc_iot;
    808 	bus_space_handle_t ioh = sc->sc_ioh;
    809 	register int i;
    810 
    811 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    812 		register u_char x;
    813 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
    814 		delay(10);
    815 		if ((x & SB_DSP_BUSY) != 0)
    816 			continue;
    817 		bus_space_write_1(iot, ioh, SBP_DSP_WRITE, v);
    818 		delay(10);
    819 		return 0;
    820 	}
    821 	++sberr.wdsp;
    822 	return -1;
    823 }
    824 
    825 /*
    826  * Read a byte from the DSP, using polling.
    827  */
    828 int
    829 sbdsp_rdsp(sc)
    830 	struct sbdsp_softc *sc;
    831 {
    832 	bus_space_tag_t iot = sc->sc_iot;
    833 	bus_space_handle_t ioh = sc->sc_ioh;
    834 	register int i;
    835 
    836 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    837 		register u_char x;
    838 		x = bus_space_read_1(iot, ioh, SBP_DSP_RSTAT);
    839 		delay(10);
    840 		if ((x & SB_DSP_READY) == 0)
    841 			continue;
    842 		x = bus_space_read_1(iot, ioh, SBP_DSP_READ);
    843 		delay(10);
    844 		return x;
    845 	}
    846 	++sberr.rdsp;
    847 	return -1;
    848 }
    849 
    850 /*
    851  * Doing certain things (like toggling the speaker) make
    852  * the SB hardware go away for a while, so pause a little.
    853  */
    854 void
    855 sbdsp_to(arg)
    856 	void *arg;
    857 {
    858 	wakeup(arg);
    859 }
    860 
    861 void
    862 sbdsp_pause(sc)
    863 	struct sbdsp_softc *sc;
    864 {
    865 	extern int hz;
    866 
    867 	timeout(sbdsp_to, sbdsp_to, hz/8);
    868 	(void)tsleep(sbdsp_to, PWAIT, "sbpause", 0);
    869 }
    870 
    871 /*
    872  * Turn on the speaker.  The SBK documention says this operation
    873  * can take up to 1/10 of a second.  Higher level layers should
    874  * probably let the task sleep for this amount of time after
    875  * calling here.  Otherwise, things might not work (because
    876  * sbdsp_wdsp() and sbdsp_rdsp() will probably timeout.)
    877  *
    878  * These engineers had their heads up their ass when
    879  * they designed this card.
    880  */
    881 void
    882 sbdsp_spkron(sc)
    883 	struct sbdsp_softc *sc;
    884 {
    885 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_ON);
    886 	sbdsp_pause(sc);
    887 }
    888 
    889 /*
    890  * Turn off the speaker; see comment above.
    891  */
    892 void
    893 sbdsp_spkroff(sc)
    894 	struct sbdsp_softc *sc;
    895 {
    896 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_OFF);
    897 	sbdsp_pause(sc);
    898 }
    899 
    900 /*
    901  * Read the version number out of the card.  Return major code
    902  * in high byte, and minor code in low byte.
    903  */
    904 short
    905 sbversion(sc)
    906 	struct sbdsp_softc *sc;
    907 {
    908 	short v;
    909 
    910 	if (sbdsp_wdsp(sc, SB_DSP_VERSION) < 0)
    911 		return 0;
    912 	v = sbdsp_rdsp(sc) << 8;
    913 	v |= sbdsp_rdsp(sc);
    914 	return ((v >= 0) ? v : 0);
    915 }
    916 
    917 /*
    918  * Halt a DMA in progress.  A low-speed transfer can be
    919  * resumed with sbdsp_contdma().
    920  */
    921 int
    922 sbdsp_haltdma(addr)
    923 	void *addr;
    924 {
    925 	register struct sbdsp_softc *sc = addr;
    926 
    927 	DPRINTF(("sbdsp_haltdma: sc=0x%x\n", sc));
    928 
    929 	sbdsp_reset(sc);
    930 	return 0;
    931 }
    932 
    933 int
    934 sbdsp_contdma(addr)
    935 	void *addr;
    936 {
    937 	register struct sbdsp_softc *sc = addr;
    938 
    939 	DPRINTF(("sbdsp_contdma: sc=0x%x\n", sc));
    940 
    941 	/* XXX how do we reinitialize the DMA controller state?  do we care? */
    942 	(void)sbdsp_wdsp(sc, SB_DSP_CONT);
    943 	return(0);
    944 }
    945 
    946 int
    947 sbdsp16_setrate(sc, sr, isdac, ratep)
    948 	register struct sbdsp_softc *sc;
    949 	int sr;
    950 	int isdac;
    951 	int *ratep;
    952 {
    953 
    954 	/*
    955 	 * XXXX
    956 	 * More checks here?
    957 	 */
    958 	if (sr < 5000 || sr > 45454)
    959 		return (EINVAL);
    960 	*ratep = sr;
    961 	return (0);
    962 }
    963 
    964 /*
    965  * Convert a linear sampling rate into the DAC time constant.
    966  * Set *mode to indicate the high/low-speed DMA operation.
    967  * Because of limitations of the card, not all rates are possible.
    968  * We return the time constant of the closest possible rate.
    969  * The sampling rate limits are different for the DAC and ADC,
    970  * so isdac indicates output, and !isdac indicates input.
    971  */
    972 int
    973 sbdsp_srtotc(sc, sr, isdac, tcp, modep)
    974 	register struct sbdsp_softc *sc;
    975 	int sr;
    976 	int isdac;
    977 	int *tcp, *modep;
    978 {
    979 	int tc, realtc, mode;
    980 
    981 	/*
    982 	 * Don't forget to compute which mode we'll be in based on whether
    983 	 * we need to double the rate for stereo on SBPRO.
    984 	 */
    985 
    986 	if (sr == 0) {
    987 		tc = SB_LS_MIN;
    988 		mode = SB_ADAC_LS;
    989 		goto out;
    990 	}
    991 
    992 	tc = 256 - (1000000 / sr);
    993 
    994 	if (sc->sc_channels == 2 && ISSBPRO(sc))
    995 		/* compute based on 2x sample rate when needed */
    996 		realtc = 256 - ( 500000 / sr);
    997 	else
    998 		realtc = tc;
    999 
   1000 	if (tc < SB_LS_MIN) {
   1001 		tc = SB_LS_MIN;
   1002 		mode = SB_ADAC_LS;	/* NB: 2x minimum speed is still low
   1003 					 * speed mode. */
   1004 		goto out;
   1005 	} else if (isdac) {
   1006 		if (realtc <= SB_DAC_LS_MAX)
   1007 			mode = SB_ADAC_LS;
   1008 		else {
   1009 			mode = SB_ADAC_HS;
   1010 			if (tc > SB_DAC_HS_MAX)
   1011 				tc = SB_DAC_HS_MAX;
   1012 		}
   1013 	} else {
   1014 		int adc_ls_max, adc_hs_max;
   1015 
   1016 		/* XXX use better rounding--compare distance to nearest tc on both
   1017 		   sides of requested speed */
   1018 		if (ISSBPROCLASS(sc)) {
   1019 			adc_ls_max = SBPRO_ADC_LS_MAX;
   1020 			adc_hs_max = SBPRO_ADC_HS_MAX;
   1021 		} else {
   1022 			adc_ls_max = SBCLA_ADC_LS_MAX;
   1023 			adc_hs_max = SBCLA_ADC_HS_MAX;
   1024 		}
   1025 
   1026 		if (realtc <= adc_ls_max)
   1027 			mode = SB_ADAC_LS;
   1028 		else {
   1029 			mode = SB_ADAC_HS;
   1030 			if (tc > adc_hs_max)
   1031 				tc = adc_hs_max;
   1032 		}
   1033 	}
   1034 
   1035 out:
   1036 	*tcp = tc;
   1037 	*modep = mode;
   1038 	return (0);
   1039 }
   1040 
   1041 /*
   1042  * Convert a DAC time constant to a sampling rate.
   1043  * See SBK, section 12.
   1044  */
   1045 int
   1046 sbdsp_tctosr(sc, tc)
   1047 	register struct sbdsp_softc *sc;
   1048 	int tc;
   1049 {
   1050 	int adc;
   1051 
   1052 	if (ISSBPROCLASS(sc))
   1053 		adc = SBPRO_ADC_HS_MAX;
   1054 	else
   1055 		adc = SBCLA_ADC_HS_MAX;
   1056 
   1057 	if (tc > adc)
   1058 		tc = adc;
   1059 
   1060 	return (1000000 / (256 - tc));
   1061 }
   1062 
   1063 int
   1064 sbdsp_set_timeconst(sc, tc)
   1065 	register struct sbdsp_softc *sc;
   1066 	int tc;
   1067 {
   1068 	/*
   1069 	 * A SBPro in stereo mode uses time constants at double the
   1070 	 * actual rate.
   1071 	 */
   1072 	if (ISSBPRO(sc) && sc->sc_channels == 2)
   1073 		tc = 256 - ((256 - tc) / 2);
   1074 
   1075 	DPRINTF(("sbdsp_set_timeconst: sc=%p tc=%d\n", sc, tc));
   1076 
   1077 	if (sbdsp_wdsp(sc, SB_DSP_TIMECONST) < 0 ||
   1078 	    sbdsp_wdsp(sc, tc) < 0)
   1079 		return (EIO);
   1080 
   1081 	return (0);
   1082 }
   1083 
   1084 int
   1085 sbdsp_dma_input(addr, p, cc, intr, arg)
   1086 	void *addr;
   1087 	void *p;
   1088 	int cc;
   1089 	void (*intr) __P((void *));
   1090 	void *arg;
   1091 {
   1092 	register struct sbdsp_softc *sc = addr;
   1093 
   1094 #ifdef AUDIO_DEBUG
   1095 	if (sbdspdebug > 1)
   1096 		Dprintf("sbdsp_dma_input: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
   1097 #endif
   1098 	if (sc->sc_channels == 2 && (cc & 1)) {
   1099 		DPRINTF(("sbdsp_dma_input: stereo input, odd bytecnt\n"));
   1100 		return EIO;
   1101 	}
   1102 
   1103 	if (sc->sc_dmadir != SB_DMA_IN) {
   1104 		if (ISSBPRO(sc)) {
   1105 			if (sc->sc_channels == 2) {
   1106 				if (ISJAZZ16(sc) && sc->sc_precision == 16) {
   1107 					if (sbdsp_wdsp(sc,
   1108 						       JAZZ16_RECORD_STEREO) < 0) {
   1109 						goto badmode;
   1110 					}
   1111 				} else if (sbdsp_wdsp(sc,
   1112 						      SB_DSP_RECORD_STEREO) < 0)
   1113 					goto badmode;
   1114 				sbdsp_mix_write(sc, SBP_INFILTER,
   1115 				    (sbdsp_mix_read(sc, SBP_INFILTER) &
   1116 				    ~SBP_IFILTER_MASK) | SBP_FILTER_OFF);
   1117 			} else {
   1118 				if (ISJAZZ16(sc) && sc->sc_precision == 16) {
   1119 					if (sbdsp_wdsp(sc,
   1120 						       JAZZ16_RECORD_MONO) < 0)
   1121 					{
   1122 						goto badmode;
   1123 					}
   1124 				} else if (sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0)
   1125 					goto badmode;
   1126 				sbdsp_mix_write(sc, SBP_INFILTER,
   1127 				    (sbdsp_mix_read(sc, SBP_INFILTER) &
   1128 				    ~SBP_IFILTER_MASK) | sc->in_filter);
   1129 			}
   1130 		}
   1131 
   1132 		if (ISSB16CLASS(sc)) {
   1133 			if (sbdsp_wdsp(sc, SB_DSP16_INPUTRATE) < 0 ||
   1134 			    sbdsp_wdsp(sc, sc->sc_irate >> 8) < 0 ||
   1135 			    sbdsp_wdsp(sc, sc->sc_irate) < 0)
   1136 				goto giveup;
   1137 		} else
   1138 			sbdsp_set_timeconst(sc, sc->sc_itc);
   1139 		sc->sc_dmadir = SB_DMA_IN;
   1140 	}
   1141 
   1142 	sc->sc_drq = sc->sc_precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
   1143 	isa_dmastart(DMAMODE_READ, p, cc, sc->sc_drq);
   1144 	sc->sc_intr = intr;
   1145 	sc->sc_arg = arg;
   1146 	sc->dmaflags = DMAMODE_READ;
   1147 	sc->dmaaddr = p;
   1148 	sc->dmacnt = cc;		/* DMA controller is strange...? */
   1149 
   1150 	if (sc->sc_precision == 16)
   1151 		cc >>= 1;
   1152 	--cc;
   1153 	if (ISSB16CLASS(sc)) {
   1154 		if (sbdsp_wdsp(sc, sc->sc_precision == 16 ? SB_DSP16_RDMA_16 :
   1155 							    SB_DSP16_RDMA_8) < 0 ||
   1156 		    sbdsp_wdsp(sc, (sc->sc_precision == 16 ? 0x10 : 0x00) |
   1157 				       (sc->sc_channels == 2 ? 0x20 : 0x00)) < 0 ||
   1158 		    sbdsp16_wait(sc) ||
   1159 		    sbdsp_wdsp(sc, cc) < 0 ||
   1160 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1161 			DPRINTF(("sbdsp_dma_input: SB16 DMA start failed\n"));
   1162 			goto giveup;
   1163 		}
   1164 	} else if (sc->sc_imode == SB_ADAC_LS) {
   1165 		if (sbdsp_wdsp(sc, SB_DSP_RDMA) < 0 ||
   1166 		    sbdsp_wdsp(sc, cc) < 0 ||
   1167 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1168 		        DPRINTF(("sbdsp_dma_input: LS DMA start failed\n"));
   1169 			goto giveup;
   1170 		}
   1171 	} else {
   1172 		if (cc != sc->sc_last_hs_size) {
   1173 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
   1174 			    sbdsp_wdsp(sc, cc) < 0 ||
   1175 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1176 				DPRINTF(("sbdsp_dma_input: HS DMA start failed\n"));
   1177 				goto giveup;
   1178 			}
   1179 			sc->sc_last_hs_size = cc;
   1180 		}
   1181 		if (sbdsp_wdsp(sc, SB_DSP_HS_INPUT) < 0) {
   1182 			DPRINTF(("sbdsp_dma_input: HS DMA restart failed\n"));
   1183 			goto giveup;
   1184 		}
   1185 	}
   1186 	return 0;
   1187 
   1188 giveup:
   1189 	sbdsp_reset(sc);
   1190 	return EIO;
   1191 
   1192 badmode:
   1193 	DPRINTF(("sbdsp_dma_input: can't set %s mode\n",
   1194 		 sc->sc_channels == 2 ? "stereo" : "mono"));
   1195 	return EIO;
   1196 }
   1197 
   1198 int
   1199 sbdsp_dma_output(addr, p, cc, intr, arg)
   1200 	void *addr;
   1201 	void *p;
   1202 	int cc;
   1203 	void (*intr) __P((void *));
   1204 	void *arg;
   1205 {
   1206 	register struct sbdsp_softc *sc = addr;
   1207 
   1208 #ifdef AUDIO_DEBUG
   1209 	if (sbdspdebug > 1)
   1210 		Dprintf("sbdsp_dma_output: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
   1211 #endif
   1212 	if (sc->sc_channels == 2 && (cc & 1)) {
   1213 		DPRINTF(("stereo playback odd bytes (%d)\n", cc));
   1214 		return EIO;
   1215 	}
   1216 
   1217 	if (sc->sc_dmadir != SB_DMA_OUT) {
   1218 		if (ISSBPRO(sc)) {
   1219 			/* make sure we re-set stereo mixer bit when we start
   1220 			   output. */
   1221 			sbdsp_mix_write(sc, SBP_STEREO,
   1222 			    (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
   1223 			    (sc->sc_channels == 2 ?  SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
   1224 			if (ISJAZZ16(sc)) {
   1225 				/* Yes, we write the record mode to set
   1226 				   16-bit playback mode. weird, huh? */
   1227 				if (sc->sc_precision == 16) {
   1228 					sbdsp_wdsp(sc,
   1229 						   sc->sc_channels == 2 ?
   1230 						   JAZZ16_RECORD_STEREO :
   1231 						   JAZZ16_RECORD_MONO);
   1232 				} else {
   1233 					sbdsp_wdsp(sc,
   1234 						   sc->sc_channels == 2 ?
   1235 						   SB_DSP_RECORD_STEREO :
   1236 						   SB_DSP_RECORD_MONO);
   1237 				}
   1238 			}
   1239 		}
   1240 
   1241 		if (ISSB16CLASS(sc)) {
   1242 			if (sbdsp_wdsp(sc, SB_DSP16_OUTPUTRATE) < 0 ||
   1243 			    sbdsp_wdsp(sc, sc->sc_orate >> 8) < 0 ||
   1244 			    sbdsp_wdsp(sc, sc->sc_orate) < 0)
   1245 				goto giveup;
   1246 		} else
   1247 			sbdsp_set_timeconst(sc, sc->sc_otc);
   1248 		sc->sc_dmadir = SB_DMA_OUT;
   1249 	}
   1250 
   1251 	sc->sc_drq = sc->sc_precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
   1252 	isa_dmastart(DMAMODE_WRITE, p, cc, sc->sc_drq);
   1253 	sc->sc_intr = intr;
   1254 	sc->sc_arg = arg;
   1255 	sc->dmaflags = DMAMODE_WRITE;
   1256 	sc->dmaaddr = p;
   1257 	sc->dmacnt = cc;	/* a vagary of how DMA works, apparently. */
   1258 
   1259 	if (sc->sc_precision == 16)
   1260 		cc >>= 1;
   1261 	--cc;
   1262 	if (ISSB16CLASS(sc)) {
   1263 		if (sbdsp_wdsp(sc, sc->sc_precision == 16 ? SB_DSP16_WDMA_16 :
   1264 							    SB_DSP16_WDMA_8) < 0 ||
   1265 		    sbdsp_wdsp(sc, (sc->sc_precision == 16 ? 0x10 : 0x00) |
   1266 				       (sc->sc_channels == 2 ? 0x20 : 0x00)) < 0 ||
   1267 		    sbdsp16_wait(sc) ||
   1268 		    sbdsp_wdsp(sc, cc) < 0 ||
   1269 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1270 			DPRINTF(("sbdsp_dma_output: SB16 DMA start failed\n"));
   1271 			goto giveup;
   1272 		}
   1273 	} else if (sc->sc_omode == SB_ADAC_LS) {
   1274 		if (sbdsp_wdsp(sc, SB_DSP_WDMA) < 0 ||
   1275 		    sbdsp_wdsp(sc, cc) < 0 ||
   1276 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1277 		        DPRINTF(("sbdsp_dma_output: LS DMA start failed\n"));
   1278 			goto giveup;
   1279 		}
   1280 	} else {
   1281 		if (cc != sc->sc_last_hs_size) {
   1282 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
   1283 			    sbdsp_wdsp(sc, cc) < 0 ||
   1284 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1285 				DPRINTF(("sbdsp_dma_output: HS DMA start failed\n"));
   1286 				goto giveup;
   1287 			}
   1288 			sc->sc_last_hs_size = cc;
   1289 		}
   1290 		if (sbdsp_wdsp(sc, SB_DSP_HS_OUTPUT) < 0) {
   1291 			DPRINTF(("sbdsp_dma_output: HS DMA restart failed\n"));
   1292 			goto giveup;
   1293 		}
   1294 	}
   1295 	return 0;
   1296 
   1297 giveup:
   1298 	sbdsp_reset(sc);
   1299 	return EIO;
   1300 }
   1301 
   1302 /*
   1303  * Only the DSP unit on the sound blaster generates interrupts.
   1304  * There are three cases of interrupt: reception of a midi byte
   1305  * (when mode is enabled), completion of dma transmission, or
   1306  * completion of a dma reception.  The three modes are mutually
   1307  * exclusive so we know a priori which event has occurred.
   1308  */
   1309 int
   1310 sbdsp_intr(arg)
   1311 	void *arg;
   1312 {
   1313 	register struct sbdsp_softc *sc = arg;
   1314 	u_char x;
   1315 
   1316 #ifdef AUDIO_DEBUG
   1317 	if (sbdspdebug > 1)
   1318 		Dprintf("sbdsp_intr: intr=0x%x\n", sc->sc_intr);
   1319 #endif
   1320 	if (!isa_dmafinished(sc->sc_drq)) {
   1321 #ifdef AUDIO_DEBUG
   1322 		printf("sbdsp_intr: not finished\n");
   1323 #endif
   1324 		return 0;
   1325 	}
   1326 	sc->sc_interrupts++;
   1327 	delay(10);
   1328 #if 0
   1329 	if (sc->sc_mintr != 0) {
   1330 		x = sbdsp_rdsp(sc);
   1331 		(*sc->sc_mintr)(sc->sc_arg, x);
   1332 	} else
   1333 #endif
   1334 	if (sc->sc_intr != 0) {
   1335 		/* clear interrupt */
   1336 		x = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
   1337 		    sc->sc_precision == 16 ? SBP_DSP_IRQACK16 :
   1338 					     SBP_DSP_IRQACK8);
   1339 		isa_dmadone(sc->dmaflags, sc->dmaaddr, sc->dmacnt, sc->sc_drq);
   1340 		(*sc->sc_intr)(sc->sc_arg);
   1341 	} else {
   1342 		return 0;
   1343 	}
   1344 	return 1;
   1345 }
   1346 
   1347 #if 0
   1348 /*
   1349  * Enter midi uart mode and arrange for read interrupts
   1350  * to vector to `intr'.  This puts the card in a mode
   1351  * which allows only midi I/O; the card must be reset
   1352  * to leave this mode.  Unfortunately, the card does not
   1353  * use transmit interrupts, so bytes must be output
   1354  * using polling.  To keep the polling overhead to a
   1355  * minimum, output should be driven off a timer.
   1356  * This is a little tricky since only 320us separate
   1357  * consecutive midi bytes.
   1358  */
   1359 void
   1360 sbdsp_set_midi_mode(sc, intr, arg)
   1361 	struct sbdsp_softc *sc;
   1362 	void (*intr)();
   1363 	void *arg;
   1364 {
   1365 
   1366 	sbdsp_wdsp(sc, SB_MIDI_UART_INTR);
   1367 	sc->sc_mintr = intr;
   1368 	sc->sc_intr = 0;
   1369 	sc->sc_arg = arg;
   1370 }
   1371 
   1372 /*
   1373  * Write a byte to the midi port, when in midi uart mode.
   1374  */
   1375 void
   1376 sbdsp_midi_output(sc, v)
   1377 	struct sbdsp_softc *sc;
   1378 	int v;
   1379 {
   1380 
   1381 	if (sbdsp_wdsp(sc, v) < 0)
   1382 		++sberr.wmidi;
   1383 }
   1384 #endif
   1385 
   1386 int
   1387 sbdsp_setfd(addr, flag)
   1388 	void *addr;
   1389 	int flag;
   1390 {
   1391 	/* Can't do full-duplex */
   1392 	return(ENOTTY);
   1393 }
   1394 
   1395 int
   1396 sbdsp_mixer_set_port(addr, cp)
   1397 	void *addr;
   1398 	mixer_ctrl_t *cp;
   1399 {
   1400 	register struct sbdsp_softc *sc = addr;
   1401 	int src, gain;
   1402 
   1403 	DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev,
   1404 	    cp->un.value.num_channels));
   1405 
   1406 	if (!ISSBPROCLASS(sc))
   1407 		return EINVAL;
   1408 
   1409 	/*
   1410 	 * Everything is a value except for SBPro BASS/TREBLE and
   1411 	 * RECORD_SOURCE
   1412 	 */
   1413 	switch (cp->dev) {
   1414 	case SB_SPEAKER:
   1415 		cp->dev = SB_MASTER_VOL;
   1416 	case SB_MIC_PORT:
   1417 	case SB_LINE_IN_PORT:
   1418 	case SB_DAC_PORT:
   1419 	case SB_FM_PORT:
   1420 	case SB_CD_PORT:
   1421 	case SB_MASTER_VOL:
   1422 		if (cp->type != AUDIO_MIXER_VALUE)
   1423 			return EINVAL;
   1424 
   1425 		/*
   1426 		 * All the mixer ports are stereo except for the microphone.
   1427 		 * If we get a single-channel gain value passed in, then we
   1428 		 * duplicate it to both left and right channels.
   1429 		 */
   1430 
   1431 		switch (cp->dev) {
   1432 		case SB_MIC_PORT:
   1433 			if (cp->un.value.num_channels != 1)
   1434 				return EINVAL;
   1435 
   1436 			/* handle funny microphone gain */
   1437 			gain = SBP_AGAIN_TO_MICGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1438 			break;
   1439 		case SB_LINE_IN_PORT:
   1440 		case SB_DAC_PORT:
   1441 		case SB_FM_PORT:
   1442 		case SB_CD_PORT:
   1443 		case SB_MASTER_VOL:
   1444 			switch (cp->un.value.num_channels) {
   1445 			case 1:
   1446 				gain = sbdsp_mono_vol(SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]));
   1447 				break;
   1448 			case 2:
   1449 				gain = sbdsp_stereo_vol(SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]),
   1450 							SBP_AGAIN_TO_SBGAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]));
   1451 				break;
   1452 			default:
   1453 				return EINVAL;
   1454 			}
   1455 			break;
   1456 		default:
   1457 			return EINVAL;
   1458 		}
   1459 
   1460 		switch (cp->dev) {
   1461 		case SB_MIC_PORT:
   1462 			src = SBP_MIC_VOL;
   1463 			break;
   1464 		case SB_MASTER_VOL:
   1465 			src = SBP_MASTER_VOL;
   1466 			break;
   1467 		case SB_LINE_IN_PORT:
   1468 			src = SBP_LINE_VOL;
   1469 			break;
   1470 		case SB_DAC_PORT:
   1471 			src = SBP_DAC_VOL;
   1472 			break;
   1473 		case SB_FM_PORT:
   1474 			src = SBP_FM_VOL;
   1475 			break;
   1476 		case SB_CD_PORT:
   1477 			src = SBP_CD_VOL;
   1478 			break;
   1479 		default:
   1480 			return EINVAL;
   1481 		}
   1482 
   1483 		sbdsp_mix_write(sc, src, gain);
   1484 		sc->gain[cp->dev] = gain;
   1485 		break;
   1486 
   1487 	case SB_TREBLE:
   1488 	case SB_BASS:
   1489 	case SB_RECORD_SOURCE:
   1490 		if (cp->type != AUDIO_MIXER_ENUM)
   1491 			return EINVAL;
   1492 
   1493 		switch (cp->dev) {
   1494 		case SB_TREBLE:
   1495 			return sbdsp_set_ifilter(addr, cp->un.ord ? SBP_TREBLE_EQ : 0);
   1496 		case SB_BASS:
   1497 			return sbdsp_set_ifilter(addr, cp->un.ord ? SBP_BASS_EQ : 0);
   1498 		case SB_RECORD_SOURCE:
   1499 			return sbdsp_set_in_port(addr, cp->un.ord);
   1500 		}
   1501 
   1502 		break;
   1503 
   1504 	default:
   1505 		return EINVAL;
   1506 	}
   1507 
   1508 	return (0);
   1509 }
   1510 
   1511 int
   1512 sbdsp_mixer_get_port(addr, cp)
   1513 	void *addr;
   1514 	mixer_ctrl_t *cp;
   1515 {
   1516 	register struct sbdsp_softc *sc = addr;
   1517 	int gain;
   1518 
   1519 	DPRINTF(("sbdsp_mixer_get_port: port=%d", cp->dev));
   1520 
   1521 	if (!ISSBPROCLASS(sc))
   1522 		return EINVAL;
   1523 
   1524 	switch (cp->dev) {
   1525 	case SB_SPEAKER:
   1526 		cp->dev = SB_MASTER_VOL;
   1527 	case SB_MIC_PORT:
   1528 	case SB_LINE_IN_PORT:
   1529 	case SB_DAC_PORT:
   1530 	case SB_FM_PORT:
   1531 	case SB_CD_PORT:
   1532 	case SB_MASTER_VOL:
   1533 		gain = sc->gain[cp->dev];
   1534 
   1535 		switch (cp->dev) {
   1536 		case SB_MIC_PORT:
   1537 			if (cp->un.value.num_channels != 1)
   1538 				return EINVAL;
   1539 
   1540 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = SBP_MICGAIN_TO_AGAIN(gain);
   1541 			break;
   1542 		case SB_LINE_IN_PORT:
   1543 		case SB_DAC_PORT:
   1544 		case SB_FM_PORT:
   1545 		case SB_CD_PORT:
   1546 		case SB_MASTER_VOL:
   1547 			switch (cp->un.value.num_channels) {
   1548 			case 1:
   1549 				cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = SBP_SBGAIN_TO_AGAIN(gain);
   1550 				break;
   1551 			case 2:
   1552 				cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = SBP_LEFTGAIN(gain);
   1553 				cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = SBP_RIGHTGAIN(gain);
   1554 				break;
   1555 			default:
   1556 				return EINVAL;
   1557 			}
   1558 			break;
   1559 		}
   1560 
   1561 		break;
   1562 
   1563 	case SB_TREBLE:
   1564 	case SB_BASS:
   1565 	case SB_RECORD_SOURCE:
   1566 		switch (cp->dev) {
   1567 		case SB_TREBLE:
   1568 			cp->un.ord = sbdsp_get_ifilter(addr) == SBP_TREBLE_EQ;
   1569 			return 0;
   1570 		case SB_BASS:
   1571 			cp->un.ord = sbdsp_get_ifilter(addr) == SBP_BASS_EQ;
   1572 			return 0;
   1573 		case SB_RECORD_SOURCE:
   1574 			cp->un.ord = sbdsp_get_in_port(addr);
   1575 			return 0;
   1576 		}
   1577 
   1578 		break;
   1579 
   1580 	default:
   1581 		return EINVAL;
   1582 	}
   1583 
   1584 	return (0);
   1585 }
   1586 
   1587 int
   1588 sbdsp_mixer_query_devinfo(addr, dip)
   1589 	void *addr;
   1590 	register mixer_devinfo_t *dip;
   1591 {
   1592 	register struct sbdsp_softc *sc = addr;
   1593 
   1594 	DPRINTF(("sbdsp_mixer_query_devinfo: index=%d\n", dip->index));
   1595 
   1596 	switch (dip->index) {
   1597 	case SB_MIC_PORT:
   1598 		dip->type = AUDIO_MIXER_VALUE;
   1599 		dip->mixer_class = SB_INPUT_CLASS;
   1600 		dip->prev = AUDIO_MIXER_LAST;
   1601 		dip->next = AUDIO_MIXER_LAST;
   1602 		strcpy(dip->label.name, AudioNmicrophone);
   1603 		dip->un.v.num_channels = 1;
   1604 		strcpy(dip->un.v.units.name, AudioNvolume);
   1605 		return 0;
   1606 
   1607 	case SB_SPEAKER:
   1608 		dip->type = AUDIO_MIXER_VALUE;
   1609 		dip->mixer_class = SB_OUTPUT_CLASS;
   1610 		dip->prev = AUDIO_MIXER_LAST;
   1611 		dip->next = AUDIO_MIXER_LAST;
   1612 		strcpy(dip->label.name, AudioNspeaker);
   1613 		dip->un.v.num_channels = 1;
   1614 		strcpy(dip->un.v.units.name, AudioNvolume);
   1615 		return 0;
   1616 
   1617 	case SB_INPUT_CLASS:
   1618 		dip->type = AUDIO_MIXER_CLASS;
   1619 		dip->mixer_class = SB_INPUT_CLASS;
   1620 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1621 		strcpy(dip->label.name, AudioCInputs);
   1622 		return 0;
   1623 
   1624 	case SB_OUTPUT_CLASS:
   1625 		dip->type = AUDIO_MIXER_CLASS;
   1626 		dip->mixer_class = SB_OUTPUT_CLASS;
   1627 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1628 		strcpy(dip->label.name, AudioCOutputs);
   1629 		return 0;
   1630 	}
   1631 
   1632 	if (ISSBPROCLASS(sc)) {
   1633 		switch (dip->index) {
   1634 		case SB_LINE_IN_PORT:
   1635 			dip->type = AUDIO_MIXER_VALUE;
   1636 			dip->mixer_class = SB_INPUT_CLASS;
   1637 			dip->prev = AUDIO_MIXER_LAST;
   1638 			dip->next = AUDIO_MIXER_LAST;
   1639 			strcpy(dip->label.name, AudioNline);
   1640 			dip->un.v.num_channels = 2;
   1641 			strcpy(dip->un.v.units.name, AudioNvolume);
   1642 			return 0;
   1643 
   1644 		case SB_DAC_PORT:
   1645 			dip->type = AUDIO_MIXER_VALUE;
   1646 			dip->mixer_class = SB_INPUT_CLASS;
   1647 			dip->prev = AUDIO_MIXER_LAST;
   1648 			dip->next = AUDIO_MIXER_LAST;
   1649 			strcpy(dip->label.name, AudioNdac);
   1650 			dip->un.v.num_channels = 2;
   1651 			strcpy(dip->un.v.units.name, AudioNvolume);
   1652 			return 0;
   1653 
   1654 		case SB_CD_PORT:
   1655 			dip->type = AUDIO_MIXER_VALUE;
   1656 			dip->mixer_class = SB_INPUT_CLASS;
   1657 			dip->prev = AUDIO_MIXER_LAST;
   1658 			dip->next = AUDIO_MIXER_LAST;
   1659 			strcpy(dip->label.name, AudioNcd);
   1660 			dip->un.v.num_channels = 2;
   1661 			strcpy(dip->un.v.units.name, AudioNvolume);
   1662 			return 0;
   1663 
   1664 		case SB_FM_PORT:
   1665 			dip->type = AUDIO_MIXER_VALUE;
   1666 			dip->mixer_class = SB_INPUT_CLASS;
   1667 			dip->prev = AUDIO_MIXER_LAST;
   1668 			dip->next = AUDIO_MIXER_LAST;
   1669 			strcpy(dip->label.name, AudioNfmsynth);
   1670 			dip->un.v.num_channels = 2;
   1671 			strcpy(dip->un.v.units.name, AudioNvolume);
   1672 			return 0;
   1673 
   1674 		case SB_MASTER_VOL:
   1675 			dip->type = AUDIO_MIXER_VALUE;
   1676 			dip->mixer_class = SB_OUTPUT_CLASS;
   1677 			dip->prev = AUDIO_MIXER_LAST;
   1678 			dip->next = AUDIO_MIXER_LAST;
   1679 			strcpy(dip->label.name, AudioNvolume);
   1680 			dip->un.v.num_channels = 2;
   1681 			strcpy(dip->un.v.units.name, AudioNvolume);
   1682 			return 0;
   1683 
   1684 		case SB_RECORD_SOURCE:
   1685 			dip->mixer_class = SB_RECORD_CLASS;
   1686 			dip->type = AUDIO_MIXER_ENUM;
   1687 			dip->prev = AUDIO_MIXER_LAST;
   1688 			dip->next = AUDIO_MIXER_LAST;
   1689 			strcpy(dip->label.name, AudioNsource);
   1690 			dip->un.e.num_mem = 3;
   1691 			strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
   1692 			dip->un.e.member[0].ord = SB_MIC_PORT;
   1693 			strcpy(dip->un.e.member[1].label.name, AudioNcd);
   1694 			dip->un.e.member[1].ord = SB_CD_PORT;
   1695 			strcpy(dip->un.e.member[2].label.name, AudioNline);
   1696 			dip->un.e.member[2].ord = SB_LINE_IN_PORT;
   1697 			return 0;
   1698 
   1699 		case SB_BASS:
   1700 			dip->type = AUDIO_MIXER_ENUM;
   1701 			dip->mixer_class = SB_INPUT_CLASS;
   1702 			dip->prev = AUDIO_MIXER_LAST;
   1703 			dip->next = AUDIO_MIXER_LAST;
   1704 			strcpy(dip->label.name, AudioNbass);
   1705 			dip->un.e.num_mem = 2;
   1706 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1707 			dip->un.e.member[0].ord = 0;
   1708 			strcpy(dip->un.e.member[1].label.name, AudioNon);
   1709 			dip->un.e.member[1].ord = 1;
   1710 			return 0;
   1711 
   1712 		case SB_TREBLE:
   1713 			dip->type = AUDIO_MIXER_ENUM;
   1714 			dip->mixer_class = SB_INPUT_CLASS;
   1715 			dip->prev = AUDIO_MIXER_LAST;
   1716 			dip->next = AUDIO_MIXER_LAST;
   1717 			strcpy(dip->label.name, AudioNtreble);
   1718 			dip->un.e.num_mem = 2;
   1719 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1720 			dip->un.e.member[0].ord = 0;
   1721 			strcpy(dip->un.e.member[1].label.name, AudioNon);
   1722 			dip->un.e.member[1].ord = 1;
   1723 			return 0;
   1724 
   1725 		case SB_RECORD_CLASS:			/* record source class */
   1726 			dip->type = AUDIO_MIXER_CLASS;
   1727 			dip->mixer_class = SB_RECORD_CLASS;
   1728 			dip->next = dip->prev = AUDIO_MIXER_LAST;
   1729 			strcpy(dip->label.name, AudioCRecord);
   1730 			return 0;
   1731 		}
   1732 	}
   1733 
   1734 	return ENXIO;
   1735 }
   1736