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