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