Home | History | Annotate | Line # | Download | only in isa
ess.c revision 1.24
      1 /*	$NetBSD: ess.c,v 1.24 1998/08/22 22:55:09 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright 1997
      5  * Digital Equipment Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and
      8  * copied only in accordance with the following terms and conditions.
      9  * Subject to these conditions, you may download, copy, install,
     10  * use, modify and distribute this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce
     14  *    and retain this copyright notice and list of conditions as
     15  *    they appear in the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19  *    Corporation" name nor any trademark or logo of Digital Equipment
     20  *    Corporation may be used to endorse or promote products derived
     21  *    from this software without the prior written permission of
     22  *    Digital Equipment Corporation.
     23  *
     24  * 3) This software is provided "AS-IS" and any express or implied
     25  *    warranties, including but not limited to, any implied warranties
     26  *    of merchantability, fitness for a particular purpose, or
     27  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28  *    liable for any damages whatsoever, and in particular, DIGITAL
     29  *    shall not be liable for special, indirect, consequential, or
     30  *    incidental damages or damages for lost profits, loss of
     31  *    revenue or loss of use, whether such damages arise in contract,
     32  *    negligence, tort, under statute, in equity, at law or otherwise,
     33  *    even if advised of the possibility of such damage.
     34  */
     35 
     36 /*
     37 **++
     38 **
     39 **  ess.c
     40 **
     41 **  FACILITY:
     42 **
     43 **	DIGITAL Network Appliance Reference Design (DNARD)
     44 **
     45 **  MODULE DESCRIPTION:
     46 **
     47 **      This module contains the device driver for the ESS
     48 **      Technologies 1888/1887/888 sound chip. The code in sbdsp.c was
     49 **	used as a reference point when implementing this driver.
     50 **
     51 **  AUTHORS:
     52 **
     53 **	Blair Fidler	Software Engineering Australia
     54 **			Gold Coast, Australia.
     55 **
     56 **  CREATION DATE:
     57 **
     58 **	March 10, 1997.
     59 **
     60 **  MODIFICATION HISTORY:
     61 **
     62 **	Heavily modified by Lennart Augustsson and Charles M. Hannum for
     63 **	bus_dma, changes to audio interface, and many bug fixes.
     64 **--
     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 *, unsigned long, int, int));
    141 void	ess_free __P((void *, void *, int));
    142 unsigned long ess_round __P((void *, unsigned long));
    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,
    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 75% 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 * 3 / 4);
    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 	ess_printsc(sc);
    888 #endif
    889 }
    890 
    891 /*
    892  * Various routines to interface to higher level audio driver
    893  */
    894 
    895 int
    896 ess_open(addr, flags)
    897 	void *addr;
    898 	int flags;
    899 {
    900 	struct ess_softc *sc = addr;
    901 
    902         DPRINTF(("ess_open: sc=%p\n", sc));
    903 
    904 	if (sc->sc_open != 0 || ess_reset(sc) != 0)
    905 		return ENXIO;
    906 
    907 	ess_setup(sc);		/* because we did a reset */
    908 
    909 	sc->sc_open = 1;
    910 
    911 	DPRINTF(("ess_open: opened\n"));
    912 
    913 	return (0);
    914 }
    915 
    916 void
    917 ess_close(addr)
    918 	void *addr;
    919 {
    920 	struct ess_softc *sc = addr;
    921 
    922         DPRINTF(("ess_close: sc=%p\n", sc));
    923 
    924 	sc->sc_open = 0;
    925 	ess_speaker_off(sc);
    926 	sc->spkr_state = SPKR_OFF;
    927 	ess_halt_output(sc);
    928 	ess_halt_input(sc);
    929 	sc->sc_in.intr = 0;
    930 	sc->sc_out.intr = 0;
    931 
    932 	DPRINTF(("ess_close: closed\n"));
    933 }
    934 
    935 /*
    936  * Wait for FIFO to drain, and analog section to settle.
    937  * XXX should check FIFO full bit.
    938  */
    939 int
    940 ess_drain(addr)
    941 	void *addr;
    942 {
    943 	extern int hz;		/* XXX */
    944 
    945 	tsleep(addr, PWAIT | PCATCH, "essdr", hz/20); /* XXX */
    946 	return (0);
    947 }
    948 
    949 int
    950 ess_speaker_ctl(addr, newstate)
    951 	void *addr;
    952 	int newstate;
    953 {
    954 	struct ess_softc *sc = addr;
    955 
    956 	if ((newstate == SPKR_ON) && (sc->spkr_state == SPKR_OFF)) {
    957 		ess_speaker_on(sc);
    958 		sc->spkr_state = SPKR_ON;
    959 	}
    960 	if ((newstate == SPKR_OFF) && (sc->spkr_state == SPKR_ON)) {
    961 		ess_speaker_off(sc);
    962 		sc->spkr_state = SPKR_OFF;
    963 	}
    964 	return (0);
    965 }
    966 
    967 int
    968 ess_getdev(addr, retp)
    969 	void *addr;
    970 	struct audio_device *retp;
    971 {
    972 	*retp = ess_device;
    973 	return (0);
    974 }
    975 
    976 int
    977 ess_query_encoding(addr, fp)
    978 	void *addr;
    979 	struct audio_encoding *fp;
    980 {
    981 	/*struct ess_softc *sc = addr;*/
    982 
    983 	switch (fp->index) {
    984 	case 0:
    985 		strcpy(fp->name, AudioEulinear);
    986 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    987 		fp->precision = 8;
    988 		fp->flags = 0;
    989 		return (0);
    990 	case 1:
    991 		strcpy(fp->name, AudioEmulaw);
    992 		fp->encoding = AUDIO_ENCODING_ULAW;
    993 		fp->precision = 8;
    994 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    995 		return (0);
    996 	case 2:
    997 		strcpy(fp->name, AudioEalaw);
    998 		fp->encoding = AUDIO_ENCODING_ALAW;
    999 		fp->precision = 8;
   1000 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1001 		return (0);
   1002 	case 3:
   1003 		strcpy(fp->name, AudioEslinear);
   1004 		fp->encoding = AUDIO_ENCODING_SLINEAR;
   1005 		fp->precision = 8;
   1006 		fp->flags = 0;
   1007 		return (0);
   1008         case 4:
   1009 		strcpy(fp->name, AudioEslinear_le);
   1010 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
   1011 		fp->precision = 16;
   1012 		fp->flags = 0;
   1013 		return (0);
   1014 	case 5:
   1015 		strcpy(fp->name, AudioEulinear_le);
   1016 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
   1017 		fp->precision = 16;
   1018 		fp->flags = 0;
   1019 		return (0);
   1020 	case 6:
   1021 		strcpy(fp->name, AudioEslinear_be);
   1022 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
   1023 		fp->precision = 16;
   1024 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1025 		return (0);
   1026 	case 7:
   1027 		strcpy(fp->name, AudioEulinear_be);
   1028 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
   1029 		fp->precision = 16;
   1030 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1031 		return (0);
   1032 	default:
   1033 		return EINVAL;
   1034 	}
   1035 	return (0);
   1036 }
   1037 
   1038 int
   1039 ess_set_params(addr, setmode, usemode, play, rec)
   1040 	void *addr;
   1041 	int setmode, usemode;
   1042 	struct audio_params *play, *rec;
   1043 {
   1044 	struct ess_softc *sc = addr;
   1045 	struct audio_params *p;
   1046 	int mode;
   1047 	int rate;
   1048 
   1049 	DPRINTF(("ess_set_params: set=%d use=%d\n", setmode, usemode));
   1050 
   1051 	/*
   1052 	 * The ES1887 manual (page 39, `Full-Duplex DMA Mode') claims that in
   1053 	 * full-duplex operation the sample rates must be the same for both
   1054 	 * channels.  This appears to be false; the only bit in common is the
   1055 	 * clock source selection.  However, we'll be conservative here.
   1056 	 * - mycroft
   1057 	 */
   1058 	if (play->sample_rate != rec->sample_rate &&
   1059 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1060 		if (setmode == AUMODE_PLAY) {
   1061 			rec->sample_rate = play->sample_rate;
   1062 			setmode |= AUMODE_RECORD;
   1063 		} else if (setmode == AUMODE_RECORD) {
   1064 			play->sample_rate = rec->sample_rate;
   1065 			setmode |= AUMODE_PLAY;
   1066 		} else
   1067 			return (EINVAL);
   1068 	}
   1069 
   1070 	for (mode = AUMODE_RECORD; mode != -1;
   1071 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1072 		if ((setmode & mode) == 0)
   1073 			continue;
   1074 
   1075 		p = mode == AUMODE_PLAY ? play : rec;
   1076 
   1077 		if (p->sample_rate < ESS_MINRATE ||
   1078 		    p->sample_rate > ESS_MAXRATE ||
   1079 		    (p->precision != 8 && p->precision != 16) ||
   1080 		    (p->channels != 1 && p->channels != 2))
   1081 			return (EINVAL);
   1082 
   1083 		p->factor = 1;
   1084 		p->sw_code = 0;
   1085 		switch (p->encoding) {
   1086 		case AUDIO_ENCODING_SLINEAR_BE:
   1087 		case AUDIO_ENCODING_ULINEAR_BE:
   1088 			if (p->precision == 16)
   1089 				p->sw_code = swap_bytes;
   1090 			break;
   1091 		case AUDIO_ENCODING_SLINEAR_LE:
   1092 		case AUDIO_ENCODING_ULINEAR_LE:
   1093 			break;
   1094 		case AUDIO_ENCODING_ULAW:
   1095 			if (mode == AUMODE_PLAY) {
   1096 				p->factor = 2;
   1097 				p->sw_code = mulaw_to_ulinear16;
   1098 			} else
   1099 				p->sw_code = ulinear8_to_mulaw;
   1100 			break;
   1101 		case AUDIO_ENCODING_ALAW:
   1102 			if (mode == AUMODE_PLAY) {
   1103 				p->factor = 2;
   1104 				p->sw_code = alaw_to_ulinear16;
   1105 			} else
   1106 				p->sw_code = ulinear8_to_alaw;
   1107 			break;
   1108 		default:
   1109 			return (EINVAL);
   1110 		}
   1111 	}
   1112 
   1113 	if (usemode == AUMODE_RECORD)
   1114 		rate = rec->sample_rate;
   1115 	else
   1116 		rate = play->sample_rate;
   1117 
   1118 	ess_write_mix_reg(sc, ESS_MREG_SAMPLE_RATE, ess_srtotc(rate));
   1119 	ess_write_mix_reg(sc, ESS_MREG_FILTER_CLOCK, ess_srtofc(rate));
   1120 
   1121 	ess_write_x_reg(sc, ESS_XCMD_SAMPLE_RATE, ess_srtotc(rate));
   1122 	ess_write_x_reg(sc, ESS_XCMD_FILTER_CLOCK, ess_srtofc(rate));
   1123 
   1124 	return (0);
   1125 }
   1126 
   1127 int
   1128 ess_trigger_output(addr, start, end, blksize, intr, arg, param)
   1129 	void *addr;
   1130 	void *start, *end;
   1131 	int blksize;
   1132 	void (*intr) __P((void *));
   1133 	void *arg;
   1134 	struct audio_params *param;
   1135 {
   1136 	struct ess_softc *sc = addr;
   1137 
   1138 	DPRINTFN(1, ("ess_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1139 	    addr, start, end, blksize, intr, arg));
   1140 
   1141 #ifdef DIAGNOSTIC
   1142 	if (param->channels == 2 && (blksize & 1)) {
   1143 		DPRINTF(("stereo playback odd bytes (%d)\n", blksize));
   1144 		return EIO;
   1145 	}
   1146 	if (sc->sc_out.active)
   1147 		panic("ess_trigger_output: already running");
   1148 #endif
   1149 	sc->sc_out.active = 1;
   1150 
   1151 	sc->sc_out.intr = intr;
   1152 	sc->sc_out.arg = arg;
   1153 
   1154 	if (param->precision * param->factor == 16)
   1155 		ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
   1156 		    ESS_AUDIO2_CTRL2_FIFO_SIZE);
   1157 	else
   1158 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
   1159 		    ESS_AUDIO2_CTRL2_FIFO_SIZE);
   1160 
   1161 	if (param->channels == 2)
   1162 		ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
   1163 		    ESS_AUDIO2_CTRL2_CHANNELS);
   1164 	else
   1165 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
   1166 		    ESS_AUDIO2_CTRL2_CHANNELS);
   1167 
   1168 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
   1169 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
   1170 		ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
   1171 		    ESS_AUDIO2_CTRL2_FIFO_SIGNED);
   1172 	else
   1173 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
   1174 		    ESS_AUDIO2_CTRL2_FIFO_SIGNED);
   1175 
   1176 	isa_dmastart(sc->sc_ic, sc->sc_out.drq, start, end - start, NULL,
   1177 	    DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
   1178 
   1179 	if (IS16BITDRQ(sc->sc_out.drq))
   1180 		blksize >>= 1;	/* use word count for 16 bit DMA */
   1181 	/* Program transfer count registers with 2's complement of count. */
   1182 	blksize = -blksize;
   1183 	ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTLO, blksize);
   1184 	ess_write_mix_reg(sc, ESS_MREG_XFER_COUNTHI, blksize >> 8);
   1185 
   1186 	if (IS16BITDRQ(sc->sc_out.drq))
   1187 		ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1,
   1188 		    ESS_AUDIO2_CTRL1_XFER_SIZE);
   1189 	else
   1190 		ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1,
   1191 		    ESS_AUDIO2_CTRL1_XFER_SIZE);
   1192 
   1193 	/* Use 8 bytes per output DMA. */
   1194 	ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1,
   1195 	    ESS_AUDIO2_CTRL1_DEMAND_8);
   1196 
   1197 	/* Start auto-init DMA */
   1198 	ess_set_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1,
   1199 			  ESS_AUDIO2_CTRL1_DAC_ENABLE |
   1200 			  ESS_AUDIO2_CTRL1_FIFO_ENABLE |
   1201 			  ESS_AUDIO2_CTRL1_AUTO_INIT);
   1202 
   1203 	return (0);
   1204 
   1205 }
   1206 
   1207 int
   1208 ess_trigger_input(addr, start, end, blksize, intr, arg, param)
   1209 	void *addr;
   1210 	void *start, *end;
   1211 	int blksize;
   1212 	void (*intr) __P((void *));
   1213 	void *arg;
   1214 	struct audio_params *param;
   1215 {
   1216 	struct ess_softc *sc = addr;
   1217 
   1218 	DPRINTFN(1, ("ess_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1219 	    addr, start, end, blksize, intr, arg));
   1220 
   1221 #ifdef DIAGNOSTIC
   1222 	if (param->channels == 2 && (blksize & 1)) {
   1223 		DPRINTF(("stereo record odd bytes (%d)\n", blksize));
   1224 		return EIO;
   1225 	}
   1226 	if (sc->sc_in.active)
   1227 		panic("ess_trigger_input: already running");
   1228 #endif
   1229 	sc->sc_in.active = 1;
   1230 
   1231 	sc->sc_in.intr = intr;
   1232 	sc->sc_in.arg = arg;
   1233 
   1234 	if (param->precision * param->factor == 16)
   1235 		ess_set_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL1,
   1236 		    ESS_AUDIO1_CTRL1_FIFO_SIZE);
   1237 	else
   1238 		ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL1,
   1239 		    ESS_AUDIO1_CTRL1_FIFO_SIZE);
   1240 
   1241 	if (param->channels == 2) {
   1242 		ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL,
   1243 		    (ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL) |
   1244 		     ESS_AUDIO_CTRL_STEREO) &~ ESS_AUDIO_CTRL_MONO);
   1245 		ess_set_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL1,
   1246 		    ESS_AUDIO1_CTRL1_FIFO_STEREO);
   1247 	} else {
   1248 		ess_write_x_reg(sc, ESS_XCMD_AUDIO_CTRL,
   1249 		    (ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL) |
   1250 		     ESS_AUDIO_CTRL_MONO) &~ ESS_AUDIO_CTRL_STEREO);
   1251 		ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL1,
   1252 		    ESS_AUDIO1_CTRL1_FIFO_STEREO);
   1253 	}
   1254 
   1255 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
   1256 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
   1257 		ess_set_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL1,
   1258 		    ESS_AUDIO1_CTRL1_FIFO_SIGNED);
   1259 	else
   1260 		ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL1,
   1261 		    ESS_AUDIO1_CTRL1_FIFO_SIGNED);
   1262 
   1263 	/* REVISIT: Hack to enable Audio1 FIFO connection to CODEC. */
   1264 	ess_set_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL1,
   1265 	    ESS_AUDIO1_CTRL1_FIFO_CONNECT);
   1266 
   1267 	isa_dmastart(sc->sc_ic, sc->sc_in.drq, start, end - start, NULL,
   1268 	    DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
   1269 
   1270 	if (IS16BITDRQ(sc->sc_in.drq))
   1271 		blksize >>= 1;	/* use word count for 16 bit DMA */
   1272 	/* Program transfer count registers with 2's complement of count. */
   1273 	blksize = -blksize;
   1274 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTLO, blksize);
   1275 	ess_write_x_reg(sc, ESS_XCMD_XFER_COUNTHI, blksize >> 8);
   1276 
   1277 	/* Use 4 bytes per input DMA. */
   1278 	ess_set_xreg_bits(sc, ESS_XCMD_DEMAND_CTRL,
   1279 	    ESS_DEMAND_CTRL_DEMAND_4);
   1280 
   1281 	/* Start auto-init DMA */
   1282 	ess_set_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL2,
   1283 			  ESS_AUDIO1_CTRL2_DMA_READ |
   1284 			  ESS_AUDIO1_CTRL2_ADC_ENABLE |
   1285 			  ESS_AUDIO1_CTRL2_FIFO_ENABLE |
   1286 			  ESS_AUDIO1_CTRL2_AUTO_INIT);
   1287 
   1288 	return (0);
   1289 
   1290 }
   1291 
   1292 int
   1293 ess_halt_output(addr)
   1294 	void *addr;
   1295 {
   1296 	struct ess_softc *sc = addr;
   1297 
   1298 	DPRINTF(("ess_halt_output: sc=%p\n", sc));
   1299 
   1300 	ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL1,
   1301 	    ESS_AUDIO2_CTRL1_FIFO_ENABLE);
   1302 
   1303 	return (0);
   1304 }
   1305 
   1306 int
   1307 ess_halt_input(addr)
   1308 	void *addr;
   1309 {
   1310 	struct ess_softc *sc = addr;
   1311 
   1312 	DPRINTF(("ess_halt_input: sc=%p\n", sc));
   1313 
   1314 	ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO1_CTRL2,
   1315 	    ESS_AUDIO1_CTRL2_FIFO_ENABLE);
   1316 
   1317 	return (0);
   1318 }
   1319 
   1320 int
   1321 ess_intr_output(arg)
   1322 	void *arg;
   1323 {
   1324 	struct ess_softc *sc = arg;
   1325 
   1326 	DPRINTFN(1,("ess_intr_output: intr=%p\n", sc->sc_out.intr));
   1327 
   1328 	/* clear interrupt on Audio channel 2 */
   1329 	ess_clear_mreg_bits(sc, ESS_MREG_AUDIO2_CTRL2,
   1330 			    ESS_AUDIO2_CTRL2_IRQ_LATCH);
   1331 
   1332 	sc->sc_out.nintr++;
   1333 
   1334 	if (sc->sc_out.intr != 0)
   1335 		(*sc->sc_out.intr)(sc->sc_out.arg);
   1336 	else
   1337 		return (0);
   1338 
   1339 	return (1);
   1340 }
   1341 
   1342 int
   1343 ess_intr_input(arg)
   1344 	void *arg;
   1345 {
   1346 	struct ess_softc *sc = arg;
   1347 	u_char x;
   1348 
   1349 	DPRINTFN(1,("ess_intr_input: intr=%p\n", sc->sc_in.intr));
   1350 
   1351 	/* clear interrupt on Audio channel 1*/
   1352 	x = EREAD1(sc->sc_iot, sc->sc_ioh, ESS_CLEAR_INTR);
   1353 
   1354 	sc->sc_in.nintr++;
   1355 
   1356 	if (sc->sc_in.intr != 0)
   1357 		(*sc->sc_in.intr)(sc->sc_in.arg);
   1358 	else
   1359 		return (0);
   1360 
   1361 	return (1);
   1362 }
   1363 
   1364 int
   1365 ess_round_blocksize(addr, blk)
   1366 	void *addr;
   1367 	int blk;
   1368 {
   1369 	return (blk & -8);	/* round for max DMA size */
   1370 }
   1371 
   1372 int
   1373 ess_set_port(addr, cp)
   1374 	void *addr;
   1375 	mixer_ctrl_t *cp;
   1376 {
   1377 	struct ess_softc *sc = addr;
   1378 	int lgain, rgain;
   1379 
   1380 	DPRINTFN(5,("ess_set_port: port=%d num_channels=%d\n",
   1381 		    cp->dev, cp->un.value.num_channels));
   1382 
   1383 	switch (cp->dev) {
   1384 	/*
   1385 	 * The following mixer ports are all stereo. If we get a
   1386 	 * single-channel gain value passed in, then we duplicate it
   1387 	 * to both left and right channels.
   1388 	 */
   1389 	case ESS_MASTER_VOL:
   1390 	case ESS_DAC_PLAY_VOL:
   1391 	case ESS_MIC_PLAY_VOL:
   1392 	case ESS_LINE_PLAY_VOL:
   1393 	case ESS_SYNTH_PLAY_VOL:
   1394 	case ESS_CD_PLAY_VOL:
   1395 	case ESS_AUXB_PLAY_VOL:
   1396 	case ESS_DAC_REC_VOL:
   1397 	case ESS_MIC_REC_VOL:
   1398 	case ESS_LINE_REC_VOL:
   1399 	case ESS_SYNTH_REC_VOL:
   1400 	case ESS_CD_REC_VOL:
   1401 	case ESS_AUXB_REC_VOL:
   1402 	case ESS_RECORD_VOL:
   1403 		if (cp->type != AUDIO_MIXER_VALUE)
   1404 			return EINVAL;
   1405 
   1406 		switch (cp->un.value.num_channels) {
   1407 		case 1:
   1408 			lgain = rgain = ESS_4BIT_GAIN(
   1409 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1410 			break;
   1411 		case 2:
   1412 			lgain = ESS_4BIT_GAIN(
   1413 			  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
   1414 			rgain = ESS_4BIT_GAIN(
   1415 			  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
   1416 			break;
   1417 		default:
   1418 			return EINVAL;
   1419 		}
   1420 
   1421 		sc->gain[cp->dev][ESS_LEFT]  = lgain;
   1422 		sc->gain[cp->dev][ESS_RIGHT] = rgain;
   1423 
   1424 		ess_set_gain(sc, cp->dev, 1);
   1425 		break;
   1426 
   1427 
   1428 	/*
   1429 	 * The PC speaker port is mono. If we get a stereo gain value
   1430 	 * passed in, then we return EINVAL.
   1431 	 */
   1432 	case ESS_PCSPEAKER_VOL:
   1433 		if (cp->un.value.num_channels != 1)
   1434 			return EINVAL;
   1435 
   1436 		sc->gain[cp->dev][ESS_LEFT]  = sc->gain[cp->dev][ESS_RIGHT] =
   1437 		  ESS_3BIT_GAIN(cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
   1438 		ess_set_gain(sc, cp->dev, 1);
   1439 		break;
   1440 
   1441 
   1442 	case ESS_MIC_PREAMP:
   1443 		if (cp->type != AUDIO_MIXER_ENUM)
   1444 			return EINVAL;
   1445 
   1446 		if (cp->un.ord)
   1447 			/* Enable microphone preamp */
   1448 			ess_set_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL,
   1449 					  ESS_PREAMP_CTRL_ENABLE);
   1450 		else
   1451 			/* Disable microphone preamp */
   1452 			ess_clear_xreg_bits(sc, ESS_XCMD_PREAMP_CTRL,
   1453 					  ESS_PREAMP_CTRL_ENABLE);
   1454 		break;
   1455 
   1456 	case ESS_RECORD_SOURCE:
   1457 		if (cp->type == AUDIO_MIXER_SET)
   1458 			return ess_set_in_ports(sc, cp->un.mask);
   1459 		else
   1460 			return EINVAL;
   1461 		break;
   1462 
   1463 	case ESS_RECORD_MONITOR:
   1464 		if (cp->type != AUDIO_MIXER_ENUM)
   1465 			return EINVAL;
   1466 
   1467 		if (cp->un.ord)
   1468 			/* Enable monitor */
   1469 			ess_set_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL,
   1470 					  ESS_AUDIO_CTRL_MONITOR);
   1471 		else
   1472 			/* Disable monitor */
   1473 			ess_clear_xreg_bits(sc, ESS_XCMD_AUDIO_CTRL,
   1474 					    ESS_AUDIO_CTRL_MONITOR);
   1475 		break;
   1476 
   1477 	default:
   1478 		return EINVAL;
   1479 	}
   1480 
   1481 	return (0);
   1482 }
   1483 
   1484 int
   1485 ess_get_port(addr, cp)
   1486 	void *addr;
   1487 	mixer_ctrl_t *cp;
   1488 {
   1489 	struct ess_softc *sc = addr;
   1490 
   1491 	DPRINTFN(5,("ess_get_port: port=%d\n", cp->dev));
   1492 
   1493 	switch (cp->dev) {
   1494 	case ESS_DAC_PLAY_VOL:
   1495 	case ESS_MIC_PLAY_VOL:
   1496 	case ESS_LINE_PLAY_VOL:
   1497 	case ESS_SYNTH_PLAY_VOL:
   1498 	case ESS_CD_PLAY_VOL:
   1499 	case ESS_AUXB_PLAY_VOL:
   1500 	case ESS_MASTER_VOL:
   1501 	case ESS_PCSPEAKER_VOL:
   1502 	case ESS_DAC_REC_VOL:
   1503 	case ESS_MIC_REC_VOL:
   1504 	case ESS_LINE_REC_VOL:
   1505 	case ESS_SYNTH_REC_VOL:
   1506 	case ESS_CD_REC_VOL:
   1507 	case ESS_AUXB_REC_VOL:
   1508 	case ESS_RECORD_VOL:
   1509 		if (cp->dev == ESS_PCSPEAKER_VOL &&
   1510 		    cp->un.value.num_channels != 1)
   1511 			return EINVAL;
   1512 
   1513 		switch (cp->un.value.num_channels) {
   1514 		case 1:
   1515 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1516 				sc->gain[cp->dev][ESS_LEFT];
   1517 			break;
   1518 		case 2:
   1519 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
   1520 				sc->gain[cp->dev][ESS_LEFT];
   1521 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
   1522 				sc->gain[cp->dev][ESS_RIGHT];
   1523 			break;
   1524 		default:
   1525 			return EINVAL;
   1526 		}
   1527 		break;
   1528 
   1529 	case ESS_MIC_PREAMP:
   1530 		cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_PREAMP_CTRL) &
   1531 			      ESS_PREAMP_CTRL_ENABLE) ? 1 : 0;
   1532 		break;
   1533 
   1534 	case ESS_RECORD_SOURCE:
   1535 		cp->un.mask = sc->in_mask;
   1536 		break;
   1537 
   1538 	case ESS_RECORD_MONITOR:
   1539 		cp->un.ord = (ess_read_x_reg(sc, ESS_XCMD_AUDIO_CTRL) &
   1540 			      ESS_AUDIO_CTRL_MONITOR) ? 1 : 0;
   1541 		break;
   1542 
   1543 	default:
   1544 		return EINVAL;
   1545 	}
   1546 
   1547 	return (0);
   1548 }
   1549 
   1550 int
   1551 ess_query_devinfo(addr, dip)
   1552 	void *addr;
   1553 	mixer_devinfo_t *dip;
   1554 {
   1555 #ifdef AUDIO_DEBUG
   1556 	struct ess_softc *sc = addr;
   1557 #endif
   1558 
   1559 	DPRINTFN(5,("ess_query_devinfo: model=%d index=%d\n",
   1560 		    sc->sc_model, dip->index));
   1561 
   1562 	/*
   1563 	 * REVISIT: There are some slight differences between the
   1564 	 *          mixers on the different ESS chips, which can
   1565 	 *          be sorted out using the chip model rather than a
   1566 	 *          separate mixer model.
   1567 	 *          This is currently coded assuming an ES1887; we
   1568 	 *          need to work out which bits are not applicable to
   1569 	 *          the other models (1888 and 888).
   1570 	 */
   1571 	switch (dip->index) {
   1572 	case ESS_DAC_PLAY_VOL:
   1573 		dip->type = AUDIO_MIXER_VALUE;
   1574 		dip->mixer_class = ESS_INPUT_CLASS;
   1575 		dip->prev = AUDIO_MIXER_LAST;
   1576 		dip->next = AUDIO_MIXER_LAST;
   1577 		strcpy(dip->label.name, AudioNdac);
   1578 		dip->un.v.num_channels = 2;
   1579 		strcpy(dip->un.v.units.name, AudioNvolume);
   1580 		return (0);
   1581 
   1582 	case ESS_MIC_PLAY_VOL:
   1583 		dip->type = AUDIO_MIXER_VALUE;
   1584 		dip->mixer_class = ESS_INPUT_CLASS;
   1585 		dip->prev = AUDIO_MIXER_LAST;
   1586 		dip->next = ESS_MIC_PREAMP;
   1587 		strcpy(dip->label.name, AudioNmicrophone);
   1588 		dip->un.v.num_channels = 2;
   1589 		strcpy(dip->un.v.units.name, AudioNvolume);
   1590 		return (0);
   1591 
   1592 	case ESS_LINE_PLAY_VOL:
   1593 		dip->type = AUDIO_MIXER_VALUE;
   1594 		dip->mixer_class = ESS_INPUT_CLASS;
   1595 		dip->prev = AUDIO_MIXER_LAST;
   1596 		dip->next = AUDIO_MIXER_LAST;
   1597 		strcpy(dip->label.name, AudioNline);
   1598 		dip->un.v.num_channels = 2;
   1599 		strcpy(dip->un.v.units.name, AudioNvolume);
   1600 		return (0);
   1601 
   1602 	case ESS_SYNTH_PLAY_VOL:
   1603 		dip->type = AUDIO_MIXER_VALUE;
   1604 		dip->mixer_class = ESS_INPUT_CLASS;
   1605 		dip->prev = AUDIO_MIXER_LAST;
   1606 		dip->next = AUDIO_MIXER_LAST;
   1607 		strcpy(dip->label.name, AudioNfmsynth);
   1608 		dip->un.v.num_channels = 2;
   1609 		strcpy(dip->un.v.units.name, AudioNvolume);
   1610 		return (0);
   1611 
   1612 	case ESS_CD_PLAY_VOL:
   1613 		dip->type = AUDIO_MIXER_VALUE;
   1614 		dip->mixer_class = ESS_INPUT_CLASS;
   1615 		dip->prev = AUDIO_MIXER_LAST;
   1616 		dip->next = AUDIO_MIXER_LAST;
   1617 		strcpy(dip->label.name, AudioNcd);
   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->type = AUDIO_MIXER_VALUE;
   1624 		dip->mixer_class = ESS_INPUT_CLASS;
   1625 		dip->prev = AUDIO_MIXER_LAST;
   1626 		dip->next = AUDIO_MIXER_LAST;
   1627 		strcpy(dip->label.name, "auxb");
   1628 		dip->un.v.num_channels = 2;
   1629 		strcpy(dip->un.v.units.name, AudioNvolume);
   1630 		return (0);
   1631 
   1632 	case ESS_INPUT_CLASS:
   1633 		dip->type = AUDIO_MIXER_CLASS;
   1634 		dip->mixer_class = ESS_INPUT_CLASS;
   1635 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1636 		strcpy(dip->label.name, AudioCinputs);
   1637 		return (0);
   1638 
   1639 
   1640 	case ESS_MASTER_VOL:
   1641 		dip->type = AUDIO_MIXER_VALUE;
   1642 		dip->mixer_class = ESS_OUTPUT_CLASS;
   1643 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1644 		strcpy(dip->label.name, AudioNmaster);
   1645 		dip->un.v.num_channels = 2;
   1646 		strcpy(dip->un.v.units.name, AudioNvolume);
   1647 		return (0);
   1648 
   1649 	case ESS_PCSPEAKER_VOL:
   1650 		dip->type = AUDIO_MIXER_VALUE;
   1651 		dip->mixer_class = ESS_OUTPUT_CLASS;
   1652 		dip->prev = AUDIO_MIXER_LAST;
   1653 		dip->next = AUDIO_MIXER_LAST;
   1654 		strcpy(dip->label.name, "pc_speaker");
   1655 		dip->un.v.num_channels = 1;
   1656 		strcpy(dip->un.v.units.name, AudioNvolume);
   1657 		return (0);
   1658 
   1659 	case ESS_OUTPUT_CLASS:
   1660 		dip->type = AUDIO_MIXER_CLASS;
   1661 		dip->mixer_class = ESS_OUTPUT_CLASS;
   1662 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1663 		strcpy(dip->label.name, AudioCoutputs);
   1664 		return (0);
   1665 
   1666 
   1667 	case ESS_DAC_REC_VOL:
   1668 		dip->type = AUDIO_MIXER_VALUE;
   1669 		dip->mixer_class = ESS_RECORD_CLASS;
   1670 		dip->prev = AUDIO_MIXER_LAST;
   1671 		dip->next = AUDIO_MIXER_LAST;
   1672 		strcpy(dip->label.name, AudioNdac);
   1673 		dip->un.v.num_channels = 2;
   1674 		strcpy(dip->un.v.units.name, AudioNvolume);
   1675 		return (0);
   1676 
   1677 	case ESS_MIC_REC_VOL:
   1678 		dip->type = AUDIO_MIXER_VALUE;
   1679 		dip->mixer_class = ESS_RECORD_CLASS;
   1680 		dip->prev = AUDIO_MIXER_LAST;
   1681 		dip->next = AUDIO_MIXER_LAST;
   1682 		strcpy(dip->label.name, AudioNmicrophone);
   1683 		dip->un.v.num_channels = 2;
   1684 		strcpy(dip->un.v.units.name, AudioNvolume);
   1685 		return (0);
   1686 
   1687 	case ESS_LINE_REC_VOL:
   1688 		dip->type = AUDIO_MIXER_VALUE;
   1689 		dip->mixer_class = ESS_RECORD_CLASS;
   1690 		dip->prev = AUDIO_MIXER_LAST;
   1691 		dip->next = AUDIO_MIXER_LAST;
   1692 		strcpy(dip->label.name, AudioNline);
   1693 		dip->un.v.num_channels = 2;
   1694 		strcpy(dip->un.v.units.name, AudioNvolume);
   1695 		return (0);
   1696 
   1697 	case ESS_SYNTH_REC_VOL:
   1698 		dip->type = AUDIO_MIXER_VALUE;
   1699 		dip->mixer_class = ESS_RECORD_CLASS;
   1700 		dip->prev = AUDIO_MIXER_LAST;
   1701 		dip->next = AUDIO_MIXER_LAST;
   1702 		strcpy(dip->label.name, AudioNfmsynth);
   1703 		dip->un.v.num_channels = 2;
   1704 		strcpy(dip->un.v.units.name, AudioNvolume);
   1705 		return (0);
   1706 
   1707 	case ESS_CD_REC_VOL:
   1708 		dip->type = AUDIO_MIXER_VALUE;
   1709 		dip->mixer_class = ESS_RECORD_CLASS;
   1710 		dip->prev = AUDIO_MIXER_LAST;
   1711 		dip->next = AUDIO_MIXER_LAST;
   1712 		strcpy(dip->label.name, AudioNcd);
   1713 		dip->un.v.num_channels = 2;
   1714 		strcpy(dip->un.v.units.name, AudioNvolume);
   1715 		return (0);
   1716 
   1717 	case ESS_AUXB_REC_VOL:
   1718 		dip->type = AUDIO_MIXER_VALUE;
   1719 		dip->mixer_class = ESS_RECORD_CLASS;
   1720 		dip->prev = AUDIO_MIXER_LAST;
   1721 		dip->next = AUDIO_MIXER_LAST;
   1722 		strcpy(dip->label.name, "auxb");
   1723 		dip->un.v.num_channels = 2;
   1724 		strcpy(dip->un.v.units.name, AudioNvolume);
   1725 		return (0);
   1726 
   1727 	case ESS_MIC_PREAMP:
   1728 		dip->type = AUDIO_MIXER_ENUM;
   1729 		dip->mixer_class = ESS_INPUT_CLASS;
   1730 		dip->prev = ESS_MIC_PLAY_VOL;
   1731 		dip->next = AUDIO_MIXER_LAST;
   1732 		strcpy(dip->label.name, AudioNpreamp);
   1733 		dip->un.e.num_mem = 2;
   1734 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1735 		dip->un.e.member[0].ord = 0;
   1736 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1737 		dip->un.e.member[1].ord = 1;
   1738 		return (0);
   1739 
   1740 	case ESS_RECORD_VOL:
   1741 		dip->type = AUDIO_MIXER_VALUE;
   1742 		dip->mixer_class = ESS_RECORD_CLASS;
   1743 		dip->prev = AUDIO_MIXER_LAST;
   1744 		dip->next = AUDIO_MIXER_LAST;
   1745 		strcpy(dip->label.name, AudioNrecord);
   1746 		dip->un.v.num_channels = 2;
   1747 		strcpy(dip->un.v.units.name, AudioNvolume);
   1748 		return (0);
   1749 
   1750 	case ESS_RECORD_SOURCE:
   1751 		dip->mixer_class = ESS_RECORD_CLASS;
   1752 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1753 		strcpy(dip->label.name, AudioNsource);
   1754 		dip->type = AUDIO_MIXER_SET;
   1755 		dip->un.s.num_mem = 6;
   1756 		strcpy(dip->un.s.member[0].label.name, AudioNdac);
   1757 		dip->un.s.member[0].mask = 1 << ESS_DAC_REC_VOL;
   1758 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
   1759 		dip->un.s.member[1].mask = 1 << ESS_MIC_REC_VOL;
   1760 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1761 		dip->un.s.member[2].mask = 1 << ESS_LINE_REC_VOL;
   1762 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1763 		dip->un.s.member[3].mask = 1 << ESS_SYNTH_REC_VOL;
   1764 		strcpy(dip->un.s.member[4].label.name, AudioNcd);
   1765 		dip->un.s.member[4].mask = 1 << ESS_CD_REC_VOL;
   1766 		strcpy(dip->un.s.member[5].label.name, "auxb");
   1767 		dip->un.s.member[5].mask = 1 << ESS_AUXB_REC_VOL;
   1768 		return (0);
   1769 
   1770 	case ESS_RECORD_CLASS:
   1771 		dip->type = AUDIO_MIXER_CLASS;
   1772 		dip->mixer_class = ESS_RECORD_CLASS;
   1773 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1774 		strcpy(dip->label.name, AudioCrecord);
   1775 		return (0);
   1776 
   1777 
   1778 	case ESS_RECORD_MONITOR:
   1779 		dip->mixer_class = ESS_MONITOR_CLASS;
   1780 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1781 		strcpy(dip->label.name, AudioNmonitor);
   1782 		dip->type = AUDIO_MIXER_ENUM;
   1783 		dip->un.e.num_mem = 2;
   1784 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1785 		dip->un.e.member[0].ord = 0;
   1786 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1787 		dip->un.e.member[1].ord = 1;
   1788 		return (0);
   1789 
   1790 	case ESS_MONITOR_CLASS:
   1791 		dip->type = AUDIO_MIXER_CLASS;
   1792 		dip->mixer_class = ESS_MONITOR_CLASS;
   1793 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1794 		strcpy(dip->label.name, AudioCmonitor);
   1795 		return (0);
   1796 	}
   1797 
   1798 	return (ENXIO);
   1799 }
   1800 
   1801 void *
   1802 ess_malloc(addr, size, pool, flags)
   1803 	void *addr;
   1804 	unsigned long size;
   1805 	int pool;
   1806 	int flags;
   1807 {
   1808 	struct ess_softc *sc = addr;
   1809 
   1810 	return isa_malloc(sc->sc_ic, 4, size, pool, flags);
   1811 }
   1812 
   1813 void
   1814 ess_free(addr, ptr, pool)
   1815 	void *addr;
   1816 	void *ptr;
   1817 	int pool;
   1818 {
   1819 	isa_free(ptr, pool);
   1820 }
   1821 
   1822 unsigned long
   1823 ess_round(addr, size)
   1824 	void *addr;
   1825 	unsigned long size;
   1826 {
   1827 	if (size > MAX_ISADMA)
   1828 		size = MAX_ISADMA;
   1829 	return size;
   1830 }
   1831 
   1832 int
   1833 ess_mappage(addr, mem, off, prot)
   1834 	void *addr;
   1835         void *mem;
   1836         int off;
   1837 	int prot;
   1838 {
   1839 	return (isa_mappage(mem, off, prot));
   1840 }
   1841 
   1842 int
   1843 ess_get_props(addr)
   1844 	void *addr;
   1845 {
   1846 	struct ess_softc *sc = addr;
   1847 
   1848 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1849 	       (sc->sc_in.drq != sc->sc_out.drq ? AUDIO_PROP_FULLDUPLEX : 0));
   1850 }
   1851 
   1852 /* ============================================
   1853  * Generic functions for ess, not used by audio h/w i/f
   1854  * =============================================
   1855  */
   1856 
   1857 /*
   1858  * Reset the chip.
   1859  * Return non-zero if the chip isn't detected.
   1860  */
   1861 int
   1862 ess_reset(sc)
   1863 	struct ess_softc *sc;
   1864 {
   1865 	bus_space_tag_t iot = sc->sc_iot;
   1866 	bus_space_handle_t ioh = sc->sc_ioh;
   1867 
   1868 	sc->sc_in.intr = 0;
   1869 	if (sc->sc_in.active) {
   1870 		isa_dmaabort(sc->sc_ic, sc->sc_in.drq);
   1871 		sc->sc_in.active = 0;
   1872 	}
   1873 
   1874 	sc->sc_out.intr = 0;
   1875 	if (sc->sc_out.active) {
   1876 		isa_dmaabort(sc->sc_ic, sc->sc_out.drq);
   1877 		sc->sc_out.active = 0;
   1878 	}
   1879 
   1880 	EWRITE1(iot, ioh, ESS_DSP_RESET, ESS_RESET_EXT);
   1881 	delay(10000);
   1882 	EWRITE1(iot, ioh, ESS_DSP_RESET, 0);
   1883 	if (ess_rdsp(sc) != ESS_MAGIC)
   1884 		return (1);
   1885 
   1886 	/* Enable access to the ESS extension commands. */
   1887 	ess_wdsp(sc, ESS_ACMD_ENABLE_EXT);
   1888 
   1889 	return (0);
   1890 }
   1891 
   1892 void
   1893 ess_set_gain(sc, port, on)
   1894 	struct ess_softc *sc;
   1895 	int port;
   1896 	int on;
   1897 {
   1898 	int gain, left, right;
   1899 	int mix;
   1900 	int src;
   1901 	int stereo;
   1902 
   1903 	/*
   1904 	 * Most gain controls are found in the mixer registers and
   1905 	 * are stereo. Any that are not, must set mix and stereo as
   1906 	 * required.
   1907 	 */
   1908 	mix = 1;
   1909 	stereo = 1;
   1910 
   1911 	switch (port) {
   1912 	case ESS_MASTER_VOL:
   1913 		src = 0x32;
   1914 		break;
   1915 	case ESS_DAC_PLAY_VOL:
   1916 		src = 0x7C;
   1917 		break;
   1918 	case ESS_MIC_PLAY_VOL:
   1919 		src = 0x1A;
   1920 		break;
   1921 	case ESS_LINE_PLAY_VOL:
   1922 		src = 0x3E;
   1923 		break;
   1924 	case ESS_SYNTH_PLAY_VOL:
   1925 		src = 0x36;
   1926 		break;
   1927 	case ESS_CD_PLAY_VOL:
   1928 		src = 0x38;
   1929 		break;
   1930 	case ESS_AUXB_PLAY_VOL:
   1931 		src = 0x3A;
   1932 		break;
   1933 	case ESS_PCSPEAKER_VOL:
   1934 		src = 0x3C;
   1935 		stereo = 0;
   1936 		break;
   1937 	case ESS_DAC_REC_VOL:
   1938 		src = 0x69;
   1939 		break;
   1940 	case ESS_MIC_REC_VOL:
   1941 		src = 0x68;
   1942 		break;
   1943 	case ESS_LINE_REC_VOL:
   1944 		src = 0x6E;
   1945 		break;
   1946 	case ESS_SYNTH_REC_VOL:
   1947 		src = 0x6B;
   1948 		break;
   1949 	case ESS_CD_REC_VOL:
   1950 		src = 0x6A;
   1951 		break;
   1952 	case ESS_AUXB_REC_VOL:
   1953 		src = 0x6C;
   1954 		break;
   1955 	case ESS_RECORD_VOL:
   1956 		src = 0xB4;
   1957 		mix = 0;
   1958 		break;
   1959 	default:
   1960 		return;
   1961 	}
   1962 
   1963 	if (on) {
   1964 		left = sc->gain[port][ESS_LEFT];
   1965 		right = sc->gain[port][ESS_RIGHT];
   1966 	} else {
   1967 		left = right = 0;
   1968 	}
   1969 
   1970 	if (stereo)
   1971 		gain = ESS_STEREO_GAIN(left, right);
   1972 	else
   1973 		gain = ESS_MONO_GAIN(left);
   1974 
   1975 	if (mix)
   1976 		ess_write_mix_reg(sc, src, gain);
   1977 	else
   1978 		ess_write_x_reg(sc, src, gain);
   1979 }
   1980 
   1981 int
   1982 ess_set_in_ports(sc, mask)
   1983 	struct ess_softc *sc;
   1984 	int mask;
   1985 {
   1986 	mixer_devinfo_t di;
   1987 	int i;
   1988 	int port;
   1989 	int tmp;
   1990 
   1991 	DPRINTF(("ess_set_in_ports: mask=0x%x\n", mask));
   1992 
   1993 	/*
   1994 	 * Get the device info for the record source control,
   1995 	 * including the list of available sources.
   1996 	 */
   1997 	di.index = ESS_RECORD_SOURCE;
   1998 	if (ess_query_devinfo(sc, &di))
   1999 		return EINVAL;
   2000 
   2001 	/*
   2002 	 * Set or disable the record volume control for each of the
   2003 	 * possible sources.
   2004 	 */
   2005 	for (i = 0; i < di.un.s.num_mem; i++)
   2006 	{
   2007 		/*
   2008 		 * Calculate the source port number from its mask.
   2009 		 */
   2010 		tmp = di.un.s.member[i].mask >> 1;
   2011 		for (port = 0; tmp; port++) {
   2012 			tmp >>= 1;
   2013 		}
   2014 
   2015 		/*
   2016 		 * Set the source gain:
   2017 		 *	to the current value if source is enabled
   2018 		 *	to zero if source is disabled
   2019 		 */
   2020 		ess_set_gain(sc, port, mask & di.un.s.member[i].mask);
   2021 	}
   2022 
   2023 	sc->in_mask = mask;
   2024 
   2025 	/*
   2026 	 * We have to fake a single port since the upper layer expects
   2027 	 * one only. We choose the lowest numbered port that is enabled.
   2028 	 */
   2029 	for(i = 0; i < ESS_NPORT; i++) {
   2030 		if (mask & (1 << i)) {
   2031 			sc->in_port = i;
   2032 			break;
   2033 		}
   2034 	}
   2035 
   2036 	return (0);
   2037 }
   2038 
   2039 void
   2040 ess_speaker_on(sc)
   2041 	struct ess_softc *sc;
   2042 {
   2043 	/* Disable mute on left- and right-master volume. */
   2044 	ess_clear_mreg_bits(sc, 0x60, 0x40);
   2045 	ess_clear_mreg_bits(sc, 0x62, 0x40);
   2046 }
   2047 
   2048 void
   2049 ess_speaker_off(sc)
   2050 	struct ess_softc *sc;
   2051 {
   2052 	/* Enable mute on left- and right-master volume. */
   2053 	ess_set_mreg_bits(sc, 0x60, 0x40);
   2054 	ess_set_mreg_bits(sc, 0x62, 0x40);
   2055 }
   2056 
   2057 /*
   2058  * Calculate the time constant for the requested sampling rate.
   2059  */
   2060 u_int
   2061 ess_srtotc(rate)
   2062 	u_int rate;
   2063 {
   2064 	u_int tc;
   2065 
   2066 	/* The following formulae are from the ESS data sheet. */
   2067 	if (rate <= 22050)
   2068 		tc = 128 - 397700L / rate;
   2069 	else
   2070 		tc = 256 - 795500L / rate;
   2071 
   2072 	return (tc);
   2073 }
   2074 
   2075 
   2076 /*
   2077  * Calculate the filter constant for the reuqested sampling rate.
   2078  */
   2079 u_int
   2080 ess_srtofc(rate)
   2081 	u_int rate;
   2082 {
   2083 	/*
   2084 	 * The following formula is derived from the information in
   2085 	 * the ES1887 data sheet, based on a roll-off frequency of
   2086 	 * 87%.
   2087 	 */
   2088 	return (256 - 200279L / rate);
   2089 }
   2090 
   2091 
   2092 /*
   2093  * Return the status of the DSP.
   2094  */
   2095 u_char
   2096 ess_get_dsp_status(sc)
   2097 	struct ess_softc *sc;
   2098 {
   2099 	bus_space_tag_t iot = sc->sc_iot;
   2100 	bus_space_handle_t ioh = sc->sc_ioh;
   2101 
   2102 	return (EREAD1(iot, ioh, ESS_DSP_RW_STATUS));
   2103 }
   2104 
   2105 
   2106 /*
   2107  * Return the read status of the DSP:	1 -> DSP ready for reading
   2108  *					0 -> DSP not ready for reading
   2109  */
   2110 u_char
   2111 ess_dsp_read_ready(sc)
   2112 	struct ess_softc *sc;
   2113 {
   2114 	return (((ess_get_dsp_status(sc) & ESS_DSP_READ_MASK) ==
   2115 		 ESS_DSP_READ_READY) ? 1 : 0);
   2116 }
   2117 
   2118 
   2119 /*
   2120  * Return the write status of the DSP:	1 -> DSP ready for writing
   2121  *					0 -> DSP not ready for writing
   2122  */
   2123 u_char
   2124 ess_dsp_write_ready(sc)
   2125 	struct ess_softc *sc;
   2126 {
   2127 	return (((ess_get_dsp_status(sc) & ESS_DSP_WRITE_MASK) ==
   2128 		 ESS_DSP_WRITE_READY) ? 1 : 0);
   2129 }
   2130 
   2131 
   2132 /*
   2133  * Read a byte from the DSP.
   2134  */
   2135 int
   2136 ess_rdsp(sc)
   2137 	struct ess_softc *sc;
   2138 {
   2139 	bus_space_tag_t iot = sc->sc_iot;
   2140 	bus_space_handle_t ioh = sc->sc_ioh;
   2141 	int i;
   2142 
   2143 	for (i = ESS_READ_TIMEOUT; i > 0; --i) {
   2144 		if (ess_dsp_read_ready(sc)) {
   2145 			i = EREAD1(iot, ioh, ESS_DSP_READ);
   2146 			DPRINTFN(8,("ess_rdsp() = 0x%02x\n", i));
   2147 			return i;
   2148 		} else
   2149 			delay(10);
   2150 	}
   2151 
   2152 	DPRINTF(("ess_rdsp: timed out\n"));
   2153 	return (-1);
   2154 }
   2155 
   2156 /*
   2157  * Write a byte to the DSP.
   2158  */
   2159 int
   2160 ess_wdsp(sc, v)
   2161 	struct ess_softc *sc;
   2162 	u_char v;
   2163 {
   2164 	bus_space_tag_t iot = sc->sc_iot;
   2165 	bus_space_handle_t ioh = sc->sc_ioh;
   2166 	int i;
   2167 
   2168 	DPRINTFN(8,("ess_wdsp(0x%02x)\n", v));
   2169 
   2170 	for (i = ESS_WRITE_TIMEOUT; i > 0; --i) {
   2171 		if (ess_dsp_write_ready(sc)) {
   2172 			EWRITE1(iot, ioh, ESS_DSP_WRITE, v);
   2173 			return (0);
   2174 		} else
   2175 			delay(10);
   2176 	}
   2177 
   2178 	DPRINTF(("ess_wdsp(0x%02x): timed out\n", v));
   2179 	return (-1);
   2180 }
   2181 
   2182 /*
   2183  * Write a value to one of the ESS extended registers.
   2184  */
   2185 int
   2186 ess_write_x_reg(sc, reg, val)
   2187 	struct ess_softc *sc;
   2188 	u_char reg;
   2189 	u_char val;
   2190 {
   2191 	int error;
   2192 
   2193 	DPRINTFN(2,("ess_write_x_reg: %02x=%02x\n", reg, val));
   2194 	if ((error = ess_wdsp(sc, reg)) == 0)
   2195 		error = ess_wdsp(sc, val);
   2196 
   2197 	return error;
   2198 }
   2199 
   2200 /*
   2201  * Read the value of one of the ESS extended registers.
   2202  */
   2203 u_char
   2204 ess_read_x_reg(sc, reg)
   2205 	struct ess_softc *sc;
   2206 	u_char reg;
   2207 {
   2208 	int error;
   2209 	int val;
   2210 
   2211 	if ((error = ess_wdsp(sc, 0xC0)) == 0)
   2212 		error = ess_wdsp(sc, reg);
   2213 	if (error)
   2214 		DPRINTF(("Error reading extended register 0x%02x\n", reg));
   2215 /* REVISIT: what if an error is returned above? */
   2216 	val = ess_rdsp(sc);
   2217 	DPRINTFN(2,("ess_write_x_reg: %02x=%02x\n", reg, val));
   2218 	return val;
   2219 }
   2220 
   2221 void
   2222 ess_clear_xreg_bits(sc, reg, mask)
   2223 	struct ess_softc *sc;
   2224 	u_char reg;
   2225 	u_char mask;
   2226 {
   2227 	if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) & ~mask) == -1)
   2228 		DPRINTF(("Error clearing bits in extended register 0x%02x\n",
   2229 			 reg));
   2230 }
   2231 
   2232 void
   2233 ess_set_xreg_bits(sc, reg, mask)
   2234 	struct ess_softc *sc;
   2235 	u_char reg;
   2236 	u_char mask;
   2237 {
   2238 	if (ess_write_x_reg(sc, reg, ess_read_x_reg(sc, reg) | mask) == -1)
   2239 		DPRINTF(("Error setting bits in extended register 0x%02x\n",
   2240 			 reg));
   2241 }
   2242 
   2243 
   2244 /*
   2245  * Write a value to one of the ESS mixer registers.
   2246  */
   2247 void
   2248 ess_write_mix_reg(sc, reg, val)
   2249 	struct ess_softc *sc;
   2250 	u_char reg;
   2251 	u_char val;
   2252 {
   2253 	bus_space_tag_t iot = sc->sc_iot;
   2254 	bus_space_handle_t ioh = sc->sc_ioh;
   2255 	int s;
   2256 
   2257 	DPRINTFN(2,("ess_write_mix_reg: %x=%x\n", reg, val));
   2258 
   2259 	s = splaudio();
   2260 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
   2261 	EWRITE1(iot, ioh, ESS_MIX_REG_DATA, val);
   2262 	splx(s);
   2263 }
   2264 
   2265 /*
   2266  * Read the value of one of the ESS mixer registers.
   2267  */
   2268 u_char
   2269 ess_read_mix_reg(sc, reg)
   2270 	struct ess_softc *sc;
   2271 	u_char reg;
   2272 {
   2273 	bus_space_tag_t iot = sc->sc_iot;
   2274 	bus_space_handle_t ioh = sc->sc_ioh;
   2275 	int s;
   2276 	u_char val;
   2277 
   2278 	s = splaudio();
   2279 	EWRITE1(iot, ioh, ESS_MIX_REG_SELECT, reg);
   2280 	val = EREAD1(iot, ioh, ESS_MIX_REG_DATA);
   2281 	splx(s);
   2282 
   2283 	DPRINTFN(2,("ess_read_mix_reg: %x=%x\n", reg, val));
   2284 	return val;
   2285 }
   2286 
   2287 void
   2288 ess_clear_mreg_bits(sc, reg, mask)
   2289 	struct ess_softc *sc;
   2290 	u_char reg;
   2291 	u_char mask;
   2292 {
   2293 	ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) & ~mask);
   2294 }
   2295 
   2296 void
   2297 ess_set_mreg_bits(sc, reg, mask)
   2298 	struct ess_softc *sc;
   2299 	u_char reg;
   2300 	u_char mask;
   2301 {
   2302 	ess_write_mix_reg(sc, reg, ess_read_mix_reg(sc, reg) | mask);
   2303 }
   2304