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