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