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