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