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