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