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