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