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