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