xref: /openbsd-src/sys/dev/pci/eso.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /*	$OpenBSD: eso.c,v 1.54 2023/03/08 04:43:08 guenther 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 int
632 eso_open(void *hdl, int flags)
633 {
634 	return (0);
635 }
636 
637 void
638 eso_close(void *hdl)
639 {
640 }
641 
642 int
643 eso_set_params(void *hdl, int setmode, int usemode,
644     struct audio_params *play, struct audio_params *rec)
645 {
646 	struct eso_softc *sc = hdl;
647 	struct audio_params *p;
648 	int mode, r[2], rd[2], ar[2], clk;
649 	uint srg, fltdiv;
650 
651 	for (mode = AUMODE_RECORD; mode != -1;
652 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
653 		if ((setmode & mode) == 0)
654 			continue;
655 
656 		p = (mode == AUMODE_PLAY) ? play : rec;
657 
658 		if (p->sample_rate < ESO_MINRATE)
659 			p->sample_rate = ESO_MINRATE;
660 		if (p->sample_rate > ESO_MAXRATE)
661 			p->sample_rate = ESO_MAXRATE;
662 		if (p->precision > 16)
663 			p->precision = 16;
664 		if (p->channels > 2)
665 			p->channels = 2;
666 
667 		switch (p->encoding) {
668 		case AUDIO_ENCODING_SLINEAR_BE:
669 		case AUDIO_ENCODING_ULINEAR_BE:
670 			if (p->precision != 8)
671 				return EINVAL;
672 			break;
673 		case AUDIO_ENCODING_SLINEAR_LE:
674 		case AUDIO_ENCODING_ULINEAR_LE:
675 			break;
676 		default:
677 			return (EINVAL);
678 		}
679 		p->bps = AUDIO_BPS(p->precision);
680 		p->msb = 1;
681 
682 		/*
683 		 * We'll compute both possible sample rate dividers and pick
684 		 * the one with the least error.
685 		 */
686 #define ABS(x) ((x) < 0 ? -(x) : (x))
687 		r[0] = ESO_CLK0 /
688 		    (128 - (rd[0] = 128 - ESO_CLK0 / p->sample_rate));
689 		r[1] = ESO_CLK1 /
690 		    (128 - (rd[1] = 128 - ESO_CLK1 / p->sample_rate));
691 
692 		ar[0] = p->sample_rate - r[0];
693 		ar[1] = p->sample_rate - r[1];
694 		clk = ABS(ar[0]) > ABS(ar[1]) ? 1 : 0;
695 		srg = rd[clk] | (clk == 1 ? ESO_CLK1_SELECT : 0x00);
696 
697 		/* Roll-off frequency of 87%, as in the ES1888 driver. */
698 		fltdiv = 256 - 200279L / r[clk];
699 
700 		/* Update to reflect the possibly inexact rate. */
701 		p->sample_rate = r[clk];
702 
703 		if (mode == AUMODE_RECORD) {
704 			/* Audio 1 */
705 			DPRINTF(("A1 srg 0x%02x fdiv 0x%02x\n", srg, fltdiv));
706 			eso_write_ctlreg(sc, ESO_CTLREG_SRG, srg);
707 			eso_write_ctlreg(sc, ESO_CTLREG_FLTDIV, fltdiv);
708 		} else {
709 			/* Audio 2 */
710 			DPRINTF(("A2 srg 0x%02x fdiv 0x%02x\n", srg, fltdiv));
711 			eso_write_mixreg(sc, ESO_MIXREG_A2SRG, srg);
712 			eso_write_mixreg(sc, ESO_MIXREG_A2FLTDIV, fltdiv);
713 		}
714 #undef ABS
715 
716 	}
717 
718 	return (0);
719 }
720 
721 int
722 eso_round_blocksize(void *hdl, int blk)
723 {
724 	return ((blk + 31) & -32); /* keep good alignment; at least 16 req'd */
725 }
726 
727 int
728 eso_halt_output(void *hdl)
729 {
730 	struct eso_softc *sc = hdl;
731 	int error;
732 
733 	DPRINTF(("%s: halt_output\n", sc->sc_dev.dv_xname));
734 
735 	/*
736 	 * Disable auto-initialize DMA, allowing the FIFO to drain and then
737 	 * stop.  The interrupt callback pointer is cleared at this
738 	 * point so that an outstanding FIFO interrupt for the remaining data
739 	 * will be acknowledged without further processing.
740 	 *
741 	 * This does not immediately `abort' an operation in progress (c.f.
742 	 * audio(9)) but is the method to leave the FIFO behind in a clean
743 	 * state with the least hair.  (Besides, that item needs to be
744 	 * rephrased for trigger_*()-based DMA environments.)
745 	 */
746 	mtx_enter(&audio_lock);
747 	eso_write_mixreg(sc, ESO_MIXREG_A2C1,
748 	    ESO_MIXREG_A2C1_FIFOENB | ESO_MIXREG_A2C1_DMAENB);
749 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM,
750 	    ESO_IO_A2DMAM_DMAENB);
751 
752 	sc->sc_pintr = NULL;
753 	error = msleep_nsec(&sc->sc_pintr, &audio_lock, PWAIT | PNORELOCK,
754 	    "esoho", MSEC_TO_NSEC(sc->sc_pdrain));
755 
756 	/* Shut down DMA completely. */
757 	eso_write_mixreg(sc, ESO_MIXREG_A2C1, 0);
758 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM, 0);
759 
760 	return (error == EWOULDBLOCK ? 0 : error);
761 }
762 
763 int
764 eso_halt_input(void *hdl)
765 {
766 	struct eso_softc *sc = hdl;
767 	int error;
768 
769 	DPRINTF(("%s: halt_input\n", sc->sc_dev.dv_xname));
770 
771 	/* Just like eso_halt_output(), but for Audio 1. */
772 	mtx_enter(&audio_lock);
773 	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
774 	    ESO_CTLREG_A1C2_READ | ESO_CTLREG_A1C2_ADC |
775 	    ESO_CTLREG_A1C2_DMAENB);
776 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MODE,
777 	    DMA37MD_WRITE | DMA37MD_DEMAND);
778 
779 	sc->sc_rintr = NULL;
780 	error = msleep_nsec(&sc->sc_rintr, &audio_lock, PWAIT | PNORELOCK,
781 	    "esohi", MSEC_TO_NSEC(sc->sc_rdrain));
782 
783 	/* Shut down DMA completely. */
784 	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
785 	    ESO_CTLREG_A1C2_READ | ESO_CTLREG_A1C2_ADC);
786 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK,
787 	    ESO_DMAC_MASK_MASK);
788 
789 	return (error == EWOULDBLOCK ? 0 : error);
790 }
791 
792 int
793 eso_set_port(void *hdl, mixer_ctrl_t *cp)
794 {
795 	struct eso_softc *sc = hdl;
796 	uint lgain, rgain;
797 	uint8_t tmp;
798 	int rc = 0;
799 
800 	mtx_enter(&audio_lock);
801 	switch (cp->dev) {
802 	case ESO_DAC_PLAY_VOL:
803 	case ESO_MIC_PLAY_VOL:
804 	case ESO_LINE_PLAY_VOL:
805 	case ESO_SYNTH_PLAY_VOL:
806 	case ESO_CD_PLAY_VOL:
807 	case ESO_AUXB_PLAY_VOL:
808 	case ESO_RECORD_VOL:
809 	case ESO_DAC_REC_VOL:
810 	case ESO_MIC_REC_VOL:
811 	case ESO_LINE_REC_VOL:
812 	case ESO_SYNTH_REC_VOL:
813 	case ESO_CD_REC_VOL:
814 	case ESO_AUXB_REC_VOL:
815 		if (cp->type != AUDIO_MIXER_VALUE)
816 			goto error;
817 
818 		/*
819 		 * Stereo-capable mixer ports: if we get a single-channel
820 		 * gain value passed in, then we duplicate it to both left
821 		 * and right channels.
822 		 */
823 		switch (cp->un.value.num_channels) {
824 		case 1:
825 			lgain = rgain = ESO_GAIN_TO_4BIT(
826 			    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
827 			break;
828 		case 2:
829 			lgain = ESO_GAIN_TO_4BIT(
830 			    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
831 			rgain = ESO_GAIN_TO_4BIT(
832 			    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
833 			break;
834 		default:
835 			goto error;
836 		}
837 
838 		sc->sc_gain[cp->dev][ESO_LEFT] = lgain;
839 		sc->sc_gain[cp->dev][ESO_RIGHT] = rgain;
840 		eso_set_gain(sc, cp->dev);
841 		break;
842 
843 	case ESO_MASTER_VOL:
844 		if (cp->type != AUDIO_MIXER_VALUE)
845 			goto error;
846 
847 		/* Like above, but a precision of 6 bits. */
848 		switch (cp->un.value.num_channels) {
849 		case 1:
850 			lgain = rgain = ESO_GAIN_TO_6BIT(
851 			    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
852 			break;
853 		case 2:
854 			lgain = ESO_GAIN_TO_6BIT(
855 			    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
856 			rgain = ESO_GAIN_TO_6BIT(
857 			    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
858 			break;
859 		default:
860 			goto error;
861 		}
862 
863 		sc->sc_gain[cp->dev][ESO_LEFT] = lgain;
864 		sc->sc_gain[cp->dev][ESO_RIGHT] = rgain;
865 		eso_set_gain(sc, cp->dev);
866 		break;
867 
868 	case ESO_SPATIALIZER:
869 		if (cp->type != AUDIO_MIXER_VALUE ||
870 		    cp->un.value.num_channels != 1)
871 			goto error;
872 
873 		sc->sc_gain[cp->dev][ESO_LEFT] =
874 		    sc->sc_gain[cp->dev][ESO_RIGHT] =
875 		    ESO_GAIN_TO_6BIT(
876 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
877 		eso_set_gain(sc, cp->dev);
878 		break;
879 
880 	case ESO_MONO_PLAY_VOL:
881 	case ESO_MONO_REC_VOL:
882 		if (cp->type != AUDIO_MIXER_VALUE ||
883 		    cp->un.value.num_channels != 1)
884 			goto error;
885 
886 		sc->sc_gain[cp->dev][ESO_LEFT] =
887 		    sc->sc_gain[cp->dev][ESO_RIGHT] =
888 		    ESO_GAIN_TO_4BIT(
889 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
890 		eso_set_gain(sc, cp->dev);
891 		break;
892 
893 	case ESO_PCSPEAKER_VOL:
894 		if (cp->type != AUDIO_MIXER_VALUE ||
895 		    cp->un.value.num_channels != 1)
896 			goto error;
897 
898 		sc->sc_gain[cp->dev][ESO_LEFT] =
899 		    sc->sc_gain[cp->dev][ESO_RIGHT] =
900 		    ESO_GAIN_TO_3BIT(
901 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
902 		eso_set_gain(sc, cp->dev);
903 		break;
904 
905 	case ESO_SPATIALIZER_ENABLE:
906 		if (cp->type != AUDIO_MIXER_ENUM)
907 			goto error;
908 
909 		sc->sc_spatializer = (cp->un.ord != 0);
910 
911 		tmp = eso_read_mixreg(sc, ESO_MIXREG_SPAT);
912 		if (sc->sc_spatializer)
913 			tmp |= ESO_MIXREG_SPAT_ENB;
914 		else
915 			tmp &= ~ESO_MIXREG_SPAT_ENB;
916 		eso_write_mixreg(sc, ESO_MIXREG_SPAT,
917 		    tmp | ESO_MIXREG_SPAT_RSTREL);
918 		break;
919 
920 	case ESO_MASTER_MUTE:
921 		if (cp->type != AUDIO_MIXER_ENUM)
922 			goto error;
923 
924 		sc->sc_mvmute = (cp->un.ord != 0);
925 
926 		if (sc->sc_mvmute) {
927 			eso_write_mixreg(sc, ESO_MIXREG_LMVM,
928 			    eso_read_mixreg(sc, ESO_MIXREG_LMVM) |
929 			    ESO_MIXREG_LMVM_MUTE);
930 			eso_write_mixreg(sc, ESO_MIXREG_RMVM,
931 			    eso_read_mixreg(sc, ESO_MIXREG_RMVM) |
932 			    ESO_MIXREG_RMVM_MUTE);
933 		} else {
934 			eso_write_mixreg(sc, ESO_MIXREG_LMVM,
935 			    eso_read_mixreg(sc, ESO_MIXREG_LMVM) &
936 			    ~ESO_MIXREG_LMVM_MUTE);
937 			eso_write_mixreg(sc, ESO_MIXREG_RMVM,
938 			    eso_read_mixreg(sc, ESO_MIXREG_RMVM) &
939 			    ~ESO_MIXREG_RMVM_MUTE);
940 		}
941 		break;
942 
943 	case ESO_MONOOUT_SOURCE:
944 		if (cp->type != AUDIO_MIXER_ENUM)
945 			goto error;
946 
947 		rc = eso_set_monooutsrc(sc, cp->un.ord);
948 		break;
949 
950 	case ESO_MONOIN_BYPASS:
951 		if (cp->type != AUDIO_MIXER_ENUM)
952 			goto error;
953 
954 		rc = eso_set_monoinbypass(sc, cp->un.ord);
955 		break;
956 
957 	case ESO_RECORD_MONITOR:
958 		if (cp->type != AUDIO_MIXER_ENUM)
959 			goto error;
960 
961 		sc->sc_recmon = (cp->un.ord != 0);
962 
963 		tmp = eso_read_ctlreg(sc, ESO_CTLREG_ACTL);
964 		if (sc->sc_recmon)
965 			tmp |= ESO_CTLREG_ACTL_RECMON;
966 		else
967 			tmp &= ~ESO_CTLREG_ACTL_RECMON;
968 		eso_write_ctlreg(sc, ESO_CTLREG_ACTL, tmp);
969 		break;
970 
971 	case ESO_RECORD_SOURCE:
972 		if (cp->type != AUDIO_MIXER_ENUM)
973 			goto error;
974 
975 		rc = eso_set_recsrc(sc, cp->un.ord);
976 		break;
977 
978 	case ESO_MIC_PREAMP:
979 		if (cp->type != AUDIO_MIXER_ENUM)
980 			goto error;
981 
982 		rc = eso_set_preamp(sc, cp->un.ord);
983 		break;
984 
985 	default:
986 		goto error;
987 	}
988 
989 	mtx_leave(&audio_lock);
990 	return rc;
991 error:
992 	mtx_leave(&audio_lock);
993 	return EINVAL;
994 }
995 
996 int
997 eso_get_port(void *hdl, mixer_ctrl_t *cp)
998 {
999 	struct eso_softc *sc = hdl;
1000 
1001 	mtx_enter(&audio_lock);
1002 	switch (cp->dev) {
1003 	case ESO_MASTER_VOL:
1004 		/* Reload from mixer after hardware volume control use. */
1005 		if (sc->sc_gain[cp->dev][ESO_LEFT] == (uint8_t)~0)
1006 			eso_reload_master_vol(sc);
1007 		/* FALLTHROUGH */
1008 	case ESO_DAC_PLAY_VOL:
1009 	case ESO_MIC_PLAY_VOL:
1010 	case ESO_LINE_PLAY_VOL:
1011 	case ESO_SYNTH_PLAY_VOL:
1012 	case ESO_CD_PLAY_VOL:
1013 	case ESO_AUXB_PLAY_VOL:
1014 	case ESO_RECORD_VOL:
1015 	case ESO_DAC_REC_VOL:
1016 	case ESO_MIC_REC_VOL:
1017 	case ESO_LINE_REC_VOL:
1018 	case ESO_SYNTH_REC_VOL:
1019 	case ESO_CD_REC_VOL:
1020 	case ESO_AUXB_REC_VOL:
1021 		/*
1022 		 * Stereo-capable ports: if a single-channel query is made,
1023 		 * just return the left channel's value (since single-channel
1024 		 * settings themselves are applied to both channels).
1025 		 */
1026 		switch (cp->un.value.num_channels) {
1027 		case 1:
1028 			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1029 			    sc->sc_gain[cp->dev][ESO_LEFT];
1030 			break;
1031 		case 2:
1032 			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1033 			    sc->sc_gain[cp->dev][ESO_LEFT];
1034 			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1035 			    sc->sc_gain[cp->dev][ESO_RIGHT];
1036 			break;
1037 		default:
1038 			goto error;
1039 		}
1040 		break;
1041 
1042 	case ESO_MONO_PLAY_VOL:
1043 	case ESO_PCSPEAKER_VOL:
1044 	case ESO_MONO_REC_VOL:
1045 	case ESO_SPATIALIZER:
1046 		if (cp->un.value.num_channels != 1)
1047 			goto error;
1048 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1049 		    sc->sc_gain[cp->dev][ESO_LEFT];
1050 		break;
1051 
1052 	case ESO_RECORD_MONITOR:
1053 		cp->un.ord = sc->sc_recmon;
1054 		break;
1055 
1056 	case ESO_RECORD_SOURCE:
1057 		cp->un.ord = sc->sc_recsrc;
1058 		break;
1059 
1060 	case ESO_MONOOUT_SOURCE:
1061 		cp->un.ord = sc->sc_monooutsrc;
1062 		break;
1063 
1064 	case ESO_MONOIN_BYPASS:
1065 		cp->un.ord = sc->sc_monoinbypass;
1066 		break;
1067 
1068 	case ESO_SPATIALIZER_ENABLE:
1069 		cp->un.ord = sc->sc_spatializer;
1070 		break;
1071 
1072 	case ESO_MIC_PREAMP:
1073 		cp->un.ord = sc->sc_preamp;
1074 		break;
1075 
1076 	case ESO_MASTER_MUTE:
1077 		/* Reload from mixer after hardware volume control use. */
1078 		if (sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] == (uint8_t)~0)
1079 			eso_reload_master_vol(sc);
1080 		cp->un.ord = sc->sc_mvmute;
1081 		break;
1082 
1083 	default:
1084 		goto error;
1085 	}
1086 
1087 	mtx_leave(&audio_lock);
1088 	return 0;
1089 error:
1090 	mtx_leave(&audio_lock);
1091 	return EINVAL;
1092 }
1093 
1094 int
1095 eso_query_devinfo(void *hdl, mixer_devinfo_t *dip)
1096 {
1097 	switch (dip->index) {
1098 	case ESO_DAC_PLAY_VOL:
1099 		dip->mixer_class = ESO_INPUT_CLASS;
1100 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1101 		strlcpy(dip->label.name, AudioNdac, sizeof dip->label.name);
1102 		dip->type = AUDIO_MIXER_VALUE;
1103 		dip->un.v.num_channels = 2;
1104 		strlcpy(dip->un.v.units.name, AudioNvolume,
1105 		    sizeof dip->un.v.units.name);
1106 		break;
1107 	case ESO_MIC_PLAY_VOL:
1108 		dip->mixer_class = ESO_INPUT_CLASS;
1109 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1110 		strlcpy(dip->label.name, AudioNmicrophone,
1111 		    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_LINE_PLAY_VOL:
1118 		dip->mixer_class = ESO_INPUT_CLASS;
1119 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1120 		strlcpy(dip->label.name, AudioNline, sizeof dip->label.name);
1121 		dip->type = AUDIO_MIXER_VALUE;
1122 		dip->un.v.num_channels = 2;
1123 		strlcpy(dip->un.v.units.name, AudioNvolume,
1124 		    sizeof dip->un.v.units.name);
1125 		break;
1126 	case ESO_SYNTH_PLAY_VOL:
1127 		dip->mixer_class = ESO_INPUT_CLASS;
1128 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1129 		strlcpy(dip->label.name, AudioNfmsynth,
1130 		    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_MONO_PLAY_VOL:
1137 		dip->mixer_class = ESO_INPUT_CLASS;
1138 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1139 		strlcpy(dip->label.name, "mono_in", sizeof dip->label.name);
1140 		dip->type = AUDIO_MIXER_VALUE;
1141 		dip->un.v.num_channels = 1;
1142 		strlcpy(dip->un.v.units.name, AudioNvolume,
1143 		    sizeof dip->un.v.units.name);
1144 		break;
1145 	case ESO_CD_PLAY_VOL:
1146 		dip->mixer_class = ESO_INPUT_CLASS;
1147 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1148 		strlcpy(dip->label.name, AudioNcd, sizeof dip->label.name);
1149 		dip->type = AUDIO_MIXER_VALUE;
1150 		dip->un.v.num_channels = 2;
1151 		strlcpy(dip->un.v.units.name, AudioNvolume,
1152 		    sizeof dip->un.v.units.name);
1153 		break;
1154 	case ESO_AUXB_PLAY_VOL:
1155 		dip->mixer_class = ESO_INPUT_CLASS;
1156 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1157 		strlcpy(dip->label.name, "auxb", sizeof dip->label.name);
1158 		dip->type = AUDIO_MIXER_VALUE;
1159 		dip->un.v.num_channels = 2;
1160 		strlcpy(dip->un.v.units.name, AudioNvolume,
1161 		    sizeof dip->un.v.units.name);
1162 		break;
1163 	case ESO_MIC_PREAMP:
1164 		dip->mixer_class = ESO_MICROPHONE_CLASS;
1165 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1166 		strlcpy(dip->label.name, AudioNpreamp, sizeof dip->label.name);
1167 		dip->type = AUDIO_MIXER_ENUM;
1168 		dip->un.e.num_mem = 2;
1169 		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1170 		    sizeof dip->un.e.member[0].label.name);
1171 		dip->un.e.member[0].ord = 0;
1172 		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1173 		    sizeof dip->un.e.member[1].label.name);
1174 		dip->un.e.member[1].ord = 1;
1175 		break;
1176 	case ESO_MICROPHONE_CLASS:
1177 		dip->mixer_class = ESO_MICROPHONE_CLASS;
1178 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1179 		strlcpy(dip->label.name, AudioNmicrophone,
1180 		    sizeof dip->label.name);
1181 		dip->type = AUDIO_MIXER_CLASS;
1182 		break;
1183 	case ESO_INPUT_CLASS:
1184 		dip->mixer_class = ESO_INPUT_CLASS;
1185 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1186 		strlcpy(dip->label.name, AudioCinputs, sizeof dip->label.name);
1187 		dip->type = AUDIO_MIXER_CLASS;
1188 		break;
1189 	case ESO_MASTER_VOL:
1190 		dip->mixer_class = ESO_OUTPUT_CLASS;
1191 		dip->prev = AUDIO_MIXER_LAST;
1192 		dip->next = ESO_MASTER_MUTE;
1193 		strlcpy(dip->label.name, AudioNmaster, sizeof dip->label.name);
1194 		dip->type = AUDIO_MIXER_VALUE;
1195 		dip->un.v.num_channels = 2;
1196 		strlcpy(dip->un.v.units.name, AudioNvolume,
1197 		    sizeof dip->un.v.units.name);
1198 		break;
1199 	case ESO_MASTER_MUTE:
1200 		dip->mixer_class = ESO_OUTPUT_CLASS;
1201 		dip->prev = ESO_MASTER_VOL;
1202 		dip->next = AUDIO_MIXER_LAST;
1203 		strlcpy(dip->label.name, AudioNmute, sizeof dip->label.name);
1204 		dip->type = AUDIO_MIXER_ENUM;
1205 		dip->un.e.num_mem = 2;
1206 		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1207 		    sizeof dip->un.e.member[0].label.name);
1208 		dip->un.e.member[0].ord = 0;
1209 		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1210 		    sizeof dip->un.e.member[1].label.name);
1211 		dip->un.e.member[1].ord = 1;
1212 		break;
1213 	case ESO_PCSPEAKER_VOL:
1214 		dip->mixer_class = ESO_OUTPUT_CLASS;
1215 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1216 		strlcpy(dip->label.name, "pc_speaker", sizeof dip->label.name);
1217 		dip->type = AUDIO_MIXER_VALUE;
1218 		dip->un.v.num_channels = 1;
1219 		strlcpy(dip->un.v.units.name, AudioNvolume,
1220 		    sizeof dip->un.v.units.name);
1221 		break;
1222 	case ESO_MONOOUT_SOURCE:
1223 		dip->mixer_class = ESO_OUTPUT_CLASS;
1224 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1225 		strlcpy(dip->label.name, "mono_out", sizeof dip->label.name);
1226 		dip->type = AUDIO_MIXER_ENUM;
1227 		dip->un.e.num_mem = 3;
1228 		strlcpy(dip->un.e.member[0].label.name, AudioNmute,
1229 		    sizeof dip->un.e.member[0].label.name);
1230 		dip->un.e.member[0].ord = ESO_MIXREG_MPM_MOMUTE;
1231 		strlcpy(dip->un.e.member[1].label.name, AudioNdac,
1232 		    sizeof dip->un.e.member[1].label.name);
1233 		dip->un.e.member[1].ord = ESO_MIXREG_MPM_MOA2R;
1234 		strlcpy(dip->un.e.member[2].label.name, AudioNmixerout,
1235 		    sizeof dip->un.e.member[2].label.name);
1236 		dip->un.e.member[2].ord = ESO_MIXREG_MPM_MOREC;
1237 		break;
1238 	case ESO_MONOIN_BYPASS:
1239 		dip->mixer_class = ESO_MONOIN_CLASS;
1240 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1241 		strlcpy(dip->label.name, "bypass", sizeof dip->label.name);
1242 		dip->type = AUDIO_MIXER_ENUM;
1243 		dip->un.e.num_mem = 2;
1244 		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1245 		    sizeof dip->un.e.member[0].label.name);
1246 		dip->un.e.member[0].ord = 0;
1247 		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1248 		    sizeof dip->un.e.member[1].label.name);
1249 		dip->un.e.member[1].ord = 1;
1250 		break;
1251 	case ESO_MONOIN_CLASS:
1252 		dip->mixer_class = ESO_MONOIN_CLASS;
1253 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1254 		strlcpy(dip->label.name, "mono_in", sizeof dip->label.name);
1255 		dip->type = AUDIO_MIXER_CLASS;
1256 		break;
1257 	case ESO_SPATIALIZER:
1258 		dip->mixer_class = ESO_OUTPUT_CLASS;
1259 		dip->prev = AUDIO_MIXER_LAST;
1260 		dip->next = ESO_SPATIALIZER_ENABLE;
1261 		strlcpy(dip->label.name, AudioNspatial,
1262 		    sizeof dip->label.name);
1263 		dip->type = AUDIO_MIXER_VALUE;
1264 		dip->un.v.num_channels = 1;
1265 		strlcpy(dip->un.v.units.name, "level",
1266 		    sizeof dip->un.v.units.name);
1267 		break;
1268 	case ESO_SPATIALIZER_ENABLE:
1269 		dip->mixer_class = ESO_OUTPUT_CLASS;
1270 		dip->prev = ESO_SPATIALIZER;
1271 		dip->next = AUDIO_MIXER_LAST;
1272 		strlcpy(dip->label.name, "enable", sizeof dip->label.name);
1273 		dip->type = AUDIO_MIXER_ENUM;
1274 		dip->un.e.num_mem = 2;
1275 		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1276 		    sizeof dip->un.e.member[0].label.name);
1277 		dip->un.e.member[0].ord = 0;
1278 		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1279 		    sizeof dip->un.e.member[1].label.name);
1280 		dip->un.e.member[1].ord = 1;
1281 		break;
1282 	case ESO_OUTPUT_CLASS:
1283 		dip->mixer_class = ESO_OUTPUT_CLASS;
1284 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1285 		strlcpy(dip->label.name, AudioCoutputs,
1286 		    sizeof dip->label.name);
1287 		dip->type = AUDIO_MIXER_CLASS;
1288 		break;
1289 	case ESO_RECORD_MONITOR:
1290 		dip->mixer_class = ESO_MONITOR_CLASS;
1291 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1292 		strlcpy(dip->label.name, AudioNmute, sizeof dip->label.name);
1293 		dip->type = AUDIO_MIXER_ENUM;
1294 		dip->un.e.num_mem = 2;
1295 		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1296 		    sizeof dip->un.e.member[0].label.name);
1297 		dip->un.e.member[0].ord = 0;
1298 		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1299 		    sizeof dip->un.e.member[1].label.name);
1300 		dip->un.e.member[1].ord = 1;
1301 		break;
1302 	case ESO_MONITOR_CLASS:
1303 		dip->mixer_class = ESO_MONITOR_CLASS;
1304 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1305 		strlcpy(dip->label.name, AudioCmonitor,
1306 		    sizeof dip->label.name);
1307 		dip->type = AUDIO_MIXER_CLASS;
1308 		break;
1309 	case ESO_RECORD_VOL:
1310 		dip->mixer_class = ESO_RECORD_CLASS;
1311 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1312 		strlcpy(dip->label.name, AudioNrecord, sizeof dip->label.name);
1313 		dip->type = AUDIO_MIXER_VALUE;
1314 		strlcpy(dip->un.v.units.name, AudioNvolume,
1315 		    sizeof dip->un.v.units.name);
1316 		break;
1317 	case ESO_RECORD_SOURCE:
1318 		dip->mixer_class = ESO_RECORD_CLASS;
1319 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1320 		strlcpy(dip->label.name, AudioNsource, sizeof dip->label.name);
1321 		dip->type = AUDIO_MIXER_ENUM;
1322 		dip->un.e.num_mem = 4;
1323 		strlcpy(dip->un.e.member[0].label.name, AudioNmicrophone,
1324 		    sizeof dip->un.e.member[0].label.name);
1325 		dip->un.e.member[0].ord = ESO_MIXREG_ERS_MIC;
1326 		strlcpy(dip->un.e.member[1].label.name, AudioNline,
1327 		    sizeof dip->un.e.member[1].label.name);
1328 		dip->un.e.member[1].ord = ESO_MIXREG_ERS_LINE;
1329 		strlcpy(dip->un.e.member[2].label.name, AudioNcd,
1330 		    sizeof dip->un.e.member[2].label.name);
1331 		dip->un.e.member[2].ord = ESO_MIXREG_ERS_CD;
1332 		strlcpy(dip->un.e.member[3].label.name, AudioNmixerout,
1333 		    sizeof dip->un.e.member[3].label.name);
1334 		dip->un.e.member[3].ord = ESO_MIXREG_ERS_MIXER;
1335 		break;
1336 	case ESO_DAC_REC_VOL:
1337 		dip->mixer_class = ESO_RECORD_CLASS;
1338 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1339 		strlcpy(dip->label.name, AudioNdac, sizeof dip->label.name);
1340 		dip->type = AUDIO_MIXER_VALUE;
1341 		dip->un.v.num_channels = 2;
1342 		strlcpy(dip->un.v.units.name, AudioNvolume,
1343 		    sizeof dip->un.v.units.name);
1344 		break;
1345 	case ESO_MIC_REC_VOL:
1346 		dip->mixer_class = ESO_RECORD_CLASS;
1347 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1348 		strlcpy(dip->label.name, AudioNmicrophone,
1349 		    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_LINE_REC_VOL:
1356 		dip->mixer_class = ESO_RECORD_CLASS;
1357 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1358 		strlcpy(dip->label.name, AudioNline, sizeof dip->label.name);
1359 		dip->type = AUDIO_MIXER_VALUE;
1360 		dip->un.v.num_channels = 2;
1361 		strlcpy(dip->un.v.units.name, AudioNvolume,
1362 		    sizeof dip->un.v.units.name);
1363 		break;
1364 	case ESO_SYNTH_REC_VOL:
1365 		dip->mixer_class = ESO_RECORD_CLASS;
1366 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1367 		strlcpy(dip->label.name, AudioNfmsynth,
1368 		    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_MONO_REC_VOL:
1375 		dip->mixer_class = ESO_RECORD_CLASS;
1376 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1377 		strlcpy(dip->label.name, "mono_in", sizeof dip->label.name);
1378 		dip->type = AUDIO_MIXER_VALUE;
1379 		dip->un.v.num_channels = 1; /* No lies */
1380 		strlcpy(dip->un.v.units.name, AudioNvolume,
1381 		    sizeof dip->un.v.units.name);
1382 		break;
1383 	case ESO_CD_REC_VOL:
1384 		dip->mixer_class = ESO_RECORD_CLASS;
1385 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1386 		strlcpy(dip->label.name, AudioNcd, sizeof dip->label.name);
1387 		dip->type = AUDIO_MIXER_VALUE;
1388 		dip->un.v.num_channels = 2;
1389 		strlcpy(dip->un.v.units.name, AudioNvolume,
1390 		    sizeof dip->un.v.units.name);
1391 		break;
1392 	case ESO_AUXB_REC_VOL:
1393 		dip->mixer_class = ESO_RECORD_CLASS;
1394 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1395 		strlcpy(dip->label.name, "auxb", sizeof dip->label.name);
1396 		dip->type = AUDIO_MIXER_VALUE;
1397 		dip->un.v.num_channels = 2;
1398 		strlcpy(dip->un.v.units.name, AudioNvolume,
1399 		    sizeof dip->un.v.units.name);
1400 		break;
1401 	case ESO_RECORD_CLASS:
1402 		dip->mixer_class = ESO_RECORD_CLASS;
1403 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1404 		strlcpy(dip->label.name, AudioCrecord, sizeof dip->label.name);
1405 		dip->type = AUDIO_MIXER_CLASS;
1406 		break;
1407 	default:
1408 		return (ENXIO);
1409 	}
1410 
1411 	return (0);
1412 }
1413 
1414 int
1415 eso_allocmem(struct eso_softc *sc, size_t size, size_t align,
1416     size_t boundary, int flags, int direction, struct eso_dma *ed)
1417 {
1418 	int error, wait;
1419 
1420 	wait = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
1421 	ed->ed_size = size;
1422 
1423 	error = bus_dmamem_alloc(ed->ed_dmat, ed->ed_size, align, boundary,
1424 	    ed->ed_segs, sizeof (ed->ed_segs) / sizeof (ed->ed_segs[0]),
1425 	    &ed->ed_nsegs, wait);
1426 	if (error)
1427 		goto out;
1428 
1429 	error = bus_dmamem_map(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs,
1430 	    ed->ed_size, &ed->ed_addr, wait | BUS_DMA_COHERENT);
1431 	if (error)
1432 		goto free;
1433 
1434 	error = bus_dmamap_create(ed->ed_dmat, ed->ed_size, 1, ed->ed_size,
1435 	    boundary,  wait, &ed->ed_map);
1436 	if (error)
1437 		goto unmap;
1438 
1439 	error = bus_dmamap_load(ed->ed_dmat, ed->ed_map, ed->ed_addr,
1440 	    ed->ed_size, NULL, wait |
1441 	    ((direction == AUMODE_RECORD) ? BUS_DMA_READ : BUS_DMA_WRITE));
1442 	if (error)
1443 		goto destroy;
1444 
1445 	return (0);
1446 
1447  destroy:
1448 	bus_dmamap_destroy(ed->ed_dmat, ed->ed_map);
1449  unmap:
1450 	bus_dmamem_unmap(ed->ed_dmat, ed->ed_addr, ed->ed_size);
1451  free:
1452 	bus_dmamem_free(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs);
1453  out:
1454 	return (error);
1455 }
1456 
1457 void
1458 eso_freemem(struct eso_dma *ed)
1459 {
1460 	bus_dmamap_unload(ed->ed_dmat, ed->ed_map);
1461 	bus_dmamap_destroy(ed->ed_dmat, ed->ed_map);
1462 	bus_dmamem_unmap(ed->ed_dmat, ed->ed_addr, ed->ed_size);
1463 	bus_dmamem_free(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs);
1464 }
1465 
1466 void *
1467 eso_allocm(void *hdl, int direction, size_t size, int type, int flags)
1468 {
1469 	struct eso_softc *sc = hdl;
1470 	struct eso_dma *ed;
1471 	size_t boundary;
1472 	int error;
1473 
1474 	if ((ed = malloc(sizeof (*ed), type, flags)) == NULL)
1475 		return (NULL);
1476 
1477 	/*
1478 	 * Apparently the Audio 1 DMA controller's current address
1479 	 * register can't roll over a 64K address boundary, so we have to
1480 	 * take care of that ourselves.  Similarly, the Audio 2 DMA
1481 	 * controller needs a 1M address boundary.
1482 	 */
1483 	if (direction == AUMODE_RECORD)
1484 		boundary = 0x10000;
1485 	else
1486 		boundary = 0x100000;
1487 
1488 	/*
1489 	 * XXX Work around allocation problems for Audio 1, which
1490 	 * XXX implements the 24 low address bits only, with
1491 	 * XXX machine-specific DMA tag use.
1492 	 */
1493 #if defined(__alpha__)
1494 	/*
1495 	 * XXX Force allocation through the (ISA) SGMAP.
1496 	 */
1497 	if (direction == AUMODE_RECORD)
1498 		ed->ed_dmat = alphabus_dma_get_tag(sc->sc_dmat, ALPHA_BUS_ISA);
1499 	else
1500 #elif defined(__amd64__) || defined(__i386__)
1501 	/*
1502 	 * XXX Force allocation through the ISA DMA tag.
1503 	 */
1504 	if (direction == AUMODE_RECORD)
1505 		ed->ed_dmat = &isa_bus_dma_tag;
1506 	else
1507 #endif
1508 		ed->ed_dmat = sc->sc_dmat;
1509 
1510 	error = eso_allocmem(sc, size, 32, boundary, flags, direction, ed);
1511 	if (error) {
1512 		free(ed, type, sizeof(*ed));
1513 		return (NULL);
1514 	}
1515 	ed->ed_next = sc->sc_dmas;
1516 	sc->sc_dmas = ed;
1517 
1518 	return (KVADDR(ed));
1519 }
1520 
1521 void
1522 eso_freem(void *hdl, void *addr, int type)
1523 {
1524 	struct eso_softc *sc = hdl;
1525 	struct eso_dma *p, **pp;
1526 
1527 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->ed_next) {
1528 		if (KVADDR(p) == addr) {
1529 			eso_freemem(p);
1530 			*pp = p->ed_next;
1531 			free(p, type, sizeof(*p));
1532 			return;
1533 		}
1534 	}
1535 }
1536 
1537 size_t
1538 eso_round_buffersize(void *hdl, int direction, size_t bufsize)
1539 {
1540 	size_t maxsize;
1541 
1542 	/*
1543 	 * The playback DMA buffer size on the Solo-1 is limited to 0xfff0
1544 	 * bytes.  This is because IO_A2DMAC is a two byte value
1545 	 * indicating the literal byte count, and the 4 least significant
1546 	 * bits are read-only.  Zero is not used as a special case for
1547 	 * 0x10000.
1548 	 *
1549 	 * For recording, DMAC_DMAC is the byte count - 1, so 0x10000 can
1550 	 * be represented.
1551 	 */
1552 	maxsize = (direction == AUMODE_PLAY) ? 0xfff0 : 0x10000;
1553 
1554 	if (bufsize > maxsize)
1555 		bufsize = maxsize;
1556 
1557 	return (bufsize);
1558 }
1559 
1560 int
1561 eso_trigger_output(void *hdl, void *start, void *end, int blksize,
1562     void (*intr)(void *), void *arg, struct audio_params *param)
1563 {
1564 	struct eso_softc *sc = hdl;
1565 	struct eso_dma *ed;
1566 	uint8_t a2c1;
1567 
1568 	DPRINTF((
1569 	    "%s: trigger_output: start %p, end %p, blksize %d, intr %p(%p)\n",
1570 	    sc->sc_dev.dv_xname, start, end, blksize, intr, arg));
1571 	DPRINTF(("%s: param: rate %lu, encoding %u, precision %u, channels %u\n",
1572 	    sc->sc_dev.dv_xname, param->sample_rate, param->encoding,
1573 	    param->precision, param->channels));
1574 
1575 	/* Find DMA buffer. */
1576 	for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != start;
1577 	     ed = ed->ed_next)
1578 		;
1579 	if (ed == NULL) {
1580 		printf("%s: trigger_output: bad addr %p\n",
1581 		    sc->sc_dev.dv_xname, start);
1582 		return (EINVAL);
1583 	}
1584 	DPRINTF(("%s: output dmaaddr %lx\n",
1585 	    sc->sc_dev.dv_xname, (unsigned long)DMAADDR(ed)));
1586 
1587 	sc->sc_pintr = intr;
1588 	sc->sc_parg = arg;
1589 
1590 	/* Compute drain timeout (milliseconds). */
1591 	sc->sc_pdrain = 1000 * (blksize * 3 / 2) /
1592 	    (param->sample_rate * param->channels * param->bps);
1593 
1594 	/* DMA transfer count (in `words'!) reload using 2's complement. */
1595 	blksize = -(blksize >> 1);
1596 	eso_write_mixreg(sc, ESO_MIXREG_A2TCRLO, blksize & 0xff);
1597 	eso_write_mixreg(sc, ESO_MIXREG_A2TCRHI, blksize >> 8);
1598 
1599 	/* Update DAC to reflect DMA count and audio parameters. */
1600 	/* Note: we cache A2C2 in order to avoid r/m/w at interrupt time. */
1601 	if (param->precision == 16)
1602 		sc->sc_a2c2 |= ESO_MIXREG_A2C2_16BIT;
1603 	else
1604 		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_16BIT;
1605 	if (param->channels == 2)
1606 		sc->sc_a2c2 |= ESO_MIXREG_A2C2_STEREO;
1607 	else
1608 		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_STEREO;
1609 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1610 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1611 		sc->sc_a2c2 |= ESO_MIXREG_A2C2_SIGNED;
1612 	else
1613 		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_SIGNED;
1614 	/* Unmask IRQ. */
1615 	sc->sc_a2c2 |= ESO_MIXREG_A2C2_IRQM;
1616 	eso_write_mixreg(sc, ESO_MIXREG_A2C2, sc->sc_a2c2);
1617 
1618 	/* Set up DMA controller. */
1619 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAA, DMAADDR(ed));
1620 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAC,
1621 	    (uint8_t *)end - (uint8_t *)start);
1622 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM,
1623 	    ESO_IO_A2DMAM_DMAENB | ESO_IO_A2DMAM_AUTO);
1624 
1625 	/* Start DMA. */
1626 	mtx_enter(&audio_lock);
1627 	a2c1 = eso_read_mixreg(sc, ESO_MIXREG_A2C1);
1628 	a2c1 &= ~ESO_MIXREG_A2C1_RESV0; /* Paranoia? XXX bit 5 */
1629 	a2c1 |= ESO_MIXREG_A2C1_FIFOENB | ESO_MIXREG_A2C1_DMAENB |
1630 	    ESO_MIXREG_A2C1_AUTO;
1631 	eso_write_mixreg(sc, ESO_MIXREG_A2C1, a2c1);
1632 	mtx_leave(&audio_lock);
1633 	return (0);
1634 }
1635 
1636 int
1637 eso_trigger_input(void *hdl, void *start, void *end, int blksize,
1638     void (*intr)(void *), void *arg, struct audio_params *param)
1639 {
1640 	struct eso_softc *sc = hdl;
1641 	struct eso_dma *ed;
1642 	uint8_t actl, a1c1;
1643 
1644 	DPRINTF((
1645 	    "%s: trigger_input: start %p, end %p, blksize %d, intr %p(%p)\n",
1646 	    sc->sc_dev.dv_xname, start, end, blksize, intr, arg));
1647 	DPRINTF(("%s: param: rate %lu, encoding %u, precision %u, channels %u\n",
1648 	    sc->sc_dev.dv_xname, param->sample_rate, param->encoding,
1649 	    param->precision, param->channels));
1650 
1651 	/*
1652 	 * If we failed to configure the Audio 1 DMA controller, bail here
1653 	 * while retaining availability of the DAC direction (in Audio 2).
1654 	 */
1655 	if (!sc->sc_dmac_configured)
1656 		return (EIO);
1657 
1658 	/* Find DMA buffer. */
1659 	for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != start;
1660 	     ed = ed->ed_next)
1661 		;
1662 	if (ed == NULL) {
1663 		printf("%s: trigger_input: bad addr %p\n",
1664 		    sc->sc_dev.dv_xname, start);
1665 		return (EINVAL);
1666 	}
1667 	DPRINTF(("%s: input dmaaddr %lx\n",
1668 	    sc->sc_dev.dv_xname, (unsigned long)DMAADDR(ed)));
1669 
1670 	sc->sc_rintr = intr;
1671 	sc->sc_rarg = arg;
1672 
1673 	/* Compute drain timeout (milliseconds). */
1674 	sc->sc_rdrain = 1000 * (blksize * 3 / 2) /
1675 	    (param->sample_rate * param->channels * param->bps);
1676 
1677 	/* Set up ADC DMA converter parameters. */
1678 	actl = eso_read_ctlreg(sc, ESO_CTLREG_ACTL);
1679 	if (param->channels == 2) {
1680 		actl &= ~ESO_CTLREG_ACTL_MONO;
1681 		actl |= ESO_CTLREG_ACTL_STEREO;
1682 	} else {
1683 		actl &= ~ESO_CTLREG_ACTL_STEREO;
1684 		actl |= ESO_CTLREG_ACTL_MONO;
1685 	}
1686 	eso_write_ctlreg(sc, ESO_CTLREG_ACTL, actl);
1687 
1688 	/* Set up Transfer Type: maybe move to attach time? */
1689 	eso_write_ctlreg(sc, ESO_CTLREG_A1TT, ESO_CTLREG_A1TT_DEMAND4);
1690 
1691 	/* DMA transfer count reload using 2's complement. */
1692 	blksize = -blksize;
1693 	eso_write_ctlreg(sc, ESO_CTLREG_A1TCRLO, blksize & 0xff);
1694 	eso_write_ctlreg(sc, ESO_CTLREG_A1TCRHI, blksize >> 8);
1695 
1696 	/* Set up and enable Audio 1 DMA FIFO. */
1697 	a1c1 = ESO_CTLREG_A1C1_RESV1 | ESO_CTLREG_A1C1_FIFOENB;
1698 	if (param->precision == 16)
1699 		a1c1 |= ESO_CTLREG_A1C1_16BIT;
1700 	if (param->channels == 2)
1701 		a1c1 |= ESO_CTLREG_A1C1_STEREO;
1702 	else
1703 		a1c1 |= ESO_CTLREG_A1C1_MONO;
1704 	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1705 	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1706 		a1c1 |= ESO_CTLREG_A1C1_SIGNED;
1707 	eso_write_ctlreg(sc, ESO_CTLREG_A1C1, a1c1);
1708 
1709 	/* Set up ADC IRQ/DRQ parameters. */
1710 	eso_write_ctlreg(sc, ESO_CTLREG_LAIC,
1711 	    ESO_CTLREG_LAIC_PINENB | ESO_CTLREG_LAIC_EXTENB);
1712 	eso_write_ctlreg(sc, ESO_CTLREG_DRQCTL,
1713 	    ESO_CTLREG_DRQCTL_ENB1 | ESO_CTLREG_DRQCTL_EXTENB);
1714 
1715 	/* Set up and enable DMA controller. */
1716 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_CLEAR, 0);
1717 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK,
1718 	    ESO_DMAC_MASK_MASK);
1719 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MODE,
1720 	    DMA37MD_WRITE | DMA37MD_LOOP | DMA37MD_DEMAND);
1721 	bus_space_write_4(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_DMAA,
1722 	    DMAADDR(ed));
1723 	bus_space_write_2(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_DMAC,
1724 	    (uint8_t *)end - (uint8_t *)start - 1);
1725 	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK, 0);
1726 
1727 	/* Start DMA. */
1728 	mtx_enter(&audio_lock);
1729 	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
1730 	    ESO_CTLREG_A1C2_DMAENB | ESO_CTLREG_A1C2_READ |
1731 	    ESO_CTLREG_A1C2_AUTO | ESO_CTLREG_A1C2_ADC);
1732 	mtx_leave(&audio_lock);
1733 	return (0);
1734 }
1735 
1736 /*
1737  * Mixer utility functions.
1738  */
1739 int
1740 eso_set_recsrc(struct eso_softc *sc, u_int recsrc)
1741 {
1742 	mixer_devinfo_t di;
1743 	int i, error;
1744 
1745 	di.index = ESO_RECORD_SOURCE;
1746 	error = eso_query_devinfo(sc, &di);
1747 	if (error != 0) {
1748 		printf("eso_set_recsrc: eso_query_devinfo failed");
1749 		return (error);
1750 	}
1751 
1752 	for (i = 0; i < di.un.e.num_mem; i++) {
1753 		if (recsrc == di.un.e.member[i].ord) {
1754 			eso_write_mixreg(sc, ESO_MIXREG_ERS, recsrc);
1755 			sc->sc_recsrc = recsrc;
1756 			return (0);
1757 		}
1758 	}
1759 
1760 	return (EINVAL);
1761 }
1762 
1763 int
1764 eso_set_monooutsrc(struct eso_softc *sc, uint monooutsrc)
1765 {
1766 	mixer_devinfo_t di;
1767 	int i, error;
1768 	uint8_t mpm;
1769 
1770 	di.index = ESO_MONOOUT_SOURCE;
1771 	error = eso_query_devinfo(sc, &di);
1772 	if (error != 0) {
1773 		printf("eso_set_monooutsrc: eso_query_devinfo failed");
1774 		return (error);
1775 	}
1776 
1777 	for (i = 0; i < di.un.e.num_mem; i++) {
1778 		if (monooutsrc == di.un.e.member[i].ord) {
1779 			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1780 			mpm &= ~ESO_MIXREG_MPM_MOMASK;
1781 			mpm |= monooutsrc;
1782 			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1783 			sc->sc_monooutsrc = monooutsrc;
1784 			return (0);
1785 		}
1786 	}
1787 
1788 	return (EINVAL);
1789 }
1790 
1791 int
1792 eso_set_monoinbypass(struct eso_softc *sc, uint monoinbypass)
1793 {
1794 	mixer_devinfo_t di;
1795 	int i, error;
1796 	uint8_t mpm;
1797 
1798 	di.index = ESO_MONOIN_BYPASS;
1799 	error = eso_query_devinfo(sc, &di);
1800 	if (error != 0) {
1801 		printf("eso_set_monoinbypass: eso_query_devinfo failed");
1802 		return (error);
1803 	}
1804 
1805 	for (i = 0; i < di.un.e.num_mem; i++) {
1806 		if (monoinbypass == di.un.e.member[i].ord) {
1807 			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1808 			mpm &= ~(ESO_MIXREG_MPM_MOMASK | ESO_MIXREG_MPM_RESV0);
1809 			mpm |= (monoinbypass ? ESO_MIXREG_MPM_MIBYPASS : 0);
1810 			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1811 			sc->sc_monoinbypass = monoinbypass;
1812 			return (0);
1813 		}
1814 	}
1815 
1816 	return (EINVAL);
1817 }
1818 
1819 int
1820 eso_set_preamp(struct eso_softc *sc, uint preamp)
1821 {
1822 	mixer_devinfo_t di;
1823 	int i, error;
1824 	uint8_t mpm;
1825 
1826 	di.index = ESO_MIC_PREAMP;
1827 	error = eso_query_devinfo(sc, &di);
1828 	if (error != 0) {
1829 		printf("eso_set_preamp: eso_query_devinfo failed");
1830 		return (error);
1831 	}
1832 
1833 	for (i = 0; i < di.un.e.num_mem; i++) {
1834 		if (preamp == di.un.e.member[i].ord) {
1835 			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1836 			mpm &= ~(ESO_MIXREG_MPM_PREAMP | ESO_MIXREG_MPM_RESV0);
1837 			mpm |= (preamp ? ESO_MIXREG_MPM_PREAMP : 0);
1838 			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1839 			sc->sc_preamp = preamp;
1840 			return (0);
1841 		}
1842 	}
1843 
1844 	return (EINVAL);
1845 }
1846 
1847 /*
1848  * Reload Master Volume and Mute values in softc from mixer; used when
1849  * those have previously been invalidated by use of hardware volume controls.
1850  */
1851 void
1852 eso_reload_master_vol(struct eso_softc *sc)
1853 {
1854 	uint8_t mv;
1855 
1856 	mv = eso_read_mixreg(sc, ESO_MIXREG_LMVM);
1857 	sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] =
1858 	    (mv & ~ESO_MIXREG_LMVM_MUTE) << 2;
1859 	mv = eso_read_mixreg(sc, ESO_MIXREG_LMVM);
1860 	sc->sc_gain[ESO_MASTER_VOL][ESO_RIGHT] =
1861 	    (mv & ~ESO_MIXREG_RMVM_MUTE) << 2;
1862 	/* Currently both channels are muted simultaneously; either is OK. */
1863 	sc->sc_mvmute = (mv & ESO_MIXREG_RMVM_MUTE) != 0;
1864 }
1865 
1866 void
1867 eso_set_gain(struct eso_softc *sc, uint port)
1868 {
1869 	uint8_t mixreg, tmp;
1870 
1871 	switch (port) {
1872 	case ESO_DAC_PLAY_VOL:
1873 		mixreg = ESO_MIXREG_PVR_A2;
1874 		break;
1875 	case ESO_MIC_PLAY_VOL:
1876 		mixreg = ESO_MIXREG_PVR_MIC;
1877 		break;
1878 	case ESO_LINE_PLAY_VOL:
1879 		mixreg = ESO_MIXREG_PVR_LINE;
1880 		break;
1881 	case ESO_SYNTH_PLAY_VOL:
1882 		mixreg = ESO_MIXREG_PVR_SYNTH;
1883 		break;
1884 	case ESO_CD_PLAY_VOL:
1885 		mixreg = ESO_MIXREG_PVR_CD;
1886 		break;
1887 	case ESO_AUXB_PLAY_VOL:
1888 		mixreg = ESO_MIXREG_PVR_AUXB;
1889 		break;
1890 	case ESO_DAC_REC_VOL:
1891 		mixreg = ESO_MIXREG_RVR_A2;
1892 		break;
1893 	case ESO_MIC_REC_VOL:
1894 		mixreg = ESO_MIXREG_RVR_MIC;
1895 		break;
1896 	case ESO_LINE_REC_VOL:
1897 		mixreg = ESO_MIXREG_RVR_LINE;
1898 		break;
1899 	case ESO_SYNTH_REC_VOL:
1900 		mixreg = ESO_MIXREG_RVR_SYNTH;
1901 		break;
1902 	case ESO_CD_REC_VOL:
1903 		mixreg = ESO_MIXREG_RVR_CD;
1904 		break;
1905 	case ESO_AUXB_REC_VOL:
1906 		mixreg = ESO_MIXREG_RVR_AUXB;
1907 		break;
1908 	case ESO_MONO_PLAY_VOL:
1909 		mixreg = ESO_MIXREG_PVR_MONO;
1910 		break;
1911 	case ESO_MONO_REC_VOL:
1912 		mixreg = ESO_MIXREG_RVR_MONO;
1913 		break;
1914 	case ESO_PCSPEAKER_VOL:
1915 		/* Special case - only 3-bit, mono, and reserved bits. */
1916 		tmp = eso_read_mixreg(sc, ESO_MIXREG_PCSVR);
1917 		tmp &= ESO_MIXREG_PCSVR_RESV;
1918 		/* Map bits 7:5 -> 2:0. */
1919 		tmp |= (sc->sc_gain[port][ESO_LEFT] >> 5);
1920 		eso_write_mixreg(sc, ESO_MIXREG_PCSVR, tmp);
1921 		return;
1922 	case ESO_MASTER_VOL:
1923 		/* Special case - separate regs, and 6-bit precision. */
1924 		/* Map bits 7:2 -> 5:0, reflect mute settings. */
1925 		eso_write_mixreg(sc, ESO_MIXREG_LMVM,
1926 		    (sc->sc_gain[port][ESO_LEFT] >> 2) |
1927 		    (sc->sc_mvmute ? ESO_MIXREG_LMVM_MUTE : 0x00));
1928 		eso_write_mixreg(sc, ESO_MIXREG_RMVM,
1929 		    (sc->sc_gain[port][ESO_RIGHT] >> 2) |
1930 		    (sc->sc_mvmute ? ESO_MIXREG_RMVM_MUTE : 0x00));
1931 		return;
1932 	case ESO_SPATIALIZER:
1933 		/* Special case - only `mono', and higher precision. */
1934 		eso_write_mixreg(sc, ESO_MIXREG_SPATLVL,
1935 		    sc->sc_gain[port][ESO_LEFT]);
1936 		return;
1937 	case ESO_RECORD_VOL:
1938 		/* Very Special case, controller register. */
1939 		eso_write_ctlreg(sc, ESO_CTLREG_RECLVL,ESO_4BIT_GAIN_TO_STEREO(
1940 		   sc->sc_gain[port][ESO_LEFT], sc->sc_gain[port][ESO_RIGHT]));
1941 		return;
1942 	default:
1943 #ifdef DIAGNOSTIC
1944 		printf("eso_set_gain: bad port %u", port);
1945 		return;
1946 		/* NOTREACHED */
1947 #else
1948 		return;
1949 #endif
1950 		}
1951 
1952 	eso_write_mixreg(sc, mixreg, ESO_4BIT_GAIN_TO_STEREO(
1953 	    sc->sc_gain[port][ESO_LEFT], sc->sc_gain[port][ESO_RIGHT]));
1954 }
1955 
1956 int
1957 eso_activate(struct device *self, int act)
1958 {
1959 	struct eso_softc *sc = (struct eso_softc *)self;
1960 	uint8_t tmp;
1961 	int rv = 0;
1962 
1963 	switch (act) {
1964 	case DVACT_QUIESCE:
1965 		rv = config_activate_children(self, act);
1966 		tmp = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL);
1967 		tmp &= ~(ESO_IO_IRQCTL_MASK);
1968 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL, tmp);
1969 		break;
1970 	case DVACT_SUSPEND:
1971 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM, 0);
1972 		bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh,
1973 		    ESO_DMAC_CLEAR, 0);
1974 		bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh,
1975 		    ESO_SB_STATUSFLAGS, 3);
1976 		/* shut down dma */
1977 		pci_conf_write(sc->sc_pa.pa_pc, sc->sc_pa.pa_tag,
1978 		    ESO_PCI_DDMAC, 0);
1979 		break;
1980 	case DVACT_RESUME:
1981 		eso_setup(sc, 1, 1);
1982 		pci_conf_write(sc->sc_pa.pa_pc, sc->sc_pa.pa_tag,
1983 		    ESO_PCI_DDMAC, sc->sc_dmac_addr | ESO_PCI_DDMAC_DE);
1984 		rv = config_activate_children(self, act);
1985 		break;
1986 	default:
1987 		rv = config_activate_children(self, act);
1988 		break;
1989 	}
1990 	return (rv);
1991 }
1992