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