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