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