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