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