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