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