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