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