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