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