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