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