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