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