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