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