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