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