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