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