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