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