Home | History | Annotate | Line # | Download | only in pci
eso.c revision 1.43
      1 /*	$NetBSD: eso.c,v 1.43 2006/08/30 01:14:24 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2000, 2004 Klaus J. Klein
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * ESS Technology Inc. Solo-1 PCI AudioDrive (ES1938/1946) device driver.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: eso.c,v 1.43 2006/08/30 01:14:24 christos Exp $");
     37 
     38 #include "mpu.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/malloc.h>
     44 #include <sys/device.h>
     45 #include <sys/proc.h>
     46 
     47 #include <dev/pci/pcidevs.h>
     48 #include <dev/pci/pcivar.h>
     49 
     50 #include <sys/audioio.h>
     51 #include <dev/audio_if.h>
     52 #include <dev/midi_if.h>
     53 
     54 #include <dev/mulaw.h>
     55 #include <dev/auconv.h>
     56 
     57 #include <dev/ic/mpuvar.h>
     58 #include <dev/ic/i8237reg.h>
     59 #include <dev/pci/esoreg.h>
     60 #include <dev/pci/esovar.h>
     61 
     62 #include <machine/bus.h>
     63 #include <machine/intr.h>
     64 
     65 /*
     66  * XXX Work around the 24-bit implementation limit of the Audio 1 DMA
     67  * XXX engine by allocating through the ISA DMA tag.
     68  */
     69 #if defined(amd64) || defined(i386)
     70 #include "isa.h"
     71 #if NISA > 0
     72 #include <dev/isa/isavar.h>
     73 #endif
     74 #endif
     75 
     76 #if defined(AUDIO_DEBUG) || defined(DEBUG)
     77 #define DPRINTF(x) printf x
     78 #else
     79 #define DPRINTF(x)
     80 #endif
     81 
     82 struct eso_dma {
     83 	bus_dma_tag_t		ed_dmat;
     84 	bus_dmamap_t		ed_map;
     85 	caddr_t			ed_addr;
     86 	bus_dma_segment_t	ed_segs[1];
     87 	int			ed_nsegs;
     88 	size_t			ed_size;
     89 	struct eso_dma *	ed_next;
     90 };
     91 
     92 #define KVADDR(dma)	((void *)(dma)->ed_addr)
     93 #define DMAADDR(dma)	((dma)->ed_map->dm_segs[0].ds_addr)
     94 
     95 /* Autoconfiguration interface */
     96 static int eso_match(struct device *, struct cfdata *, void *);
     97 static void eso_attach(struct device *, struct device *, void *);
     98 static void eso_defer(struct device *);
     99 static int eso_print(void *, const char *);
    100 
    101 CFATTACH_DECL(eso, sizeof (struct eso_softc),
    102     eso_match, eso_attach, NULL, NULL);
    103 
    104 /* PCI interface */
    105 static int eso_intr(void *);
    106 
    107 /* MI audio layer interface */
    108 static int	eso_query_encoding(void *, struct audio_encoding *);
    109 static int	eso_set_params(void *, int, int, audio_params_t *,
    110 		    audio_params_t *, stream_filter_list_t *,
    111 		    stream_filter_list_t *);
    112 static int	eso_round_blocksize(void *, int, int, const audio_params_t *);
    113 static int	eso_halt_output(void *);
    114 static int	eso_halt_input(void *);
    115 static int	eso_getdev(void *, struct audio_device *);
    116 static int	eso_set_port(void *, mixer_ctrl_t *);
    117 static int	eso_get_port(void *, mixer_ctrl_t *);
    118 static int	eso_query_devinfo(void *, mixer_devinfo_t *);
    119 static void *	eso_allocm(void *, int, size_t, struct malloc_type *, int);
    120 static void	eso_freem(void *, void *, struct malloc_type *);
    121 static size_t	eso_round_buffersize(void *, int, size_t);
    122 static paddr_t	eso_mappage(void *, void *, off_t, int);
    123 static int	eso_get_props(void *);
    124 static int	eso_trigger_output(void *, void *, void *, int,
    125 		    void (*)(void *), void *, const audio_params_t *);
    126 static int	eso_trigger_input(void *, void *, void *, int,
    127 		    void (*)(void *), void *, const audio_params_t *);
    128 
    129 static const struct audio_hw_if eso_hw_if = {
    130 	NULL,			/* open */
    131 	NULL,			/* close */
    132 	NULL,			/* drain */
    133 	eso_query_encoding,
    134 	eso_set_params,
    135 	eso_round_blocksize,
    136 	NULL,			/* commit_settings */
    137 	NULL,			/* init_output */
    138 	NULL,			/* init_input */
    139 	NULL,			/* start_output */
    140 	NULL,			/* start_input */
    141 	eso_halt_output,
    142 	eso_halt_input,
    143 	NULL,			/* speaker_ctl */
    144 	eso_getdev,
    145 	NULL,			/* setfd */
    146 	eso_set_port,
    147 	eso_get_port,
    148 	eso_query_devinfo,
    149 	eso_allocm,
    150 	eso_freem,
    151 	eso_round_buffersize,
    152 	eso_mappage,
    153 	eso_get_props,
    154 	eso_trigger_output,
    155 	eso_trigger_input,
    156 	NULL,			/* dev_ioctl */
    157 	NULL,			/* powerstate */
    158 };
    159 
    160 static const char * const eso_rev2model[] = {
    161 	"ES1938",
    162 	"ES1946",
    163 	"ES1946 Revision E"
    164 };
    165 
    166 #define ESO_NFORMATS	8
    167 static const struct audio_format eso_formats[ESO_NFORMATS] = {
    168 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    169 	 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
    170 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    171 	 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
    172 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
    173 	 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
    174 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
    175 	 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
    176 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
    177 	 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
    178 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
    179 	 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}},
    180 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    181 	 2, AUFMT_STEREO, 0, {ESO_MINRATE, ESO_MAXRATE}},
    182 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    183 	 1, AUFMT_MONAURAL, 0, {ESO_MINRATE, ESO_MAXRATE}}
    184 };
    185 
    186 
    187 /*
    188  * Utility routines
    189  */
    190 /* Register access etc. */
    191 static uint8_t	eso_read_ctlreg(struct eso_softc *, uint8_t);
    192 static uint8_t	eso_read_mixreg(struct eso_softc *, uint8_t);
    193 static uint8_t	eso_read_rdr(struct eso_softc *);
    194 static void	eso_reload_master_vol(struct eso_softc *);
    195 static int	eso_reset(struct eso_softc *);
    196 static void	eso_set_gain(struct eso_softc *, unsigned int);
    197 static int	eso_set_recsrc(struct eso_softc *, unsigned int);
    198 static int	eso_set_monooutsrc(struct eso_softc *, unsigned int);
    199 static int	eso_set_monoinbypass(struct eso_softc *, unsigned int);
    200 static int	eso_set_preamp(struct eso_softc *, unsigned int);
    201 static void	eso_write_cmd(struct eso_softc *, uint8_t);
    202 static void	eso_write_ctlreg(struct eso_softc *, uint8_t, uint8_t);
    203 static void	eso_write_mixreg(struct eso_softc *, uint8_t, uint8_t);
    204 /* DMA memory allocation */
    205 static int	eso_allocmem(struct eso_softc *, size_t, size_t, size_t,
    206 		    int, int, struct eso_dma *);
    207 static void	eso_freemem(struct eso_dma *);
    208 
    209 
    210 static int
    211 eso_match(struct device *parent, struct cfdata *match, void *aux)
    212 {
    213 	struct pci_attach_args *pa;
    214 
    215 	pa = aux;
    216 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ESSTECH &&
    217 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ESSTECH_SOLO1)
    218 		return 1;
    219 
    220 	return 0;
    221 }
    222 
    223 static void
    224 eso_attach(struct device *parent, struct device *self, void *aux)
    225 {
    226 	struct eso_softc *sc;
    227 	struct pci_attach_args *pa;
    228 	struct audio_attach_args aa;
    229 	pci_intr_handle_t ih;
    230 	bus_addr_t vcbase;
    231 	const char *intrstring;
    232 	int idx;
    233 	uint8_t a2mode, mvctl;
    234 
    235 	sc = (struct eso_softc *)self;
    236 	pa = aux;
    237 	aprint_naive(": Audio controller\n");
    238 
    239 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    240 
    241 	aprint_normal(": ESS Solo-1 PCI AudioDrive ");
    242 	if (sc->sc_revision <
    243 	    sizeof (eso_rev2model) / sizeof (eso_rev2model[0]))
    244 		aprint_normal("%s\n", eso_rev2model[sc->sc_revision]);
    245 	else
    246 		aprint_normal("(unknown rev. 0x%02x)\n", sc->sc_revision);
    247 
    248 	/* Map I/O registers. */
    249 	if (pci_mapreg_map(pa, ESO_PCI_BAR_IO, PCI_MAPREG_TYPE_IO, 0,
    250 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    251 		aprint_error("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
    252 		return;
    253 	}
    254 	if (pci_mapreg_map(pa, ESO_PCI_BAR_SB, PCI_MAPREG_TYPE_IO, 0,
    255 	    &sc->sc_sb_iot, &sc->sc_sb_ioh, NULL, NULL)) {
    256 		aprint_error("%s: can't map SB I/O space\n",
    257 		    sc->sc_dev.dv_xname);
    258 		return;
    259 	}
    260 	if (pci_mapreg_map(pa, ESO_PCI_BAR_VC, PCI_MAPREG_TYPE_IO, 0,
    261 	    &sc->sc_dmac_iot, &sc->sc_dmac_ioh, &vcbase, &sc->sc_vcsize)) {
    262 		aprint_error("%s: can't map VC I/O space\n",
    263 		    sc->sc_dev.dv_xname);
    264 		/* Don't bail out yet: we can map it later, see below. */
    265 		vcbase = 0;
    266 		sc->sc_vcsize = 0x10; /* From the data sheet. */
    267 	}
    268 	if (pci_mapreg_map(pa, ESO_PCI_BAR_MPU, PCI_MAPREG_TYPE_IO, 0,
    269 	    &sc->sc_mpu_iot, &sc->sc_mpu_ioh, NULL, NULL)) {
    270 		aprint_error("%s: can't map MPU I/O space\n",
    271 		    sc->sc_dev.dv_xname);
    272 		return;
    273 	}
    274 	if (pci_mapreg_map(pa, ESO_PCI_BAR_GAME, PCI_MAPREG_TYPE_IO, 0,
    275 	    &sc->sc_game_iot, &sc->sc_game_ioh, NULL, NULL)) {
    276 		aprint_error("%s: can't map Game I/O space\n",
    277 		    sc->sc_dev.dv_xname);
    278 		return;
    279 	}
    280 
    281 	sc->sc_dmat = pa->pa_dmat;
    282 	sc->sc_dmas = NULL;
    283 	sc->sc_dmac_configured = 0;
    284 
    285 	/* Enable bus mastering. */
    286 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    287 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    288 	    PCI_COMMAND_MASTER_ENABLE);
    289 
    290 	/* Reset the device; bail out upon failure. */
    291 	if (eso_reset(sc) != 0) {
    292 		aprint_error("%s: can't reset\n", sc->sc_dev.dv_xname);
    293 		return;
    294 	}
    295 
    296 	/* Select the DMA/IRQ policy: DDMA, ISA IRQ emulation disabled. */
    297 	pci_conf_write(pa->pa_pc, pa->pa_tag, ESO_PCI_S1C,
    298 	    pci_conf_read(pa->pa_pc, pa->pa_tag, ESO_PCI_S1C) &
    299 	    ~(ESO_PCI_S1C_IRQP_MASK | ESO_PCI_S1C_DMAP_MASK));
    300 
    301 	/* Enable the relevant (DMA) interrupts. */
    302 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL,
    303 	    ESO_IO_IRQCTL_A1IRQ | ESO_IO_IRQCTL_A2IRQ | ESO_IO_IRQCTL_HVIRQ |
    304 	    ESO_IO_IRQCTL_MPUIRQ);
    305 
    306 	/* Set up A1's sample rate generator for new-style parameters. */
    307 	a2mode = eso_read_mixreg(sc, ESO_MIXREG_A2MODE);
    308 	a2mode |= ESO_MIXREG_A2MODE_NEWA1 | ESO_MIXREG_A2MODE_ASYNC;
    309 	eso_write_mixreg(sc, ESO_MIXREG_A2MODE, a2mode);
    310 
    311 	/* Slave Master Volume to Hardware Volume Control Counter, unmask IRQ.*/
    312 	mvctl = eso_read_mixreg(sc, ESO_MIXREG_MVCTL);
    313 	mvctl &= ~ESO_MIXREG_MVCTL_SPLIT;
    314 	mvctl |= ESO_MIXREG_MVCTL_HVIRQM;
    315 	eso_write_mixreg(sc, ESO_MIXREG_MVCTL, mvctl);
    316 
    317 	/* Set mixer regs to something reasonable, needs work. */
    318 	sc->sc_recmon = sc->sc_spatializer = sc->sc_mvmute = 0;
    319 	eso_set_monooutsrc(sc, ESO_MIXREG_MPM_MOMUTE);
    320 	eso_set_monoinbypass(sc, 0);
    321 	eso_set_preamp(sc, 1);
    322 	for (idx = 0; idx < ESO_NGAINDEVS; idx++) {
    323 		int v;
    324 
    325 		switch (idx) {
    326 		case ESO_MIC_PLAY_VOL:
    327 		case ESO_LINE_PLAY_VOL:
    328 		case ESO_CD_PLAY_VOL:
    329 		case ESO_MONO_PLAY_VOL:
    330 		case ESO_AUXB_PLAY_VOL:
    331 		case ESO_DAC_REC_VOL:
    332 		case ESO_LINE_REC_VOL:
    333 		case ESO_SYNTH_REC_VOL:
    334 		case ESO_CD_REC_VOL:
    335 		case ESO_MONO_REC_VOL:
    336 		case ESO_AUXB_REC_VOL:
    337 		case ESO_SPATIALIZER:
    338 			v = 0;
    339 			break;
    340 		case ESO_MASTER_VOL:
    341 			v = ESO_GAIN_TO_6BIT(AUDIO_MAX_GAIN / 2);
    342 			break;
    343 		default:
    344 			v = ESO_GAIN_TO_4BIT(AUDIO_MAX_GAIN / 2);
    345 			break;
    346 		}
    347 		sc->sc_gain[idx][ESO_LEFT] = sc->sc_gain[idx][ESO_RIGHT] = v;
    348 		eso_set_gain(sc, idx);
    349 	}
    350 	eso_set_recsrc(sc, ESO_MIXREG_ERS_MIC);
    351 
    352 	/* Map and establish the interrupt. */
    353 	if (pci_intr_map(pa, &ih)) {
    354 		aprint_error("%s: couldn't map interrupt\n",
    355 		    sc->sc_dev.dv_xname);
    356 		return;
    357 	}
    358 	intrstring = pci_intr_string(pa->pa_pc, ih);
    359 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO, eso_intr, sc);
    360 	if (sc->sc_ih == NULL) {
    361 		aprint_error("%s: couldn't establish interrupt",
    362 		    sc->sc_dev.dv_xname);
    363 		if (intrstring != NULL)
    364 			aprint_normal(" at %s", intrstring);
    365 		aprint_normal("\n");
    366 		return;
    367 	}
    368 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    369 	    intrstring);
    370 
    371 	/*
    372 	 * Set up the DDMA Control register; a suitable I/O region has been
    373 	 * supposedly mapped in the VC base address register.
    374 	 *
    375 	 * The Solo-1 has an ... interesting silicon bug that causes it to
    376 	 * not respond to I/O space accesses to the Audio 1 DMA controller
    377 	 * if the latter's mapping base address is aligned on a 1K boundary.
    378 	 * As a consequence, it is quite possible for the mapping provided
    379 	 * in the VC BAR to be useless.  To work around this, we defer this
    380 	 * part until all autoconfiguration on our parent bus is completed
    381 	 * and then try to map it ourselves in fulfillment of the constraint.
    382 	 *
    383 	 * According to the register map we may write to the low 16 bits
    384 	 * only, but experimenting has shown we're safe.
    385 	 * -kjk
    386 	 */
    387 	if (ESO_VALID_DDMAC_BASE(vcbase)) {
    388 		pci_conf_write(pa->pa_pc, pa->pa_tag, ESO_PCI_DDMAC,
    389 		    vcbase | ESO_PCI_DDMAC_DE);
    390 		sc->sc_dmac_configured = 1;
    391 
    392 		aprint_normal(
    393 		    "%s: mapping Audio 1 DMA using VC I/O space at 0x%lx\n",
    394 		    sc->sc_dev.dv_xname, (unsigned long)vcbase);
    395 	} else {
    396 		DPRINTF(("%s: VC I/O space at 0x%lx not suitable, deferring\n",
    397 		    sc->sc_dev.dv_xname, (unsigned long)vcbase));
    398 		sc->sc_pa = *pa;
    399 		config_defer(self, eso_defer);
    400 	}
    401 
    402 	audio_attach_mi(&eso_hw_if, sc, &sc->sc_dev);
    403 
    404 	aa.type = AUDIODEV_TYPE_OPL;
    405 	aa.hwif = NULL;
    406 	aa.hdl = NULL;
    407 	(void)config_found(&sc->sc_dev, &aa, audioprint);
    408 
    409 	aa.type = AUDIODEV_TYPE_MPU;
    410 	aa.hwif = NULL;
    411 	aa.hdl = NULL;
    412 	sc->sc_mpudev = config_found(&sc->sc_dev, &aa, audioprint);
    413 	if (sc->sc_mpudev != NULL) {
    414 		/* Unmask the MPU irq. */
    415 		mvctl = eso_read_mixreg(sc, ESO_MIXREG_MVCTL);
    416 		mvctl |= ESO_MIXREG_MVCTL_MPUIRQM;
    417 		eso_write_mixreg(sc, ESO_MIXREG_MVCTL, mvctl);
    418 	}
    419 
    420 	aa.type = AUDIODEV_TYPE_AUX;
    421 	aa.hwif = NULL;
    422 	aa.hdl = NULL;
    423 	(void)config_found(&sc->sc_dev, &aa, eso_print);
    424 }
    425 
    426 static void
    427 eso_defer(struct device *self)
    428 {
    429 	struct eso_softc *sc;
    430 	struct pci_attach_args *pa;
    431 	bus_addr_t addr, start;
    432 
    433 	sc = (struct eso_softc *)self;
    434 	pa = &sc->sc_pa;
    435 	aprint_normal("%s: ", sc->sc_dev.dv_xname);
    436 
    437 	/*
    438 	 * This is outright ugly, but since we must not make assumptions
    439 	 * on the underlying allocator's behaviour it's the most straight-
    440 	 * forward way to implement it.  Note that we skip over the first
    441 	 * 1K region, which is typically occupied by an attached ISA bus.
    442 	 */
    443 	for (start = 0x0400; start < 0xffff; start += 0x0400) {
    444 		if (bus_space_alloc(sc->sc_iot,
    445 		    start + sc->sc_vcsize, start + 0x0400 - 1,
    446 		    sc->sc_vcsize, sc->sc_vcsize, 0, 0, &addr,
    447 		    &sc->sc_dmac_ioh) != 0)
    448 			continue;
    449 
    450 		pci_conf_write(pa->pa_pc, pa->pa_tag, ESO_PCI_DDMAC,
    451 		    addr | ESO_PCI_DDMAC_DE);
    452 		sc->sc_dmac_iot = sc->sc_iot;
    453 		sc->sc_dmac_configured = 1;
    454 		aprint_normal("mapping Audio 1 DMA using I/O space at 0x%lx\n",
    455 		    (unsigned long)addr);
    456 
    457 		return;
    458 	}
    459 
    460 	aprint_error("can't map Audio 1 DMA into I/O space\n");
    461 }
    462 
    463 /* ARGSUSED */
    464 static int
    465 eso_print(void *aux, const char *pnp)
    466 {
    467 
    468 	/* Only joys can attach via this; easy. */
    469 	if (pnp)
    470 		aprint_normal("joy at %s:", pnp);
    471 
    472 	return UNCONF;
    473 }
    474 
    475 static void
    476 eso_write_cmd(struct eso_softc *sc, uint8_t cmd)
    477 {
    478 	int i;
    479 
    480 	/* Poll for busy indicator to become clear. */
    481 	for (i = 0; i < ESO_WDR_TIMEOUT; i++) {
    482 		if ((bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_RSR)
    483 		    & ESO_SB_RSR_BUSY) == 0) {
    484 			bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh,
    485 			    ESO_SB_WDR, cmd);
    486 			return;
    487 		} else {
    488 			delay(10);
    489 		}
    490 	}
    491 
    492 	printf("%s: WDR timeout\n", sc->sc_dev.dv_xname);
    493 	return;
    494 }
    495 
    496 /* Write to a controller register */
    497 static void
    498 eso_write_ctlreg(struct eso_softc *sc, uint8_t reg, uint8_t val)
    499 {
    500 
    501 	/* DPRINTF(("ctlreg 0x%02x = 0x%02x\n", reg, val)); */
    502 
    503 	eso_write_cmd(sc, reg);
    504 	eso_write_cmd(sc, val);
    505 }
    506 
    507 /* Read out the Read Data Register */
    508 static uint8_t
    509 eso_read_rdr(struct eso_softc *sc)
    510 {
    511 	int i;
    512 
    513 	for (i = 0; i < ESO_RDR_TIMEOUT; i++) {
    514 		if (bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
    515 		    ESO_SB_RBSR) & ESO_SB_RBSR_RDAV) {
    516 			return (bus_space_read_1(sc->sc_sb_iot,
    517 			    sc->sc_sb_ioh, ESO_SB_RDR));
    518 		} else {
    519 			delay(10);
    520 		}
    521 	}
    522 
    523 	printf("%s: RDR timeout\n", sc->sc_dev.dv_xname);
    524 	return (-1);
    525 }
    526 
    527 static uint8_t
    528 eso_read_ctlreg(struct eso_softc *sc, uint8_t reg)
    529 {
    530 
    531 	eso_write_cmd(sc, ESO_CMD_RCR);
    532 	eso_write_cmd(sc, reg);
    533 	return eso_read_rdr(sc);
    534 }
    535 
    536 static void
    537 eso_write_mixreg(struct eso_softc *sc, uint8_t reg, uint8_t val)
    538 {
    539 	int s;
    540 
    541 	/* DPRINTF(("mixreg 0x%02x = 0x%02x\n", reg, val)); */
    542 
    543 	s = splaudio();
    544 	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERADDR, reg);
    545 	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERDATA, val);
    546 	splx(s);
    547 }
    548 
    549 static uint8_t
    550 eso_read_mixreg(struct eso_softc *sc, uint8_t reg)
    551 {
    552 	int s;
    553 	uint8_t val;
    554 
    555 	s = splaudio();
    556 	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERADDR, reg);
    557 	val = bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERDATA);
    558 	splx(s);
    559 
    560 	return val;
    561 }
    562 
    563 static int
    564 eso_intr(void *hdl)
    565 {
    566 	struct eso_softc *sc;
    567 	uint8_t irqctl;
    568 
    569 	sc = hdl;
    570 	irqctl = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL);
    571 
    572 	/* If it wasn't ours, that's all she wrote. */
    573 	if ((irqctl & (ESO_IO_IRQCTL_A1IRQ | ESO_IO_IRQCTL_A2IRQ |
    574 	    ESO_IO_IRQCTL_HVIRQ | ESO_IO_IRQCTL_MPUIRQ)) == 0)
    575 		return 0;
    576 
    577 	if (irqctl & ESO_IO_IRQCTL_A1IRQ) {
    578 		/* Clear interrupt. */
    579 		(void)bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
    580 		    ESO_SB_RBSR);
    581 
    582 		if (sc->sc_rintr)
    583 			sc->sc_rintr(sc->sc_rarg);
    584 		else
    585 			wakeup(&sc->sc_rintr);
    586 	}
    587 
    588 	if (irqctl & ESO_IO_IRQCTL_A2IRQ) {
    589 		/*
    590 		 * Clear the A2 IRQ latch: the cached value reflects the
    591 		 * current DAC settings with the IRQ latch bit not set.
    592 		 */
    593 		eso_write_mixreg(sc, ESO_MIXREG_A2C2, sc->sc_a2c2);
    594 
    595 		if (sc->sc_pintr)
    596 			sc->sc_pintr(sc->sc_parg);
    597 		else
    598 			wakeup(&sc->sc_pintr);
    599 	}
    600 
    601 	if (irqctl & ESO_IO_IRQCTL_HVIRQ) {
    602 		/* Clear interrupt. */
    603 		eso_write_mixreg(sc, ESO_MIXREG_CHVIR, ESO_MIXREG_CHVIR_CHVIR);
    604 
    605 		/*
    606 		 * Raise a flag to cause a lazy update of the in-softc gain
    607 		 * values the next time the software mixer is read to keep
    608 		 * interrupt service cost low.  ~0 cannot occur otherwise
    609 		 * as the master volume has a precision of 6 bits only.
    610 		 */
    611 		sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] = (uint8_t)~0;
    612 	}
    613 
    614 #if NMPU > 0
    615 	if ((irqctl & ESO_IO_IRQCTL_MPUIRQ) && sc->sc_mpudev != NULL)
    616 		mpu_intr(sc->sc_mpudev);
    617 #endif
    618 
    619 	return 1;
    620 }
    621 
    622 /* Perform a software reset, including DMA FIFOs. */
    623 static int
    624 eso_reset(struct eso_softc *sc)
    625 {
    626 	int i;
    627 
    628 	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_RESET,
    629 	    ESO_SB_RESET_SW | ESO_SB_RESET_FIFO);
    630 	/* `Delay' suggested in the data sheet. */
    631 	(void)bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_STATUS);
    632 	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_RESET, 0);
    633 
    634 	/* Wait for reset to take effect. */
    635 	for (i = 0; i < ESO_RESET_TIMEOUT; i++) {
    636 		/* Poll for data to become available. */
    637 		if ((bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
    638 		    ESO_SB_RBSR) & ESO_SB_RBSR_RDAV) != 0 &&
    639 		    bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
    640 			ESO_SB_RDR) == ESO_SB_RDR_RESETMAGIC) {
    641 
    642 			/* Activate Solo-1 extension commands. */
    643 			eso_write_cmd(sc, ESO_CMD_EXTENB);
    644 			/* Reset mixer registers. */
    645 			eso_write_mixreg(sc, ESO_MIXREG_RESET,
    646 			    ESO_MIXREG_RESET_RESET);
    647 
    648 			return 0;
    649 		} else {
    650 			delay(1000);
    651 		}
    652 	}
    653 
    654 	printf("%s: reset timeout\n", sc->sc_dev.dv_xname);
    655 	return -1;
    656 }
    657 
    658 static int
    659 eso_query_encoding(void *hdl, struct audio_encoding *fp)
    660 {
    661 
    662 	switch (fp->index) {
    663 	case 0:
    664 		strcpy(fp->name, AudioEulinear);
    665 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    666 		fp->precision = 8;
    667 		fp->flags = 0;
    668 		break;
    669 	case 1:
    670 		strcpy(fp->name, AudioEmulaw);
    671 		fp->encoding = AUDIO_ENCODING_ULAW;
    672 		fp->precision = 8;
    673 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    674 		break;
    675 	case 2:
    676 		strcpy(fp->name, AudioEalaw);
    677 		fp->encoding = AUDIO_ENCODING_ALAW;
    678 		fp->precision = 8;
    679 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    680 		break;
    681 	case 3:
    682 		strcpy(fp->name, AudioEslinear);
    683 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    684 		fp->precision = 8;
    685 		fp->flags = 0;
    686 		break;
    687 	case 4:
    688 		strcpy(fp->name, AudioEslinear_le);
    689 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    690 		fp->precision = 16;
    691 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    692 		break;
    693 	case 5:
    694 		strcpy(fp->name, AudioEulinear_le);
    695 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    696 		fp->precision = 16;
    697 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    698 		break;
    699 	case 6:
    700 		strcpy(fp->name, AudioEslinear_be);
    701 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    702 		fp->precision = 16;
    703 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    704 		break;
    705 	case 7:
    706 		strcpy(fp->name, AudioEulinear_be);
    707 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    708 		fp->precision = 16;
    709 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    710 		break;
    711 	default:
    712 		return EINVAL;
    713 	}
    714 
    715 	return 0;
    716 }
    717 
    718 static int
    719 eso_set_params(void *hdl, int setmode, int usemode, audio_params_t *play,
    720     audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    721 {
    722 	struct eso_softc *sc;
    723 	struct audio_params *p;
    724 	stream_filter_list_t *fil;
    725 	int mode, r[2], rd[2], clk;
    726 	unsigned int srg, fltdiv;
    727 	int i;
    728 
    729 	sc = hdl;
    730 	for (mode = AUMODE_RECORD; mode != -1;
    731 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    732 		if ((setmode & mode) == 0)
    733 			continue;
    734 
    735 		p = (mode == AUMODE_PLAY) ? play : rec;
    736 
    737 		if (p->sample_rate < ESO_MINRATE ||
    738 		    p->sample_rate > ESO_MAXRATE ||
    739 		    (p->precision != 8 && p->precision != 16) ||
    740 		    (p->channels != 1 && p->channels != 2))
    741 			return EINVAL;
    742 
    743 		/*
    744 		 * We'll compute both possible sample rate dividers and pick
    745 		 * the one with the least error.
    746 		 */
    747 #define ABS(x) ((x) < 0 ? -(x) : (x))
    748 		r[0] = ESO_CLK0 /
    749 		    (128 - (rd[0] = 128 - ESO_CLK0 / p->sample_rate));
    750 		r[1] = ESO_CLK1 /
    751 		    (128 - (rd[1] = 128 - ESO_CLK1 / p->sample_rate));
    752 
    753 		if (r[0] > r[1])
    754 			clk = p->sample_rate - r[1];
    755 		else
    756 			clk = p->sample_rate - r[0];
    757 		srg = rd[clk] | (clk == 1 ? ESO_CLK1_SELECT : 0x00);
    758 
    759 		/* Roll-off frequency of 87%, as in the ES1888 driver. */
    760 		fltdiv = 256 - 200279L / r[clk];
    761 
    762 		/* Update to reflect the possibly inexact rate. */
    763 		p->sample_rate = r[clk];
    764 
    765 		fil = (mode == AUMODE_PLAY) ? pfil : rfil;
    766 		i = auconv_set_converter(eso_formats, ESO_NFORMATS,
    767 					 mode, p, FALSE, fil);
    768 		if (i < 0)
    769 			return EINVAL;
    770 		if (mode == AUMODE_RECORD) {
    771 			/* Audio 1 */
    772 			DPRINTF(("A1 srg 0x%02x fdiv 0x%02x\n", srg, fltdiv));
    773 			eso_write_ctlreg(sc, ESO_CTLREG_SRG, srg);
    774 			eso_write_ctlreg(sc, ESO_CTLREG_FLTDIV, fltdiv);
    775 		} else {
    776 			/* Audio 2 */
    777 			DPRINTF(("A2 srg 0x%02x fdiv 0x%02x\n", srg, fltdiv));
    778 			eso_write_mixreg(sc, ESO_MIXREG_A2SRG, srg);
    779 			eso_write_mixreg(sc, ESO_MIXREG_A2FLTDIV, fltdiv);
    780 		}
    781 #undef ABS
    782 
    783 	}
    784 
    785 	return 0;
    786 }
    787 
    788 static int
    789 eso_round_blocksize(void *hdl, int blk, int mode, const audio_params_t *param)
    790 {
    791 
    792 	return blk & -32;	/* keep good alignment; at least 16 req'd */
    793 }
    794 
    795 static int
    796 eso_halt_output(void *hdl)
    797 {
    798 	struct eso_softc *sc;
    799 	int error, s;
    800 
    801 	sc = hdl;
    802 	DPRINTF(("%s: halt_output\n", sc->sc_dev.dv_xname));
    803 
    804 	/*
    805 	 * Disable auto-initialize DMA, allowing the FIFO to drain and then
    806 	 * stop.  The interrupt callback pointer is cleared at this
    807 	 * point so that an outstanding FIFO interrupt for the remaining data
    808 	 * will be acknowledged without further processing.
    809 	 *
    810 	 * This does not immediately `abort' an operation in progress (c.f.
    811 	 * audio(9)) but is the method to leave the FIFO behind in a clean
    812 	 * state with the least hair.  (Besides, that item needs to be
    813 	 * rephrased for trigger_*()-based DMA environments.)
    814 	 */
    815 	s = splaudio();
    816 	eso_write_mixreg(sc, ESO_MIXREG_A2C1,
    817 	    ESO_MIXREG_A2C1_FIFOENB | ESO_MIXREG_A2C1_DMAENB);
    818 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM,
    819 	    ESO_IO_A2DMAM_DMAENB);
    820 
    821 	sc->sc_pintr = NULL;
    822 	error = tsleep(&sc->sc_pintr, PCATCH | PWAIT, "esoho", sc->sc_pdrain);
    823 	splx(s);
    824 
    825 	/* Shut down DMA completely. */
    826 	eso_write_mixreg(sc, ESO_MIXREG_A2C1, 0);
    827 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM, 0);
    828 
    829 	return error == EWOULDBLOCK ? 0 : error;
    830 }
    831 
    832 static int
    833 eso_halt_input(void *hdl)
    834 {
    835 	struct eso_softc *sc;
    836 	int error, s;
    837 
    838 	sc = hdl;
    839 	DPRINTF(("%s: halt_input\n", sc->sc_dev.dv_xname));
    840 
    841 	/* Just like eso_halt_output(), but for Audio 1. */
    842 	s = splaudio();
    843 	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
    844 	    ESO_CTLREG_A1C2_READ | ESO_CTLREG_A1C2_ADC |
    845 	    ESO_CTLREG_A1C2_DMAENB);
    846 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MODE,
    847 	    DMA37MD_WRITE | DMA37MD_DEMAND);
    848 
    849 	sc->sc_rintr = NULL;
    850 	error = tsleep(&sc->sc_rintr, PCATCH | PWAIT, "esohi", sc->sc_rdrain);
    851 	splx(s);
    852 
    853 	/* Shut down DMA completely. */
    854 	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
    855 	    ESO_CTLREG_A1C2_READ | ESO_CTLREG_A1C2_ADC);
    856 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK,
    857 	    ESO_DMAC_MASK_MASK);
    858 
    859 	return error == EWOULDBLOCK ? 0 : error;
    860 }
    861 
    862 static int
    863 eso_getdev(void *hdl, struct audio_device *retp)
    864 {
    865 	struct eso_softc *sc;
    866 
    867 	sc = hdl;
    868 	strncpy(retp->name, "ESS Solo-1", sizeof (retp->name));
    869 	snprintf(retp->version, sizeof (retp->version), "0x%02x",
    870 	    sc->sc_revision);
    871 	if (sc->sc_revision <
    872 	    sizeof (eso_rev2model) / sizeof (eso_rev2model[0]))
    873 		strncpy(retp->config, eso_rev2model[sc->sc_revision],
    874 		    sizeof (retp->config));
    875 	else
    876 		strncpy(retp->config, "unknown", sizeof (retp->config));
    877 
    878 	return 0;
    879 }
    880 
    881 static int
    882 eso_set_port(void *hdl, mixer_ctrl_t *cp)
    883 {
    884 	struct eso_softc *sc;
    885 	unsigned int lgain, rgain;
    886 	uint8_t tmp;
    887 
    888 	sc = hdl;
    889 	switch (cp->dev) {
    890 	case ESO_DAC_PLAY_VOL:
    891 	case ESO_MIC_PLAY_VOL:
    892 	case ESO_LINE_PLAY_VOL:
    893 	case ESO_SYNTH_PLAY_VOL:
    894 	case ESO_CD_PLAY_VOL:
    895 	case ESO_AUXB_PLAY_VOL:
    896 	case ESO_RECORD_VOL:
    897 	case ESO_DAC_REC_VOL:
    898 	case ESO_MIC_REC_VOL:
    899 	case ESO_LINE_REC_VOL:
    900 	case ESO_SYNTH_REC_VOL:
    901 	case ESO_CD_REC_VOL:
    902 	case ESO_AUXB_REC_VOL:
    903 		if (cp->type != AUDIO_MIXER_VALUE)
    904 			return EINVAL;
    905 
    906 		/*
    907 		 * Stereo-capable mixer ports: if we get a single-channel
    908 		 * gain value passed in, then we duplicate it to both left
    909 		 * and right channels.
    910 		 */
    911 		switch (cp->un.value.num_channels) {
    912 		case 1:
    913 			lgain = rgain = ESO_GAIN_TO_4BIT(
    914 			    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
    915 			break;
    916 		case 2:
    917 			lgain = ESO_GAIN_TO_4BIT(
    918 			    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
    919 			rgain = ESO_GAIN_TO_4BIT(
    920 			    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
    921 			break;
    922 		default:
    923 			return EINVAL;
    924 		}
    925 
    926 		sc->sc_gain[cp->dev][ESO_LEFT] = lgain;
    927 		sc->sc_gain[cp->dev][ESO_RIGHT] = rgain;
    928 		eso_set_gain(sc, cp->dev);
    929 		break;
    930 
    931 	case ESO_MASTER_VOL:
    932 		if (cp->type != AUDIO_MIXER_VALUE)
    933 			return EINVAL;
    934 
    935 		/* Like above, but a precision of 6 bits. */
    936 		switch (cp->un.value.num_channels) {
    937 		case 1:
    938 			lgain = rgain = ESO_GAIN_TO_6BIT(
    939 			    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
    940 			break;
    941 		case 2:
    942 			lgain = ESO_GAIN_TO_6BIT(
    943 			    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
    944 			rgain = ESO_GAIN_TO_6BIT(
    945 			    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
    946 			break;
    947 		default:
    948 			return EINVAL;
    949 		}
    950 
    951 		sc->sc_gain[cp->dev][ESO_LEFT] = lgain;
    952 		sc->sc_gain[cp->dev][ESO_RIGHT] = rgain;
    953 		eso_set_gain(sc, cp->dev);
    954 		break;
    955 
    956 	case ESO_SPATIALIZER:
    957 		if (cp->type != AUDIO_MIXER_VALUE ||
    958 		    cp->un.value.num_channels != 1)
    959 			return EINVAL;
    960 
    961 		sc->sc_gain[cp->dev][ESO_LEFT] =
    962 		    sc->sc_gain[cp->dev][ESO_RIGHT] =
    963 		    ESO_GAIN_TO_6BIT(
    964 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
    965 		eso_set_gain(sc, cp->dev);
    966 		break;
    967 
    968 	case ESO_MONO_PLAY_VOL:
    969 	case ESO_MONO_REC_VOL:
    970 		if (cp->type != AUDIO_MIXER_VALUE ||
    971 		    cp->un.value.num_channels != 1)
    972 			return EINVAL;
    973 
    974 		sc->sc_gain[cp->dev][ESO_LEFT] =
    975 		    sc->sc_gain[cp->dev][ESO_RIGHT] =
    976 		    ESO_GAIN_TO_4BIT(
    977 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
    978 		eso_set_gain(sc, cp->dev);
    979 		break;
    980 
    981 	case ESO_PCSPEAKER_VOL:
    982 		if (cp->type != AUDIO_MIXER_VALUE ||
    983 		    cp->un.value.num_channels != 1)
    984 			return EINVAL;
    985 
    986 		sc->sc_gain[cp->dev][ESO_LEFT] =
    987 		    sc->sc_gain[cp->dev][ESO_RIGHT] =
    988 		    ESO_GAIN_TO_3BIT(
    989 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
    990 		eso_set_gain(sc, cp->dev);
    991 		break;
    992 
    993 	case ESO_SPATIALIZER_ENABLE:
    994 		if (cp->type != AUDIO_MIXER_ENUM)
    995 			return EINVAL;
    996 
    997 		sc->sc_spatializer = (cp->un.ord != 0);
    998 
    999 		tmp = eso_read_mixreg(sc, ESO_MIXREG_SPAT);
   1000 		if (sc->sc_spatializer)
   1001 			tmp |= ESO_MIXREG_SPAT_ENB;
   1002 		else
   1003 			tmp &= ~ESO_MIXREG_SPAT_ENB;
   1004 		eso_write_mixreg(sc, ESO_MIXREG_SPAT,
   1005 		    tmp | ESO_MIXREG_SPAT_RSTREL);
   1006 		break;
   1007 
   1008 	case ESO_MASTER_MUTE:
   1009 		if (cp->type != AUDIO_MIXER_ENUM)
   1010 			return EINVAL;
   1011 
   1012 		sc->sc_mvmute = (cp->un.ord != 0);
   1013 
   1014 		if (sc->sc_mvmute) {
   1015 			eso_write_mixreg(sc, ESO_MIXREG_LMVM,
   1016 			    eso_read_mixreg(sc, ESO_MIXREG_LMVM) |
   1017 			    ESO_MIXREG_LMVM_MUTE);
   1018 			eso_write_mixreg(sc, ESO_MIXREG_RMVM,
   1019 			    eso_read_mixreg(sc, ESO_MIXREG_RMVM) |
   1020 			    ESO_MIXREG_RMVM_MUTE);
   1021 		} else {
   1022 			eso_write_mixreg(sc, ESO_MIXREG_LMVM,
   1023 			    eso_read_mixreg(sc, ESO_MIXREG_LMVM) &
   1024 			    ~ESO_MIXREG_LMVM_MUTE);
   1025 			eso_write_mixreg(sc, ESO_MIXREG_RMVM,
   1026 			    eso_read_mixreg(sc, ESO_MIXREG_RMVM) &
   1027 			    ~ESO_MIXREG_RMVM_MUTE);
   1028 		}
   1029 		break;
   1030 
   1031 	case ESO_MONOOUT_SOURCE:
   1032 		if (cp->type != AUDIO_MIXER_ENUM)
   1033 			return EINVAL;
   1034 
   1035 		return eso_set_monooutsrc(sc, cp->un.ord);
   1036 
   1037 	case ESO_MONOIN_BYPASS:
   1038 		if (cp->type != AUDIO_MIXER_ENUM)
   1039 			return EINVAL;
   1040 
   1041 		return (eso_set_monoinbypass(sc, cp->un.ord));
   1042 
   1043 	case ESO_RECORD_MONITOR:
   1044 		if (cp->type != AUDIO_MIXER_ENUM)
   1045 			return EINVAL;
   1046 
   1047 		sc->sc_recmon = (cp->un.ord != 0);
   1048 
   1049 		tmp = eso_read_ctlreg(sc, ESO_CTLREG_ACTL);
   1050 		if (sc->sc_recmon)
   1051 			tmp |= ESO_CTLREG_ACTL_RECMON;
   1052 		else
   1053 			tmp &= ~ESO_CTLREG_ACTL_RECMON;
   1054 		eso_write_ctlreg(sc, ESO_CTLREG_ACTL, tmp);
   1055 		break;
   1056 
   1057 	case ESO_RECORD_SOURCE:
   1058 		if (cp->type != AUDIO_MIXER_ENUM)
   1059 			return EINVAL;
   1060 
   1061 		return eso_set_recsrc(sc, cp->un.ord);
   1062 
   1063 	case ESO_MIC_PREAMP:
   1064 		if (cp->type != AUDIO_MIXER_ENUM)
   1065 			return EINVAL;
   1066 
   1067 		return eso_set_preamp(sc, cp->un.ord);
   1068 
   1069 	default:
   1070 		return EINVAL;
   1071 	}
   1072 
   1073 	return 0;
   1074 }
   1075 
   1076 static int
   1077 eso_get_port(void *hdl, mixer_ctrl_t *cp)
   1078 {
   1079 	struct eso_softc *sc;
   1080 
   1081 	sc = hdl;
   1082 	switch (cp->dev) {
   1083 	case ESO_MASTER_VOL:
   1084 		/* Reload from mixer after hardware volume control use. */
   1085 		if (sc->sc_gain[cp->dev][ESO_LEFT] == (uint8_t)~0)
   1086 			eso_reload_master_vol(sc);
   1087 		/* FALLTHROUGH */
   1088 	case ESO_DAC_PLAY_VOL:
   1089 	case ESO_MIC_PLAY_VOL:
   1090 	case ESO_LINE_PLAY_VOL:
   1091 	case ESO_SYNTH_PLAY_VOL:
   1092 	case ESO_CD_PLAY_VOL:
   1093 	case ESO_AUXB_PLAY_VOL:
   1094 	case ESO_RECORD_VOL:
   1095 	case ESO_DAC_REC_VOL:
   1096 	case ESO_MIC_REC_VOL:
   1097 	case ESO_LINE_REC_VOL:
   1098 	case ESO_SYNTH_REC_VOL:
   1099 	case ESO_CD_REC_VOL:
   1100 	case ESO_AUXB_REC_VOL:
   1101 		/*
   1102 		 * Stereo-capable ports: if a single-channel query is made,
   1103 		 * just return the left channel's value (since single-channel
   1104 		 * settings themselves are applied to both channels).
   1105 		 */
   1106 		switch (cp->un.value.num_channels) {
   1107 		case 1:
   1108 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1109 			    sc->sc_gain[cp->dev][ESO_LEFT];
   1110 			break;
   1111 		case 2:
   1112 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
   1113 			    sc->sc_gain[cp->dev][ESO_LEFT];
   1114 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
   1115 			    sc->sc_gain[cp->dev][ESO_RIGHT];
   1116 			break;
   1117 		default:
   1118 			return EINVAL;
   1119 		}
   1120 		break;
   1121 
   1122 	case ESO_MONO_PLAY_VOL:
   1123 	case ESO_PCSPEAKER_VOL:
   1124 	case ESO_MONO_REC_VOL:
   1125 	case ESO_SPATIALIZER:
   1126 		if (cp->un.value.num_channels != 1)
   1127 			return EINVAL;
   1128 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1129 		    sc->sc_gain[cp->dev][ESO_LEFT];
   1130 		break;
   1131 
   1132 	case ESO_RECORD_MONITOR:
   1133 		cp->un.ord = sc->sc_recmon;
   1134 		break;
   1135 
   1136 	case ESO_RECORD_SOURCE:
   1137 		cp->un.ord = sc->sc_recsrc;
   1138 		break;
   1139 
   1140 	case ESO_MONOOUT_SOURCE:
   1141 		cp->un.ord = sc->sc_monooutsrc;
   1142 		break;
   1143 
   1144 	case ESO_MONOIN_BYPASS:
   1145 		cp->un.ord = sc->sc_monoinbypass;
   1146 		break;
   1147 
   1148 	case ESO_SPATIALIZER_ENABLE:
   1149 		cp->un.ord = sc->sc_spatializer;
   1150 		break;
   1151 
   1152 	case ESO_MIC_PREAMP:
   1153 		cp->un.ord = sc->sc_preamp;
   1154 		break;
   1155 
   1156 	case ESO_MASTER_MUTE:
   1157 		/* Reload from mixer after hardware volume control use. */
   1158 		eso_reload_master_vol(sc);
   1159 		cp->un.ord = sc->sc_mvmute;
   1160 		break;
   1161 
   1162 	default:
   1163 		return EINVAL;
   1164 	}
   1165 
   1166 	return 0;
   1167 }
   1168 
   1169 static int
   1170 eso_query_devinfo(void *hdl, mixer_devinfo_t *dip)
   1171 {
   1172 
   1173 	switch (dip->index) {
   1174 	case ESO_DAC_PLAY_VOL:
   1175 		dip->mixer_class = ESO_INPUT_CLASS;
   1176 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1177 		strcpy(dip->label.name, AudioNdac);
   1178 		dip->type = AUDIO_MIXER_VALUE;
   1179 		dip->un.v.num_channels = 2;
   1180 		strcpy(dip->un.v.units.name, AudioNvolume);
   1181 		break;
   1182 	case ESO_MIC_PLAY_VOL:
   1183 		dip->mixer_class = ESO_INPUT_CLASS;
   1184 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1185 		strcpy(dip->label.name, AudioNmicrophone);
   1186 		dip->type = AUDIO_MIXER_VALUE;
   1187 		dip->un.v.num_channels = 2;
   1188 		strcpy(dip->un.v.units.name, AudioNvolume);
   1189 		break;
   1190 	case ESO_LINE_PLAY_VOL:
   1191 		dip->mixer_class = ESO_INPUT_CLASS;
   1192 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1193 		strcpy(dip->label.name, AudioNline);
   1194 		dip->type = AUDIO_MIXER_VALUE;
   1195 		dip->un.v.num_channels = 2;
   1196 		strcpy(dip->un.v.units.name, AudioNvolume);
   1197 		break;
   1198 	case ESO_SYNTH_PLAY_VOL:
   1199 		dip->mixer_class = ESO_INPUT_CLASS;
   1200 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1201 		strcpy(dip->label.name, AudioNfmsynth);
   1202 		dip->type = AUDIO_MIXER_VALUE;
   1203 		dip->un.v.num_channels = 2;
   1204 		strcpy(dip->un.v.units.name, AudioNvolume);
   1205 		break;
   1206 	case ESO_MONO_PLAY_VOL:
   1207 		dip->mixer_class = ESO_INPUT_CLASS;
   1208 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1209 		strcpy(dip->label.name, "mono_in");
   1210 		dip->type = AUDIO_MIXER_VALUE;
   1211 		dip->un.v.num_channels = 1;
   1212 		strcpy(dip->un.v.units.name, AudioNvolume);
   1213 		break;
   1214 	case ESO_CD_PLAY_VOL:
   1215 		dip->mixer_class = ESO_INPUT_CLASS;
   1216 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1217 		strcpy(dip->label.name, AudioNcd);
   1218 		dip->type = AUDIO_MIXER_VALUE;
   1219 		dip->un.v.num_channels = 2;
   1220 		strcpy(dip->un.v.units.name, AudioNvolume);
   1221 		break;
   1222 	case ESO_AUXB_PLAY_VOL:
   1223 		dip->mixer_class = ESO_INPUT_CLASS;
   1224 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1225 		strcpy(dip->label.name, "auxb");
   1226 		dip->type = AUDIO_MIXER_VALUE;
   1227 		dip->un.v.num_channels = 2;
   1228 		strcpy(dip->un.v.units.name, AudioNvolume);
   1229 		break;
   1230 
   1231 	case ESO_MIC_PREAMP:
   1232 		dip->mixer_class = ESO_MICROPHONE_CLASS;
   1233 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1234 		strcpy(dip->label.name, AudioNpreamp);
   1235 		dip->type = AUDIO_MIXER_ENUM;
   1236 		dip->un.e.num_mem = 2;
   1237 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1238 		dip->un.e.member[0].ord = 0;
   1239 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1240 		dip->un.e.member[1].ord = 1;
   1241 		break;
   1242 	case ESO_MICROPHONE_CLASS:
   1243 		dip->mixer_class = ESO_MICROPHONE_CLASS;
   1244 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1245 		strcpy(dip->label.name, AudioNmicrophone);
   1246 		dip->type = AUDIO_MIXER_CLASS;
   1247 		break;
   1248 
   1249 	case ESO_INPUT_CLASS:
   1250 		dip->mixer_class = ESO_INPUT_CLASS;
   1251 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1252 		strcpy(dip->label.name, AudioCinputs);
   1253 		dip->type = AUDIO_MIXER_CLASS;
   1254 		break;
   1255 
   1256 	case ESO_MASTER_VOL:
   1257 		dip->mixer_class = ESO_OUTPUT_CLASS;
   1258 		dip->prev = AUDIO_MIXER_LAST;
   1259 		dip->next = ESO_MASTER_MUTE;
   1260 		strcpy(dip->label.name, AudioNmaster);
   1261 		dip->type = AUDIO_MIXER_VALUE;
   1262 		dip->un.v.num_channels = 2;
   1263 		strcpy(dip->un.v.units.name, AudioNvolume);
   1264 		break;
   1265 	case ESO_MASTER_MUTE:
   1266 		dip->mixer_class = ESO_OUTPUT_CLASS;
   1267 		dip->prev = ESO_MASTER_VOL;
   1268 		dip->next = AUDIO_MIXER_LAST;
   1269 		strcpy(dip->label.name, AudioNmute);
   1270 		dip->type = AUDIO_MIXER_ENUM;
   1271 		dip->un.e.num_mem = 2;
   1272 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1273 		dip->un.e.member[0].ord = 0;
   1274 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1275 		dip->un.e.member[1].ord = 1;
   1276 		break;
   1277 
   1278 	case ESO_PCSPEAKER_VOL:
   1279 		dip->mixer_class = ESO_OUTPUT_CLASS;
   1280 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1281 		strcpy(dip->label.name, "pc_speaker");
   1282 		dip->type = AUDIO_MIXER_VALUE;
   1283 		dip->un.v.num_channels = 1;
   1284 		strcpy(dip->un.v.units.name, AudioNvolume);
   1285 		break;
   1286 	case ESO_MONOOUT_SOURCE:
   1287 		dip->mixer_class = ESO_OUTPUT_CLASS;
   1288 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1289 		strcpy(dip->label.name, "mono_out");
   1290 		dip->type = AUDIO_MIXER_ENUM;
   1291 		dip->un.e.num_mem = 3;
   1292 		strcpy(dip->un.e.member[0].label.name, AudioNmute);
   1293 		dip->un.e.member[0].ord = ESO_MIXREG_MPM_MOMUTE;
   1294 		strcpy(dip->un.e.member[1].label.name, AudioNdac);
   1295 		dip->un.e.member[1].ord = ESO_MIXREG_MPM_MOA2R;
   1296 		strcpy(dip->un.e.member[2].label.name, AudioNmixerout);
   1297 		dip->un.e.member[2].ord = ESO_MIXREG_MPM_MOREC;
   1298 		break;
   1299 
   1300 	case ESO_MONOIN_BYPASS:
   1301 		dip->mixer_class = ESO_MONOIN_CLASS;
   1302 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1303 		strcpy(dip->label.name, "bypass");
   1304 		dip->type = AUDIO_MIXER_ENUM;
   1305 		dip->un.e.num_mem = 2;
   1306 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1307 		dip->un.e.member[0].ord = 0;
   1308 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1309 		dip->un.e.member[1].ord = 1;
   1310 		break;
   1311 	case ESO_MONOIN_CLASS:
   1312 		dip->mixer_class = ESO_MONOIN_CLASS;
   1313 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1314 		strcpy(dip->label.name, "mono_in");
   1315 		dip->type = AUDIO_MIXER_CLASS;
   1316 		break;
   1317 
   1318 	case ESO_SPATIALIZER:
   1319 		dip->mixer_class = ESO_OUTPUT_CLASS;
   1320 		dip->prev = AUDIO_MIXER_LAST;
   1321 		dip->next = ESO_SPATIALIZER_ENABLE;
   1322 		strcpy(dip->label.name, AudioNspatial);
   1323 		dip->type = AUDIO_MIXER_VALUE;
   1324 		dip->un.v.num_channels = 1;
   1325 		strcpy(dip->un.v.units.name, "level");
   1326 		break;
   1327 	case ESO_SPATIALIZER_ENABLE:
   1328 		dip->mixer_class = ESO_OUTPUT_CLASS;
   1329 		dip->prev = ESO_SPATIALIZER;
   1330 		dip->next = AUDIO_MIXER_LAST;
   1331 		strcpy(dip->label.name, "enable");
   1332 		dip->type = AUDIO_MIXER_ENUM;
   1333 		dip->un.e.num_mem = 2;
   1334 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1335 		dip->un.e.member[0].ord = 0;
   1336 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1337 		dip->un.e.member[1].ord = 1;
   1338 		break;
   1339 
   1340 	case ESO_OUTPUT_CLASS:
   1341 		dip->mixer_class = ESO_OUTPUT_CLASS;
   1342 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1343 		strcpy(dip->label.name, AudioCoutputs);
   1344 		dip->type = AUDIO_MIXER_CLASS;
   1345 		break;
   1346 
   1347 	case ESO_RECORD_MONITOR:
   1348 		dip->mixer_class = ESO_MONITOR_CLASS;
   1349 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1350 		strcpy(dip->label.name, AudioNmute);
   1351 		dip->type = AUDIO_MIXER_ENUM;
   1352 		dip->un.e.num_mem = 2;
   1353 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1354 		dip->un.e.member[0].ord = 0;
   1355 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1356 		dip->un.e.member[1].ord = 1;
   1357 		break;
   1358 	case ESO_MONITOR_CLASS:
   1359 		dip->mixer_class = ESO_MONITOR_CLASS;
   1360 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1361 		strcpy(dip->label.name, AudioCmonitor);
   1362 		dip->type = AUDIO_MIXER_CLASS;
   1363 		break;
   1364 
   1365 	case ESO_RECORD_VOL:
   1366 		dip->mixer_class = ESO_RECORD_CLASS;
   1367 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1368 		strcpy(dip->label.name, AudioNrecord);
   1369 		dip->type = AUDIO_MIXER_VALUE;
   1370 		strcpy(dip->un.v.units.name, AudioNvolume);
   1371 		break;
   1372 	case ESO_RECORD_SOURCE:
   1373 		dip->mixer_class = ESO_RECORD_CLASS;
   1374 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1375 		strcpy(dip->label.name, AudioNsource);
   1376 		dip->type = AUDIO_MIXER_ENUM;
   1377 		dip->un.e.num_mem = 4;
   1378 		strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
   1379 		dip->un.e.member[0].ord = ESO_MIXREG_ERS_MIC;
   1380 		strcpy(dip->un.e.member[1].label.name, AudioNline);
   1381 		dip->un.e.member[1].ord = ESO_MIXREG_ERS_LINE;
   1382 		strcpy(dip->un.e.member[2].label.name, AudioNcd);
   1383 		dip->un.e.member[2].ord = ESO_MIXREG_ERS_CD;
   1384 		strcpy(dip->un.e.member[3].label.name, AudioNmixerout);
   1385 		dip->un.e.member[3].ord = ESO_MIXREG_ERS_MIXER;
   1386 		break;
   1387 	case ESO_DAC_REC_VOL:
   1388 		dip->mixer_class = ESO_RECORD_CLASS;
   1389 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1390 		strcpy(dip->label.name, AudioNdac);
   1391 		dip->type = AUDIO_MIXER_VALUE;
   1392 		dip->un.v.num_channels = 2;
   1393 		strcpy(dip->un.v.units.name, AudioNvolume);
   1394 		break;
   1395 	case ESO_MIC_REC_VOL:
   1396 		dip->mixer_class = ESO_RECORD_CLASS;
   1397 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1398 		strcpy(dip->label.name, AudioNmicrophone);
   1399 		dip->type = AUDIO_MIXER_VALUE;
   1400 		dip->un.v.num_channels = 2;
   1401 		strcpy(dip->un.v.units.name, AudioNvolume);
   1402 		break;
   1403 	case ESO_LINE_REC_VOL:
   1404 		dip->mixer_class = ESO_RECORD_CLASS;
   1405 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1406 		strcpy(dip->label.name, AudioNline);
   1407 		dip->type = AUDIO_MIXER_VALUE;
   1408 		dip->un.v.num_channels = 2;
   1409 		strcpy(dip->un.v.units.name, AudioNvolume);
   1410 		break;
   1411 	case ESO_SYNTH_REC_VOL:
   1412 		dip->mixer_class = ESO_RECORD_CLASS;
   1413 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1414 		strcpy(dip->label.name, AudioNfmsynth);
   1415 		dip->type = AUDIO_MIXER_VALUE;
   1416 		dip->un.v.num_channels = 2;
   1417 		strcpy(dip->un.v.units.name, AudioNvolume);
   1418 		break;
   1419 	case ESO_MONO_REC_VOL:
   1420 		dip->mixer_class = ESO_RECORD_CLASS;
   1421 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1422 		strcpy(dip->label.name, "mono_in");
   1423 		dip->type = AUDIO_MIXER_VALUE;
   1424 		dip->un.v.num_channels = 1; /* No lies */
   1425 		strcpy(dip->un.v.units.name, AudioNvolume);
   1426 		break;
   1427 	case ESO_CD_REC_VOL:
   1428 		dip->mixer_class = ESO_RECORD_CLASS;
   1429 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1430 		strcpy(dip->label.name, AudioNcd);
   1431 		dip->type = AUDIO_MIXER_VALUE;
   1432 		dip->un.v.num_channels = 2;
   1433 		strcpy(dip->un.v.units.name, AudioNvolume);
   1434 		break;
   1435 	case ESO_AUXB_REC_VOL:
   1436 		dip->mixer_class = ESO_RECORD_CLASS;
   1437 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1438 		strcpy(dip->label.name, "auxb");
   1439 		dip->type = AUDIO_MIXER_VALUE;
   1440 		dip->un.v.num_channels = 2;
   1441 		strcpy(dip->un.v.units.name, AudioNvolume);
   1442 		break;
   1443 	case ESO_RECORD_CLASS:
   1444 		dip->mixer_class = ESO_RECORD_CLASS;
   1445 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1446 		strcpy(dip->label.name, AudioCrecord);
   1447 		dip->type = AUDIO_MIXER_CLASS;
   1448 		break;
   1449 
   1450 	default:
   1451 		return ENXIO;
   1452 	}
   1453 
   1454 	return 0;
   1455 }
   1456 
   1457 static int
   1458 eso_allocmem(struct eso_softc *sc, size_t size, size_t align, size_t boundary,
   1459     int flags, int direction, struct eso_dma *ed)
   1460 {
   1461 	int error, wait;
   1462 
   1463 	wait = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
   1464 	ed->ed_size = size;
   1465 
   1466 	error = bus_dmamem_alloc(ed->ed_dmat, ed->ed_size, align, boundary,
   1467 	    ed->ed_segs, sizeof (ed->ed_segs) / sizeof (ed->ed_segs[0]),
   1468 	    &ed->ed_nsegs, wait);
   1469 	if (error)
   1470 		goto out;
   1471 
   1472 	error = bus_dmamem_map(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs,
   1473 	    ed->ed_size, &ed->ed_addr, wait | BUS_DMA_COHERENT);
   1474 	if (error)
   1475 		goto free;
   1476 
   1477 	error = bus_dmamap_create(ed->ed_dmat, ed->ed_size, 1, ed->ed_size, 0,
   1478 	    wait, &ed->ed_map);
   1479 	if (error)
   1480 		goto unmap;
   1481 
   1482 	error = bus_dmamap_load(ed->ed_dmat, ed->ed_map, ed->ed_addr,
   1483 	    ed->ed_size, NULL, wait |
   1484 	    (direction == AUMODE_RECORD) ? BUS_DMA_READ : BUS_DMA_WRITE);
   1485 	if (error)
   1486 		goto destroy;
   1487 
   1488 	return 0;
   1489 
   1490  destroy:
   1491 	bus_dmamap_destroy(ed->ed_dmat, ed->ed_map);
   1492  unmap:
   1493 	bus_dmamem_unmap(ed->ed_dmat, ed->ed_addr, ed->ed_size);
   1494  free:
   1495 	bus_dmamem_free(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs);
   1496  out:
   1497 	return error;
   1498 }
   1499 
   1500 static void
   1501 eso_freemem(struct eso_dma *ed)
   1502 {
   1503 
   1504 	bus_dmamap_unload(ed->ed_dmat, ed->ed_map);
   1505 	bus_dmamap_destroy(ed->ed_dmat, ed->ed_map);
   1506 	bus_dmamem_unmap(ed->ed_dmat, ed->ed_addr, ed->ed_size);
   1507 	bus_dmamem_free(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs);
   1508 }
   1509 
   1510 static void *
   1511 eso_allocm(void *hdl, int direction, size_t size, struct malloc_type *type,
   1512     int flags)
   1513 {
   1514 	struct eso_softc *sc;
   1515 	struct eso_dma *ed;
   1516 	size_t boundary;
   1517 	int error;
   1518 
   1519 	sc = hdl;
   1520 	if ((ed = malloc(sizeof (*ed), type, flags)) == NULL)
   1521 		return NULL;
   1522 
   1523 	/*
   1524 	 * Apparently the Audio 1 DMA controller's current address
   1525 	 * register can't roll over a 64K address boundary, so we have to
   1526 	 * take care of that ourselves.  Similarly, the Audio 2 DMA
   1527 	 * controller needs a 1M address boundary.
   1528 	 */
   1529 	if (direction == AUMODE_RECORD)
   1530 		boundary = 0x10000;
   1531 	else
   1532 		boundary = 0x100000;
   1533 
   1534 	/*
   1535 	 * XXX Work around allocation problems for Audio 1, which
   1536 	 * XXX implements the 24 low address bits only, with
   1537 	 * XXX machine-specific DMA tag use.
   1538 	 */
   1539 #ifdef alpha
   1540 	/*
   1541 	 * XXX Force allocation through the (ISA) SGMAP.
   1542 	 */
   1543 	if (direction == AUMODE_RECORD)
   1544 		ed->ed_dmat = alphabus_dma_get_tag(sc->sc_dmat, ALPHA_BUS_ISA);
   1545 	else
   1546 #elif defined(amd64) || defined(i386)
   1547 	/*
   1548 	 * XXX Force allocation through the ISA DMA tag.
   1549 	 */
   1550 	if (direction == AUMODE_RECORD)
   1551 		ed->ed_dmat = &isa_bus_dma_tag;
   1552 	else
   1553 #endif
   1554 		ed->ed_dmat = sc->sc_dmat;
   1555 
   1556 	error = eso_allocmem(sc, size, 32, boundary, flags, direction, ed);
   1557 	if (error) {
   1558 		free(ed, type);
   1559 		return NULL;
   1560 	}
   1561 	ed->ed_next = sc->sc_dmas;
   1562 	sc->sc_dmas = ed;
   1563 
   1564 	return KVADDR(ed);
   1565 }
   1566 
   1567 static void
   1568 eso_freem(void *hdl, void *addr, struct malloc_type *type)
   1569 {
   1570 	struct eso_softc *sc;
   1571 	struct eso_dma *p, **pp;
   1572 
   1573 	sc = hdl;
   1574 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->ed_next) {
   1575 		if (KVADDR(p) == addr) {
   1576 			eso_freemem(p);
   1577 			*pp = p->ed_next;
   1578 			free(p, type);
   1579 			return;
   1580 		}
   1581 	}
   1582 }
   1583 
   1584 static size_t
   1585 eso_round_buffersize(void *hdl, int direction, size_t bufsize)
   1586 {
   1587 	size_t maxsize;
   1588 
   1589 	/*
   1590 	 * The playback DMA buffer size on the Solo-1 is limited to 0xfff0
   1591 	 * bytes.  This is because IO_A2DMAC is a two byte value
   1592 	 * indicating the literal byte count, and the 4 least significant
   1593 	 * bits are read-only.  Zero is not used as a special case for
   1594 	 * 0x10000.
   1595 	 *
   1596 	 * For recording, DMAC_DMAC is the byte count - 1, so 0x10000 can
   1597 	 * be represented.
   1598 	 */
   1599 	maxsize = (direction == AUMODE_PLAY) ? 0xfff0 : 0x10000;
   1600 
   1601 	if (bufsize > maxsize)
   1602 		bufsize = maxsize;
   1603 
   1604 	return bufsize;
   1605 }
   1606 
   1607 static paddr_t
   1608 eso_mappage(void *hdl, void *addr, off_t offs, int prot)
   1609 {
   1610 	struct eso_softc *sc;
   1611 	struct eso_dma *ed;
   1612 
   1613 	sc = hdl;
   1614 	if (offs < 0)
   1615 		return -1;
   1616 	for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != addr;
   1617 	     ed = ed->ed_next)
   1618 		continue;
   1619 	if (ed == NULL)
   1620 		return -1;
   1621 
   1622 	return bus_dmamem_mmap(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs,
   1623 	    offs, prot, BUS_DMA_WAITOK);
   1624 }
   1625 
   1626 /* ARGSUSED */
   1627 static int
   1628 eso_get_props(void *hdl)
   1629 {
   1630 
   1631 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1632 	    AUDIO_PROP_FULLDUPLEX;
   1633 }
   1634 
   1635 static int
   1636 eso_trigger_output(void *hdl, void *start, void *end, int blksize,
   1637     void (*intr)(void *), void *arg, const audio_params_t *param)
   1638 {
   1639 	struct eso_softc *sc;
   1640 	struct eso_dma *ed;
   1641 	uint8_t a2c1;
   1642 
   1643 	sc = hdl;
   1644 	DPRINTF((
   1645 	    "%s: trigger_output: start %p, end %p, blksize %d, intr %p(%p)\n",
   1646 	    sc->sc_dev.dv_xname, start, end, blksize, intr, arg));
   1647 	DPRINTF(("%s: param: rate %u, encoding %u, precision %u, channels %u\n",
   1648 	    sc->sc_dev.dv_xname, param->sample_rate, param->encoding,
   1649 	    param->precision, param->channels));
   1650 
   1651 	/* Find DMA buffer. */
   1652 	for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != start;
   1653 	     ed = ed->ed_next)
   1654 		continue;
   1655 	if (ed == NULL) {
   1656 		printf("%s: trigger_output: bad addr %p\n",
   1657 		    sc->sc_dev.dv_xname, start);
   1658 		return EINVAL;
   1659 	}
   1660 	DPRINTF(("%s: dmaaddr %lx\n",
   1661 	    sc->sc_dev.dv_xname, (unsigned long)DMAADDR(ed)));
   1662 
   1663 	sc->sc_pintr = intr;
   1664 	sc->sc_parg = arg;
   1665 
   1666 	/* Compute drain timeout. */
   1667 	sc->sc_pdrain = (blksize * NBBY * hz) /
   1668 	    (param->sample_rate * param->channels *
   1669 	     param->precision) + 2;	/* slop */
   1670 
   1671 	/* DMA transfer count (in `words'!) reload using 2's complement. */
   1672 	blksize = -(blksize >> 1);
   1673 	eso_write_mixreg(sc, ESO_MIXREG_A2TCRLO, blksize & 0xff);
   1674 	eso_write_mixreg(sc, ESO_MIXREG_A2TCRHI, blksize >> 8);
   1675 
   1676 	/* Update DAC to reflect DMA count and audio parameters. */
   1677 	/* Note: we cache A2C2 in order to avoid r/m/w at interrupt time. */
   1678 	if (param->precision == 16)
   1679 		sc->sc_a2c2 |= ESO_MIXREG_A2C2_16BIT;
   1680 	else
   1681 		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_16BIT;
   1682 	if (param->channels == 2)
   1683 		sc->sc_a2c2 |= ESO_MIXREG_A2C2_STEREO;
   1684 	else
   1685 		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_STEREO;
   1686 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
   1687 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
   1688 		sc->sc_a2c2 |= ESO_MIXREG_A2C2_SIGNED;
   1689 	else
   1690 		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_SIGNED;
   1691 	/* Unmask IRQ. */
   1692 	sc->sc_a2c2 |= ESO_MIXREG_A2C2_IRQM;
   1693 	eso_write_mixreg(sc, ESO_MIXREG_A2C2, sc->sc_a2c2);
   1694 
   1695 	/* Set up DMA controller. */
   1696 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAA,
   1697 	    DMAADDR(ed));
   1698 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAC,
   1699 	    (uint8_t *)end - (uint8_t *)start);
   1700 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM,
   1701 	    ESO_IO_A2DMAM_DMAENB | ESO_IO_A2DMAM_AUTO);
   1702 
   1703 	/* Start DMA. */
   1704 	a2c1 = eso_read_mixreg(sc, ESO_MIXREG_A2C1);
   1705 	a2c1 &= ~ESO_MIXREG_A2C1_RESV0; /* Paranoia? XXX bit 5 */
   1706 	a2c1 |= ESO_MIXREG_A2C1_FIFOENB | ESO_MIXREG_A2C1_DMAENB |
   1707 	    ESO_MIXREG_A2C1_AUTO;
   1708 	eso_write_mixreg(sc, ESO_MIXREG_A2C1, a2c1);
   1709 
   1710 	return 0;
   1711 }
   1712 
   1713 static int
   1714 eso_trigger_input(void *hdl, void *start, void *end, int blksize,
   1715     void (*intr)(void *), void *arg, const audio_params_t *param)
   1716 {
   1717 	struct eso_softc *sc;
   1718 	struct eso_dma *ed;
   1719 	uint8_t actl, a1c1;
   1720 
   1721 	sc = hdl;
   1722 	DPRINTF((
   1723 	    "%s: trigger_input: start %p, end %p, blksize %d, intr %p(%p)\n",
   1724 	    sc->sc_dev.dv_xname, start, end, blksize, intr, arg));
   1725 	DPRINTF(("%s: param: rate %u, encoding %u, precision %u, channels %u\n",
   1726 	    sc->sc_dev.dv_xname, param->sample_rate, param->encoding,
   1727 	    param->precision, param->channels));
   1728 
   1729 	/*
   1730 	 * If we failed to configure the Audio 1 DMA controller, bail here
   1731 	 * while retaining availability of the DAC direction (in Audio 2).
   1732 	 */
   1733 	if (!sc->sc_dmac_configured)
   1734 		return EIO;
   1735 
   1736 	/* Find DMA buffer. */
   1737 	for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != start;
   1738 	     ed = ed->ed_next)
   1739 		continue;
   1740 	if (ed == NULL) {
   1741 		printf("%s: trigger_output: bad addr %p\n",
   1742 		    sc->sc_dev.dv_xname, start);
   1743 		return EINVAL;
   1744 	}
   1745 	DPRINTF(("%s: dmaaddr %lx\n",
   1746 	    sc->sc_dev.dv_xname, (unsigned long)DMAADDR(ed)));
   1747 
   1748 	sc->sc_rintr = intr;
   1749 	sc->sc_rarg = arg;
   1750 
   1751 	/* Compute drain timeout. */
   1752 	sc->sc_rdrain = (blksize * NBBY * hz) /
   1753 	    (param->sample_rate * param->channels *
   1754 	     param->precision) + 2;	/* slop */
   1755 
   1756 	/* Set up ADC DMA converter parameters. */
   1757 	actl = eso_read_ctlreg(sc, ESO_CTLREG_ACTL);
   1758 	if (param->channels == 2) {
   1759 		actl &= ~ESO_CTLREG_ACTL_MONO;
   1760 		actl |= ESO_CTLREG_ACTL_STEREO;
   1761 	} else {
   1762 		actl &= ~ESO_CTLREG_ACTL_STEREO;
   1763 		actl |= ESO_CTLREG_ACTL_MONO;
   1764 	}
   1765 	eso_write_ctlreg(sc, ESO_CTLREG_ACTL, actl);
   1766 
   1767 	/* Set up Transfer Type: maybe move to attach time? */
   1768 	eso_write_ctlreg(sc, ESO_CTLREG_A1TT, ESO_CTLREG_A1TT_DEMAND4);
   1769 
   1770 	/* DMA transfer count reload using 2's complement. */
   1771 	blksize = -blksize;
   1772 	eso_write_ctlreg(sc, ESO_CTLREG_A1TCRLO, blksize & 0xff);
   1773 	eso_write_ctlreg(sc, ESO_CTLREG_A1TCRHI, blksize >> 8);
   1774 
   1775 	/* Set up and enable Audio 1 DMA FIFO. */
   1776 	a1c1 = ESO_CTLREG_A1C1_RESV1 | ESO_CTLREG_A1C1_FIFOENB;
   1777 	if (param->precision == 16)
   1778 		a1c1 |= ESO_CTLREG_A1C1_16BIT;
   1779 	if (param->channels == 2)
   1780 		a1c1 |= ESO_CTLREG_A1C1_STEREO;
   1781 	else
   1782 		a1c1 |= ESO_CTLREG_A1C1_MONO;
   1783 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
   1784 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
   1785 		a1c1 |= ESO_CTLREG_A1C1_SIGNED;
   1786 	eso_write_ctlreg(sc, ESO_CTLREG_A1C1, a1c1);
   1787 
   1788 	/* Set up ADC IRQ/DRQ parameters. */
   1789 	eso_write_ctlreg(sc, ESO_CTLREG_LAIC,
   1790 	    ESO_CTLREG_LAIC_PINENB | ESO_CTLREG_LAIC_EXTENB);
   1791 	eso_write_ctlreg(sc, ESO_CTLREG_DRQCTL,
   1792 	    ESO_CTLREG_DRQCTL_ENB1 | ESO_CTLREG_DRQCTL_EXTENB);
   1793 
   1794 	/* Set up and enable DMA controller. */
   1795 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_CLEAR, 0);
   1796 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK,
   1797 	    ESO_DMAC_MASK_MASK);
   1798 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MODE,
   1799 	    DMA37MD_WRITE | DMA37MD_LOOP | DMA37MD_DEMAND);
   1800 	bus_space_write_4(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_DMAA,
   1801 	    DMAADDR(ed));
   1802 	bus_space_write_2(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_DMAC,
   1803 	    (uint8_t *)end - (uint8_t *)start - 1);
   1804 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK, 0);
   1805 
   1806 	/* Start DMA. */
   1807 	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
   1808 	    ESO_CTLREG_A1C2_DMAENB | ESO_CTLREG_A1C2_READ |
   1809 	    ESO_CTLREG_A1C2_AUTO | ESO_CTLREG_A1C2_ADC);
   1810 
   1811 	return 0;
   1812 }
   1813 
   1814 /*
   1815  * Mixer utility functions.
   1816  */
   1817 static int
   1818 eso_set_recsrc(struct eso_softc *sc, unsigned int recsrc)
   1819 {
   1820 	mixer_devinfo_t di;
   1821 	int i;
   1822 
   1823 	di.index = ESO_RECORD_SOURCE;
   1824 	if (eso_query_devinfo(sc, &di) != 0)
   1825 		panic("eso_set_recsrc: eso_query_devinfo failed");
   1826 
   1827 	for (i = 0; i < di.un.e.num_mem; i++) {
   1828 		if (recsrc == di.un.e.member[i].ord) {
   1829 			eso_write_mixreg(sc, ESO_MIXREG_ERS, recsrc);
   1830 			sc->sc_recsrc = recsrc;
   1831 			return 0;
   1832 		}
   1833 	}
   1834 
   1835 	return EINVAL;
   1836 }
   1837 
   1838 static int
   1839 eso_set_monooutsrc(struct eso_softc *sc, unsigned int monooutsrc)
   1840 {
   1841 	mixer_devinfo_t di;
   1842 	int i;
   1843 	uint8_t mpm;
   1844 
   1845 	di.index = ESO_MONOOUT_SOURCE;
   1846 	if (eso_query_devinfo(sc, &di) != 0)
   1847 		panic("eso_set_monooutsrc: eso_query_devinfo failed");
   1848 
   1849 	for (i = 0; i < di.un.e.num_mem; i++) {
   1850 		if (monooutsrc == di.un.e.member[i].ord) {
   1851 			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
   1852 			mpm &= ~ESO_MIXREG_MPM_MOMASK;
   1853 			mpm |= monooutsrc;
   1854 			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
   1855 			sc->sc_monooutsrc = monooutsrc;
   1856 			return 0;
   1857 		}
   1858 	}
   1859 
   1860 	return EINVAL;
   1861 }
   1862 
   1863 static int
   1864 eso_set_monoinbypass(struct eso_softc *sc, unsigned int monoinbypass)
   1865 {
   1866 	mixer_devinfo_t di;
   1867 	int i;
   1868 	uint8_t mpm;
   1869 
   1870 	di.index = ESO_MONOIN_BYPASS;
   1871 	if (eso_query_devinfo(sc, &di) != 0)
   1872 		panic("eso_set_monoinbypass: eso_query_devinfo failed");
   1873 
   1874 	for (i = 0; i < di.un.e.num_mem; i++) {
   1875 		if (monoinbypass == di.un.e.member[i].ord) {
   1876 			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
   1877 			mpm &= ~(ESO_MIXREG_MPM_MOMASK | ESO_MIXREG_MPM_RESV0);
   1878 			mpm |= (monoinbypass ? ESO_MIXREG_MPM_MIBYPASS : 0);
   1879 			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
   1880 			sc->sc_monoinbypass = monoinbypass;
   1881 			return 0;
   1882 		}
   1883 	}
   1884 
   1885 	return EINVAL;
   1886 }
   1887 
   1888 static int
   1889 eso_set_preamp(struct eso_softc *sc, unsigned int preamp)
   1890 {
   1891 	mixer_devinfo_t di;
   1892 	int i;
   1893 	uint8_t mpm;
   1894 
   1895 	di.index = ESO_MIC_PREAMP;
   1896 	if (eso_query_devinfo(sc, &di) != 0)
   1897 		panic("eso_set_preamp: eso_query_devinfo failed");
   1898 
   1899 	for (i = 0; i < di.un.e.num_mem; i++) {
   1900 		if (preamp == di.un.e.member[i].ord) {
   1901 			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
   1902 			mpm &= ~(ESO_MIXREG_MPM_PREAMP | ESO_MIXREG_MPM_RESV0);
   1903 			mpm |= (preamp ? ESO_MIXREG_MPM_PREAMP : 0);
   1904 			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
   1905 			sc->sc_preamp = preamp;
   1906 			return 0;
   1907 		}
   1908 	}
   1909 
   1910 	return EINVAL;
   1911 }
   1912 
   1913 /*
   1914  * Reload Master Volume and Mute values in softc from mixer; used when
   1915  * those have previously been invalidated by use of hardware volume controls.
   1916  */
   1917 static void
   1918 eso_reload_master_vol(struct eso_softc *sc)
   1919 {
   1920 	uint8_t mv;
   1921 
   1922 	mv = eso_read_mixreg(sc, ESO_MIXREG_LMVM);
   1923 	sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] =
   1924 	    (mv & ~ESO_MIXREG_LMVM_MUTE) << 2;
   1925 	mv = eso_read_mixreg(sc, ESO_MIXREG_LMVM);
   1926 	sc->sc_gain[ESO_MASTER_VOL][ESO_RIGHT] =
   1927 	    (mv & ~ESO_MIXREG_RMVM_MUTE) << 2;
   1928 	/* Currently both channels are muted simultaneously; either is OK. */
   1929 	sc->sc_mvmute = (mv & ESO_MIXREG_RMVM_MUTE) != 0;
   1930 }
   1931 
   1932 static void
   1933 eso_set_gain(struct eso_softc *sc, unsigned int port)
   1934 {
   1935 	uint8_t mixreg, tmp;
   1936 
   1937 	switch (port) {
   1938 	case ESO_DAC_PLAY_VOL:
   1939 		mixreg = ESO_MIXREG_PVR_A2;
   1940 		break;
   1941 	case ESO_MIC_PLAY_VOL:
   1942 		mixreg = ESO_MIXREG_PVR_MIC;
   1943 		break;
   1944 	case ESO_LINE_PLAY_VOL:
   1945 		mixreg = ESO_MIXREG_PVR_LINE;
   1946 		break;
   1947 	case ESO_SYNTH_PLAY_VOL:
   1948 		mixreg = ESO_MIXREG_PVR_SYNTH;
   1949 		break;
   1950 	case ESO_CD_PLAY_VOL:
   1951 		mixreg = ESO_MIXREG_PVR_CD;
   1952 		break;
   1953 	case ESO_AUXB_PLAY_VOL:
   1954 		mixreg = ESO_MIXREG_PVR_AUXB;
   1955 		break;
   1956 
   1957 	case ESO_DAC_REC_VOL:
   1958 		mixreg = ESO_MIXREG_RVR_A2;
   1959 		break;
   1960 	case ESO_MIC_REC_VOL:
   1961 		mixreg = ESO_MIXREG_RVR_MIC;
   1962 		break;
   1963 	case ESO_LINE_REC_VOL:
   1964 		mixreg = ESO_MIXREG_RVR_LINE;
   1965 		break;
   1966 	case ESO_SYNTH_REC_VOL:
   1967 		mixreg = ESO_MIXREG_RVR_SYNTH;
   1968 		break;
   1969 	case ESO_CD_REC_VOL:
   1970 		mixreg = ESO_MIXREG_RVR_CD;
   1971 		break;
   1972 	case ESO_AUXB_REC_VOL:
   1973 		mixreg = ESO_MIXREG_RVR_AUXB;
   1974 		break;
   1975 	case ESO_MONO_PLAY_VOL:
   1976 		mixreg = ESO_MIXREG_PVR_MONO;
   1977 		break;
   1978 	case ESO_MONO_REC_VOL:
   1979 		mixreg = ESO_MIXREG_RVR_MONO;
   1980 		break;
   1981 
   1982 	case ESO_PCSPEAKER_VOL:
   1983 		/* Special case - only 3-bit, mono, and reserved bits. */
   1984 		tmp = eso_read_mixreg(sc, ESO_MIXREG_PCSVR);
   1985 		tmp &= ESO_MIXREG_PCSVR_RESV;
   1986 		/* Map bits 7:5 -> 2:0. */
   1987 		tmp |= (sc->sc_gain[port][ESO_LEFT] >> 5);
   1988 		eso_write_mixreg(sc, ESO_MIXREG_PCSVR, tmp);
   1989 		return;
   1990 
   1991 	case ESO_MASTER_VOL:
   1992 		/* Special case - separate regs, and 6-bit precision. */
   1993 		/* Map bits 7:2 -> 5:0, reflect mute settings. */
   1994 		eso_write_mixreg(sc, ESO_MIXREG_LMVM,
   1995 		    (sc->sc_gain[port][ESO_LEFT] >> 2) |
   1996 		    (sc->sc_mvmute ? ESO_MIXREG_LMVM_MUTE : 0x00));
   1997 		eso_write_mixreg(sc, ESO_MIXREG_RMVM,
   1998 		    (sc->sc_gain[port][ESO_RIGHT] >> 2) |
   1999 		    (sc->sc_mvmute ? ESO_MIXREG_RMVM_MUTE : 0x00));
   2000 		return;
   2001 
   2002 	case ESO_SPATIALIZER:
   2003 		/* Special case - only `mono', and higher precision. */
   2004 		eso_write_mixreg(sc, ESO_MIXREG_SPATLVL,
   2005 		    sc->sc_gain[port][ESO_LEFT]);
   2006 		return;
   2007 
   2008 	case ESO_RECORD_VOL:
   2009 		/* Very Special case, controller register. */
   2010 		eso_write_ctlreg(sc, ESO_CTLREG_RECLVL,ESO_4BIT_GAIN_TO_STEREO(
   2011 		   sc->sc_gain[port][ESO_LEFT], sc->sc_gain[port][ESO_RIGHT]));
   2012 		return;
   2013 
   2014 	default:
   2015 #ifdef DIAGNOSTIC
   2016 		panic("eso_set_gain: bad port %u", port);
   2017 		/* NOTREACHED */
   2018 #else
   2019 		return;
   2020 #endif
   2021 	}
   2022 
   2023 	eso_write_mixreg(sc, mixreg, ESO_4BIT_GAIN_TO_STEREO(
   2024 	    sc->sc_gain[port][ESO_LEFT], sc->sc_gain[port][ESO_RIGHT]));
   2025 }
   2026