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