Home | History | Annotate | Line # | Download | only in isa
ess.c revision 1.42
      1 /*	$NetBSD: ess.c,v 1.42 1999/03/18 04:31:36 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright 1997
      5  * Digital Equipment Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and
      8  * copied only in accordance with the following terms and conditions.
      9  * Subject to these conditions, you may download, copy, install,
     10  * use, modify and distribute this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce
     14  *    and retain this copyright notice and list of conditions as
     15  *    they appear in the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19  *    Corporation" name nor any trademark or logo of Digital Equipment
     20  *    Corporation may be used to endorse or promote products derived
     21  *    from this software without the prior written permission of
     22  *    Digital Equipment Corporation.
     23  *
     24  * 3) This software is provided "AS-IS" and any express or implied
     25  *    warranties, including but not limited to, any implied warranties
     26  *    of merchantability, fitness for a particular purpose, or
     27  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28  *    liable for any damages whatsoever, and in particular, DIGITAL
     29  *    shall not be liable for special, indirect, consequential, or
     30  *    incidental damages or damages for lost profits, loss of
     31  *    revenue or loss of use, whether such damages arise in contract,
     32  *    negligence, tort, under statute, in equity, at law or otherwise,
     33  *    even if advised of the possibility of such damage.
     34  */
     35 
     36 /*
     37 **++
     38 **
     39 **  ess.c
     40 **
     41 **  FACILITY:
     42 **
     43 **	DIGITAL Network Appliance Reference Design (DNARD)
     44 **
     45 **  MODULE DESCRIPTION:
     46 **
     47 **      This module contains the device driver for the ESS
     48 **      Technologies 1888/1887/888 sound chip. The code in sbdsp.c was
     49 **	used as a reference point when implementing this driver.
     50 **
     51 **  AUTHORS:
     52 **
     53 **	Blair Fidler	Software Engineering Australia
     54 **			Gold Coast, Australia.
     55 **
     56 **  CREATION DATE:
     57 **
     58 **	March 10, 1997.
     59 **
     60 **  MODIFICATION HISTORY:
     61 **
     62 **	Heavily modified by Lennart Augustsson and Charles M. Hannum for
     63 **	bus_dma, changes to audio interface, and many bug fixes.
     64 **	ESS1788 support by Nathan J. Williams and Charles M. Hannum.
     65 **--
     66 */
     67 
     68 #include <sys/param.h>
     69 #include <sys/systm.h>
     70 #include <sys/errno.h>
     71 #include <sys/ioctl.h>
     72 #include <sys/syslog.h>
     73 #include <sys/device.h>
     74 #include <sys/proc.h>
     75 #include <sys/kernel.h>
     76 
     77 #include <machine/cpu.h>
     78 #include <machine/intr.h>
     79 #include <machine/bus.h>
     80 
     81 #include <sys/audioio.h>
     82 #include <dev/audio_if.h>
     83 #include <dev/auconv.h>
     84 #include <dev/mulaw.h>
     85 
     86 #include <dev/isa/isavar.h>
     87 #include <dev/isa/isadmavar.h>
     88 
     89 #include <dev/isa/essvar.h>
     90 #include <dev/isa/essreg.h>
     91 
     92 #ifdef AUDIO_DEBUG
     93 #define DPRINTF(x)	if (essdebug) printf x
     94 #define DPRINTFN(n,x)	if (essdebug>(n)) printf x
     95 int	essdebug = 0;
     96 #else
     97 #define DPRINTF(x)
     98 #define DPRINTFN(n,x)
     99 #endif
    100 
    101 #if 0
    102 unsigned uuu;
    103 #define EREAD1(t, h, a) (uuu=bus_space_read_1(t, h, a),printf("EREAD  %02x=%02x\n", ((int)h&0xfff)+a, uuu),uuu)
    104 #define EWRITE1(t, h, a, d) (printf("EWRITE %02x=%02x\n", ((int)h & 0xfff)+a, d), bus_space_write_1(t, h, a, d))
    105 #else
    106 #define EREAD1(t, h, a) bus_space_read_1(t, h, a)
    107 #define EWRITE1(t, h, a, d) bus_space_write_1(t, h, a, d)
    108 #endif
    109 
    110 
    111 int	ess_setup_sc __P((struct ess_softc *, int));
    112 
    113 int	ess_open __P((void *, int));
    114 void	ess_1788_close __P((void *));
    115 void	ess_1888_close __P((void *));
    116 int	ess_getdev __P((void *, struct audio_device *));
    117 int	ess_drain __P((void *));
    118 
    119 int	ess_query_encoding __P((void *, struct audio_encoding *));
    120 
    121 int	ess_set_params __P((void *, int, int, struct audio_params *,
    122 	    struct audio_params *));
    123 
    124 int	ess_round_blocksize __P((void *, int));
    125 
    126 int	ess_audio1_trigger_output __P((void *, void *, void *, int,
    127 	    void (*)(void *), void *, struct audio_params *));
    128 int	ess_audio2_trigger_output __P((void *, void *, void *, int,
    129 	    void (*)(void *), void *, struct audio_params *));
    130 int	ess_audio1_trigger_input __P((void *, void *, void *, int,
    131 	    void (*)(void *), void *, struct audio_params *));
    132 int	ess_audio1_halt __P((void *));
    133 int	ess_audio2_halt __P((void *));
    134 int	ess_audio1_intr __P((void *));
    135 int	ess_audio2_intr __P((void *));
    136 void	ess_audio1_poll __P((void *));
    137 void	ess_audio2_poll __P((void *));
    138 
    139 int	ess_speaker_ctl __P((void *, int));
    140 
    141 int	ess_getdev __P((void *, struct audio_device *));
    142 
    143 int	ess_set_port __P((void *, mixer_ctrl_t *));
    144 int	ess_get_port __P((void *, mixer_ctrl_t *));
    145 
    146 void   *ess_malloc __P((void *, int, size_t, int, int));
    147 void	ess_free __P((void *, void *, int));
    148 size_t	ess_round_buffersize __P((void *, int, size_t));
    149 int	ess_mappage __P((void *, void *, int, int));
    150 
    151 
    152 int	ess_query_devinfo __P((void *, mixer_devinfo_t *));
    153 int	ess_1788_get_props __P((void *));
    154 int	ess_1888_get_props __P((void *));
    155 
    156 void	ess_speaker_on __P((struct ess_softc *));
    157 void	ess_speaker_off __P((struct ess_softc *));
    158 
    159 int	ess_config_addr __P((struct ess_softc *));
    160 void	ess_config_irq __P((struct ess_softc *));
    161 void	ess_config_drq __P((struct ess_softc *));
    162 void	ess_setup __P((struct ess_softc *));
    163 int	ess_identify __P((struct ess_softc *));
    164 
    165 int	ess_reset __P((struct ess_softc *));
    166 void	ess_set_gain __P((struct ess_softc *, int, int));
    167 int	ess_set_in_port __P((struct ess_softc *, int));
    168 int	ess_set_in_ports __P((struct ess_softc *, int));
    169 u_int	ess_srtotc __P((u_int));
    170 u_int	ess_srtofc __P((u_int));
    171 u_char	ess_get_dsp_status __P((struct ess_softc *));
    172 u_char	ess_dsp_read_ready __P((struct ess_softc *));
    173 u_char	ess_dsp_write_ready __P((struct ess_softc *sc));
    174 int	ess_rdsp __P((struct ess_softc *));
    175 int	ess_wdsp __P((struct ess_softc *, u_char));
    176 u_char	ess_read_x_reg __P((struct ess_softc *, u_char));
    177 int	ess_write_x_reg __P((struct ess_softc *, u_char, u_char));
    178 void	ess_clear_xreg_bits __P((struct ess_softc *, u_char, u_char));
    179 void	ess_set_xreg_bits __P((struct ess_softc *, u_char, u_char));
    180 u_char	ess_read_mix_reg __P((struct ess_softc *, u_char));
    181 void	ess_write_mix_reg __P((struct ess_softc *, u_char, u_char));
    182 void	ess_clear_mreg_bits __P((struct ess_softc *, u_char, u_char));
    183 void	ess_set_mreg_bits __P((struct ess_softc *, u_char, u_char));
    184 
    185 static char *essmodel[] = {
    186 	"unsupported",
    187 	"1888",
    188 	"1887",
    189 	"888",
    190 	"1788"
    191 };
    192 
    193 struct audio_device ess_device = {
    194 	"ESS Technology",
    195 	"x",
    196 	"ess"
    197 };
    198 
    199 /*
    200  * Define our interface to the higher level audio driver.
    201  */
    202 
    203 struct audio_hw_if ess_1788_hw_if = {
    204 	ess_open,
    205 	ess_1788_close,
    206 	ess_drain,
    207 	ess_query_encoding,
    208 	ess_set_params,
    209 	ess_round_blocksize,
    210 	NULL,
    211 	NULL,
    212 	NULL,
    213 	NULL,
    214 	NULL,
    215 	ess_audio1_halt,
    216 	ess_audio1_halt,
    217 	ess_speaker_ctl,
    218 	ess_getdev,
    219 	NULL,
    220 	ess_set_port,
    221 	ess_get_port,
    222 	ess_query_devinfo,
    223 	ess_malloc,
    224 	ess_free,
    225 	ess_round_buffersize,
    226 	ess_mappage,
    227 	ess_1788_get_props,
    228 	ess_audio1_trigger_output,
    229 	ess_audio1_trigger_input,
    230 };
    231 
    232 struct audio_hw_if ess_1888_hw_if = {
    233 	ess_open,
    234 	ess_1888_close,
    235 	ess_drain,
    236 	ess_query_encoding,
    237 	ess_set_params,
    238 	ess_round_blocksize,
    239 	NULL,
    240 	NULL,
    241 	NULL,
    242 	NULL,
    243 	NULL,
    244 	ess_audio2_halt,
    245 	ess_audio1_halt,
    246 	ess_speaker_ctl,
    247 	ess_getdev,
    248 	NULL,
    249 	ess_set_port,
    250 	ess_get_port,
    251 	ess_query_devinfo,
    252 	ess_malloc,
    253 	ess_free,
    254 	ess_round_buffersize,
    255 	ess_mappage,
    256 	ess_1888_get_props,
    257 	ess_audio2_trigger_output,
    258 	ess_audio1_trigger_input,
    259 };
    260 
    261 #ifdef AUDIO_DEBUG
    262 void ess_printsc __P((struct ess_softc *));
    263 void ess_dump_mixer __P((struct ess_softc *));
    264 
    265 void
    266 ess_printsc(sc)
    267 	struct ess_softc *sc;
    268 {
    269 	int i;
    270 
    271 	printf("open %d iobase 0x%x outport %u inport %u speaker %s\n",
    272 	       (int)sc->sc_open, sc->sc_iobase, sc->out_port,
    273 	       sc->in_port, sc->spkr_state ? "on" : "off");
    274 
    275 	printf("audio1: dmachan %d irq %d nintr %lu intr %p arg %p\n",
    276 	       sc->sc_audio1.drq, sc->sc_audio1.irq, sc->sc_audio1.nintr,
    277 	       sc->sc_audio1.intr, sc->sc_audio1.arg);
    278 
    279 	if (sc->sc_model != ESS_1788) {
    280 		printf("audio2: dmachan %d irq %d nintr %lu intr %p arg %p\n",
    281 		       sc->sc_audio2.drq, sc->sc_audio2.irq, sc->sc_audio2.nintr,
    282 		       sc->sc_audio2.intr, sc->sc_audio2.arg);
    283 	}
    284 
    285 	printf("gain:");
    286 	for (i = 0; i < sc->ndevs; i++)
    287 		printf(" %u,%u", sc->gain[i][ESS_LEFT], sc->gain[i][ESS_RIGHT]);
    288 	printf("\n");
    289 }
    290 
    291 void
    292 ess_dump_mixer(sc)
    293 	struct ess_softc *sc;
    294 {
    295 	printf("ESS_DAC_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
    296 	       0x7C, ess_read_mix_reg(sc, 0x7C));
    297 	printf("ESS_MIC_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
    298 	       0x1A, ess_read_mix_reg(sc, 0x1A));
    299 	printf("ESS_LINE_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
    300 	       0x3E, ess_read_mix_reg(sc, 0x3E));
    301 	printf("ESS_SYNTH_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
    302 	       0x36, ess_read_mix_reg(sc, 0x36));
    303 	printf("ESS_CD_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
    304 	       0x38, ess_read_mix_reg(sc, 0x38));
    305 	printf("ESS_AUXB_PLAY_VOL: mix reg 0x%02x=0x%02x\n",
    306 	       0x3A, ess_read_mix_reg(sc, 0x3A));
    307 	printf("ESS_MASTER_VOL: mix reg 0x%02x=0x%02x\n",
    308 	       0x32, ess_read_mix_reg(sc, 0x32));
    309 	printf("ESS_PCSPEAKER_VOL: mix reg 0x%02x=0x%02x\n",
    310 	       0x3C, ess_read_mix_reg(sc, 0x3C));
    311 	printf("ESS_DAC_REC_VOL: mix reg 0x%02x=0x%02x\n",
    312 	       0x69, ess_read_mix_reg(sc, 0x69));
    313 	printf("ESS_MIC_REC_VOL: mix reg 0x%02x=0x%02x\n",
    314 	       0x68, ess_read_mix_reg(sc, 0x68));
    315 	printf("ESS_LINE_REC_VOL: mix reg 0x%02x=0x%02x\n",
    316 	       0x6E, ess_read_mix_reg(sc, 0x6E));
    317 	printf("ESS_SYNTH_REC_VOL: mix reg 0x%02x=0x%02x\n",
    318 	       0x6B, ess_read_mix_reg(sc, 0x6B));
    319 	printf("ESS_CD_REC_VOL: mix reg 0x%02x=0x%02x\n",
    320 	       0x6A, ess_read_mix_reg(sc, 0x6A));
    321 	printf("ESS_AUXB_REC_VOL: mix reg 0x%02x=0x%02x\n",
    322 	       0x6C, ess_read_mix_reg(sc, 0x6C));
    323 	printf("ESS_RECORD_VOL: x reg 0x%02x=0x%02x\n",
    324 	       0xB4, ess_read_x_reg(sc, 0xB4));
    325 	printf("Audio 1 play vol (unused): mix reg 0x%02x=0x%02x\n",
    326 	       0x14, ess_read_mix_reg(sc, 0x14));
    327 
    328 	printf("ESS_MIC_PREAMP: x reg 0x%02x=0x%02x\n",
    329 	       ESS_XCMD_PREAMP_CTRL, ess_read_x_reg(sc, ESS_XCMD_PREAMP_CTRL));
    330 	printf("ESS_RECORD_MONITOR: x reg 0x%02x=0x%02x\n",
    331 	       ESS_XCMD_AUDIO_CTRL, ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL));
    332 	printf("Record source: mix reg 0x%02x=0x%02x, 0x%02x=0x%02x\n",
    333 	       ESS_MREG_ADC_SOURCE, ess_read_mix_reg(sc, ESS_MREG_ADC_SOURCE),
    334 	       ESS_MREG_AUDIO2_CTRL2, ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2));
    335 }
    336 
    337 #endif
    338 
    339 /*
    340  * Configure the ESS chip for the desired audio base address.
    341  */
    342 int
    343 ess_config_addr(sc)
    344 	struct ess_softc *sc;
    345 {
    346 	int iobase = sc->sc_iobase;
    347 	bus_space_tag_t iot = sc->sc_iot;
    348 
    349 	/*
    350 	 * Configure using the System Control Register method.  This
    351 	 * method is used when the AMODE line is tied high, which is
    352 	 * the case for the Shark, but not for the evaluation board.
    353 	 */
    354 
    355 	bus_space_handle_t scr_access_ioh;
    356 	bus_space_handle_t scr_ioh;
    357 	u_short scr_value;
    358 
    359 	/*
    360 	 * Set the SCR bit to enable audio.
    361 	 */
    362 	scr_value = ESS_SCR_AUDIO_ENABLE;
    363 
    364 	/*
    365 	 * Set the SCR bits necessary to select the specified audio
    366 	 * base address.
    367 	 */
    368 	switch(iobase) {
    369 	case 0x220:
    370 		scr_value |= ESS_SCR_AUDIO_220;
    371 		break;
    372 	case 0x230:
    373 		scr_value |= ESS_SCR_AUDIO_230;
    374 		break;
    375 	case 0x240:
    376 		scr_value |= ESS_SCR_AUDIO_240;
    377 		break;
    378 	case 0x250:
    379 		scr_value |= ESS_SCR_AUDIO_250;
    380 		break;
    381 	default:
    382 		printf("ess: configured iobase 0x%x invalid\n", iobase);
    383 		return (1);
    384 		break;
    385 	}
    386 
    387 	/*
    388 	 * Get a mapping for the System Control Register (SCR) access
    389 	 * registers and the SCR data registers.
    390 	 */
    391 	if (bus_space_map(iot, ESS_SCR_ACCESS_BASE, ESS_SCR_ACCESS_PORTS,
    392 			  0, &scr_access_ioh)) {
    393 		printf("ess: can't map SCR access registers\n");
    394 		return (1);
    395 	}
    396 	if (bus_space_map(iot, ESS_SCR_BASE, ESS_SCR_PORTS,
    397 			  0, &scr_ioh)) {
    398 		printf("ess: can't map SCR registers\n");
    399 		bus_space_unmap(iot, scr_access_ioh, ESS_SCR_ACCESS_PORTS);
    400 		return (1);
    401 	}
    402 
    403 	/* Unlock the SCR. */
    404 	EWRITE1(iot, scr_access_ioh, ESS_SCR_UNLOCK, 0);
    405 
    406 	/* Write the base address information into SCR[0]. */
    407 	EWRITE1(iot, scr_ioh, ESS_SCR_INDEX, 0);
    408 	EWRITE1(iot, scr_ioh, ESS_SCR_DATA, scr_value);
    409 
    410 	/* Lock the SCR. */
    411 	EWRITE1(iot, scr_access_ioh, ESS_SCR_LOCK, 0);
    412 
    413 	/* Unmap the SCR access ports and the SCR data ports. */
    414 	bus_space_unmap(iot, scr_access_ioh, ESS_SCR_ACCESS_PORTS);
    415 	bus_space_unmap(iot, scr_ioh, ESS_SCR_PORTS);
    416 
    417 	return 0;
    418 }
    419 
    420 
    421 /*
    422  * Configure the ESS chip for the desired IRQ and DMA channels.
    423  * ESS  ISA
    424  * --------
    425  * IRQA irq9
    426  * IRQB irq5
    427  * IRQC irq7
    428  * IRQD irq10
    429  * IRQE irq15
    430  *
    431  * DRQA drq0
    432  * DRQB drq1
    433  * DRQC drq3
    434  * DRQD drq5
    435  */
    436 void
    437 ess_config_irq(sc)
    438 	struct ess_softc *sc;
    439 {
    440 	int v;
    441 
    442 	DPRINTFN(2,("ess_config_irq\n"));
    443 
    444 	if (sc->sc_model == ESS_1887 &&
    445 	    sc->sc_audio1.irq == sc->sc_audio2.irq &&
    446 	    sc->sc_audio1.irq != -1) {
    447 		/* Use new method, both interrupts are the same. */
    448 		v = ESS_IS_SELECT_IRQ;	/* enable intrs */
    449 		switch (sc->sc_audio1.irq) {
    450 		case 5:
    451 			v |= ESS_IS_INTRB;
    452 			break;
    453 		case 7:
    454 			v |= ESS_IS_INTRC;
    455 			break;
    456 		case 9:
    457 			v |= ESS_IS_INTRA;
    458 			break;
    459 		case 10:
    460 			v |= ESS_IS_INTRD;
    461 			break;
    462 		case 15:
    463 			v |= ESS_IS_INTRE;
    464 			break;
    465 #ifdef DIAGNOSTIC
    466 		default:
    467 			printf("ess_config_irq: configured irq %d not supported for Audio 1\n",
    468 			       sc->sc_audio1.irq);
    469 			return;
    470 #endif
    471 		}
    472 		/* Set the IRQ */
    473 		ess_write_mix_reg(sc, ESS_MREG_INTR_ST, v);
    474 		return;
    475 	}
    476 
    477 	if (sc->sc_model == ESS_1887) {
    478 		/* Tell the 1887 to use the old interrupt method. */
    479 		ess_write_mix_reg(sc, ESS_MREG_INTR_ST, ESS_IS_ES1888);
    480 	}
    481 
    482 	if (sc->sc_audio1.polled) {
    483 		/* Turn off Audio1 interrupts. */
    484 		v = 0;
    485 	} else {
    486 		/* Configure Audio 1 for the appropriate IRQ line. */
    487 		v = ESS_IRQ_CTRL_MASK | ESS_IRQ_CTRL_EXT; /* All intrs on */
    488 		switch (sc->sc_audio1.irq) {
    489 		case 5:
    490 			v |= ESS_IRQ_CTRL_INTRB;
    491 			break;
    492 		case 7:
    493 			v |= ESS_IRQ_CTRL_INTRC;
    494 			break;
    495 		case 9:
    496 			v |= ESS_IRQ_CTRL_INTRA;
    497 			break;
    498 		case 10:
    499 			v |= ESS_IRQ_CTRL_INTRD;
    500 			break;
    501 #ifdef DIAGNOSTIC
    502 		default:
    503 			printf("ess: configured irq %d not supported for Audio 1\n",
    504 			       sc->sc_audio1.irq);
    505 			return;
    506 #endif
    507 		}
    508 	}
    509 	ess_write_x_reg(sc, ESS_XCMD_IRQ_CTRL, v);
    510 
    511 	if (sc->sc_model == ESS_1788)
    512 		return;
    513 
    514 	if (sc->sc_audio2.polled) {
    515 		/* Turn off Audio2 interrupts. */
    516 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
    517 				    ESS_AUDIO2_CTRL2_IRQ2_ENABLE);
    518 	} else {
    519 		/* Audio2 is hardwired to INTRE in this mode. */
    520 		ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
    521 				  ESS_AUDIO2_CTRL2_IRQ2_ENABLE);
    522 	}
    523 }
    524 
    525 
    526 void
    527 ess_config_drq(sc)
    528 	struct ess_softc *sc;
    529 {
    530 	int v;
    531 
    532 	DPRINTFN(2,("ess_config_drq\n"));
    533 
    534 	/* Configure Audio 1 (record) for DMA on the appropriate channel. */
    535 	v = ESS_DRQ_CTRL_PU | ESS_DRQ_CTRL_EXT;
    536 	switch (sc->sc_audio1.drq) {
    537 	case 0:
    538 		v |= ESS_DRQ_CTRL_DRQA;
    539 		break;
    540 	case 1:
    541 		v |= ESS_DRQ_CTRL_DRQB;
    542 		break;
    543 	case 3:
    544 		v |= ESS_DRQ_CTRL_DRQC;
    545 		break;
    546 #ifdef DIAGNOSTIC
    547 	default:
    548 		printf("ess_config_drq: configured dma chan %d not supported for Audio 1\n",
    549 		       sc->sc_audio1.drq);
    550 		return;
    551 #endif
    552 	}
    553 	/* Set DRQ1 */
    554 	ess_write_x_reg(sc, ESS_XCMD_DRQ_CTRL, v);
    555 
    556 	if (sc->sc_model == ESS_1788)
    557 		return;
    558 
    559 	/* Configure DRQ2 */
    560 	v = ESS_AUDIO2_CTRL3_DRQ_PD;
    561 	switch (sc->sc_audio2.drq) {
    562 	case 0:
    563 		v |= ESS_AUDIO2_CTRL3_DRQA;
    564 		break;
    565 	case 1:
    566 		v |= ESS_AUDIO2_CTRL3_DRQB;
    567 		break;
    568 	case 3:
    569 		v |= ESS_AUDIO2_CTRL3_DRQC;
    570 		break;
    571 	case 5:
    572 		v |= ESS_AUDIO2_CTRL3_DRQD;
    573 		break;
    574 #ifdef DIAGNOSTIC
    575 	default:
    576 		printf("ess_config_drq: configured dma chan %d not supported for Audio 2\n",
    577 		       sc->sc_audio2.drq);
    578 		return;
    579 #endif
    580 	}
    581 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL3, v);
    582 	/* Enable DMA 2 */
    583 	ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
    584 			  ESS_AUDIO2_CTRL2_DMA_ENABLE);
    585 }
    586 
    587 /*
    588  * Set up registers after a reset.
    589  */
    590 void
    591 ess_setup(sc)
    592 	struct ess_softc *sc;
    593 {
    594 
    595 	ess_config_irq(sc);
    596 	ess_config_drq(sc);
    597 
    598 	DPRINTFN(2,("ess_setup: done\n"));
    599 }
    600 
    601 /*
    602  * Determine the model of ESS chip we are talking to.  Currently we
    603  * only support ES1888, ES1887 and ES888.  The method of determining
    604  * the chip is based on the information on page 27 of the ES1887 data
    605  * sheet.
    606  *
    607  * This routine sets the values of sc->sc_model and sc->sc_version.
    608  */
    609 int
    610 ess_identify(sc)
    611 	struct ess_softc *sc;
    612 {
    613 	u_char reg1;
    614 	u_char reg2;
    615 	u_char reg3;
    616 
    617 	sc->sc_model = ESS_UNSUPPORTED;
    618 	sc->sc_version = 0;
    619 
    620 
    621 	/*
    622 	 * 1. Check legacy ID bytes.  These should be 0x68 0x8n, where
    623 	 *    n >= 8 for an ES1887 or an ES888.  Other values indicate
    624 	 *    earlier (unsupported) chips.
    625 	 */
    626 	ess_wdsp(sc, ESS_ACMD_LEGACY_ID);
    627 
    628 	if ((reg1 = ess_rdsp(sc)) != 0x68) {
    629 		printf("ess: First ID byte wrong (0x%02x)\n", reg1);
    630 		return 1;
    631 	}
    632 
    633 	reg2 = ess_rdsp(sc);
    634 	if (((reg2 & 0xf0) != 0x80) ||
    635 	    ((reg2 & 0x0f) < 8)) {
    636 		printf("ess: Second ID byte wrong (0x%02x)\n", reg2);
    637 		return 1;
    638 	}
    639 
    640 	/*
    641 	 * Store the ID bytes as the version.
    642 	 */
    643 	sc->sc_version = (reg1 << 8) + reg2;
    644 
    645 
    646 	/*
    647 	 * 2. Verify we can change bit 2 in mixer register 0x64.  This
    648 	 *    should be possible on all supported chips.
    649 	 */
    650 	reg1 = ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL);
    651 	reg2 = reg1 ^ 0x04;  /* toggle bit 2 */
    652 
    653 	ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg2);
    654 
    655 	if (ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL) != reg2) {
    656 		printf("ess: Hardware error (unable to toggle bit 2 of mixer register 0x64)\n");
    657 		return 1;
    658 	}
    659 
    660 	/*
    661 	 * Restore the original value of mixer register 0x64.
    662 	 */
    663 	ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg1);
    664 
    665 
    666 	/*
    667 	 * 3. Verify we can change the value of mixer register
    668 	 *    ESS_MREG_SAMPLE_RATE.
    669 	 *    This is possible on the 1888/1887/888, but not on the 1788.
    670 	 *    It is not necessary to restore the value of this mixer register.
    671 	 */
    672 	reg1 = ess_read_mix_reg(sc, ESS_MREG_SAMPLE_RATE);
    673 	reg2 = reg1 ^ 0xff;  /* toggle all bits */
    674 
    675 	ess_write_mix_reg(sc, ESS_MREG_SAMPLE_RATE, reg2);
    676 
    677 	if (ess_read_mix_reg(sc, ESS_MREG_SAMPLE_RATE) != reg2) {
    678 		/* If we got this far before failing, it's a 1788. */
    679 		sc->sc_model = ESS_1788;
    680 	} else {
    681 		/*
    682 		 * 4. Determine if we can change bit 5 in mixer register 0x64.
    683 		 *    This determines whether we have an ES1887:
    684 		 *
    685 		 *    - can change indicates ES1887
    686 		 *    - can't change indicates ES1888 or ES888
    687 		 */
    688 		reg1 = ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL);
    689 		reg2 = reg1 ^ 0x20;  /* toggle bit 5 */
    690 
    691 		ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg2);
    692 
    693 		if (ess_read_mix_reg(sc, ESS_MREG_VOLUME_CTRL) == reg2) {
    694 			sc->sc_model = ESS_1887;
    695 
    696 			/*
    697 			 * Restore the original value of mixer register 0x64.
    698 			 */
    699 			ess_write_mix_reg(sc, ESS_MREG_VOLUME_CTRL, reg1);
    700 		} else {
    701 			/*
    702 			 * 5. Determine if we can change the value of mixer
    703 			 *    register 0x69 independently of mixer register
    704 			 *    0x68. This determines which chip we have:
    705 			 *
    706 			 *    - can modify idependently indicates ES888
    707 			 *    - register 0x69 is an alias of 0x68 indicates ES1888
    708 			 */
    709 			reg1 = ess_read_mix_reg(sc, 0x68);
    710 			reg2 = ess_read_mix_reg(sc, 0x69);
    711 			reg3 = reg2 ^ 0xff;  /* toggle all bits */
    712 
    713 			/*
    714 			 * Write different values to each register.
    715 			 */
    716 			ess_write_mix_reg(sc, 0x68, reg2);
    717 			ess_write_mix_reg(sc, 0x69, reg3);
    718 
    719 			if (ess_read_mix_reg(sc, 0x68) == reg2 &&
    720 			    ess_read_mix_reg(sc, 0x69) == reg3)
    721 				sc->sc_model = ESS_888;
    722 			else
    723 				sc->sc_model = ESS_1888;
    724 
    725 			/*
    726 			 * Restore the original value of the registers.
    727 			 */
    728 			ess_write_mix_reg(sc, 0x68, reg1);
    729 			ess_write_mix_reg(sc, 0x69, reg2);
    730 		}
    731 	}
    732 
    733 	return 0;
    734 }
    735 
    736 
    737 int
    738 ess_setup_sc(sc, doinit)
    739 	struct ess_softc *sc;
    740 	int doinit;
    741 {
    742 	/* Reset the chip. */
    743 	if (ess_reset(sc) != 0) {
    744 		DPRINTF(("ess_setup_sc: couldn't reset chip\n"));
    745 		return (1);
    746 	}
    747 
    748 	/* Identify the ESS chip, and check that it is supported. */
    749 	if (ess_identify(sc)) {
    750 		DPRINTF(("ess_setup_sc: couldn't identify\n"));
    751 		return (1);
    752 	}
    753 
    754 	return (0);
    755 }
    756 
    757 /*
    758  * Probe for the ESS hardware.
    759  */
    760 int
    761 essmatch(sc)
    762 	struct ess_softc *sc;
    763 {
    764 	if (!ESS_BASE_VALID(sc->sc_iobase)) {
    765 		printf("ess: configured iobase 0x%x invalid\n", sc->sc_iobase);
    766 		return (0);
    767 	}
    768 
    769 	/* Configure the ESS chip for the desired audio base address. */
    770 	if (ess_config_addr(sc))
    771 		return (0);
    772 
    773 	if (ess_setup_sc(sc, 1))
    774 		return (0);
    775 
    776 	if (sc->sc_model == ESS_UNSUPPORTED) {
    777 		DPRINTF(("ess: Unsupported model\n"));
    778 		return (0);
    779 	}
    780 
    781 	/* Check that requested DMA channels are valid and different. */
    782 	if (!ESS_DRQ1_VALID(sc->sc_audio1.drq)) {
    783 		printf("ess: record drq %d invalid\n", sc->sc_audio1.drq);
    784 		return (0);
    785 	}
    786 	if (!isa_drq_isfree(sc->sc_ic, sc->sc_audio1.drq))
    787 		return (0);
    788 	if (sc->sc_model != ESS_1788) {
    789 		if (!ESS_DRQ2_VALID(sc->sc_audio2.drq, sc->sc_model)) {
    790 			printf("ess: play drq %d invalid\n", sc->sc_audio2.drq);
    791 			return (0);
    792 		}
    793 		if (sc->sc_audio1.drq == sc->sc_audio2.drq) {
    794 			printf("ess: play and record drq both %d\n",
    795 			       sc->sc_audio1.drq);
    796 			return (0);
    797 		}
    798 		if (!isa_drq_isfree(sc->sc_ic, sc->sc_audio2.drq))
    799 			return (0);
    800 	}
    801 
    802 	/*
    803 	 * The 1887 has an additional IRQ mode where both channels are mapped
    804 	 * to the same IRQ.
    805 	 */
    806 	if (sc->sc_model == ESS_1887 &&
    807 	    sc->sc_audio1.irq == sc->sc_audio2.irq &&
    808 	    sc->sc_audio1.irq != -1 &&
    809 	    ESS_IRQ12_VALID(sc->sc_audio1.irq))
    810 		goto irq_not1888;
    811 
    812 	/* Check that requested IRQ lines are valid and different. */
    813 	if (sc->sc_audio1.irq != -1 &&
    814 	    !ESS_IRQ1_VALID(sc->sc_audio1.irq)) {
    815 		printf("ess: record irq %d invalid\n", sc->sc_audio1.irq);
    816 		return (0);
    817 	}
    818 	if (sc->sc_model != ESS_1788) {
    819 		if (sc->sc_audio2.irq != -1 &&
    820 		    !ESS_IRQ2_VALID(sc->sc_audio2.irq)) {
    821 			printf("ess: play irq %d invalid\n", sc->sc_audio2.irq);
    822 			return (0);
    823 		}
    824 		if (sc->sc_audio1.irq == sc->sc_audio2.irq &&
    825 		    sc->sc_audio1.irq != -1) {
    826 			printf("ess: play and record irq both %d\n",
    827 			       sc->sc_audio1.irq);
    828 			return (0);
    829 		}
    830 	}
    831 
    832 irq_not1888:
    833 	/* XXX should we check IRQs as well? */
    834 
    835 	return (1);
    836 }
    837 
    838 
    839 /*
    840  * Attach hardware to driver, attach hardware driver to audio
    841  * pseudo-device driver.
    842  */
    843 void
    844 essattach(sc)
    845 	struct ess_softc *sc;
    846 {
    847 	struct audio_attach_args arg;
    848 	struct audio_params pparams, rparams;
    849 	int i;
    850 	u_int v;
    851 
    852 	if (ess_setup_sc(sc, 0)) {
    853 		printf(": setup failed\n");
    854 		return;
    855 	}
    856 
    857 	printf(": ESS Technology ES%s [version 0x%04x]\n",
    858 	       essmodel[sc->sc_model], sc->sc_version);
    859 
    860 	sc->sc_audio1.polled = sc->sc_audio1.irq == -1;
    861 	if (!sc->sc_audio1.polled) {
    862 		sc->sc_audio1.ih = isa_intr_establish(sc->sc_ic,
    863 		    sc->sc_audio1.irq, sc->sc_audio1.ist, IPL_AUDIO,
    864 		    ess_audio1_intr, sc);
    865 		printf("%s: audio1 interrupting at irq %d\n",
    866 		    sc->sc_dev.dv_xname, sc->sc_audio1.irq);
    867 	} else
    868 		printf("%s: audio1 polled\n", sc->sc_dev.dv_xname);
    869 	if (isa_dmamap_create(sc->sc_ic, sc->sc_audio1.drq,
    870 	    MAX_ISADMA, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    871 		printf("%s: can't create map for drq %d\n",
    872 		       sc->sc_dev.dv_xname, sc->sc_audio1.drq);
    873 		return;
    874 	}
    875 
    876 	if (sc->sc_model != ESS_1788) {
    877 		sc->sc_audio2.polled = sc->sc_audio2.irq == -1;
    878 		if (!sc->sc_audio2.polled) {
    879 			sc->sc_audio2.ih = isa_intr_establish(sc->sc_ic,
    880 			    sc->sc_audio2.irq, sc->sc_audio2.ist, IPL_AUDIO,
    881 			    ess_audio2_intr, sc);
    882 			printf("%s: audio2 interrupting at irq %d\n",
    883 			    sc->sc_dev.dv_xname, sc->sc_audio2.irq);
    884 		} else
    885 			printf("%s: audio2 polled\n", sc->sc_dev.dv_xname);
    886 		if (isa_dmamap_create(sc->sc_ic, sc->sc_audio2.drq,
    887 		    MAX_ISADMA, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    888 			printf("%s: can't create map for drq %d\n",
    889 			       sc->sc_dev.dv_xname, sc->sc_audio2.drq);
    890 			return;
    891 		}
    892 	}
    893 
    894 	/*
    895 	 * Set record and play parameters to default values defined in
    896 	 * generic audio driver.
    897 	 */
    898 	pparams = audio_default;
    899 	rparams = audio_default;
    900 	ess_set_params(sc, AUMODE_RECORD|AUMODE_PLAY, 0, &pparams, &rparams);
    901 
    902 	/* Do a hardware reset on the mixer. */
    903 	ess_write_mix_reg(sc, ESS_MIX_RESET, ESS_MIX_RESET);
    904 
    905 	/*
    906 	 * Set volume of Audio 1 to zero and disable Audio 1 DAC input
    907 	 * to playback mixer, since playback is always through Audio 2.
    908 	 */
    909 	if (sc->sc_model != ESS_1788)
    910 		ess_write_mix_reg(sc, ESS_MREG_VOLUME_VOICE, 0);
    911 	ess_wdsp(sc, ESS_ACMD_DISABLE_SPKR);
    912 
    913 	if (sc->sc_model == ESS_1788) {
    914 		ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ESS_SOURCE_MIC);
    915 		sc->in_port = ESS_SOURCE_MIC;
    916 		sc->ndevs = ESS_1788_NDEVS;
    917 	} else {
    918 		/*
    919 		 * Set hardware record source to use output of the record
    920 		 * mixer. We do the selection of record source in software by
    921 		 * setting the gain of the unused sources to zero. (See
    922 		 * ess_set_in_ports.)
    923 		 */
    924 		ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ESS_SOURCE_MIXER);
    925 		sc->in_mask = 1 << ESS_MIC_REC_VOL;
    926 		sc->ndevs = ESS_1888_NDEVS;
    927 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 0x10);
    928 		ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2, 0x08);
    929 	}
    930 
    931 	/*
    932 	 * Set gain on each mixer device to a sensible value.
    933 	 * Devices not normally used are turned off, and other devices
    934 	 * are set to 50% volume.
    935 	 */
    936 	for (i = 0; i < sc->ndevs; i++) {
    937 		switch (i) {
    938 		case ESS_MIC_PLAY_VOL:
    939 		case ESS_LINE_PLAY_VOL:
    940 		case ESS_CD_PLAY_VOL:
    941 		case ESS_AUXB_PLAY_VOL:
    942 		case ESS_DAC_REC_VOL:
    943 		case ESS_LINE_REC_VOL:
    944 		case ESS_SYNTH_REC_VOL:
    945 		case ESS_CD_REC_VOL:
    946 		case ESS_AUXB_REC_VOL:
    947 			v = 0;
    948 			break;
    949 		default:
    950 			v = ESS_4BIT_GAIN(AUDIO_MAX_GAIN / 2);
    951 			break;
    952 		}
    953 		sc->gain[i][ESS_LEFT] = sc->gain[i][ESS_RIGHT] = v;
    954 		ess_set_gain(sc, i, 1);
    955 	}
    956 
    957 	ess_setup(sc);
    958 
    959 	/* Disable the speaker until the device is opened.  */
    960 	ess_speaker_off(sc);
    961 	sc->spkr_state = SPKR_OFF;
    962 
    963 	sprintf(ess_device.name, "ES%s", essmodel[sc->sc_model]);
    964 	sprintf(ess_device.version, "0x%04x", sc->sc_version);
    965 
    966 	if (sc->sc_model == ESS_1788)
    967 		audio_attach_mi(&ess_1788_hw_if, sc, &sc->sc_dev);
    968 	else
    969 		audio_attach_mi(&ess_1888_hw_if, sc, &sc->sc_dev);
    970 
    971 	arg.type = AUDIODEV_TYPE_OPL;
    972 	arg.hwif = 0;
    973 	arg.hdl = 0;
    974 	(void)config_found(&sc->sc_dev, &arg, audioprint);
    975 
    976 #ifdef AUDIO_DEBUG
    977 	if (essdebug > 0)
    978 		ess_printsc(sc);
    979 #endif
    980 }
    981 
    982 /*
    983  * Various routines to interface to higher level audio driver
    984  */
    985 
    986 int
    987 ess_open(addr, flags)
    988 	void *addr;
    989 	int flags;
    990 {
    991 	struct ess_softc *sc = addr;
    992 
    993 	DPRINTF(("ess_open: sc=%p\n", sc));
    994 
    995 	if (sc->sc_open != 0 || ess_reset(sc) != 0)
    996 		return ENXIO;
    997 
    998 	ess_setup(sc);		/* because we did a reset */
    999 
   1000 	sc->sc_open = 1;
   1001 
   1002 	DPRINTF(("ess_open: opened\n"));
   1003 
   1004 	return (0);
   1005 }
   1006 
   1007 void
   1008 ess_1788_close(addr)
   1009 	void *addr;
   1010 {
   1011 	struct ess_softc *sc = addr;
   1012 
   1013 	DPRINTF(("ess_1788_close: sc=%p\n", sc));
   1014 
   1015 	ess_speaker_off(sc);
   1016 	sc->spkr_state = SPKR_OFF;
   1017 
   1018 	ess_audio1_halt(sc);
   1019 
   1020 	sc->sc_open = 0;
   1021 	DPRINTF(("ess_1788_close: closed\n"));
   1022 }
   1023 
   1024 void
   1025 ess_1888_close(addr)
   1026 	void *addr;
   1027 {
   1028 	struct ess_softc *sc = addr;
   1029 
   1030 	DPRINTF(("ess_1888_close: sc=%p\n", sc));
   1031 
   1032 	ess_speaker_off(sc);
   1033 	sc->spkr_state = SPKR_OFF;
   1034 
   1035 	ess_audio1_halt(sc);
   1036 	ess_audio2_halt(sc);
   1037 
   1038 	sc->sc_open = 0;
   1039 	DPRINTF(("ess_1888_close: closed\n"));
   1040 }
   1041 
   1042 /*
   1043  * Wait for FIFO to drain, and analog section to settle.
   1044  * XXX should check FIFO empty bit.
   1045  */
   1046 int
   1047 ess_drain(addr)
   1048 	void *addr;
   1049 {
   1050 	tsleep(addr, PWAIT | PCATCH, "essdr", hz/20); /* XXX */
   1051 	return (0);
   1052 }
   1053 
   1054 /* XXX should use reference count */
   1055 int
   1056 ess_speaker_ctl(addr, newstate)
   1057 	void *addr;
   1058 	int newstate;
   1059 {
   1060 	struct ess_softc *sc = addr;
   1061 
   1062 	if ((newstate == SPKR_ON) && (sc->spkr_state == SPKR_OFF)) {
   1063 		ess_speaker_on(sc);
   1064 		sc->spkr_state = SPKR_ON;
   1065 	}
   1066 	if ((newstate == SPKR_OFF) && (sc->spkr_state == SPKR_ON)) {
   1067 		ess_speaker_off(sc);
   1068 		sc->spkr_state = SPKR_OFF;
   1069 	}
   1070 	return (0);
   1071 }
   1072 
   1073 int
   1074 ess_getdev(addr, retp)
   1075 	void *addr;
   1076 	struct audio_device *retp;
   1077 {
   1078 	*retp = ess_device;
   1079 	return (0);
   1080 }
   1081 
   1082 int
   1083 ess_query_encoding(addr, fp)
   1084 	void *addr;
   1085 	struct audio_encoding *fp;
   1086 {
   1087 	/*struct ess_softc *sc = addr;*/
   1088 
   1089 	switch (fp->index) {
   1090 	case 0:
   1091 		strcpy(fp->name, AudioEulinear);
   1092 		fp->encoding = AUDIO_ENCODING_ULINEAR;
   1093 		fp->precision = 8;
   1094 		fp->flags = 0;
   1095 		return (0);
   1096 	case 1:
   1097 		strcpy(fp->name, AudioEmulaw);
   1098 		fp->encoding = AUDIO_ENCODING_ULAW;
   1099 		fp->precision = 8;
   1100 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1101 		return (0);
   1102 	case 2:
   1103 		strcpy(fp->name, AudioEalaw);
   1104 		fp->encoding = AUDIO_ENCODING_ALAW;
   1105 		fp->precision = 8;
   1106 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1107 		return (0);
   1108 	case 3:
   1109 		strcpy(fp->name, AudioEslinear);
   1110 		fp->encoding = AUDIO_ENCODING_SLINEAR;
   1111 		fp->precision = 8;
   1112 		fp->flags = 0;
   1113 		return (0);
   1114 	case 4:
   1115 		strcpy(fp->name, AudioEslinear_le);
   1116 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
   1117 		fp->precision = 16;
   1118 		fp->flags = 0;
   1119 		return (0);
   1120 	case 5:
   1121 		strcpy(fp->name, AudioEulinear_le);
   1122 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
   1123 		fp->precision = 16;
   1124 		fp->flags = 0;
   1125 		return (0);
   1126 	case 6:
   1127 		strcpy(fp->name, AudioEslinear_be);
   1128 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
   1129 		fp->precision = 16;
   1130 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1131 		return (0);
   1132 	case 7:
   1133 		strcpy(fp->name, AudioEulinear_be);
   1134 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
   1135 		fp->precision = 16;
   1136 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1137 		return (0);
   1138 	default:
   1139 		return EINVAL;
   1140 	}
   1141 	return (0);
   1142 }
   1143 
   1144 int
   1145 ess_set_params(addr, setmode, usemode, play, rec)
   1146 	void *addr;
   1147 	int setmode, usemode;
   1148 	struct audio_params *play, *rec;
   1149 {
   1150 	struct ess_softc *sc = addr;
   1151 	struct audio_params *p;
   1152 	int mode;
   1153 	int rate;
   1154 
   1155 	DPRINTF(("ess_set_params: set=%d use=%d\n", setmode, usemode));
   1156 
   1157 	/*
   1158 	 * The ES1887 manual (page 39, `Full-Duplex DMA Mode') claims that in
   1159 	 * full-duplex operation the sample rates must be the same for both
   1160 	 * channels.  This appears to be false; the only bit in common is the
   1161 	 * clock source selection.  However, we'll be conservative here.
   1162 	 * - mycroft
   1163 	 */
   1164 	if (play->sample_rate != rec->sample_rate &&
   1165 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1166 		if (setmode == AUMODE_PLAY) {
   1167 			rec->sample_rate = play->sample_rate;
   1168 			setmode |= AUMODE_RECORD;
   1169 		} else if (setmode == AUMODE_RECORD) {
   1170 			play->sample_rate = rec->sample_rate;
   1171 			setmode |= AUMODE_PLAY;
   1172 		} else
   1173 			return (EINVAL);
   1174 	}
   1175 
   1176 	for (mode = AUMODE_RECORD; mode != -1;
   1177 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1178 		if ((setmode & mode) == 0)
   1179 			continue;
   1180 
   1181 		p = mode == AUMODE_PLAY ? play : rec;
   1182 
   1183 		if (p->sample_rate < ESS_MINRATE ||
   1184 		    p->sample_rate > ESS_MAXRATE ||
   1185 		    (p->precision != 8 && p->precision != 16) ||
   1186 		    (p->channels != 1 && p->channels != 2))
   1187 			return (EINVAL);
   1188 
   1189 		p->factor = 1;
   1190 		p->sw_code = 0;
   1191 		switch (p->encoding) {
   1192 		case AUDIO_ENCODING_SLINEAR_BE:
   1193 		case AUDIO_ENCODING_ULINEAR_BE:
   1194 			if (p->precision == 16)
   1195 				p->sw_code = swap_bytes;
   1196 			break;
   1197 		case AUDIO_ENCODING_SLINEAR_LE:
   1198 		case AUDIO_ENCODING_ULINEAR_LE:
   1199 			break;
   1200 		case AUDIO_ENCODING_ULAW:
   1201 			if (mode == AUMODE_PLAY) {
   1202 				p->factor = 2;
   1203 				p->sw_code = mulaw_to_ulinear16;
   1204 			} else
   1205 				p->sw_code = ulinear8_to_mulaw;
   1206 			break;
   1207 		case AUDIO_ENCODING_ALAW:
   1208 			if (mode == AUMODE_PLAY) {
   1209 				p->factor = 2;
   1210 				p->sw_code = alaw_to_ulinear16;
   1211 			} else
   1212 				p->sw_code = ulinear8_to_alaw;
   1213 			break;
   1214 		default:
   1215 			return (EINVAL);
   1216 		}
   1217 	}
   1218 
   1219 	if (usemode == AUMODE_RECORD)
   1220 		rate = rec->sample_rate;
   1221 	else
   1222 		rate = play->sample_rate;
   1223 
   1224 	ess_write_x_reg(sc, ESS_XCMD_SAMPLE_RATE, ess_srtotc(rate));
   1225 	ess_write_x_reg(sc, ESS_XCMD_FILTER_CLOCK, ess_srtofc(rate));
   1226 
   1227 	if (sc->sc_model != ESS_1788) {
   1228 		ess_write_mix_reg(sc, ESS_MREG_SAMPLE_RATE, ess_srtotc(rate));
   1229 		ess_write_mix_reg(sc, ESS_MREG_FILTER_CLOCK, ess_srtofc(rate));
   1230 	}
   1231 
   1232 	return (0);
   1233 }
   1234 
   1235 int
   1236 ess_audio1_trigger_output(addr, start, end, blksize, intr, arg, param)
   1237 	void *addr;
   1238 	void *start, *end;
   1239 	int blksize;
   1240 	void (*intr) __P((void *));
   1241 	void *arg;
   1242 	struct audio_params *param;
   1243 {
   1244 	struct ess_softc *sc = addr;
   1245 	u_int8_t reg;
   1246 
   1247 	DPRINTFN(1, ("ess_audio1_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1248 	    addr, start, end, blksize, intr, arg));
   1249 
   1250 	if (sc->sc_audio1.active)
   1251 		panic("ess_audio1_trigger_output: already running");
   1252 
   1253 	sc->sc_audio1.active = 1;
   1254 	sc->sc_audio1.intr = intr;
   1255 	sc->sc_audio1.arg = arg;
   1256 	if (sc->sc_audio1.polled) {
   1257 		sc->sc_audio1.dmacount = 0;
   1258 		sc->sc_audio1.dmapos = sc->sc_audio1.buffersize =
   1259 		    (char *)end - (char *)start;
   1260 		sc->sc_audio1.blksize = blksize;
   1261 		timeout(ess_audio1_poll, sc, hz/30);
   1262 	}
   1263 
   1264 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL);
   1265 	if (param->channels == 2) {
   1266 		reg &= ~ESS_AUDIO_CTRL_MONO;
   1267 		reg |= ESS_AUDIO_CTRL_STEREO;
   1268 	} else {
   1269 		reg |= ESS_AUDIO_CTRL_MONO;
   1270 		reg &= ~ESS_AUDIO_CTRL_STEREO;
   1271 	}
   1272 	ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL, reg);
   1273 
   1274 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1);
   1275 	if (param->precision * param->factor == 16)
   1276 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIZE;
   1277 	else
   1278 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIZE;
   1279 	if (param->channels == 2)
   1280 		reg |= ESS_AUDIO1_CTRL1_FIFO_STEREO;
   1281 	else
   1282 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_STEREO;
   1283 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
   1284 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
   1285 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIGNED;
   1286 	else
   1287 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIGNED;
   1288 	reg |= ESS_AUDIO1_CTRL1_FIFO_CONNECT;
   1289 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1, reg);
   1290 
   1291 	isa_dmastart(sc->sc_ic, sc->sc_audio1.drq, start,
   1292 		     (char *)end - (char *)start, NULL,
   1293 	    DMAMODE_WRITE | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT);
   1294 
   1295 	/* Program transfer count registers with 2's complement of count. */
   1296 	blksize = -blksize;
   1297 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize);
   1298 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8);
   1299 
   1300 	/* Use 4 bytes per output DMA. */
   1301 	ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL, ESS_DEMAND_CTRL_DEMAND_4);
   1302 
   1303 	/* Start auto-init DMA */
   1304   	ess_wdsp(sc, ESS_ACMD_ENABLE_SPKR);
   1305 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2);
   1306 	reg &= ~(ESS_AUDIO1_CTRL2_DMA_READ | ESS_AUDIO1_CTRL2_ADC_ENABLE);
   1307 	reg |= ESS_AUDIO1_CTRL2_FIFO_ENABLE | ESS_AUDIO1_CTRL2_AUTO_INIT;
   1308 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2, reg);
   1309 
   1310 	return (0);
   1311 }
   1312 
   1313 int
   1314 ess_audio2_trigger_output(addr, start, end, blksize, intr, arg, param)
   1315 	void *addr;
   1316 	void *start, *end;
   1317 	int blksize;
   1318 	void (*intr) __P((void *));
   1319 	void *arg;
   1320 	struct audio_params *param;
   1321 {
   1322 	struct ess_softc *sc = addr;
   1323 	u_int8_t reg;
   1324 
   1325 	DPRINTFN(1, ("ess_audio2_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1326 	    addr, start, end, blksize, intr, arg));
   1327 
   1328 	if (sc->sc_audio2.active)
   1329 		panic("ess_audio2_trigger_output: already running");
   1330 
   1331 	sc->sc_audio2.active = 1;
   1332 	sc->sc_audio2.intr = intr;
   1333 	sc->sc_audio2.arg = arg;
   1334 	if (sc->sc_audio2.polled) {
   1335 		sc->sc_audio2.dmacount = 0;
   1336 		sc->sc_audio2.dmapos = sc->sc_audio2.buffersize =
   1337 		    (char *)end - (char *)start;
   1338 		sc->sc_audio2.blksize = blksize;
   1339 		timeout(ess_audio2_poll, sc, hz/30);
   1340 	}
   1341 
   1342 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2);
   1343 	if (param->precision * param->factor == 16)
   1344 		reg |= ESS_AUDIO2_CTRL2_FIFO_SIZE;
   1345 	else
   1346 		reg &= ~ESS_AUDIO2_CTRL2_FIFO_SIZE;
   1347 	if (param->channels == 2)
   1348 		reg |= ESS_AUDIO2_CTRL2_CHANNELS;
   1349 	else
   1350 		reg &= ~ESS_AUDIO2_CTRL2_CHANNELS;
   1351 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
   1352 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
   1353 		reg |= ESS_AUDIO2_CTRL2_FIFO_SIGNED;
   1354 	else
   1355 		reg &= ~ESS_AUDIO2_CTRL2_FIFO_SIGNED;
   1356 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2, reg);
   1357 
   1358 	isa_dmastart(sc->sc_ic, sc->sc_audio2.drq, start,
   1359 		     (char *)end - (char *)start, NULL,
   1360 	    DMAMODE_WRITE | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT);
   1361 
   1362 	if (IS16BITDRQ(sc->sc_audio2.drq))
   1363 		blksize >>= 1;	/* use word count for 16 bit DMA */
   1364 	/* Program transfer count registers with 2's complement of count. */
   1365 	blksize = -blksize;
   1366 	ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTLO, blksize);
   1367 	ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTHI, blksize >> 8);
   1368 
   1369 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL1);
   1370 	if (IS16BITDRQ(sc->sc_audio2.drq))
   1371 		reg |= ESS_AUDIO2_CTRL1_XFER_SIZE;
   1372 	else
   1373 		reg &= ~ESS_AUDIO2_CTRL1_XFER_SIZE;
   1374 	reg |= ESS_AUDIO2_CTRL1_DEMAND_8;
   1375 	reg |= ESS_AUDIO2_CTRL1_DAC_ENABLE | ESS_AUDIO2_CTRL1_FIFO_ENABLE |
   1376 	       ESS_AUDIO2_CTRL1_AUTO_INIT;
   1377 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL1, reg);
   1378 
   1379 	return (0);
   1380 }
   1381 
   1382 int
   1383 ess_audio1_trigger_input(addr, start, end, blksize, intr, arg, param)
   1384 	void *addr;
   1385 	void *start, *end;
   1386 	int blksize;
   1387 	void (*intr) __P((void *));
   1388 	void *arg;
   1389 	struct audio_params *param;
   1390 {
   1391 	struct ess_softc *sc = addr;
   1392 	u_int8_t reg;
   1393 
   1394 	DPRINTFN(1, ("ess_audio1_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1395 	    addr, start, end, blksize, intr, arg));
   1396 
   1397 	if (sc->sc_audio1.active)
   1398 		panic("ess_audio1_trigger_input: already running");
   1399 
   1400 	sc->sc_audio1.active = 1;
   1401 	sc->sc_audio1.intr = intr;
   1402 	sc->sc_audio1.arg = arg;
   1403 	if (sc->sc_audio1.polled) {
   1404 		sc->sc_audio1.dmacount = 0;
   1405 		sc->sc_audio1.dmapos = sc->sc_audio1.buffersize =
   1406 		    (char *)end - (char *)start;
   1407 		sc->sc_audio1.blksize = blksize;
   1408 		timeout(ess_audio1_poll, sc, hz/30);
   1409 	}
   1410 
   1411 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL);
   1412 	if (param->channels == 2) {
   1413 		reg &= ~ESS_AUDIO_CTRL_MONO;
   1414 		reg |= ESS_AUDIO_CTRL_STEREO;
   1415 	} else {
   1416 		reg |= ESS_AUDIO_CTRL_MONO;
   1417 		reg &= ~ESS_AUDIO_CTRL_STEREO;
   1418 	}
   1419 	ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL, reg);
   1420 
   1421 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1);
   1422 	if (param->precision * param->factor == 16)
   1423 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIZE;
   1424 	else
   1425 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIZE;
   1426 	if (param->channels == 2)
   1427 		reg |= ESS_AUDIO1_CTRL1_FIFO_STEREO;
   1428 	else
   1429 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_STEREO;
   1430 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
   1431 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
   1432 		reg |= ESS_AUDIO1_CTRL1_FIFO_SIGNED;
   1433 	else
   1434 		reg &= ~ESS_AUDIO1_CTRL1_FIFO_SIGNED;
   1435 	reg |= ESS_AUDIO1_CTRL1_FIFO_CONNECT;
   1436 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL1, reg);
   1437 
   1438 	isa_dmastart(sc->sc_ic, sc->sc_audio1.drq, start,
   1439 		     (char *)end - (char *)start, NULL,
   1440 	    DMAMODE_READ | DMAMODE_LOOPDEMAND, BUS_DMA_NOWAIT);
   1441 
   1442 	/* Program transfer count registers with 2's complement of count. */
   1443 	blksize = -blksize;
   1444 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize);
   1445 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8);
   1446 
   1447 	/* Use 4 bytes per input DMA. */
   1448 	ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL, ESS_DEMAND_CTRL_DEMAND_4);
   1449 
   1450 	/* Start auto-init DMA */
   1451   	ess_wdsp(sc, ESS_ACMD_DISABLE_SPKR);
   1452 	reg = ess_read_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2);
   1453 	reg |= ESS_AUDIO1_CTRL2_DMA_READ | ESS_AUDIO1_CTRL2_ADC_ENABLE;
   1454 	reg |= ESS_AUDIO1_CTRL2_FIFO_ENABLE | ESS_AUDIO1_CTRL2_AUTO_INIT;
   1455 	ess_write_x_reg(sc, ESS_XCMD_AUDIO1_CTRL2, reg);
   1456 
   1457 	return (0);
   1458 }
   1459 
   1460 int
   1461 ess_audio1_halt(addr)
   1462 	void *addr;
   1463 {
   1464 	struct ess_softc *sc = addr;
   1465 
   1466 	DPRINTF(("ess_audio1_halt: sc=%p\n", sc));
   1467 
   1468 	if (sc->sc_audio1.active) {
   1469 		ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL2,
   1470 		    ESS_AUDIO1_CTRL2_FIFO_ENABLE);
   1471 		isa_dmaabort(sc->sc_ic, sc->sc_audio1.drq);
   1472 		if (sc->sc_audio1.polled)
   1473 			untimeout(ess_audio1_poll, sc);
   1474 		sc->sc_audio1.active = 0;
   1475 	}
   1476 
   1477 	return (0);
   1478 }
   1479 
   1480 int
   1481 ess_audio2_halt(addr)
   1482 	void *addr;
   1483 {
   1484 	struct ess_softc *sc = addr;
   1485 
   1486 	DPRINTF(("ess_audio2_halt: sc=%p\n", sc));
   1487 
   1488 	if (sc->sc_audio2.active) {
   1489 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1,
   1490 		    ESS_AUDIO2_CTRL1_DAC_ENABLE |
   1491 		    ESS_AUDIO2_CTRL1_FIFO_ENABLE);
   1492 		isa_dmaabort(sc->sc_ic, sc->sc_audio2.drq);
   1493 		if (sc->sc_audio2.polled)
   1494 			untimeout(ess_audio2_poll, sc);
   1495 		sc->sc_audio2.active = 0;
   1496 	}
   1497 
   1498 	return (0);
   1499 }
   1500 
   1501 int
   1502 ess_audio1_intr(arg)
   1503 	void *arg;
   1504 {
   1505 	struct ess_softc *sc = arg;
   1506 	u_int8_t reg;
   1507 
   1508 	DPRINTFN(1,("ess_audio1_intr: intr=%p\n", sc->sc_audio1.intr));
   1509 
   1510 	/* Check and clear interrupt on Audio1. */
   1511 	reg = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_DSP_RW_STATUS);
   1512 	if ((reg & ESS_DSP_READ_OFLOW) == 0)
   1513 		return (0);
   1514 	reg = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_CLEAR_INTR);
   1515 
   1516 	sc->sc_audio1.nintr++;
   1517 
   1518 	if (sc->sc_audio1.active) {
   1519 		(*sc->sc_audio1.intr)(sc->sc_audio1.arg);
   1520 		return (1);
   1521 	} else
   1522 		return (0);
   1523 }
   1524 
   1525 int
   1526 ess_audio2_intr(arg)
   1527 	void *arg;
   1528 {
   1529 	struct ess_softc *sc = arg;
   1530 	u_int8_t reg;
   1531 
   1532 	DPRINTFN(1,("ess_audio2_intr: intr=%p\n", sc->sc_audio2.intr));
   1533 
   1534 	/* Check and clear interrupt on Audio2. */
   1535 	reg = ess_read_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2);
   1536 	if ((reg & ESS_AUDIO2_CTRL2_IRQ_LATCH) == 0)
   1537 		return (0);
   1538 	reg &= ~ESS_AUDIO2_CTRL2_IRQ_LATCH;
   1539 	ess_write_mix_reg(sc, ESS_MREG_AUDIO2_CTRL2, reg);
   1540 
   1541 	sc->sc_audio2.nintr++;
   1542 
   1543 	if (sc->sc_audio2.active) {
   1544 		(*sc->sc_audio2.intr)(sc->sc_audio2.arg);
   1545 		return (1);
   1546 	} else
   1547 		return (0);
   1548 }
   1549 
   1550 void
   1551 ess_audio1_poll(addr)
   1552 	void *addr;
   1553 {
   1554 	struct ess_softc *sc = addr;
   1555 	int dmapos, dmacount;
   1556 
   1557 	if (!sc->sc_audio1.active)
   1558 		return;
   1559 
   1560 	sc->sc_audio1.nintr++;
   1561 
   1562 	dmapos = isa_dmacount(sc->sc_ic, sc->sc_audio1.drq);
   1563 	dmacount = sc->sc_audio1.dmacount + sc->sc_audio1.dmapos - dmapos;
   1564 	if (dmapos > sc->sc_audio1.dmapos)
   1565 		dmacount += sc->sc_audio1.buffersize;
   1566 	while (dmacount > sc->sc_audio1.blksize) {
   1567 		dmacount -= sc->sc_audio1.blksize;
   1568 		(*sc->sc_audio1.intr)(sc->sc_audio1.arg);
   1569 	}
   1570 	sc->sc_audio1.dmapos = dmapos;
   1571 	sc->sc_audio1.dmacount = dmacount;
   1572 
   1573 	timeout(ess_audio1_poll, sc, hz/30);
   1574 }
   1575 
   1576 void
   1577 ess_audio2_poll(addr)
   1578 	void *addr;
   1579 {
   1580 	struct ess_softc *sc = addr;
   1581 	int dmapos, dmacount;
   1582 
   1583 	if (!sc->sc_audio2.active)
   1584 		return;
   1585 
   1586 	sc->sc_audio2.nintr++;
   1587 
   1588 	dmapos = isa_dmacount(sc->sc_ic, sc->sc_audio2.drq);
   1589 	dmacount = sc->sc_audio2.dmacount + sc->sc_audio2.dmapos - dmapos;
   1590 	if (dmapos > sc->sc_audio2.dmapos)
   1591 		dmacount += sc->sc_audio2.buffersize;
   1592 	while (dmacount > sc->sc_audio2.blksize) {
   1593 		dmacount -= sc->sc_audio2.blksize;
   1594 		(*sc->sc_audio2.intr)(sc->sc_audio2.arg);
   1595 	}
   1596 	sc->sc_audio2.dmapos = dmapos;
   1597 	sc->sc_audio2.dmacount = dmacount;
   1598 
   1599 	timeout(ess_audio2_poll, sc, hz/30);
   1600 }
   1601 
   1602 int
   1603 ess_round_blocksize(addr, blk)
   1604 	void *addr;
   1605 	int blk;
   1606 {
   1607 	return (blk & -8);	/* round for max DMA size */
   1608 }
   1609 
   1610 int
   1611 ess_set_port(addr, cp)
   1612 	void *addr;
   1613 	mixer_ctrl_t *cp;
   1614 {
   1615 	struct ess_softc *sc = addr;
   1616 	int lgain, rgain;
   1617 
   1618 	DPRINTFN(5,("ess_set_port: port=%d num_channels=%d\n",
   1619 		    cp->dev, cp->un.value.num_channels));
   1620 
   1621 	switch (cp->dev) {
   1622 	/*
   1623 	 * The following mixer ports are all stereo. If we get a
   1624 	 * single-channel gain value passed in, then we duplicate it
   1625 	 * to both left and right channels.
   1626 	 */
   1627 	case ESS_MASTER_VOL:
   1628 	case ESS_DAC_PLAY_VOL:
   1629 	case ESS_MIC_PLAY_VOL:
   1630 	case ESS_LINE_PLAY_VOL:
   1631 	case ESS_SYNTH_PLAY_VOL:
   1632 	case ESS_CD_PLAY_VOL:
   1633 	case ESS_AUXB_PLAY_VOL:
   1634 	case ESS_RECORD_VOL:
   1635 		if (cp->type != AUDIO_MIXER_VALUE)
   1636 			return EINVAL;
   1637 
   1638 		switch (cp->un.value.num_channels) {
   1639 		case 1:
   1640 			lgain = rgain = ESS_4BIT_GAIN(
   1641 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1642 			break;
   1643 		case 2:
   1644 			lgain = ESS_4BIT_GAIN(
   1645 			  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
   1646 			rgain = ESS_4BIT_GAIN(
   1647 			  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
   1648 			break;
   1649 		default:
   1650 			return EINVAL;
   1651 		}
   1652 
   1653 		sc->gain[cp->dev][ESS_LEFT]  = lgain;
   1654 		sc->gain[cp->dev][ESS_RIGHT] = rgain;
   1655 		ess_set_gain(sc, cp->dev, 1);
   1656 		return (0);
   1657 
   1658 	/*
   1659 	 * The PC speaker port is mono. If we get a stereo gain value
   1660 	 * passed in, then we return EINVAL.
   1661 	 */
   1662 	case ESS_PCSPEAKER_VOL:
   1663 		if (cp->un.value.num_channels != 1)
   1664 			return EINVAL;
   1665 
   1666 		sc->gain[cp->dev][ESS_LEFT] = sc->gain[cp->dev][ESS_RIGHT] =
   1667 		  ESS_3BIT_GAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1668 		ess_set_gain(sc, cp->dev, 1);
   1669 		return (0);
   1670 
   1671 	case ESS_RECORD_SOURCE:
   1672 		if (sc->sc_model == ESS_1788) {
   1673 			if (cp->type == AUDIO_MIXER_ENUM)
   1674 				return (ess_set_in_port(sc, cp->un.ord));
   1675 			else
   1676 				return (EINVAL);
   1677 		} else {
   1678 			if (cp->type == AUDIO_MIXER_SET)
   1679 				return (ess_set_in_ports(sc, cp->un.mask));
   1680 			else
   1681 				return (EINVAL);
   1682 		}
   1683 		return (0);
   1684 
   1685 	case ESS_RECORD_MONITOR:
   1686 		if (cp->type != AUDIO_MIXER_ENUM)
   1687 			return EINVAL;
   1688 
   1689 		if (cp->un.ord)
   1690 			/* Enable monitor */
   1691 			ess_set_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL,
   1692 					  ESS_AUDIO_CTRL_MONITOR);
   1693 		else
   1694 			/* Disable monitor */
   1695 			ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL,
   1696 					    ESS_AUDIO_CTRL_MONITOR);
   1697 		return (0);
   1698 	}
   1699 
   1700 	if (sc->sc_model == ESS_1788)
   1701 		return (EINVAL);
   1702 
   1703 	switch (cp->dev) {
   1704 	case ESS_DAC_REC_VOL:
   1705 	case ESS_MIC_REC_VOL:
   1706 	case ESS_LINE_REC_VOL:
   1707 	case ESS_SYNTH_REC_VOL:
   1708 	case ESS_CD_REC_VOL:
   1709 	case ESS_AUXB_REC_VOL:
   1710 		if (cp->type != AUDIO_MIXER_VALUE)
   1711 			return EINVAL;
   1712 
   1713 		switch (cp->un.value.num_channels) {
   1714 		case 1:
   1715 			lgain = rgain = ESS_4BIT_GAIN(
   1716 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1717 			break;
   1718 		case 2:
   1719 			lgain = ESS_4BIT_GAIN(
   1720 			  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
   1721 			rgain = ESS_4BIT_GAIN(
   1722 			  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
   1723 			break;
   1724 		default:
   1725 			return EINVAL;
   1726 		}
   1727 
   1728 		sc->gain[cp->dev][ESS_LEFT]  = lgain;
   1729 		sc->gain[cp->dev][ESS_RIGHT] = rgain;
   1730 		ess_set_gain(sc, cp->dev, 1);
   1731 		return (0);
   1732 
   1733 	case ESS_MIC_PREAMP:
   1734 		if (cp->type != AUDIO_MIXER_ENUM)
   1735 			return EINVAL;
   1736 
   1737 		if (cp->un.ord)
   1738 			/* Enable microphone preamp */
   1739 			ess_set_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL,
   1740 					  ESS_PREAMP_CTRL_ENABLE);
   1741 		else
   1742 			/* Disable microphone preamp */
   1743 			ess_clear_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL,
   1744 					  ESS_PREAMP_CTRL_ENABLE);
   1745 		return (0);
   1746 	}
   1747 
   1748 	return (EINVAL);
   1749 }
   1750 
   1751 int
   1752 ess_get_port(addr, cp)
   1753 	void *addr;
   1754 	mixer_ctrl_t *cp;
   1755 {
   1756 	struct ess_softc *sc = addr;
   1757 
   1758 	DPRINTFN(5,("ess_get_port: port=%d\n", cp->dev));
   1759 
   1760 	switch (cp->dev) {
   1761 	case ESS_MASTER_VOL:
   1762 	case ESS_DAC_PLAY_VOL:
   1763 	case ESS_MIC_PLAY_VOL:
   1764 	case ESS_LINE_PLAY_VOL:
   1765 	case ESS_SYNTH_PLAY_VOL:
   1766 	case ESS_CD_PLAY_VOL:
   1767 	case ESS_AUXB_PLAY_VOL:
   1768 	case ESS_RECORD_VOL:
   1769 		switch (cp->un.value.num_channels) {
   1770 		case 1:
   1771 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1772 				sc->gain[cp->dev][ESS_LEFT];
   1773 			break;
   1774 		case 2:
   1775 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
   1776 				sc->gain[cp->dev][ESS_LEFT];
   1777 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
   1778 				sc->gain[cp->dev][ESS_RIGHT];
   1779 			break;
   1780 		default:
   1781 			return EINVAL;
   1782 		}
   1783 		return (0);
   1784 
   1785 	case ESS_PCSPEAKER_VOL:
   1786 		if (cp->un.value.num_channels != 1)
   1787 			return EINVAL;
   1788 
   1789 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1790 			sc->gain[cp->dev][ESS_LEFT];
   1791 		return (0);
   1792 
   1793 	case ESS_RECORD_SOURCE:
   1794 		if (sc->sc_model == ESS_1788)
   1795 			cp->un.ord = sc->in_port;
   1796 		else
   1797 			cp->un.mask = sc->in_mask;
   1798 		return (0);
   1799 
   1800 	case ESS_RECORD_MONITOR:
   1801 		cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL) &
   1802 			      ESS_AUDIO_CTRL_MONITOR) ? 1 : 0;
   1803 		return (0);
   1804 	}
   1805 
   1806 	if (sc->sc_model == ESS_1788)
   1807 		return (EINVAL);
   1808 
   1809 	switch (cp->dev) {
   1810 	case ESS_DAC_REC_VOL:
   1811 	case ESS_MIC_REC_VOL:
   1812 	case ESS_LINE_REC_VOL:
   1813 	case ESS_SYNTH_REC_VOL:
   1814 	case ESS_CD_REC_VOL:
   1815 	case ESS_AUXB_REC_VOL:
   1816 		switch (cp->un.value.num_channels) {
   1817 		case 1:
   1818 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1819 				sc->gain[cp->dev][ESS_LEFT];
   1820 			break;
   1821 		case 2:
   1822 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
   1823 				sc->gain[cp->dev][ESS_LEFT];
   1824 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
   1825 				sc->gain[cp->dev][ESS_RIGHT];
   1826 			break;
   1827 		default:
   1828 			return EINVAL;
   1829 		}
   1830 		return (0);
   1831 
   1832 	case ESS_MIC_PREAMP:
   1833 		cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_PREAMP_CTRL) &
   1834 			      ESS_PREAMP_CTRL_ENABLE) ? 1 : 0;
   1835 		return (0);
   1836 	}
   1837 
   1838 	return (EINVAL);
   1839 }
   1840 
   1841 int
   1842 ess_query_devinfo(addr, dip)
   1843 	void *addr;
   1844 	mixer_devinfo_t *dip;
   1845 {
   1846 	struct ess_softc *sc = addr;
   1847 
   1848 	DPRINTFN(5,("ess_query_devinfo: model=%d index=%d\n",
   1849 		    sc->sc_model, dip->index));
   1850 
   1851 	/*
   1852 	 * REVISIT: There are some slight differences between the
   1853 	 *          mixers on the different ESS chips, which can
   1854 	 *          be sorted out using the chip model rather than a
   1855 	 *          separate mixer model.
   1856 	 *          This is currently coded assuming an ES1887; we
   1857 	 *          need to work out which bits are not applicable to
   1858 	 *          the other models (1888 and 888).
   1859 	 */
   1860 	switch (dip->index) {
   1861 	case ESS_DAC_PLAY_VOL:
   1862 		dip->mixer_class = ESS_INPUT_CLASS;
   1863 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1864 		strcpy(dip->label.name, AudioNdac);
   1865 		dip->type = AUDIO_MIXER_VALUE;
   1866 		dip->un.v.num_channels = 2;
   1867 		strcpy(dip->un.v.units.name, AudioNvolume);
   1868 		return (0);
   1869 
   1870 	case ESS_MIC_PLAY_VOL:
   1871 		dip->mixer_class = ESS_INPUT_CLASS;
   1872 		dip->prev = AUDIO_MIXER_LAST;
   1873 		if (sc->sc_model == ESS_1788)
   1874 			dip->next = AUDIO_MIXER_LAST;
   1875 		else
   1876 			dip->next = ESS_MIC_PREAMP;
   1877 		strcpy(dip->label.name, AudioNmicrophone);
   1878 		dip->type = AUDIO_MIXER_VALUE;
   1879 		dip->un.v.num_channels = 2;
   1880 		strcpy(dip->un.v.units.name, AudioNvolume);
   1881 		return (0);
   1882 
   1883 	case ESS_LINE_PLAY_VOL:
   1884 		dip->mixer_class = ESS_INPUT_CLASS;
   1885 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1886 		strcpy(dip->label.name, AudioNline);
   1887 		dip->type = AUDIO_MIXER_VALUE;
   1888 		dip->un.v.num_channels = 2;
   1889 		strcpy(dip->un.v.units.name, AudioNvolume);
   1890 		return (0);
   1891 
   1892 	case ESS_SYNTH_PLAY_VOL:
   1893 		dip->mixer_class = ESS_INPUT_CLASS;
   1894 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1895 		strcpy(dip->label.name, AudioNfmsynth);
   1896 		dip->type = AUDIO_MIXER_VALUE;
   1897 		dip->un.v.num_channels = 2;
   1898 		strcpy(dip->un.v.units.name, AudioNvolume);
   1899 		return (0);
   1900 
   1901 	case ESS_CD_PLAY_VOL:
   1902 		dip->mixer_class = ESS_INPUT_CLASS;
   1903 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1904 		strcpy(dip->label.name, AudioNcd);
   1905 		dip->type = AUDIO_MIXER_VALUE;
   1906 		dip->un.v.num_channels = 2;
   1907 		strcpy(dip->un.v.units.name, AudioNvolume);
   1908 		return (0);
   1909 
   1910 	case ESS_AUXB_PLAY_VOL:
   1911 		dip->mixer_class = ESS_INPUT_CLASS;
   1912 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1913 		strcpy(dip->label.name, "auxb");
   1914 		dip->type = AUDIO_MIXER_VALUE;
   1915 		dip->un.v.num_channels = 2;
   1916 		strcpy(dip->un.v.units.name, AudioNvolume);
   1917 		return (0);
   1918 
   1919 	case ESS_INPUT_CLASS:
   1920 		dip->mixer_class = ESS_INPUT_CLASS;
   1921 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1922 		strcpy(dip->label.name, AudioCinputs);
   1923 		dip->type = AUDIO_MIXER_CLASS;
   1924 		return (0);
   1925 
   1926 	case ESS_MASTER_VOL:
   1927 		dip->mixer_class = ESS_OUTPUT_CLASS;
   1928 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1929 		strcpy(dip->label.name, AudioNmaster);
   1930 		dip->type = AUDIO_MIXER_VALUE;
   1931 		dip->un.v.num_channels = 2;
   1932 		strcpy(dip->un.v.units.name, AudioNvolume);
   1933 		return (0);
   1934 
   1935 	case ESS_PCSPEAKER_VOL:
   1936 		dip->mixer_class = ESS_OUTPUT_CLASS;
   1937 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1938 		strcpy(dip->label.name, "pc_speaker");
   1939 		dip->type = AUDIO_MIXER_VALUE;
   1940 		dip->un.v.num_channels = 1;
   1941 		strcpy(dip->un.v.units.name, AudioNvolume);
   1942 		return (0);
   1943 
   1944 	case ESS_OUTPUT_CLASS:
   1945 		dip->mixer_class = ESS_OUTPUT_CLASS;
   1946 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1947 		strcpy(dip->label.name, AudioCoutputs);
   1948 		dip->type = AUDIO_MIXER_CLASS;
   1949 		return (0);
   1950 
   1951 	case ESS_RECORD_VOL:
   1952 		dip->mixer_class = ESS_RECORD_CLASS;
   1953 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1954 		strcpy(dip->label.name, AudioNrecord);
   1955 		dip->type = AUDIO_MIXER_VALUE;
   1956 		dip->un.v.num_channels = 2;
   1957 		strcpy(dip->un.v.units.name, AudioNvolume);
   1958 		return (0);
   1959 
   1960 	case ESS_RECORD_SOURCE:
   1961 		dip->mixer_class = ESS_RECORD_CLASS;
   1962 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1963 		strcpy(dip->label.name, AudioNsource);
   1964 		if (sc->sc_model == ESS_1788) {
   1965 			/*
   1966 			 * The 1788 doesn't use the input mixer control that
   1967 			 * the 1888 uses, because it's a pain when you only
   1968 			 * have one mixer.
   1969 			 * Perhaps it could be emulated by keeping both sets of
   1970 			 * gain values, and doing a `context switch' of the
   1971 			 * mixer registers when shifting from playing to
   1972 			 * recording.
   1973 			 */
   1974 			dip->type = AUDIO_MIXER_ENUM;
   1975 			dip->un.e.num_mem = 4;
   1976 			strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
   1977 			dip->un.e.member[0].ord = ESS_SOURCE_MIC;
   1978 			strcpy(dip->un.e.member[1].label.name, AudioNline);
   1979 			dip->un.e.member[1].ord = ESS_SOURCE_LINE;
   1980 			strcpy(dip->un.e.member[2].label.name, AudioNcd);
   1981 			dip->un.e.member[2].ord = ESS_SOURCE_CD;
   1982 			strcpy(dip->un.e.member[3].label.name, AudioNmixerout);
   1983 			dip->un.e.member[3].ord = ESS_SOURCE_MIXER;
   1984 		} else {
   1985 			dip->type = AUDIO_MIXER_SET;
   1986 			dip->un.s.num_mem = 6;
   1987 			strcpy(dip->un.s.member[0].label.name, AudioNdac);
   1988 			dip->un.s.member[0].mask = 1 << ESS_DAC_REC_VOL;
   1989 			strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
   1990 			dip->un.s.member[1].mask = 1 << ESS_MIC_REC_VOL;
   1991 			strcpy(dip->un.s.member[2].label.name, AudioNline);
   1992 			dip->un.s.member[2].mask = 1 << ESS_LINE_REC_VOL;
   1993 			strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1994 			dip->un.s.member[3].mask = 1 << ESS_SYNTH_REC_VOL;
   1995 			strcpy(dip->un.s.member[4].label.name, AudioNcd);
   1996 			dip->un.s.member[4].mask = 1 << ESS_CD_REC_VOL;
   1997 			strcpy(dip->un.s.member[5].label.name, "auxb");
   1998 			dip->un.s.member[5].mask = 1 << ESS_AUXB_REC_VOL;
   1999 		}
   2000 		return (0);
   2001 
   2002 	case ESS_RECORD_CLASS:
   2003 		dip->mixer_class = ESS_RECORD_CLASS;
   2004 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2005 		strcpy(dip->label.name, AudioCrecord);
   2006 		dip->type = AUDIO_MIXER_CLASS;
   2007 		return (0);
   2008 
   2009 	case ESS_RECORD_MONITOR:
   2010 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   2011 		strcpy(dip->label.name, AudioNmonitor);
   2012 		dip->type = AUDIO_MIXER_ENUM;
   2013 		dip->mixer_class = ESS_MONITOR_CLASS;
   2014 		dip->un.e.num_mem = 2;
   2015 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   2016 		dip->un.e.member[0].ord = 0;
   2017 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   2018 		dip->un.e.member[1].ord = 1;
   2019 		return (0);
   2020 
   2021 	case ESS_MONITOR_CLASS:
   2022 		dip->mixer_class = ESS_MONITOR_CLASS;
   2023 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2024 		strcpy(dip->label.name, AudioCmonitor);
   2025 		dip->type = AUDIO_MIXER_CLASS;
   2026 		return (0);
   2027 	}
   2028 
   2029 	if (sc->sc_model == ESS_1788)
   2030 		return (ENXIO);
   2031 
   2032 	switch (dip->index) {
   2033 	case ESS_DAC_REC_VOL:
   2034 		dip->mixer_class = ESS_RECORD_CLASS;
   2035 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2036 		strcpy(dip->label.name, AudioNdac);
   2037 		dip->type = AUDIO_MIXER_VALUE;
   2038 		dip->un.v.num_channels = 2;
   2039 		strcpy(dip->un.v.units.name, AudioNvolume);
   2040 		return (0);
   2041 
   2042 	case ESS_MIC_REC_VOL:
   2043 		dip->mixer_class = ESS_RECORD_CLASS;
   2044 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2045 		strcpy(dip->label.name, AudioNmicrophone);
   2046 		dip->type = AUDIO_MIXER_VALUE;
   2047 		dip->un.v.num_channels = 2;
   2048 		strcpy(dip->un.v.units.name, AudioNvolume);
   2049 		return (0);
   2050 
   2051 	case ESS_LINE_REC_VOL:
   2052 		dip->mixer_class = ESS_RECORD_CLASS;
   2053 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2054 		strcpy(dip->label.name, AudioNline);
   2055 		dip->type = AUDIO_MIXER_VALUE;
   2056 		dip->un.v.num_channels = 2;
   2057 		strcpy(dip->un.v.units.name, AudioNvolume);
   2058 		return (0);
   2059 
   2060 	case ESS_SYNTH_REC_VOL:
   2061 		dip->mixer_class = ESS_RECORD_CLASS;
   2062 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2063 		strcpy(dip->label.name, AudioNfmsynth);
   2064 		dip->type = AUDIO_MIXER_VALUE;
   2065 		dip->un.v.num_channels = 2;
   2066 		strcpy(dip->un.v.units.name, AudioNvolume);
   2067 		return (0);
   2068 
   2069 	case ESS_CD_REC_VOL:
   2070 		dip->mixer_class = ESS_RECORD_CLASS;
   2071 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2072 		strcpy(dip->label.name, AudioNcd);
   2073 		dip->type = AUDIO_MIXER_VALUE;
   2074 		dip->un.v.num_channels = 2;
   2075 		strcpy(dip->un.v.units.name, AudioNvolume);
   2076 		return (0);
   2077 
   2078 	case ESS_AUXB_REC_VOL:
   2079 		dip->mixer_class = ESS_RECORD_CLASS;
   2080 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   2081 		strcpy(dip->label.name, "auxb");
   2082 		dip->type = AUDIO_MIXER_VALUE;
   2083 		dip->un.v.num_channels = 2;
   2084 		strcpy(dip->un.v.units.name, AudioNvolume);
   2085 		return (0);
   2086 
   2087 	case ESS_MIC_PREAMP:
   2088 		dip->mixer_class = ESS_INPUT_CLASS;
   2089 		dip->prev = ESS_MIC_PLAY_VOL;
   2090 		dip->next = AUDIO_MIXER_LAST;
   2091 		strcpy(dip->label.name, AudioNpreamp);
   2092 		dip->type = AUDIO_MIXER_ENUM;
   2093 		dip->un.e.num_mem = 2;
   2094 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   2095 		dip->un.e.member[0].ord = 0;
   2096 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   2097 		dip->un.e.member[1].ord = 1;
   2098 		return (0);
   2099 	}
   2100 
   2101 	return (ENXIO);
   2102 }
   2103 
   2104 void *
   2105 ess_malloc(addr, direction, size, pool, flags)
   2106 	void *addr;
   2107 	int direction;
   2108 	size_t size;
   2109 	int pool, flags;
   2110 {
   2111 	struct ess_softc *sc = addr;
   2112 	int drq;
   2113 
   2114 	if (sc->sc_model != ESS_1788 && direction == AUMODE_PLAY)
   2115 		drq = sc->sc_audio2.drq;
   2116 	else
   2117 		drq = sc->sc_audio1.drq;
   2118 	return (isa_malloc(sc->sc_ic, drq, size, pool, flags));
   2119 }
   2120 
   2121 void
   2122 ess_free(addr, ptr, pool)
   2123 	void *addr;
   2124 	void *ptr;
   2125 	int pool;
   2126 {
   2127 	isa_free(ptr, pool);
   2128 }
   2129 
   2130 size_t
   2131 ess_round_buffersize(addr, direction, size)
   2132 	void *addr;
   2133 	int direction;
   2134 	size_t size;
   2135 {
   2136 	if (size > MAX_ISADMA)
   2137 		size = MAX_ISADMA;
   2138 	return (size);
   2139 }
   2140 
   2141 int
   2142 ess_mappage(addr, mem, off, prot)
   2143 	void *addr;
   2144 	void *mem;
   2145 	int off;
   2146 	int prot;
   2147 {
   2148 	return (isa_mappage(mem, off, prot));
   2149 }
   2150 
   2151 int
   2152 ess_1788_get_props(addr)
   2153 	void *addr;
   2154 {
   2155 
   2156 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT);
   2157 }
   2158 
   2159 int
   2160 ess_1888_get_props(addr)
   2161 	void *addr;
   2162 {
   2163 
   2164 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
   2165 }
   2166 
   2167 /* ============================================
   2168  * Generic functions for ess, not used by audio h/w i/f
   2169  * =============================================
   2170  */
   2171 
   2172 /*
   2173  * Reset the chip.
   2174  * Return non-zero if the chip isn't detected.
   2175  */
   2176 int
   2177 ess_reset(sc)
   2178 	struct ess_softc *sc;
   2179 {
   2180 	bus_space_tag_t iot = sc->sc_iot;
   2181 	bus_space_handle_t ioh = sc->sc_ioh;
   2182 
   2183 	sc->sc_audio1.active = 0;
   2184 	sc->sc_audio2.active = 0;
   2185 
   2186 	EWRITE1(iot, ioh, ESS_DSP_RESET, ESS_RESET_EXT);
   2187 	delay(10000);
   2188 	EWRITE1(iot, ioh, ESS_DSP_RESET, 0);
   2189 	if (ess_rdsp(sc) != ESS_MAGIC)
   2190 		return (1);
   2191 
   2192 	/* Enable access to the ESS extension commands. */
   2193 	ess_wdsp(sc, ESS_ACMD_ENABLE_EXT);
   2194 
   2195 	return (0);
   2196 }
   2197 
   2198 void
   2199 ess_set_gain(sc, port, on)
   2200 	struct ess_softc *sc;
   2201 	int port;
   2202 	int on;
   2203 {
   2204 	int gain, left, right;
   2205 	int mix;
   2206 	int src;
   2207 	int stereo;
   2208 
   2209 	/*
   2210 	 * Most gain controls are found in the mixer registers and
   2211 	 * are stereo. Any that are not, must set mix and stereo as
   2212 	 * required.
   2213 	 */
   2214 	mix = 1;
   2215 	stereo = 1;
   2216 
   2217 	switch (port) {
   2218 	case ESS_MASTER_VOL:
   2219 		src = ESS_MREG_VOLUME_MASTER;
   2220 		break;
   2221 	case ESS_DAC_PLAY_VOL:
   2222 		if (sc->sc_model == ESS_1788)
   2223 			src = ESS_MREG_VOLUME_VOICE;
   2224 		else
   2225 			src = 0x7C;
   2226 		break;
   2227 	case ESS_MIC_PLAY_VOL:
   2228 		src = ESS_MREG_VOLUME_MIC;
   2229 		break;
   2230 	case ESS_LINE_PLAY_VOL:
   2231 		src = ESS_MREG_VOLUME_LINE;
   2232 		break;
   2233 	case ESS_SYNTH_PLAY_VOL:
   2234 		src = ESS_MREG_VOLUME_SYNTH;
   2235 		break;
   2236 	case ESS_CD_PLAY_VOL:
   2237 		src = ESS_MREG_VOLUME_CD;
   2238 		break;
   2239 	case ESS_AUXB_PLAY_VOL:
   2240 		src = ESS_MREG_VOLUME_AUXB;
   2241 		break;
   2242 	case ESS_PCSPEAKER_VOL:
   2243 		src = ESS_MREG_VOLUME_PCSPKR;
   2244 		stereo = 0;
   2245 		break;
   2246 	case ESS_DAC_REC_VOL:
   2247 		src = 0x69;
   2248 		break;
   2249 	case ESS_MIC_REC_VOL:
   2250 		src = 0x68;
   2251 		break;
   2252 	case ESS_LINE_REC_VOL:
   2253 		src = 0x6E;
   2254 		break;
   2255 	case ESS_SYNTH_REC_VOL:
   2256 		src = 0x6B;
   2257 		break;
   2258 	case ESS_CD_REC_VOL:
   2259 		src = 0x6A;
   2260 		break;
   2261 	case ESS_AUXB_REC_VOL:
   2262 		src = 0x6C;
   2263 		break;
   2264 	case ESS_RECORD_VOL:
   2265 		src = ESS_XCMD_VOLIN_CTRL;
   2266 		mix = 0;
   2267 		break;
   2268 	default:
   2269 		return;
   2270 	}
   2271 
   2272 	/* 1788 doesn't have a separate recording mixer */
   2273 	if (sc->sc_model == ESS_1788 && mix && src > 0x62)
   2274 		return;
   2275 
   2276 	if (on) {
   2277 		left = sc->gain[port][ESS_LEFT];
   2278 		right = sc->gain[port][ESS_RIGHT];
   2279 	} else {
   2280 		left = right = 0;
   2281 	}
   2282 
   2283 	if (stereo)
   2284 		gain = ESS_STEREO_GAIN(left, right);
   2285 	else
   2286 		gain = ESS_MONO_GAIN(left);
   2287 
   2288 	if (mix)
   2289 		ess_write_mix_reg(sc, src, gain);
   2290 	else
   2291 		ess_write_x_reg(sc, src, gain);
   2292 }
   2293 
   2294 /* Set the input device on devices without an input mixer. */
   2295 int
   2296 ess_set_in_port(sc, ord)
   2297 	struct ess_softc *sc;
   2298 	int ord;
   2299 {
   2300 	mixer_devinfo_t di;
   2301 	int i;
   2302 
   2303 	DPRINTF(("ess_set_in_port: ord=0x%x\n", ord));
   2304 
   2305 	/*
   2306 	 * Get the device info for the record source control,
   2307 	 * including the list of available sources.
   2308 	 */
   2309 	di.index = ESS_RECORD_SOURCE;
   2310 	if (ess_query_devinfo(sc, &di))
   2311 		return EINVAL;
   2312 
   2313 	/* See if the given ord value was anywhere in the list. */
   2314 	for (i = 0; i < di.un.e.num_mem; i++) {
   2315 		if (ord == di.un.e.member[i].ord)
   2316 			break;
   2317 	}
   2318 	if (i == di.un.e.num_mem)
   2319 		return EINVAL;
   2320 
   2321 	ess_write_mix_reg(sc, ESS_MREG_ADC_SOURCE, ord);
   2322 
   2323 	sc->in_port = ord;
   2324 	return (0);
   2325 }
   2326 
   2327 /* Set the input device levels on input-mixer-enabled devices. */
   2328 int
   2329 ess_set_in_ports(sc, mask)
   2330 	struct ess_softc *sc;
   2331 	int mask;
   2332 {
   2333 	mixer_devinfo_t di;
   2334 	int i, port;
   2335 
   2336 	DPRINTF(("ess_set_in_ports: mask=0x%x\n", mask));
   2337 
   2338 	/*
   2339 	 * Get the device info for the record source control,
   2340 	 * including the list of available sources.
   2341 	 */
   2342 	di.index = ESS_RECORD_SOURCE;
   2343 	if (ess_query_devinfo(sc, &di))
   2344 		return EINVAL;
   2345 
   2346 	/*
   2347 	 * Set or disable the record volume control for each of the
   2348 	 * possible sources.
   2349 	 */
   2350 	for (i = 0; i < di.un.s.num_mem; i++) {
   2351 		/*
   2352 		 * Calculate the source port number from its mask.
   2353 		 */
   2354 		port = ffs(di.un.s.member[i].mask);
   2355 
   2356 		/*
   2357 		 * Set the source gain:
   2358 		 *	to the current value if source is enabled
   2359 		 *	to zero if source is disabled
   2360 		 */
   2361 		ess_set_gain(sc, port, mask & di.un.s.member[i].mask);
   2362 	}
   2363 
   2364 	sc->in_mask = mask;
   2365 	return (0);
   2366 }
   2367 
   2368 void
   2369 ess_speaker_on(sc)
   2370 	struct ess_softc *sc;
   2371 {
   2372 	/* Disable mute on left- and right-master volume. */
   2373 	ess_clear_mreg_bits(sc, ESS_MREG_VOLUME_LEFT, ESS_VOLUME_MUTE);
   2374 	ess_clear_mreg_bits(sc, ESS_MREG_VOLUME_RIGHT, ESS_VOLUME_MUTE);
   2375 }
   2376 
   2377 void
   2378 ess_speaker_off(sc)
   2379 	struct ess_softc *sc;
   2380 {
   2381 	/* Enable mute on left- and right-master volume. */
   2382 	ess_set_mreg_bits(sc, ESS_MREG_VOLUME_LEFT, ESS_VOLUME_MUTE);
   2383 	ess_set_mreg_bits(sc, ESS_MREG_VOLUME_RIGHT, ESS_VOLUME_MUTE);
   2384 }
   2385 
   2386 /*
   2387  * Calculate the time constant for the requested sampling rate.
   2388  */
   2389 u_int
   2390 ess_srtotc(rate)
   2391 	u_int rate;
   2392 {
   2393 	u_int tc;
   2394 
   2395 	/* The following formulae are from the ESS data sheet. */
   2396 	if (rate <= 22050)
   2397 		tc = 128 - 397700L / rate;
   2398 	else
   2399 		tc = 256 - 795500L / rate;
   2400 
   2401 	return (tc);
   2402 }
   2403 
   2404 
   2405 /*
   2406  * Calculate the filter constant for the reuqested sampling rate.
   2407  */
   2408 u_int
   2409 ess_srtofc(rate)
   2410 	u_int rate;
   2411 {
   2412 	/*
   2413 	 * The following formula is derived from the information in
   2414 	 * the ES1887 data sheet, based on a roll-off frequency of
   2415 	 * 87%.
   2416 	 */
   2417 	return (256 - 200279L / rate);
   2418 }
   2419 
   2420 
   2421 /*
   2422  * Return the status of the DSP.
   2423  */
   2424 u_char
   2425 ess_get_dsp_status(sc)
   2426 	struct ess_softc *sc;
   2427 {
   2428 	return (EREAD1(sc->sc_iot, sc->sc_ioh, ESS_DSP_RW_STATUS));
   2429 }
   2430 
   2431 
   2432 /*
   2433  * Return the read status of the DSP:	1 -> DSP ready for reading
   2434  *					0 -> DSP not ready for reading
   2435  */
   2436 u_char
   2437 ess_dsp_read_ready(sc)
   2438 	struct ess_softc *sc;
   2439 {
   2440 	return ((ess_get_dsp_status(sc) & ESS_DSP_READ_READY) ? 1 : 0);
   2441 }
   2442 
   2443 
   2444 /*
   2445  * Return the write status of the DSP:	1 -> DSP ready for writing
   2446  *					0 -> DSP not ready for writing
   2447  */
   2448 u_char
   2449 ess_dsp_write_ready(sc)
   2450 	struct ess_softc *sc;
   2451 {
   2452 	return ((ess_get_dsp_status(sc) & ESS_DSP_WRITE_BUSY) ? 0 : 1);
   2453 }
   2454 
   2455 
   2456 /*
   2457  * Read a byte from the DSP.
   2458  */
   2459 int
   2460 ess_rdsp(sc)
   2461 	struct ess_softc *sc;
   2462 {
   2463 	bus_space_tag_t iot = sc->sc_iot;
   2464 	bus_space_handle_t ioh = sc->sc_ioh;
   2465 	int i;
   2466 
   2467 	for (i = ESS_READ_TIMEOUT; i > 0; --i) {
   2468 		if (ess_dsp_read_ready(sc)) {
   2469 			i = EREAD1(iot, ioh, ESS_DSP_READ);
   2470 			DPRINTFN(8,("ess_rdsp() = 0x%02x\n", i));
   2471 			return i;
   2472 		} else
   2473 			delay(10);
   2474 	}
   2475 
   2476 	DPRINTF(("ess_rdsp: timed out\n"));
   2477 	return (-1);
   2478 }
   2479 
   2480 /*
   2481  * Write a byte to the DSP.
   2482  */
   2483 int
   2484 ess_wdsp(sc, v)
   2485 	struct ess_softc *sc;
   2486 	u_char v;
   2487 {
   2488 	bus_space_tag_t iot = sc->sc_iot;
   2489 	bus_space_handle_t ioh = sc->sc_ioh;
   2490 	int i;
   2491 
   2492 	DPRINTFN(8,("ess_wdsp(0x%02x)\n", v));
   2493 
   2494 	for (i = ESS_WRITE_TIMEOUT; i > 0; --i) {
   2495 		if (ess_dsp_write_ready(sc)) {
   2496 			EWRITE1(iot, ioh, ESS_DSP_WRITE, v);
   2497 			return (0);
   2498 		} else
   2499 			delay(10);
   2500 	}
   2501 
   2502 	DPRINTF(("ess_wdsp(0x%02x): timed out\n", v));
   2503 	return (-1);
   2504 }
   2505 
   2506 /*
   2507  * Write a value to one of the ESS extended registers.
   2508  */
   2509 int
   2510 ess_write_x_reg(sc, reg, val)
   2511 	struct ess_softc *sc;
   2512 	u_char reg;
   2513 	u_char val;
   2514 {
   2515 	int error;
   2516 
   2517 	DPRINTFN(2,("ess_write_x_reg: %02x=%02x\n", reg, val));
   2518 	if ((error = ess_wdsp(sc, reg)) == 0)
   2519 		error = ess_wdsp(sc, val);
   2520 
   2521 	return error;
   2522 }
   2523 
   2524 /*
   2525  * Read the value of one of the ESS extended registers.
   2526  */
   2527 u_char
   2528 ess_read_x_reg(sc, reg)
   2529 	struct ess_softc *sc;
   2530 	u_char reg;
   2531 {
   2532 	int error;
   2533 	int val;
   2534 
   2535 	if ((error = ess_wdsp(sc, 0xC0)) == 0)
   2536 		error = ess_wdsp(sc, reg);
   2537 	if (error)
   2538 		DPRINTF(("Error reading extended register 0x%02x\n", reg));
   2539 /* REVISIT: what if an error is returned above? */
   2540 	val = ess_rdsp(sc);
   2541 	DPRINTFN(2,("ess_read_x_reg: %02x=%02x\n", reg, val));
   2542 	return val;
   2543 }
   2544 
   2545 void
   2546 ess_clear_xreg_bits(sc, reg, mask)
   2547 	struct ess_softc *sc;
   2548 	u_char reg;
   2549 	u_char mask;
   2550 {
   2551 	if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) & ~mask) == -1)
   2552 		DPRINTF(("Error clearing bits in extended register 0x%02x\n",
   2553 			 reg));
   2554 }
   2555 
   2556 void
   2557 ess_set_xreg_bits(sc, reg, mask)
   2558 	struct ess_softc *sc;
   2559 	u_char reg;
   2560 	u_char mask;
   2561 {
   2562 	if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) | mask) == -1)
   2563 		DPRINTF(("Error setting bits in extended register 0x%02x\n",
   2564 			 reg));
   2565 }
   2566 
   2567 
   2568 /*
   2569  * Write a value to one of the ESS mixer registers.
   2570  */
   2571 void
   2572 ess_write_mix_reg(sc, reg, val)
   2573 	struct ess_softc *sc;
   2574 	u_char reg;
   2575 	u_char val;
   2576 {
   2577 	bus_space_tag_t iot = sc->sc_iot;
   2578 	bus_space_handle_t ioh = sc->sc_ioh;
   2579 	int s;
   2580 
   2581 	DPRINTFN(2,("ess_write_mix_reg: %x=%x\n", reg, val));
   2582 
   2583 	s = splaudio();
   2584 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
   2585 	EWRITE1(iot, ioh, ESS_MIX_REG_DATA, val);
   2586 	splx(s);
   2587 }
   2588 
   2589 /*
   2590  * Read the value of one of the ESS mixer registers.
   2591  */
   2592 u_char
   2593 ess_read_mix_reg(sc, reg)
   2594 	struct ess_softc *sc;
   2595 	u_char reg;
   2596 {
   2597 	bus_space_tag_t iot = sc->sc_iot;
   2598 	bus_space_handle_t ioh = sc->sc_ioh;
   2599 	int s;
   2600 	u_char val;
   2601 
   2602 	s = splaudio();
   2603 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
   2604 	val = EREAD1(iot, ioh, ESS_MIX_REG_DATA);
   2605 	splx(s);
   2606 
   2607 	DPRINTFN(2,("ess_read_mix_reg: %x=%x\n", reg, val));
   2608 	return val;
   2609 }
   2610 
   2611 void
   2612 ess_clear_mreg_bits(sc, reg, mask)
   2613 	struct ess_softc *sc;
   2614 	u_char reg;
   2615 	u_char mask;
   2616 {
   2617 	ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) & ~mask);
   2618 }
   2619 
   2620 void
   2621 ess_set_mreg_bits(sc, reg, mask)
   2622 	struct ess_softc *sc;
   2623 	u_char reg;
   2624 	u_char mask;
   2625 {
   2626 	ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) | mask);
   2627 }
   2628