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