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