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