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