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