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