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