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