Home | History | Annotate | Line # | Download | only in isa
sbdsp.c revision 1.68
      1 /*	$NetBSD: sbdsp.c,v 1.68 1997/08/24 22:31:35 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  * Lots of rewrites by Lennart Augustsson <augustss (at) cs.chalmers.se>
     43  * with information from SB "Hardware Programming Guide" and the
     44  * Linux drivers.
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/errno.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/syslog.h>
     52 #include <sys/device.h>
     53 #include <sys/proc.h>
     54 #include <sys/buf.h>
     55 #include <vm/vm.h>
     56 
     57 #include <machine/cpu.h>
     58 #include <machine/intr.h>
     59 #include <machine/bus.h>
     60 
     61 #include <sys/audioio.h>
     62 #include <dev/audio_if.h>
     63 #include <dev/mulaw.h>
     64 #include <dev/auconv.h>
     65 
     66 #include <dev/isa/isavar.h>
     67 #include <dev/isa/isadmavar.h>
     68 
     69 #include <dev/isa/sbreg.h>
     70 #include <dev/isa/sbdspvar.h>
     71 
     72 #ifdef AUDIO_DEBUG
     73 #define DPRINTF(x)	if (sbdspdebug) printf x
     74 int	sbdspdebug = 0;
     75 #else
     76 #define DPRINTF(x)
     77 #endif
     78 
     79 #ifndef SBDSP_NPOLL
     80 #define SBDSP_NPOLL 3000
     81 #endif
     82 
     83 struct {
     84 	int wdsp;
     85 	int rdsp;
     86 	int wmidi;
     87 } sberr;
     88 
     89 /*
     90  * Time constant routines follow.  See SBK, section 12.
     91  * Although they don't come out and say it (in the docs),
     92  * the card clearly uses a 1MHz countdown timer, as the
     93  * low-speed formula (p. 12-4) is:
     94  *	tc = 256 - 10^6 / sr
     95  * In high-speed mode, the constant is the upper byte of a 16-bit counter,
     96  * and a 256MHz clock is used:
     97  *	tc = 65536 - 256 * 10^ 6 / sr
     98  * Since we can only use the upper byte of the HS TC, the two formulae
     99  * are equivalent.  (Why didn't they say so?)  E.g.,
    100  * 	(65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
    101  *
    102  * The crossover point (from low- to high-speed modes) is different
    103  * for the SBPRO and SB20.  The table on p. 12-5 gives the following data:
    104  *
    105  *				SBPRO			SB20
    106  *				-----			--------
    107  * input ls min			4	KHz		4	KHz
    108  * input ls max			23	KHz		13	KHz
    109  * input hs max			44.1	KHz		15	KHz
    110  * output ls min		4	KHz		4	KHz
    111  * output ls max		23	KHz		23	KHz
    112  * output hs max		44.1	KHz		44.1	KHz
    113  */
    114 /* XXX Should we round the tc?
    115 #define SB_RATE_TO_TC(x) (((65536 - 256 * 1000000 / (x)) + 128) >> 8)
    116 */
    117 #define SB_RATE_TO_TC(x) (256 - 1000000 / (x))
    118 #define SB_TC_TO_RATE(tc) (1000000 / (256 - (tc)))
    119 
    120 struct sbmode {
    121 	short	model;
    122 	u_char	channels;
    123 	u_char	precision;
    124 	u_short	lowrate, highrate;
    125 	u_char	cmd;
    126 	u_char	cmdchan;
    127 };
    128 static struct sbmode sbpmodes[] = {
    129  { SB_1,    1,  8,  4000, 22727, SB_DSP_WDMA      },
    130  { SB_20,   1,  8,  4000, 22727, SB_DSP_WDMA_LOOP },
    131  { SB_2x,   1,  8,  4000, 22727, SB_DSP_WDMA_LOOP },
    132  { SB_2x,   1,  8, 22727, 45454, SB_DSP_HS_OUTPUT },
    133  { SB_PRO,  1,  8,  4000, 22727, SB_DSP_WDMA_LOOP },
    134  { SB_PRO,  1,  8, 22727, 45454, SB_DSP_HS_OUTPUT },
    135  { SB_PRO,  2,  8, 11025, 22727, SB_DSP_HS_OUTPUT },
    136  /* Yes, we write the record mode to set 16-bit playback mode. weird, huh? */
    137  { SB_JAZZ, 1,  8,  4000, 22727, SB_DSP_WDMA_LOOP, SB_DSP_RECORD_MONO },
    138  { SB_JAZZ, 1,  8, 22727, 45454, SB_DSP_HS_OUTPUT, SB_DSP_RECORD_MONO },
    139  { SB_JAZZ, 2,  8, 11025, 22727, SB_DSP_HS_OUTPUT, SB_DSP_RECORD_STEREO },
    140  { SB_JAZZ, 1, 16,  4000, 22727, SB_DSP_WDMA_LOOP, JAZZ16_RECORD_MONO },
    141  { SB_JAZZ, 1, 16, 22727, 45454, SB_DSP_HS_OUTPUT, JAZZ16_RECORD_MONO },
    142  { SB_JAZZ, 2, 16, 11025, 22727, SB_DSP_HS_OUTPUT, JAZZ16_RECORD_STEREO },
    143  { SB_16,   1,  8,  5000, 45000, SB_DSP16_WDMA_8  },
    144  { SB_16,   2,  8,  5000, 45000, SB_DSP16_WDMA_8  },
    145 #define PLAY16 15 /* must be the index of the next entry in the table */
    146  { SB_16,   1, 16,  5000, 45000, SB_DSP16_WDMA_16 },
    147  { SB_16,   2, 16,  5000, 45000, SB_DSP16_WDMA_16 },
    148  { -1 }
    149 };
    150 static struct sbmode sbrmodes[] = {
    151  { SB_1,    1,  8,  4000, 12987, SB_DSP_RDMA      },
    152  { SB_20,   1,  8,  4000, 12987, SB_DSP_RDMA_LOOP },
    153  { SB_2x,   1,  8,  4000, 12987, SB_DSP_RDMA_LOOP },
    154  { SB_2x,   1,  8, 12987, 14925, SB_DSP_HS_INPUT  },
    155  { SB_PRO,  1,  8,  4000, 22727, SB_DSP_RDMA_LOOP, SB_DSP_RECORD_MONO },
    156  { SB_PRO,  1,  8, 22727, 45454, SB_DSP_HS_INPUT,  SB_DSP_RECORD_MONO },
    157  { SB_PRO,  2,  8, 11025, 22727, SB_DSP_HS_INPUT,  SB_DSP_RECORD_STEREO },
    158  { SB_JAZZ, 1,  8,  4000, 22727, SB_DSP_RDMA_LOOP, SB_DSP_RECORD_MONO },
    159  { SB_JAZZ, 1,  8, 22727, 45454, SB_DSP_HS_INPUT,  SB_DSP_RECORD_MONO },
    160  { SB_JAZZ, 2,  8, 11025, 22727, SB_DSP_HS_INPUT,  SB_DSP_RECORD_STEREO },
    161  { SB_JAZZ, 1, 16,  4000, 22727, SB_DSP_RDMA_LOOP, JAZZ16_RECORD_MONO },
    162  { SB_JAZZ, 1, 16, 22727, 45454, SB_DSP_HS_INPUT,  JAZZ16_RECORD_MONO },
    163  { SB_JAZZ, 2, 16, 11025, 22727, SB_DSP_HS_INPUT,  JAZZ16_RECORD_STEREO },
    164  { SB_16,   1,  8,  5000, 45000, SB_DSP16_RDMA_8  },
    165  { SB_16,   2,  8,  5000, 45000, SB_DSP16_RDMA_8  },
    166  { SB_16,   1, 16,  5000, 45000, SB_DSP16_RDMA_16 },
    167  { SB_16,   2, 16,  5000, 45000, SB_DSP16_RDMA_16 },
    168  { -1 }
    169 };
    170 
    171 void	sbversion __P((struct sbdsp_softc *));
    172 void	sbdsp_jazz16_probe __P((struct sbdsp_softc *));
    173 void	sbdsp_set_mixer_gain __P((struct sbdsp_softc *sc, int port));
    174 int	sbdsp16_wait __P((struct sbdsp_softc *));
    175 void	sbdsp_to __P((void *));
    176 void	sbdsp_pause __P((struct sbdsp_softc *));
    177 int	sbdsp_set_timeconst __P((struct sbdsp_softc *, int));
    178 int	sbdsp16_set_rate __P((struct sbdsp_softc *, int, int));
    179 int	sbdsp_set_in_ports __P((struct sbdsp_softc *, int));
    180 void	sbdsp_set_ifilter __P((void *, int));
    181 int	sbdsp_get_ifilter __P((void *));
    182 
    183 static	int sbdsp_adjust __P((int, int));
    184 
    185 #ifdef AUDIO_DEBUG
    186 void sb_printsc __P((struct sbdsp_softc *));
    187 
    188 void
    189 sb_printsc(sc)
    190 	struct sbdsp_softc *sc;
    191 {
    192 	int i;
    193 
    194 	printf("open %d dmachan %d/%d/%d iobase 0x%x irq %d\n",
    195 	    (int)sc->sc_open, sc->dmachan, sc->sc_drq8, sc->sc_drq16,
    196 	    sc->sc_iobase, sc->sc_irq);
    197 	printf("irate %d itc %x orate %d otc %x\n",
    198 	    sc->sc_irate, sc->sc_itc,
    199 	    sc->sc_orate, sc->sc_otc);
    200 	printf("outport %u inport %u spkron %u nintr %lu\n",
    201 	    sc->out_port, sc->in_port, sc->spkr_state, sc->sc_interrupts);
    202 	printf("intr %p arg %p\n",
    203 	    sc->sc_intr, sc->sc_arg);
    204 	printf("gain:");
    205 	for (i = 0; i < SB_NDEVS; i++)
    206 		printf(" %u,%u", sc->gain[i][SB_LEFT], sc->gain[i][SB_RIGHT]);
    207 	printf("\n");
    208 }
    209 #endif /* AUDIO_DEBUG */
    210 
    211 /*
    212  * Probe / attach routines.
    213  */
    214 
    215 /*
    216  * Probe for the soundblaster hardware.
    217  */
    218 int
    219 sbdsp_probe(sc)
    220 	struct sbdsp_softc *sc;
    221 {
    222 
    223 	if (sbdsp_reset(sc) < 0) {
    224 		DPRINTF(("sbdsp: couldn't reset card\n"));
    225 		return 0;
    226 	}
    227 	/* if flags set, go and probe the jazz16 stuff */
    228 	if (sc->sc_dev.dv_cfdata->cf_flags & 1)
    229 		sbdsp_jazz16_probe(sc);
    230 	else
    231 		sbversion(sc);
    232 	if (sc->sc_model == SB_UNK) {
    233 		/* Unknown SB model found. */
    234 		DPRINTF(("sbdsp: unknown SB model found\n"));
    235 		return 0;
    236 	}
    237 	return 1;
    238 }
    239 
    240 /*
    241  * Try add-on stuff for Jazz16.
    242  */
    243 void
    244 sbdsp_jazz16_probe(sc)
    245 	struct sbdsp_softc *sc;
    246 {
    247 	static u_char jazz16_irq_conf[16] = {
    248 	    -1, -1, 0x02, 0x03,
    249 	    -1, 0x01, -1, 0x04,
    250 	    -1, 0x02, 0x05, -1,
    251 	    -1, -1, -1, 0x06};
    252 	static u_char jazz16_drq_conf[8] = {
    253 	    -1, 0x01, -1, 0x02,
    254 	    -1, 0x03, -1, 0x04};
    255 
    256 	bus_space_tag_t iot = sc->sc_iot;
    257 	bus_space_handle_t ioh;
    258 
    259 	sbversion(sc);
    260 
    261 	DPRINTF(("jazz16 probe\n"));
    262 
    263 	if (bus_space_map(iot, JAZZ16_CONFIG_PORT, 1, 0, &ioh)) {
    264 		DPRINTF(("bus map failed\n"));
    265 		return;
    266 	}
    267 
    268 	if (jazz16_drq_conf[sc->sc_drq8] == (u_char)-1 ||
    269 	    jazz16_irq_conf[sc->sc_irq] == (u_char)-1) {
    270 		DPRINTF(("drq/irq check failed\n"));
    271 		goto done;		/* give up, we can't do it. */
    272 	}
    273 
    274 	bus_space_write_1(iot, ioh, 0, JAZZ16_WAKEUP);
    275 	delay(10000);			/* delay 10 ms */
    276 	bus_space_write_1(iot, ioh, 0, JAZZ16_SETBASE);
    277 	bus_space_write_1(iot, ioh, 0, sc->sc_iobase & 0x70);
    278 
    279 	if (sbdsp_reset(sc) < 0) {
    280 		DPRINTF(("sbdsp_reset check failed\n"));
    281 		goto done;		/* XXX? what else could we do? */
    282 	}
    283 
    284 	if (sbdsp_wdsp(sc, JAZZ16_READ_VER)) {
    285 		DPRINTF(("read16 setup failed\n"));
    286 		goto done;
    287 	}
    288 
    289 	if (sbdsp_rdsp(sc) != JAZZ16_VER_JAZZ) {
    290 		DPRINTF(("read16 failed\n"));
    291 		goto done;
    292 	}
    293 
    294 	/* XXX set both 8 & 16-bit drq to same channel, it works fine. */
    295 	sc->sc_drq16 = sc->sc_drq8;
    296 	if (sbdsp_wdsp(sc, JAZZ16_SET_DMAINTR) ||
    297 	    sbdsp_wdsp(sc, (jazz16_drq_conf[sc->sc_drq16] << 4) |
    298 		jazz16_drq_conf[sc->sc_drq8]) ||
    299 	    sbdsp_wdsp(sc, jazz16_irq_conf[sc->sc_irq])) {
    300 		DPRINTF(("sbdsp: can't write jazz16 probe stuff\n"));
    301 	} else {
    302 		DPRINTF(("jazz16 detected!\n"));
    303 		sc->sc_model = SB_JAZZ;
    304 		sc->sc_mixer_model = SBM_CT1345; /* XXX really? */
    305 	}
    306 
    307 done:
    308 	bus_space_unmap(iot, ioh, 1);
    309 }
    310 
    311 /*
    312  * Attach hardware to driver, attach hardware driver to audio
    313  * pseudo-device driver .
    314  */
    315 void
    316 sbdsp_attach(sc)
    317 	struct sbdsp_softc *sc;
    318 {
    319 	struct audio_params pparams, rparams;
    320         int i;
    321         u_int v;
    322 
    323 	/*
    324 	 * Create our DMA maps.
    325 	 */
    326 	if (sc->sc_drq8 != -1) {
    327 		if (isa_dmamap_create(sc->sc_isa, sc->sc_drq8,
    328 		    MAX_ISADMA, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    329 			printf("%s: can't create map for drq %d\n",
    330 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    331 			return;
    332 		}
    333 	}
    334 	if (sc->sc_drq16 != -1 && sc->sc_drq16 != sc->sc_drq8) {
    335 		if (isa_dmamap_create(sc->sc_isa, sc->sc_drq16,
    336 		    MAX_ISADMA, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    337 			printf("%s: can't create map for drq %d\n",
    338 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    339 			return;
    340 		}
    341 	}
    342 
    343 	pparams = audio_default;
    344 	rparams = audio_default;
    345         sbdsp_set_params(sc, AUMODE_RECORD|AUMODE_PLAY, 0, &pparams, &rparams);
    346 
    347 	sbdsp_set_in_port(sc, SB_MIC_VOL);
    348 	sbdsp_set_out_port(sc, SB_MASTER_VOL);
    349 
    350 	if (sc->sc_mixer_model != SBM_NONE) {
    351         	/* Reset the mixer.*/
    352 		sbdsp_mix_write(sc, SBP_MIX_RESET, SBP_MIX_RESET);
    353                 /* And set our own default values */
    354 		for (i = 0; i < SB_NDEVS; i++) {
    355 			switch(i) {
    356 			case SB_MIC_VOL:
    357 			case SB_LINE_IN_VOL:
    358 				v = 0;
    359 				break;
    360 			case SB_BASS:
    361 			case SB_TREBLE:
    362 				v = SB_ADJUST_GAIN(sc, AUDIO_MAX_GAIN/2);
    363 				break;
    364 			default:
    365 				v = SB_ADJUST_GAIN(sc, AUDIO_MAX_GAIN * 3 / 4);
    366 				break;
    367 			}
    368 			sc->gain[i][SB_LEFT] = sc->gain[i][SB_RIGHT] = v;
    369 			sbdsp_set_mixer_gain(sc, i);
    370 		}
    371 		sc->in_filter = 0;	/* no filters turned on, please */
    372 	}
    373 
    374 	DPRINTF((" drq16 %d", sc->sc_drq16));
    375 	printf(": dsp v%d.%02d%s\n",
    376 	       SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version),
    377 	       sc->sc_model == SB_JAZZ ? ": <Jazz16>" : "");
    378 }
    379 
    380 void
    381 sbdsp_mix_write(sc, mixerport, val)
    382 	struct sbdsp_softc *sc;
    383 	int mixerport;
    384 	int val;
    385 {
    386 	bus_space_tag_t iot = sc->sc_iot;
    387 	bus_space_handle_t ioh = sc->sc_ioh;
    388 	int s;
    389 
    390 	s = splaudio();
    391 	bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
    392 	delay(20);
    393 	bus_space_write_1(iot, ioh, SBP_MIXER_DATA, val);
    394 	delay(30);
    395 	splx(s);
    396 }
    397 
    398 int
    399 sbdsp_mix_read(sc, mixerport)
    400 	struct sbdsp_softc *sc;
    401 	int mixerport;
    402 {
    403 	bus_space_tag_t iot = sc->sc_iot;
    404 	bus_space_handle_t ioh = sc->sc_ioh;
    405 	int val;
    406 	int s;
    407 
    408 	s = splaudio();
    409 	bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
    410 	delay(20);
    411 	val = bus_space_read_1(iot, ioh, SBP_MIXER_DATA);
    412 	delay(30);
    413 	splx(s);
    414 	return val;
    415 }
    416 
    417 /*
    418  * Various routines to interface to higher level audio driver
    419  */
    420 
    421 int
    422 sbdsp_query_encoding(addr, fp)
    423 	void *addr;
    424 	struct audio_encoding *fp;
    425 {
    426 	struct sbdsp_softc *sc = addr;
    427 	int emul;
    428 
    429 	emul = ISSB16CLASS(sc) ? 0 : AUDIO_ENCODINGFLAG_EMULATED;
    430 
    431 	switch (fp->index) {
    432 	case 0:
    433 		strcpy(fp->name, AudioEulinear);
    434 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    435 		fp->precision = 8;
    436 		fp->flags = 0;
    437 		return 0;
    438 	case 1:
    439 		strcpy(fp->name, AudioEmulaw);
    440 		fp->encoding = AUDIO_ENCODING_ULAW;
    441 		fp->precision = 8;
    442 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    443 		return 0;
    444 	case 2:
    445 		strcpy(fp->name, AudioEalaw);
    446 		fp->encoding = AUDIO_ENCODING_ALAW;
    447 		fp->precision = 8;
    448 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    449 		return 0;
    450 	case 3:
    451 		strcpy(fp->name, AudioElinear);
    452 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    453 		fp->precision = 8;
    454 		fp->flags = emul;
    455 		return 0;
    456         }
    457         if (!ISSB16CLASS(sc) && sc->sc_model != SB_JAZZ)
    458 		return EINVAL;
    459 
    460         switch(fp->index) {
    461         case 4:
    462 		strcpy(fp->name, AudioElinear_le);
    463 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    464 		fp->precision = 16;
    465 		fp->flags = 0;
    466 		return 0;
    467 	case 5:
    468 		strcpy(fp->name, AudioEulinear_le);
    469 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    470 		fp->precision = 16;
    471 		fp->flags = emul;
    472 		return 0;
    473 	case 6:
    474 		strcpy(fp->name, AudioElinear_be);
    475 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    476 		fp->precision = 16;
    477 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    478 		return 0;
    479 	case 7:
    480 		strcpy(fp->name, AudioEulinear_be);
    481 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    482 		fp->precision = 16;
    483 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    484 		return 0;
    485 	default:
    486 		return EINVAL;
    487 	}
    488 	return 0;
    489 }
    490 
    491 int
    492 sbdsp_set_params(addr, setmode, usemode, play, rec)
    493 	void *addr;
    494 	int setmode, usemode;
    495 	struct audio_params *play, *rec;
    496 {
    497 	struct sbdsp_softc *sc = addr;
    498 	struct sbmode *m;
    499 	u_int rate, tc, bmode;
    500 	void (*swcode) __P((void *, u_char *buf, int cnt));
    501 	int factor;
    502 	int model;
    503 	struct audio_params *p;
    504 	int mode;
    505 
    506 	model = sc->sc_model;
    507 	if (model > SB_16)
    508 		model = SB_16;	/* later models work like SB16 */
    509 
    510 	/* Set first record info, then play info */
    511 	for(mode = AUMODE_RECORD; mode != -1;
    512 	    mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    513 		if ((setmode & mode) == 0)
    514 			continue;
    515 
    516 		p = mode == AUMODE_PLAY ? play : rec;
    517 		for(m = mode == AUMODE_PLAY ? sbpmodes : sbrmodes;
    518 		    m->model != -1; m++) {
    519 			if (model == m->model &&
    520 			    p->channels == m->channels &&
    521 			    p->precision == m->precision &&
    522 			    p->sample_rate >= m->lowrate &&
    523 			    p->sample_rate < m->highrate)
    524 				break;
    525 		}
    526 		if (m->model == -1)
    527 			return EINVAL;
    528 		rate = p->sample_rate;
    529 		swcode = 0;
    530 		factor = 1;
    531 		tc = 1;
    532 		bmode = -1;
    533 		if (model == SB_16) {
    534 			switch (p->encoding) {
    535 			case AUDIO_ENCODING_SLINEAR_BE:
    536 				if (p->precision == 16)
    537 					swcode = swap_bytes;
    538 				/* fall into */
    539 			case AUDIO_ENCODING_SLINEAR_LE:
    540 				bmode = SB_BMODE_SIGNED;
    541 				break;
    542 			case AUDIO_ENCODING_ULINEAR_BE:
    543 				if (p->precision == 16)
    544 					swcode = swap_bytes;
    545 				/* fall into */
    546 			case AUDIO_ENCODING_ULINEAR_LE:
    547 				bmode = SB_BMODE_UNSIGNED;
    548 				break;
    549 			case AUDIO_ENCODING_ULAW:
    550 				if (mode == AUMODE_PLAY) {
    551 					swcode = mulaw_to_ulinear16;
    552 					factor = 2;
    553 					m = &sbpmodes[PLAY16];
    554 				} else
    555 					swcode = ulinear8_to_mulaw;
    556 				bmode = SB_BMODE_UNSIGNED;
    557 				break;
    558 			case AUDIO_ENCODING_ALAW:
    559 				if (mode == AUMODE_PLAY) {
    560 					swcode = alaw_to_ulinear16;
    561 					factor = 2;
    562 					m = &sbpmodes[PLAY16];
    563 				} else
    564 					swcode = ulinear8_to_alaw;
    565 				bmode = SB_BMODE_UNSIGNED;
    566 				break;
    567 			default:
    568 				return EINVAL;
    569 			}
    570 			if (p->channels == 2)
    571 				bmode |= SB_BMODE_STEREO;
    572 		} else if (m->model == SB_JAZZ && m->precision == 16) {
    573 			switch (p->encoding) {
    574 			case AUDIO_ENCODING_SLINEAR_LE:
    575 				break;
    576 			case AUDIO_ENCODING_ULINEAR_LE:
    577 				swcode = change_sign16;
    578 				break;
    579 			case AUDIO_ENCODING_SLINEAR_BE:
    580 				swcode = swap_bytes;
    581 				break;
    582 			case AUDIO_ENCODING_ULINEAR_BE:
    583 				swcode = mode == AUMODE_PLAY ?
    584 					swap_bytes_change_sign16 : change_sign16_swap_bytes;
    585 				break;
    586 			case AUDIO_ENCODING_ULAW:
    587 				swcode = mode == AUMODE_PLAY ?
    588 					mulaw_to_ulinear8 : ulinear8_to_mulaw;
    589 				break;
    590 			case AUDIO_ENCODING_ALAW:
    591 				swcode = mode == AUMODE_PLAY ?
    592 					alaw_to_ulinear8 : ulinear8_to_alaw;
    593 				break;
    594 			default:
    595 				return EINVAL;
    596 			}
    597 			tc = SB_RATE_TO_TC(p->sample_rate * p->channels);
    598 			p->sample_rate = SB_TC_TO_RATE(tc) / p->channels;
    599 		} else {
    600 			switch (p->encoding) {
    601 			case AUDIO_ENCODING_SLINEAR_BE:
    602 			case AUDIO_ENCODING_SLINEAR_LE:
    603 				swcode = change_sign8;
    604 				break;
    605 			case AUDIO_ENCODING_ULINEAR_BE:
    606 			case AUDIO_ENCODING_ULINEAR_LE:
    607 				break;
    608 			case AUDIO_ENCODING_ULAW:
    609 				swcode = mode == AUMODE_PLAY ?
    610 					mulaw_to_ulinear8 : ulinear8_to_mulaw;
    611 				break;
    612 			case AUDIO_ENCODING_ALAW:
    613 				swcode = mode == AUMODE_PLAY ?
    614 					alaw_to_ulinear8 : ulinear8_to_alaw;
    615 				break;
    616 			default:
    617 				return EINVAL;
    618 			}
    619 			tc = SB_RATE_TO_TC(p->sample_rate * p->channels);
    620 			p->sample_rate = SB_TC_TO_RATE(tc) / p->channels;
    621 		}
    622 
    623 		if (mode == AUMODE_PLAY) {
    624 			sc->sc_orate = rate;
    625 			sc->sc_otc = tc;
    626 			sc->sc_omodep = m;
    627 			sc->sc_obmode = bmode;
    628 		} else {
    629 			sc->sc_irate = rate;
    630 			sc->sc_itc = tc;
    631 			sc->sc_imodep = m;
    632 			sc->sc_ibmode = bmode;
    633 		}
    634 
    635 		p->sw_code = swcode;
    636 		p->factor = factor;
    637 		DPRINTF(("set_params: model=%d, mode=%d, rate=%ld, prec=%d, chan=%d, enc=%d -> tc=%02x, cmd=%02x, bmode=%02x, cmdchan=%02x, swcode=%p, factor=%d\n",
    638 			 sc->sc_model, mode, p->sample_rate, p->precision, p->channels,
    639 			 p->encoding, tc, m->cmd, bmode, m->cmdchan, swcode, factor));
    640 	}
    641 
    642 	/*
    643 	 * XXX
    644 	 * Should wait for chip to be idle.
    645 	 */
    646 	sc->sc_dmadir = SB_DMA_NONE;
    647 
    648 
    649 	return 0;
    650 }
    651 
    652 void
    653 sbdsp_set_ifilter(addr, which)
    654 	void *addr;
    655 	int which;
    656 {
    657 	struct sbdsp_softc *sc = addr;
    658 	int mixval;
    659 
    660 	mixval = sbdsp_mix_read(sc, SBP_INFILTER) & ~SBP_IFILTER_MASK;
    661 	switch (which) {
    662 	case 0:
    663 		mixval |= SBP_FILTER_OFF;
    664 		break;
    665 	case SB_TREBLE:
    666 		mixval |= SBP_FILTER_ON | SBP_IFILTER_HIGH;
    667 		break;
    668 	case SB_BASS:
    669 		mixval |= SBP_FILTER_ON | SBP_IFILTER_LOW;
    670 		break;
    671 	default:
    672 		return;
    673 	}
    674 	sc->in_filter = mixval & SBP_IFILTER_MASK;
    675 	sbdsp_mix_write(sc, SBP_INFILTER, mixval);
    676 }
    677 
    678 int
    679 sbdsp_get_ifilter(addr)
    680 	void *addr;
    681 {
    682 	struct sbdsp_softc *sc = addr;
    683 
    684 	sc->in_filter =
    685 		sbdsp_mix_read(sc, SBP_INFILTER) & SBP_IFILTER_MASK;
    686 	switch (sc->in_filter) {
    687 	case SBP_FILTER_ON|SBP_IFILTER_HIGH:
    688 		return SB_TREBLE;
    689 	case SBP_FILTER_ON|SBP_IFILTER_LOW:
    690 		return SB_BASS;
    691 	default:
    692 		return 0;
    693 	}
    694 }
    695 
    696 int
    697 sbdsp_set_out_port(addr, port)
    698 	void *addr;
    699 	int port;
    700 {
    701 	struct sbdsp_softc *sc = addr;
    702 
    703 	sc->out_port = port; /* Just record it */
    704 
    705 	return 0;
    706 }
    707 
    708 int
    709 sbdsp_get_out_port(addr)
    710 	void *addr;
    711 {
    712 	struct sbdsp_softc *sc = addr;
    713 
    714 	return sc->out_port;
    715 }
    716 
    717 
    718 int
    719 sbdsp_set_in_port(addr, port)
    720 	void *addr;
    721 	int port;
    722 {
    723 	return sbdsp_set_in_ports(addr, 1 << port);
    724 }
    725 
    726 int
    727 sbdsp_set_in_ports(sc, mask)
    728 	struct sbdsp_softc *sc;
    729 	int mask;
    730 {
    731 	int bitsl, bitsr;
    732 	int sbport;
    733 	int i;
    734 
    735 	DPRINTF(("sbdsp_set_in_ports: model=%d, mask=%x\n",
    736 		 sc->sc_mixer_model, mask));
    737 
    738 	switch(sc->sc_mixer_model) {
    739 	case SBM_NONE:
    740 		return EINVAL;
    741 	case SBM_CT1335:
    742 		if (mask != (1 << SB_MIC_VOL))
    743 			return EINVAL;
    744 		break;
    745 	case SBM_CT1345:
    746 		switch (mask) {
    747 		case 1 << SB_MIC_VOL:
    748 			sbport = SBP_FROM_MIC;
    749 			break;
    750 		case 1 << SB_LINE_IN_VOL:
    751 			sbport = SBP_FROM_LINE;
    752 			break;
    753 		case 1 << SB_CD_VOL:
    754 			sbport = SBP_FROM_CD;
    755 			break;
    756 		default:
    757 			return (EINVAL);
    758 		}
    759 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE,
    760 		    SBP_RECORD_FROM(sbport, SBP_FILTER_OFF, SBP_IFILTER_HIGH));
    761 		break;
    762 	case SBM_CT1XX5:
    763 	case SBM_CT1745:
    764 		if (mask & ~((1<<SB_MIDI_VOL) | (1<<SB_LINE_IN_VOL) |
    765 			     (1<<SB_CD_VOL) | (1<<SB_MIC_VOL)))
    766 			return EINVAL;
    767 		bitsr = 0;
    768 		if (mask & (1<<SB_MIDI_VOL))    bitsr |= SBP_MIDI_SRC_R;
    769 		if (mask & (1<<SB_LINE_IN_VOL)) bitsr |= SBP_LINE_SRC_R;
    770 		if (mask & (1<<SB_CD_VOL))      bitsr |= SBP_CD_SRC_R;
    771 		bitsl = SB_SRC_R_TO_L(bitsr);
    772 		if (mask & (1<<SB_MIC_VOL)) {
    773 			bitsl |= SBP_MIC_SRC;
    774 			bitsr |= SBP_MIC_SRC;
    775 		}
    776 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE_L, bitsl);
    777 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE_R, bitsr);
    778 		break;
    779 	}
    780 
    781 	sc->in_mask = mask;
    782 
    783 	/* XXX
    784 	 * We have to fake a single port since the upper layer
    785 	 * expects one.
    786 	 */
    787 	for(i = 0; i < SB_NPORT; i++) {
    788 		if (mask & (1 << i)) {
    789 			sc->in_port = i;
    790 			break;
    791 		}
    792 	}
    793 	return 0;
    794 }
    795 
    796 int
    797 sbdsp_get_in_port(addr)
    798 	void *addr;
    799 {
    800 	struct sbdsp_softc *sc = addr;
    801 
    802 	return sc->in_port;
    803 }
    804 
    805 
    806 int
    807 sbdsp_speaker_ctl(addr, newstate)
    808 	void *addr;
    809 	int newstate;
    810 {
    811 	struct sbdsp_softc *sc = addr;
    812 
    813 	if ((newstate == SPKR_ON) &&
    814 	    (sc->spkr_state == SPKR_OFF)) {
    815 		sbdsp_spkron(sc);
    816 		sc->spkr_state = SPKR_ON;
    817 	}
    818 	if ((newstate == SPKR_OFF) &&
    819 	    (sc->spkr_state == SPKR_ON)) {
    820 		sbdsp_spkroff(sc);
    821 		sc->spkr_state = SPKR_OFF;
    822 	}
    823 	return 0;
    824 }
    825 
    826 int
    827 sbdsp_round_blocksize(addr, blk)
    828 	void *addr;
    829 	int blk;
    830 {
    831 	blk &= -4;		/* round to biggest sample size */
    832 	return blk;
    833 }
    834 
    835 int
    836 sbdsp_open(addr, flags)
    837 	void *addr;
    838 	int flags;
    839 {
    840 	struct sbdsp_softc *sc = addr;
    841 
    842         DPRINTF(("sbdsp_open: sc=%p\n", sc));
    843 
    844 	if (sc->sc_open != 0 || sbdsp_reset(sc) != 0)
    845 		return ENXIO;
    846 
    847 	sc->sc_open = 1;
    848 	sc->sc_mintr = 0;
    849 	if (ISSBPRO(sc) &&
    850 	    sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0) {
    851 		DPRINTF(("sbdsp_open: can't set mono mode\n"));
    852 		/* we'll readjust when it's time for DMA. */
    853 	}
    854 
    855 	/*
    856 	 * Leave most things as they were; users must change things if
    857 	 * the previous process didn't leave it they way they wanted.
    858 	 * Looked at another way, it's easy to set up a configuration
    859 	 * in one program and leave it for another to inherit.
    860 	 */
    861 	DPRINTF(("sbdsp_open: opened\n"));
    862 
    863 	return 0;
    864 }
    865 
    866 void
    867 sbdsp_close(addr)
    868 	void *addr;
    869 {
    870 	struct sbdsp_softc *sc = addr;
    871 
    872         DPRINTF(("sbdsp_close: sc=%p\n", sc));
    873 
    874 	sc->sc_open = 0;
    875 	sbdsp_spkroff(sc);
    876 	sc->spkr_state = SPKR_OFF;
    877 	sc->sc_intr = 0;
    878 	sc->sc_mintr = 0;
    879 	sbdsp_haltdma(sc);
    880 
    881 	DPRINTF(("sbdsp_close: closed\n"));
    882 }
    883 
    884 /*
    885  * Lower-level routines
    886  */
    887 
    888 /*
    889  * Reset the card.
    890  * Return non-zero if the card isn't detected.
    891  */
    892 int
    893 sbdsp_reset(sc)
    894 	struct sbdsp_softc *sc;
    895 {
    896 	bus_space_tag_t iot = sc->sc_iot;
    897 	bus_space_handle_t ioh = sc->sc_ioh;
    898 
    899 	sc->sc_intr = 0;
    900 	if (sc->sc_dmadir != SB_DMA_NONE) {
    901 		isa_dmaabort(sc->sc_isa, sc->dmachan);
    902 		sc->sc_dmadir = SB_DMA_NONE;
    903 	}
    904 
    905 	/*
    906 	 * See SBK, section 11.3.
    907 	 * We pulse a reset signal into the card.
    908 	 * Gee, what a brilliant hardware design.
    909 	 */
    910 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 1);
    911 	delay(10);
    912 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 0);
    913 	delay(30);
    914 	if (sbdsp_rdsp(sc) != SB_MAGIC)
    915 		return -1;
    916 
    917 	return 0;
    918 }
    919 
    920 int
    921 sbdsp16_wait(sc)
    922 	struct sbdsp_softc *sc;
    923 {
    924 	bus_space_tag_t iot = sc->sc_iot;
    925 	bus_space_handle_t ioh = sc->sc_ioh;
    926 	int i;
    927 	u_char x;
    928 
    929 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    930 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
    931 		delay(10);
    932 		if ((x & SB_DSP_BUSY) == 0)
    933 			continue;
    934 		return 0;
    935 	}
    936 	++sberr.wdsp;
    937 	return -1;
    938 }
    939 
    940 /*
    941  * Write a byte to the dsp.
    942  * XXX We are at the mercy of the card as we use a
    943  * polling loop and wait until it can take the byte.
    944  */
    945 int
    946 sbdsp_wdsp(sc, v)
    947 	struct sbdsp_softc *sc;
    948 	int v;
    949 {
    950 	bus_space_tag_t iot = sc->sc_iot;
    951 	bus_space_handle_t ioh = sc->sc_ioh;
    952 	int i;
    953 	u_char x;
    954 
    955 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    956 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
    957 		delay(10);
    958 		if ((x & SB_DSP_BUSY) != 0)
    959 			continue;
    960 		bus_space_write_1(iot, ioh, SBP_DSP_WRITE, v);
    961 		delay(10);
    962 		return 0;
    963 	}
    964 	++sberr.wdsp;
    965 	return -1;
    966 }
    967 
    968 /*
    969  * Read a byte from the DSP, using polling.
    970  */
    971 int
    972 sbdsp_rdsp(sc)
    973 	struct sbdsp_softc *sc;
    974 {
    975 	bus_space_tag_t iot = sc->sc_iot;
    976 	bus_space_handle_t ioh = sc->sc_ioh;
    977 	int i;
    978 	u_char x;
    979 
    980 	for (i = SBDSP_NPOLL; --i >= 0; ) {
    981 		x = bus_space_read_1(iot, ioh, SBP_DSP_RSTAT);
    982 		delay(10);
    983 		if ((x & SB_DSP_READY) == 0)
    984 			continue;
    985 		x = bus_space_read_1(iot, ioh, SBP_DSP_READ);
    986 		delay(10);
    987 		return x;
    988 	}
    989 	++sberr.rdsp;
    990 	return -1;
    991 }
    992 
    993 /*
    994  * Doing certain things (like toggling the speaker) make
    995  * the SB hardware go away for a while, so pause a little.
    996  */
    997 void
    998 sbdsp_to(arg)
    999 	void *arg;
   1000 {
   1001 	wakeup(arg);
   1002 }
   1003 
   1004 void
   1005 sbdsp_pause(sc)
   1006 	struct sbdsp_softc *sc;
   1007 {
   1008 	extern int hz;
   1009 
   1010 	timeout(sbdsp_to, sbdsp_to, hz/8);
   1011 	(void)tsleep(sbdsp_to, PWAIT, "sbpause", 0);
   1012 }
   1013 
   1014 /*
   1015  * Turn on the speaker.  The SBK documention says this operation
   1016  * can take up to 1/10 of a second.  Higher level layers should
   1017  * probably let the task sleep for this amount of time after
   1018  * calling here.  Otherwise, things might not work (because
   1019  * sbdsp_wdsp() and sbdsp_rdsp() will probably timeout.)
   1020  *
   1021  * These engineers had their heads up their ass when
   1022  * they designed this card.
   1023  */
   1024 void
   1025 sbdsp_spkron(sc)
   1026 	struct sbdsp_softc *sc;
   1027 {
   1028 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_ON);
   1029 	sbdsp_pause(sc);
   1030 }
   1031 
   1032 /*
   1033  * Turn off the speaker; see comment above.
   1034  */
   1035 void
   1036 sbdsp_spkroff(sc)
   1037 	struct sbdsp_softc *sc;
   1038 {
   1039 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_OFF);
   1040 	sbdsp_pause(sc);
   1041 }
   1042 
   1043 /*
   1044  * Read the version number out of the card.
   1045  * Store version information in the softc.
   1046  */
   1047 void
   1048 sbversion(sc)
   1049 	struct sbdsp_softc *sc;
   1050 {
   1051 	int v;
   1052 
   1053 	sc->sc_model = SB_UNK;
   1054 	sc->sc_version = 0;
   1055 	if (sbdsp_wdsp(sc, SB_DSP_VERSION) < 0)
   1056 		return;
   1057 	v = sbdsp_rdsp(sc) << 8;
   1058 	v |= sbdsp_rdsp(sc);
   1059 	if (v < 0)
   1060 		return;
   1061 	sc->sc_version = v;
   1062 	switch(SBVER_MAJOR(v)) {
   1063 	case 1:
   1064 		sc->sc_mixer_model = SBM_NONE;
   1065 		sc->sc_model = SB_1;
   1066 		break;
   1067 	case 2:
   1068 		/* Some SB2 have a mixer, some don't. */
   1069 		sbdsp_mix_write(sc, SBP_1335_MASTER_VOL, 0x04);
   1070 		sbdsp_mix_write(sc, SBP_1335_MIDI_VOL,   0x06);
   1071 		/* Check if we can read back the mixer values. */
   1072 		if ((sbdsp_mix_read(sc, SBP_1335_MASTER_VOL) & 0x0e) == 0x04 &&
   1073 		    (sbdsp_mix_read(sc, SBP_1335_MIDI_VOL)   & 0x0e) == 0x06)
   1074 			sc->sc_mixer_model = SBM_CT1335;
   1075 		else
   1076 			sc->sc_mixer_model = SBM_NONE;
   1077 		if (SBVER_MINOR(v) == 0)
   1078 			sc->sc_model = SB_20;
   1079 		else
   1080 			sc->sc_model = SB_2x;
   1081 		break;
   1082 	case 3:
   1083 		sc->sc_mixer_model = SBM_CT1345;
   1084 		sc->sc_model = SB_PRO;
   1085 		break;
   1086 	case 4:
   1087 #if 0
   1088 /* XXX This does not work */
   1089 		/* Most SB16 have a tone controls, but some don't. */
   1090 		sbdsp_mix_write(sc, SB16P_TREBLE_L, 0x80);
   1091 		/* Check if we can read back the mixer value. */
   1092 		if ((sbdsp_mix_read(sc, SB16P_TREBLE_L) & 0xf0) == 0x80)
   1093 			sc->sc_mixer_model = SBM_CT1745;
   1094 		else
   1095 			sc->sc_mixer_model = SBM_CT1XX5;
   1096 #else
   1097 		sc->sc_mixer_model = SBM_CT1745;
   1098 #endif
   1099 		/* XXX what about SB_32 */
   1100 		if (SBVER_MINOR(v) == 16)
   1101 			sc->sc_model = SB_64;
   1102 		else
   1103 			sc->sc_model = SB_16;
   1104 		break;
   1105 	}
   1106 }
   1107 
   1108 /*
   1109  * Halt a DMA in progress.  A low-speed transfer can be
   1110  * resumed with sbdsp_contdma().
   1111  */
   1112 int
   1113 sbdsp_haltdma(addr)
   1114 	void *addr;
   1115 {
   1116 	struct sbdsp_softc *sc = addr;
   1117 
   1118 	DPRINTF(("sbdsp_haltdma: sc=%p\n", sc));
   1119 
   1120 	sbdsp_reset(sc);
   1121 	return 0;
   1122 }
   1123 
   1124 int
   1125 sbdsp_contdma(addr)
   1126 	void *addr;
   1127 {
   1128 	struct sbdsp_softc *sc = addr;
   1129 
   1130 	DPRINTF(("sbdsp_contdma: sc=%p\n", sc));
   1131 
   1132 	/* XXX how do we reinitialize the DMA controller state?  do we care? */
   1133 	(void)sbdsp_wdsp(sc, SB_DSP_CONT);
   1134 	return 0;
   1135 }
   1136 
   1137 int
   1138 sbdsp_set_timeconst(sc, tc)
   1139 	struct sbdsp_softc *sc;
   1140 	int tc;
   1141 {
   1142 	DPRINTF(("sbdsp_set_timeconst: sc=%p tc=%d\n", sc, tc));
   1143 
   1144 	if (sbdsp_wdsp(sc, SB_DSP_TIMECONST) < 0 ||
   1145 	    sbdsp_wdsp(sc, tc) < 0)
   1146 		return EIO;
   1147 
   1148 	return 0;
   1149 }
   1150 
   1151 int
   1152 sbdsp16_set_rate(sc, cmd, rate)
   1153 	struct sbdsp_softc *sc;
   1154 	int cmd, rate;
   1155 {
   1156 	DPRINTF(("sbdsp16_set_rate: sc=%p rate=%d\n", sc, rate));
   1157 
   1158 	if (sbdsp_wdsp(sc, cmd) < 0 ||
   1159 	    sbdsp_wdsp(sc, rate >> 8) < 0 ||
   1160 	    sbdsp_wdsp(sc, rate) < 0)
   1161 		return EIO;
   1162 	return 0;
   1163 }
   1164 
   1165 int
   1166 sbdsp_dma_init_input(addr, buf, cc)
   1167 	void *addr;
   1168 	void *buf;
   1169 	int cc;
   1170 {
   1171 	struct sbdsp_softc *sc = addr;
   1172 
   1173 	if (sc->sc_model == SB_1)
   1174 		return 0;
   1175 	sc->dmaflags = DMAMODE_READ | DMAMODE_LOOP;
   1176 	sc->dmaaddr = buf;
   1177 	sc->dmacnt = cc;
   1178 	sc->dmachan = sc->sc_imodep->precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
   1179 	DPRINTF(("sbdsp: dma start loop input addr=%p cc=%d chan=%d\n",
   1180 		 buf, cc, sc->dmachan));
   1181 	isa_dmastart(sc->sc_isa, sc->dmachan, sc->dmaaddr,
   1182 		     sc->dmacnt, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
   1183 	return 0;
   1184 }
   1185 
   1186 int
   1187 sbdsp_dma_input(addr, p, cc, intr, arg)
   1188 	void *addr;
   1189 	void *p;
   1190 	int cc;
   1191 	void (*intr) __P((void *));
   1192 	void *arg;
   1193 {
   1194 	struct sbdsp_softc *sc = addr;
   1195 	int loop = sc->sc_model != SB_1;
   1196 	int stereo = sc->sc_imodep->channels == 2;
   1197 	int filter;
   1198 
   1199 #ifdef AUDIO_DEBUG
   1200 	if (sbdspdebug > 1)
   1201 		printf("sbdsp_dma_input: cc=%d %p (%p)\n", cc, intr, arg);
   1202 #endif
   1203 #ifdef DIAGNOSTIC
   1204 	if (sc->sc_imodep->channels == 2 && (cc & 1)) {
   1205 		DPRINTF(("stereo record odd bytes (%d)\n", cc));
   1206 		return EIO;
   1207 	}
   1208 #endif
   1209 
   1210 	if (sc->sc_dmadir != SB_DMA_IN) {
   1211 		if (ISSBPRO(sc)) {
   1212 			if (sbdsp_wdsp(sc, sc->sc_imodep->cmdchan) < 0)
   1213 				goto badmode;
   1214 			filter = stereo ? SBP_FILTER_OFF : sc->in_filter;
   1215 			sbdsp_mix_write(sc, SBP_INFILTER,
   1216 					(sbdsp_mix_read(sc, SBP_INFILTER) &
   1217 					 ~SBP_IFILTER_MASK) | filter);
   1218 		}
   1219 
   1220 		if (ISSB16CLASS(sc)) {
   1221 			if (sbdsp16_set_rate(sc, SB_DSP16_INPUTRATE,
   1222 					     sc->sc_irate)) {
   1223 				DPRINTF(("sbdsp_dma_input: rate=%d set failed\n",
   1224 					 sc->sc_irate));
   1225 				goto giveup;
   1226 			}
   1227 		} else {
   1228 			if (sbdsp_set_timeconst(sc, sc->sc_itc)) {
   1229 				DPRINTF(("sbdsp_dma_output: tc=%d set failed\n",
   1230 					 sc->sc_irate));
   1231 				goto giveup;
   1232 			}
   1233 		}
   1234 
   1235 		sc->sc_dmadir = SB_DMA_IN;
   1236 		sc->dmaflags = DMAMODE_READ;
   1237 	} else {
   1238 		/* If already started just return. */
   1239 		if (loop)
   1240 			return 0;
   1241 	}
   1242 
   1243 	if (!loop) {
   1244 	    sc->dmaaddr = p;
   1245 	    sc->dmacnt = cc;
   1246 	    sc->dmachan = sc->sc_imodep->precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
   1247 #ifdef AUDIO_DEBUG
   1248 	    if (sbdspdebug > 2)
   1249 		printf("sbdsp_dma_input: dmastart %x %p %ld %d\n",
   1250 			sc->dmaflags, sc->dmaaddr, sc->dmacnt, sc->dmachan);
   1251 #endif
   1252 	    isa_dmastart(sc->sc_isa, sc->dmachan, sc->dmaaddr,
   1253 			 sc->dmacnt, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
   1254 	}
   1255 	sc->sc_intr = intr;
   1256 	sc->sc_arg = arg;
   1257 
   1258 	if ((sc->sc_model == SB_JAZZ && sc->dmachan > 3) ||
   1259 	    (sc->sc_model != SB_JAZZ && sc->sc_imodep->precision == 16))
   1260 		cc >>= 1;
   1261 	--cc;
   1262 	if (ISSB16CLASS(sc)) {
   1263 #ifdef AUDIO_DEBUG
   1264 		if (sbdspdebug > 2)
   1265 			printf("sbdsp16 input command %02x %02x %d\n",
   1266 			       sc->sc_imodep->cmd, sc->sc_ibmode, cc);
   1267 #endif
   1268 		if (sbdsp_wdsp(sc, sc->sc_imodep->cmd) < 0 ||
   1269 		    sbdsp_wdsp(sc, sc->sc_ibmode) < 0 ||
   1270 		    sbdsp16_wait(sc) ||
   1271 		    sbdsp_wdsp(sc, cc) < 0 ||
   1272 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1273 			DPRINTF(("sbdsp_dma_input: SB16 DMA start failed\n"));
   1274 			goto giveup;
   1275 		}
   1276 	} else {
   1277 		if (loop) {
   1278 			DPRINTF(("sbdsp_dma_input: set blocksize=%d\n", cc));
   1279 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
   1280 			    sbdsp_wdsp(sc, cc) < 0 ||
   1281 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1282 				DPRINTF(("sbdsp_dma_input: SB2 DMA blocksize failed\n"));
   1283 				goto giveup;
   1284 			}
   1285 			if (sbdsp_wdsp(sc, sc->sc_imodep->cmd) < 0) {
   1286 				DPRINTF(("sbdsp_dma_input: SB2 DMA start failed\n"));
   1287 				goto giveup;
   1288 			}
   1289 		} else {
   1290 			if (sbdsp_wdsp(sc, sc->sc_imodep->cmd) < 0 ||
   1291 			    sbdsp_wdsp(sc, cc) < 0 ||
   1292 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1293 				DPRINTF(("sbdsp_dma_input: SB1 DMA start failed\n"));
   1294 				goto giveup;
   1295 			}
   1296 		}
   1297 	}
   1298 	return 0;
   1299 
   1300 giveup:
   1301 	sbdsp_reset(sc);
   1302 	return EIO;
   1303 
   1304 badmode:
   1305 	DPRINTF(("sbdsp_dma_input: can't set mode\n"));
   1306 	return EIO;
   1307 }
   1308 
   1309 int
   1310 sbdsp_dma_init_output(addr, buf, cc)
   1311 	void *addr;
   1312 	void *buf;
   1313 	int cc;
   1314 {
   1315 	struct sbdsp_softc *sc = addr;
   1316 
   1317 	if (sc->sc_model == SB_1)
   1318 		return 0;
   1319 	sc->dmaflags = DMAMODE_WRITE | DMAMODE_LOOP;
   1320 	sc->dmaaddr = buf;
   1321 	sc->dmacnt = cc;
   1322 	sc->dmachan = sc->sc_omodep->precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
   1323 	DPRINTF(("sbdsp: dma start loop output addr=%p cc=%d chan=%d\n",
   1324 		 buf, cc, sc->dmachan));
   1325 	isa_dmastart(sc->sc_isa, sc->dmachan, sc->dmaaddr,
   1326 		     sc->dmacnt, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
   1327 	return 0;
   1328 }
   1329 
   1330 int
   1331 sbdsp_dma_output(addr, p, cc, intr, arg)
   1332 	void *addr;
   1333 	void *p;
   1334 	int cc;
   1335 	void (*intr) __P((void *));
   1336 	void *arg;
   1337 {
   1338 	struct sbdsp_softc *sc = addr;
   1339 	int loop = sc->sc_model != SB_1;
   1340 	int stereo = sc->sc_omodep->channels == 2;
   1341 	int cmd;
   1342 
   1343 #ifdef AUDIO_DEBUG
   1344 	if (sbdspdebug > 1)
   1345 		printf("sbdsp_dma_output: cc=%d %p (%p)\n", cc, intr, arg);
   1346 #endif
   1347 #ifdef DIAGNOSTIC
   1348 	if (stereo && (cc & 1)) {
   1349 		DPRINTF(("stereo playback odd bytes (%d)\n", cc));
   1350 		return EIO;
   1351 	}
   1352 #endif
   1353 
   1354 	if (sc->sc_dmadir != SB_DMA_OUT) {
   1355 		if (ISSBPRO(sc)) {
   1356 			/* make sure we re-set stereo mixer bit when we start
   1357 			   output. */
   1358 			sbdsp_mix_write(sc, SBP_STEREO,
   1359 			    (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
   1360 			    (stereo ?  SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
   1361 			cmd = sc->sc_omodep->cmdchan;
   1362 			if (cmd && sbdsp_wdsp(sc, cmd) < 0)
   1363 				goto badmode;
   1364 		}
   1365 
   1366 		if (ISSB16CLASS(sc)) {
   1367 			if (sbdsp16_set_rate(sc, SB_DSP16_OUTPUTRATE,
   1368 					     sc->sc_orate)) {
   1369 				DPRINTF(("sbdsp_dma_output: rate=%d set failed\n",
   1370 					 sc->sc_orate));
   1371 				goto giveup;
   1372 			}
   1373 		} else {
   1374 			if (sbdsp_set_timeconst(sc, sc->sc_otc)) {
   1375 				DPRINTF(("sbdsp_dma_output: tc=%d set failed\n",
   1376 					 sc->sc_orate));
   1377 				goto giveup;
   1378 			}
   1379 		}
   1380 
   1381 		sc->sc_dmadir = SB_DMA_OUT;
   1382 		sc->dmaflags = DMAMODE_WRITE;
   1383 	} else {
   1384 		/* If already started just return. */
   1385 		if (loop)
   1386 			return 0;
   1387 	}
   1388 
   1389 	if (!loop) {
   1390 	    sc->dmaaddr = p;
   1391 	    sc->dmacnt = cc;
   1392 	    sc->dmachan = sc->sc_omodep->precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
   1393 #ifdef AUDIO_DEBUG
   1394 	    if (sbdspdebug > 2)
   1395 		    printf("sbdsp: start dma out flags=%x, addr=%p, cnt=%ld, chan=%d\n",
   1396 			   sc->dmaflags, sc->dmaaddr, sc->dmacnt, sc->dmachan);
   1397 #endif
   1398 	    isa_dmastart(sc->sc_isa, sc->dmachan, sc->dmaaddr,
   1399 			 sc->dmacnt, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
   1400 	}
   1401 	sc->sc_intr = intr;
   1402 	sc->sc_arg = arg;
   1403 
   1404 	if ((sc->sc_model == SB_JAZZ && sc->dmachan > 3) ||
   1405 	    (sc->sc_model != SB_JAZZ && sc->sc_omodep->precision == 16))
   1406 		cc >>= 1;
   1407 	--cc;
   1408 	if (ISSB16CLASS(sc)) {
   1409 		if (sbdsp_wdsp(sc, sc->sc_omodep->cmd) < 0 ||
   1410 		    sbdsp_wdsp(sc, sc->sc_obmode) < 0 ||
   1411 		    sbdsp16_wait(sc) ||
   1412 		    sbdsp_wdsp(sc, cc) < 0 ||
   1413 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1414 			DPRINTF(("sbdsp_dma_output: SB16 DMA start failed\n"));
   1415 			goto giveup;
   1416 		}
   1417 	} else {
   1418 		if (loop) {
   1419 			DPRINTF(("sbdsp_dma_output: set blocksize=%d\n", cc));
   1420 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
   1421 			    sbdsp_wdsp(sc, cc) < 0 ||
   1422 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1423 				DPRINTF(("sbdsp_dma_output: SB2 DMA blocksize failed\n"));
   1424 				goto giveup;
   1425 			}
   1426 			if (sbdsp_wdsp(sc, sc->sc_omodep->cmd) < 0) {
   1427 				DPRINTF(("sbdsp_dma_output: SB2 DMA start failed\n"));
   1428 				goto giveup;
   1429 			}
   1430 		} else {
   1431 			if (sbdsp_wdsp(sc, sc->sc_omodep->cmd) < 0 ||
   1432 			    sbdsp_wdsp(sc, cc) < 0 ||
   1433 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
   1434 				DPRINTF(("sbdsp_dma_output: SB1 DMA start failed\n"));
   1435 				goto giveup;
   1436 			}
   1437 		}
   1438 	}
   1439 	return 0;
   1440 
   1441 giveup:
   1442 	sbdsp_reset(sc);
   1443 	return EIO;
   1444 
   1445 badmode:
   1446 	DPRINTF(("sbdsp_dma_output: can't set mode\n"));
   1447 	return EIO;
   1448 }
   1449 
   1450 /*
   1451  * Only the DSP unit on the sound blaster generates interrupts.
   1452  * There are three cases of interrupt: reception of a midi byte
   1453  * (when mode is enabled), completion of dma transmission, or
   1454  * completion of a dma reception.  The three modes are mutually
   1455  * exclusive so we know a priori which event has occurred.
   1456  *
   1457  * If there is interrupt sharing or a spurious interrupt occurs
   1458  * there is no way to distinuish this on an SB2.  So if you have
   1459  * an SB2 and experience problems, buy an SB16 (it's only $25).
   1460  */
   1461 int
   1462 sbdsp_intr(arg)
   1463 	void *arg;
   1464 {
   1465 	struct sbdsp_softc *sc = arg;
   1466 	int loop = sc->sc_model != SB_1;
   1467 	u_char irq;
   1468 
   1469 #ifdef AUDIO_DEBUG
   1470 	if (sbdspdebug > 1)
   1471 		printf("sbdsp_intr: intr=%p\n", sc->sc_intr);
   1472 #endif
   1473 	if (ISSB16CLASS(sc)) {
   1474 		irq = sbdsp_mix_read(sc, SBP_IRQ_STATUS);
   1475 		if ((irq & (SBP_IRQ_DMA8 | SBP_IRQ_DMA16)) == 0)
   1476 			return 0;
   1477 	} else {
   1478 		if (!loop && !isa_dmafinished(sc->sc_isa, sc->dmachan))
   1479 			return 0;
   1480 		irq = SBP_IRQ_DMA8;
   1481 	}
   1482 	sc->sc_interrupts++;
   1483 	delay(10);		/* XXX why? */
   1484 #if 0
   1485 	if (sc->sc_mintr != 0) {
   1486 		x = sbdsp_rdsp(sc);
   1487 		(*sc->sc_mintr)(sc->sc_arg, x);
   1488 	} else
   1489 #endif
   1490 	if (sc->sc_intr != 0) {
   1491 		/* clear interrupt */
   1492 		if (irq & SBP_IRQ_DMA8)
   1493 			bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_IRQACK8);
   1494 		if (irq & SBP_IRQ_DMA16)
   1495 			bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_IRQACK16);
   1496 		if (!loop)
   1497 			isa_dmadone(sc->sc_isa, sc->dmachan);
   1498 		(*sc->sc_intr)(sc->sc_arg);
   1499 	} else {
   1500 		return 0;
   1501 	}
   1502 	return 1;
   1503 }
   1504 
   1505 #if 0
   1506 /*
   1507  * Enter midi uart mode and arrange for read interrupts
   1508  * to vector to `intr'.  This puts the card in a mode
   1509  * which allows only midi I/O; the card must be reset
   1510  * to leave this mode.  Unfortunately, the card does not
   1511  * use transmit interrupts, so bytes must be output
   1512  * using polling.  To keep the polling overhead to a
   1513  * minimum, output should be driven off a timer.
   1514  * This is a little tricky since only 320us separate
   1515  * consecutive midi bytes.
   1516  */
   1517 void
   1518 sbdsp_set_midi_mode(sc, intr, arg)
   1519 	struct sbdsp_softc *sc;
   1520 	void (*intr)();
   1521 	void *arg;
   1522 {
   1523 
   1524 	sbdsp_wdsp(sc, SB_MIDI_UART_INTR);
   1525 	sc->sc_mintr = intr;
   1526 	sc->sc_intr = 0;
   1527 	sc->sc_arg = arg;
   1528 }
   1529 
   1530 /*
   1531  * Write a byte to the midi port, when in midi uart mode.
   1532  */
   1533 void
   1534 sbdsp_midi_output(sc, v)
   1535 	struct sbdsp_softc *sc;
   1536 	int v;
   1537 {
   1538 
   1539 	if (sbdsp_wdsp(sc, v) < 0)
   1540 		++sberr.wmidi;
   1541 }
   1542 #endif
   1543 
   1544 /* Mask a value 0-255, but round it first */
   1545 #define MAXVAL 256
   1546 static int
   1547 sbdsp_adjust(val, mask)
   1548 	int val, mask;
   1549 {
   1550 	val += (MAXVAL - mask) >> 1;
   1551 	if (val >= MAXVAL)
   1552 		val = MAXVAL-1;
   1553 	return val & mask;
   1554 }
   1555 
   1556 void
   1557 sbdsp_set_mixer_gain(sc, port)
   1558 	struct sbdsp_softc *sc;
   1559 	int port;
   1560 {
   1561 	int src, gain;
   1562 
   1563 	switch(sc->sc_mixer_model) {
   1564 	case SBM_NONE:
   1565 		return;
   1566 	case SBM_CT1335:
   1567 		gain = SB_1335_GAIN(sc->gain[port][SB_LEFT]);
   1568 		switch(port) {
   1569 		case SB_MASTER_VOL:
   1570 			src = SBP_1335_MASTER_VOL;
   1571 			break;
   1572 		case SB_MIDI_VOL:
   1573 			src = SBP_1335_MIDI_VOL;
   1574 			break;
   1575 		case SB_CD_VOL:
   1576 			src = SBP_1335_CD_VOL;
   1577 			break;
   1578 		case SB_VOICE_VOL:
   1579 			src = SBP_1335_VOICE_VOL;
   1580 			gain = SB_1335_MASTER_GAIN(sc->gain[port][SB_LEFT]);
   1581 			break;
   1582 		default:
   1583 			return;
   1584 		}
   1585 		sbdsp_mix_write(sc, src, gain);
   1586 		break;
   1587 	case SBM_CT1345:
   1588 		gain = SB_STEREO_GAIN(sc->gain[port][SB_LEFT],
   1589 				      sc->gain[port][SB_RIGHT]);
   1590 		switch (port) {
   1591 		case SB_MIC_VOL:
   1592 			src = SBP_MIC_VOL;
   1593 			gain = SB_MIC_GAIN(sc->gain[port][SB_LEFT]);
   1594 			break;
   1595 		case SB_MASTER_VOL:
   1596 			src = SBP_MASTER_VOL;
   1597 			break;
   1598 		case SB_LINE_IN_VOL:
   1599 			src = SBP_LINE_VOL;
   1600 			break;
   1601 		case SB_VOICE_VOL:
   1602 			src = SBP_VOICE_VOL;
   1603 			break;
   1604 		case SB_MIDI_VOL:
   1605 			src = SBP_MIDI_VOL;
   1606 			break;
   1607 		case SB_CD_VOL:
   1608 			src = SBP_CD_VOL;
   1609 			break;
   1610 		default:
   1611 			return;
   1612 		}
   1613 		sbdsp_mix_write(sc, src, gain);
   1614 		break;
   1615 	case SBM_CT1XX5:
   1616 	case SBM_CT1745:
   1617 		switch (port) {
   1618 		case SB_MIC_VOL:
   1619 			src = SB16P_MIC_L;
   1620 			break;
   1621 		case SB_MASTER_VOL:
   1622 			src = SB16P_MASTER_L;
   1623 			break;
   1624 		case SB_LINE_IN_VOL:
   1625 			src = SB16P_LINE_L;
   1626 			break;
   1627 		case SB_VOICE_VOL:
   1628 			src = SB16P_VOICE_L;
   1629 			break;
   1630 		case SB_MIDI_VOL:
   1631 			src = SB16P_MIDI_L;
   1632 			break;
   1633 		case SB_CD_VOL:
   1634 			src = SB16P_CD_L;
   1635 			break;
   1636 		case SB_INPUT_GAIN:
   1637 			src = SB16P_INPUT_GAIN_L;
   1638 			break;
   1639 		case SB_OUTPUT_GAIN:
   1640 			src = SB16P_OUTPUT_GAIN_L;
   1641 			break;
   1642 		case SB_TREBLE:
   1643 			src = SB16P_TREBLE_L;
   1644 			break;
   1645 		case SB_BASS:
   1646 			src = SB16P_BASS_L;
   1647 			break;
   1648 		case SB_PCSPEAKER:
   1649 			sbdsp_mix_write(sc, SB16P_PCSPEAKER, sc->gain[port][SB_LEFT]);
   1650 			return;
   1651 		default:
   1652 			return;
   1653 		}
   1654 		sbdsp_mix_write(sc, src, sc->gain[port][SB_LEFT]);
   1655 		sbdsp_mix_write(sc, SB16P_L_TO_R(src), sc->gain[port][SB_RIGHT]);
   1656 		break;
   1657 	}
   1658 }
   1659 
   1660 int
   1661 sbdsp_mixer_set_port(addr, cp)
   1662 	void *addr;
   1663 	mixer_ctrl_t *cp;
   1664 {
   1665 	struct sbdsp_softc *sc = addr;
   1666 	int lgain, rgain;
   1667 
   1668 	DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev,
   1669 	    cp->un.value.num_channels));
   1670 
   1671 	if (sc->sc_mixer_model == SBM_NONE)
   1672 		return EINVAL;
   1673 
   1674 	switch (cp->dev) {
   1675 	case SB_TREBLE:
   1676 	case SB_BASS:
   1677 		if (sc->sc_mixer_model == SBM_CT1345 ||
   1678                     sc->sc_mixer_model == SBM_CT1XX5) {
   1679 			if (cp->type != AUDIO_MIXER_ENUM)
   1680 				return EINVAL;
   1681 			switch (cp->dev) {
   1682 			case SB_TREBLE:
   1683 				sbdsp_set_ifilter(addr, cp->un.ord ? SB_TREBLE : 0);
   1684 				return 0;
   1685 			case SB_BASS:
   1686 				sbdsp_set_ifilter(addr, cp->un.ord ? SB_BASS : 0);
   1687 				return 0;
   1688 			}
   1689 		}
   1690 	case SB_PCSPEAKER:
   1691 	case SB_INPUT_GAIN:
   1692 	case SB_OUTPUT_GAIN:
   1693 		if (!ISSBM1745(sc))
   1694 			return EINVAL;
   1695 	case SB_MIC_VOL:
   1696 	case SB_LINE_IN_VOL:
   1697 		if (sc->sc_mixer_model == SBM_CT1335)
   1698 			return EINVAL;
   1699 	case SB_VOICE_VOL:
   1700 	case SB_MIDI_VOL:
   1701 	case SB_CD_VOL:
   1702 	case SB_MASTER_VOL:
   1703 		if (cp->type != AUDIO_MIXER_VALUE)
   1704 			return EINVAL;
   1705 
   1706 		/*
   1707 		 * All the mixer ports are stereo except for the microphone.
   1708 		 * If we get a single-channel gain value passed in, then we
   1709 		 * duplicate it to both left and right channels.
   1710 		 */
   1711 
   1712 		switch (cp->dev) {
   1713 		case SB_MIC_VOL:
   1714 			if (cp->un.value.num_channels != 1)
   1715 				return EINVAL;
   1716 
   1717 			lgain = rgain = SB_ADJUST_MIC_GAIN(sc,
   1718 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1719 			break;
   1720 		case SB_PCSPEAKER:
   1721 			if (cp->un.value.num_channels != 1)
   1722 				return EINVAL;
   1723 			/* fall into */
   1724 		case SB_INPUT_GAIN:
   1725 		case SB_OUTPUT_GAIN:
   1726 			lgain = rgain = SB_ADJUST_2_GAIN(sc,
   1727 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1728 			break;
   1729 		default:
   1730 			switch (cp->un.value.num_channels) {
   1731 			case 1:
   1732 				lgain = rgain = SB_ADJUST_GAIN(sc,
   1733 				  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1734 				break;
   1735 			case 2:
   1736 				if (sc->sc_mixer_model == SBM_CT1335)
   1737 					return EINVAL;
   1738 				lgain = SB_ADJUST_GAIN(sc,
   1739 				  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
   1740 				rgain = SB_ADJUST_GAIN(sc,
   1741 				  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
   1742 				break;
   1743 			default:
   1744 				return EINVAL;
   1745 			}
   1746 			break;
   1747 		}
   1748 		sc->gain[cp->dev][SB_LEFT]  = lgain;
   1749 		sc->gain[cp->dev][SB_RIGHT] = rgain;
   1750 
   1751 		sbdsp_set_mixer_gain(sc, cp->dev);
   1752 		break;
   1753 
   1754 	case SB_RECORD_SOURCE:
   1755 		if (ISSBM1745(sc)) {
   1756 			if (cp->type != AUDIO_MIXER_SET)
   1757 				return EINVAL;
   1758 			return sbdsp_set_in_ports(sc, cp->un.mask);
   1759 		} else {
   1760 			if (cp->type != AUDIO_MIXER_ENUM)
   1761 				return EINVAL;
   1762 			return sbdsp_set_in_port(sc, cp->un.ord);
   1763 		}
   1764 		break;
   1765 
   1766 	case SB_AGC:
   1767 		if (!ISSBM1745(sc) || cp->type != AUDIO_MIXER_ENUM)
   1768 			return EINVAL;
   1769 		sbdsp_mix_write(sc, SB16P_AGC, cp->un.ord & 1);
   1770 		break;
   1771 
   1772 	default:
   1773 		return EINVAL;
   1774 	}
   1775 
   1776 	return 0;
   1777 }
   1778 
   1779 int
   1780 sbdsp_mixer_get_port(addr, cp)
   1781 	void *addr;
   1782 	mixer_ctrl_t *cp;
   1783 {
   1784 	struct sbdsp_softc *sc = addr;
   1785 
   1786 	DPRINTF(("sbdsp_mixer_get_port: port=%d\n", cp->dev));
   1787 
   1788 	if (sc->sc_mixer_model == SBM_NONE)
   1789 		return EINVAL;
   1790 
   1791 	switch (cp->dev) {
   1792 	case SB_TREBLE:
   1793 	case SB_BASS:
   1794 		if (sc->sc_mixer_model == SBM_CT1345 ||
   1795                     sc->sc_mixer_model == SBM_CT1XX5) {
   1796 			switch (cp->dev) {
   1797 			case SB_TREBLE:
   1798 				cp->un.ord = sbdsp_get_ifilter(addr) == SB_TREBLE;
   1799 				return 0;
   1800 			case SB_BASS:
   1801 				cp->un.ord = sbdsp_get_ifilter(addr) == SB_BASS;
   1802 				return 0;
   1803 			}
   1804 		}
   1805 	case SB_PCSPEAKER:
   1806 	case SB_INPUT_GAIN:
   1807 	case SB_OUTPUT_GAIN:
   1808 		if (!ISSBM1745(sc))
   1809 			return EINVAL;
   1810 	case SB_MIC_VOL:
   1811 	case SB_LINE_IN_VOL:
   1812 		if (sc->sc_mixer_model == SBM_CT1335)
   1813 			return EINVAL;
   1814 	case SB_VOICE_VOL:
   1815 	case SB_MIDI_VOL:
   1816 	case SB_CD_VOL:
   1817 	case SB_MASTER_VOL:
   1818 		switch (cp->dev) {
   1819 		case SB_MIC_VOL:
   1820 		case SB_PCSPEAKER:
   1821 			if (cp->un.value.num_channels != 1)
   1822 				return EINVAL;
   1823 			/* fall into */
   1824 		default:
   1825 			switch (cp->un.value.num_channels) {
   1826 			case 1:
   1827 				cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1828 					sc->gain[cp->dev][SB_LEFT];
   1829 				break;
   1830 			case 2:
   1831 				cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
   1832 					sc->gain[cp->dev][SB_LEFT];
   1833 				cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
   1834 					sc->gain[cp->dev][SB_RIGHT];
   1835 				break;
   1836 			default:
   1837 				return EINVAL;
   1838 			}
   1839 			break;
   1840 		}
   1841 		break;
   1842 
   1843 	case SB_RECORD_SOURCE:
   1844 		if (ISSBM1745(sc))
   1845 			cp->un.mask = sc->in_mask;
   1846 		else
   1847 			cp->un.ord = sc->in_port;
   1848 		break;
   1849 
   1850 	case SB_AGC:
   1851 		if (!ISSBM1745(sc))
   1852 			return EINVAL;
   1853 		cp->un.ord = sbdsp_mix_read(sc, SB16P_AGC);
   1854 		break;
   1855 
   1856 	default:
   1857 		return EINVAL;
   1858 	}
   1859 
   1860 	return 0;
   1861 }
   1862 
   1863 int
   1864 sbdsp_mixer_query_devinfo(addr, dip)
   1865 	void *addr;
   1866 	mixer_devinfo_t *dip;
   1867 {
   1868 	struct sbdsp_softc *sc = addr;
   1869 	int chan, class;
   1870 
   1871 	DPRINTF(("sbdsp_mixer_query_devinfo: model=%d index=%d\n",
   1872 		 sc->sc_mixer_model, dip->index));
   1873 
   1874 	if (sc->sc_mixer_model == SBM_NONE)
   1875 		return ENXIO;
   1876 
   1877 	chan = sc->sc_mixer_model == SBM_CT1335 ? 1 : 2;
   1878 	class = ISSBM1745(sc) ? SB_INPUT_CLASS : SB_OUTPUT_CLASS;
   1879 
   1880 	switch (dip->index) {
   1881 	case SB_MASTER_VOL:
   1882 		dip->type = AUDIO_MIXER_VALUE;
   1883 		dip->mixer_class = SB_OUTPUT_CLASS;
   1884 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1885 		strcpy(dip->label.name, AudioNmaster);
   1886 		dip->un.v.num_channels = chan;
   1887 		strcpy(dip->un.v.units.name, AudioNvolume);
   1888 		return 0;
   1889 	case SB_MIDI_VOL:
   1890 		dip->type = AUDIO_MIXER_VALUE;
   1891 		dip->mixer_class = class;
   1892 		dip->prev = AUDIO_MIXER_LAST;
   1893 		dip->next = AUDIO_MIXER_LAST;
   1894 		strcpy(dip->label.name, AudioNfmsynth);
   1895 		dip->un.v.num_channels = chan;
   1896 		strcpy(dip->un.v.units.name, AudioNvolume);
   1897 		return 0;
   1898 	case SB_CD_VOL:
   1899 		dip->type = AUDIO_MIXER_VALUE;
   1900 		dip->mixer_class = class;
   1901 		dip->prev = AUDIO_MIXER_LAST;
   1902 		dip->next = AUDIO_MIXER_LAST;
   1903 		strcpy(dip->label.name, AudioNcd);
   1904 		dip->un.v.num_channels = chan;
   1905 		strcpy(dip->un.v.units.name, AudioNvolume);
   1906 		return 0;
   1907 	case SB_VOICE_VOL:
   1908 		dip->type = AUDIO_MIXER_VALUE;
   1909 		dip->mixer_class = class;
   1910 		dip->prev = AUDIO_MIXER_LAST;
   1911 		dip->next = AUDIO_MIXER_LAST;
   1912 		strcpy(dip->label.name, AudioNdac);
   1913 		dip->un.v.num_channels = chan;
   1914 		strcpy(dip->un.v.units.name, AudioNvolume);
   1915 		return 0;
   1916 	case SB_OUTPUT_CLASS:
   1917 		dip->type = AUDIO_MIXER_CLASS;
   1918 		dip->mixer_class = SB_OUTPUT_CLASS;
   1919 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1920 		strcpy(dip->label.name, AudioCOutputs);
   1921 		return 0;
   1922 	}
   1923 
   1924 	if (sc->sc_mixer_model == SBM_CT1335)
   1925 		return ENXIO;
   1926 
   1927 	switch (dip->index) {
   1928 	case SB_MIC_VOL:
   1929 		dip->type = AUDIO_MIXER_VALUE;
   1930 		dip->mixer_class = class;
   1931 		dip->prev = AUDIO_MIXER_LAST;
   1932 		dip->next = AUDIO_MIXER_LAST;
   1933 		strcpy(dip->label.name, AudioNmicrophone);
   1934 		dip->un.v.num_channels = 1;
   1935 		strcpy(dip->un.v.units.name, AudioNvolume);
   1936 		return 0;
   1937 
   1938 	case SB_LINE_IN_VOL:
   1939 		dip->type = AUDIO_MIXER_VALUE;
   1940 		dip->mixer_class = class;
   1941 		dip->prev = AUDIO_MIXER_LAST;
   1942 		dip->next = AUDIO_MIXER_LAST;
   1943 		strcpy(dip->label.name, AudioNline);
   1944 		dip->un.v.num_channels = 2;
   1945 		strcpy(dip->un.v.units.name, AudioNvolume);
   1946 		return 0;
   1947 
   1948 	case SB_RECORD_SOURCE:
   1949 		dip->mixer_class = SB_RECORD_CLASS;
   1950 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1951 		strcpy(dip->label.name, AudioNsource);
   1952 		if (ISSBM1745(sc)) {
   1953 			dip->type = AUDIO_MIXER_SET;
   1954 			dip->un.s.num_mem = 4;
   1955 			strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1956 			dip->un.s.member[0].mask = 1 << SB_MIC_VOL;
   1957 			strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1958 			dip->un.s.member[1].mask = 1 << SB_CD_VOL;
   1959 			strcpy(dip->un.s.member[2].label.name, AudioNline);
   1960 			dip->un.s.member[2].mask = 1 << SB_LINE_IN_VOL;
   1961 			strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1962 			dip->un.s.member[3].mask = 1 << SB_MIDI_VOL;
   1963 		} else {
   1964 			dip->type = AUDIO_MIXER_ENUM;
   1965 			dip->un.e.num_mem = 3;
   1966 			strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
   1967 			dip->un.e.member[0].ord = SB_MIC_VOL;
   1968 			strcpy(dip->un.e.member[1].label.name, AudioNcd);
   1969 			dip->un.e.member[1].ord = SB_CD_VOL;
   1970 			strcpy(dip->un.e.member[2].label.name, AudioNline);
   1971 			dip->un.e.member[2].ord = SB_LINE_IN_VOL;
   1972 		}
   1973 		return 0;
   1974 
   1975 	case SB_BASS:
   1976 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1977 		strcpy(dip->label.name, AudioNbass);
   1978 		if (sc->sc_mixer_model == SBM_CT1745) {
   1979 			dip->type = AUDIO_MIXER_VALUE;
   1980 			dip->mixer_class = SB_EQUALIZATION_CLASS;
   1981 			dip->un.v.num_channels = 2;
   1982 			strcpy(dip->un.v.units.name, AudioNbass);
   1983 		} else {
   1984 			dip->type = AUDIO_MIXER_ENUM;
   1985 			dip->mixer_class = SB_INPUT_CLASS;
   1986 			dip->un.e.num_mem = 2;
   1987 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1988 			dip->un.e.member[0].ord = 0;
   1989 			strcpy(dip->un.e.member[1].label.name, AudioNon);
   1990 			dip->un.e.member[1].ord = 1;
   1991 		}
   1992 		return 0;
   1993 
   1994 	case SB_TREBLE:
   1995 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1996 		strcpy(dip->label.name, AudioNtreble);
   1997 		if (sc->sc_mixer_model == SBM_CT1745) {
   1998 			dip->type = AUDIO_MIXER_VALUE;
   1999 			dip->mixer_class = SB_EQUALIZATION_CLASS;
   2000 			dip->un.v.num_channels = 2;
   2001 			strcpy(dip->un.v.units.name, AudioNtreble);
   2002 		} else {
   2003 			dip->type = AUDIO_MIXER_ENUM;
   2004 			dip->mixer_class = SB_INPUT_CLASS;
   2005 			dip->un.e.num_mem = 2;
   2006 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
   2007 			dip->un.e.member[0].ord = 0;
   2008 			strcpy(dip->un.e.member[1].label.name, AudioNon);
   2009 			dip->un.e.member[1].ord = 1;
   2010 		}
   2011 		return 0;
   2012 
   2013 	case SB_RECORD_CLASS:			/* record source class */
   2014 		dip->type = AUDIO_MIXER_CLASS;
   2015 		dip->mixer_class = SB_RECORD_CLASS;
   2016 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2017 		strcpy(dip->label.name, AudioCRecord);
   2018 		return 0;
   2019 
   2020 	}
   2021 
   2022 	if (sc->sc_mixer_model == SBM_CT1345)
   2023 		return ENXIO;
   2024 
   2025 	switch(dip->index) {
   2026 	case SB_PCSPEAKER:
   2027 		dip->type = AUDIO_MIXER_VALUE;
   2028 		dip->mixer_class = SB_INPUT_CLASS;
   2029 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   2030 		strcpy(dip->label.name, "pc_speaker");
   2031 		dip->un.v.num_channels = 1;
   2032 		strcpy(dip->un.v.units.name, AudioNvolume);
   2033 		return 0;
   2034 
   2035 	case SB_INPUT_GAIN:
   2036 		dip->type = AUDIO_MIXER_VALUE;
   2037 		dip->mixer_class = SB_INPUT_CLASS;
   2038 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   2039 		strcpy(dip->label.name, AudioNinput);
   2040 		dip->un.v.num_channels = 2;
   2041 		strcpy(dip->un.v.units.name, AudioNvolume);
   2042 		return 0;
   2043 
   2044 	case SB_OUTPUT_GAIN:
   2045 		dip->type = AUDIO_MIXER_VALUE;
   2046 		dip->mixer_class = SB_OUTPUT_CLASS;
   2047 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   2048 		strcpy(dip->label.name, AudioNoutput);
   2049 		dip->un.v.num_channels = 2;
   2050 		strcpy(dip->un.v.units.name, AudioNvolume);
   2051 		return 0;
   2052 
   2053 	case SB_AGC:
   2054 		dip->type = AUDIO_MIXER_ENUM;
   2055 		dip->mixer_class = SB_INPUT_CLASS;
   2056 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   2057 		strcpy(dip->label.name, "AGC");
   2058 		dip->un.e.num_mem = 2;
   2059 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   2060 		dip->un.e.member[0].ord = 0;
   2061 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   2062 		dip->un.e.member[1].ord = 1;
   2063 		return 0;
   2064 
   2065 	case SB_INPUT_CLASS:
   2066 		dip->type = AUDIO_MIXER_CLASS;
   2067 		dip->mixer_class = SB_INPUT_CLASS;
   2068 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2069 		strcpy(dip->label.name, AudioCInputs);
   2070 		return 0;
   2071 
   2072 	case SB_EQUALIZATION_CLASS:
   2073 		dip->type = AUDIO_MIXER_CLASS;
   2074 		dip->mixer_class = SB_EQUALIZATION_CLASS;
   2075 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2076 		strcpy(dip->label.name, AudioCEqualization);
   2077 		return 0;
   2078 	}
   2079 
   2080 	return ENXIO;
   2081 }
   2082 
   2083 void *
   2084 sb_malloc(addr, size, pool, flags)
   2085 	void *addr;
   2086 	unsigned long size;
   2087 	int pool;
   2088 	int flags;
   2089 {
   2090 	struct sbdsp_softc *sc = addr;
   2091 
   2092 	return isa_malloc(sc->sc_isa, 4, size, pool, flags);
   2093 }
   2094 
   2095 void
   2096 sb_free(addr, ptr, pool)
   2097 	void *addr;
   2098 	void *ptr;
   2099 	int pool;
   2100 {
   2101 	isa_free(ptr, pool);
   2102 }
   2103 
   2104 unsigned long
   2105 sb_round(addr, size)
   2106 	void *addr;
   2107 	unsigned long size;
   2108 {
   2109 	if (size > MAX_ISADMA)
   2110 		size = MAX_ISADMA;
   2111 	return size;
   2112 }
   2113 
   2114 int
   2115 sb_mappage(addr, mem, off, prot)
   2116 	void *addr;
   2117         void *mem;
   2118         int off;
   2119 	int prot;
   2120 {
   2121 	return isa_mappage(mem, off, prot);
   2122 }
   2123 
   2124 int
   2125 sbdsp_get_props(addr)
   2126 	void *addr;
   2127 {
   2128 	return AUDIO_PROP_MMAP;
   2129 }
   2130