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