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