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