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