eso.c revision 1.35
1/*	$OpenBSD: eso.c,v 1.35 2010/09/21 20:11:44 jakemsr 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, eso_intr, sc,
263					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	int s;
538
539	/* DPRINTF(("mixreg 0x%02x = 0x%02x\n", reg, val)); */
540
541	s = splaudio();
542	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERADDR, reg);
543	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERDATA, val);
544	splx(s);
545}
546
547uint8_t
548eso_read_mixreg(struct eso_softc *sc, uint8_t reg)
549{
550	int s;
551	uint8_t val;
552
553	s = splaudio();
554	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERADDR, reg);
555	val = bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_MIXERDATA);
556	splx(s);
557
558	return (val);
559}
560
561int
562eso_intr(void *hdl)
563{
564	struct eso_softc *sc = hdl;
565	uint8_t irqctl;
566
567	irqctl = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL);
568
569	/* If it wasn't ours, that's all she wrote. */
570	if ((irqctl & (ESO_IO_IRQCTL_A1IRQ | ESO_IO_IRQCTL_A2IRQ |
571	    ESO_IO_IRQCTL_HVIRQ | ESO_IO_IRQCTL_MPUIRQ)) == 0)
572		return (0);
573
574	if (irqctl & ESO_IO_IRQCTL_A1IRQ) {
575		/* Clear interrupt. */
576		(void)bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
577		    ESO_SB_RBSR);
578
579		if (sc->sc_rintr)
580			sc->sc_rintr(sc->sc_rarg);
581		else
582			wakeup(&sc->sc_rintr);
583	}
584
585	if (irqctl & ESO_IO_IRQCTL_A2IRQ) {
586		/*
587		 * Clear the A2 IRQ latch: the cached value reflects the
588		 * current DAC settings with the IRQ latch bit not set.
589		 */
590		eso_write_mixreg(sc, ESO_MIXREG_A2C2, sc->sc_a2c2);
591
592		if (sc->sc_pintr)
593			sc->sc_pintr(sc->sc_parg);
594		else
595			wakeup(&sc->sc_pintr);
596	}
597
598	if (irqctl & ESO_IO_IRQCTL_HVIRQ) {
599		/* Clear interrupt. */
600		eso_write_mixreg(sc, ESO_MIXREG_CHVIR, ESO_MIXREG_CHVIR_CHVIR);
601
602		/*
603		 * Raise a flag to cause a lazy update of the in-softc gain
604		 * values the next time the software mixer is read to keep
605		 * interrupt service cost low.  ~0 cannot occur otherwise
606		 * as the master volume has a precision of 6 bits only.
607		 */
608		sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] = (uint8_t)~0;
609	}
610
611#if NMPU > 0
612	if ((irqctl & ESO_IO_IRQCTL_MPUIRQ) && sc->sc_mpudev != NULL)
613		mpu_intr(sc->sc_mpudev);
614#endif
615
616	return (1);
617}
618
619/* Perform a software reset, including DMA FIFOs. */
620int
621eso_reset(struct eso_softc *sc)
622{
623	int i;
624
625	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_RESET,
626	    ESO_SB_RESET_SW | ESO_SB_RESET_FIFO);
627	/* `Delay' suggested in the data sheet. */
628	(void)bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_STATUS);
629	bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh, ESO_SB_RESET, 0);
630
631	/* Wait for reset to take effect. */
632	for (i = 0; i < ESO_RESET_TIMEOUT; i++) {
633		/* Poll for data to become available. */
634		if ((bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
635		    ESO_SB_RBSR) & ESO_SB_RBSR_RDAV) != 0 &&
636		    bus_space_read_1(sc->sc_sb_iot, sc->sc_sb_ioh,
637			ESO_SB_RDR) == ESO_SB_RDR_RESETMAGIC) {
638
639			/* Activate Solo-1 extension commands. */
640			eso_write_cmd(sc, ESO_CMD_EXTENB);
641			/* Reset mixer registers. */
642			eso_write_mixreg(sc, ESO_MIXREG_RESET,
643			    ESO_MIXREG_RESET_RESET);
644
645			return (0);
646		} else {
647			delay(1000);
648		}
649	}
650
651	printf("%s: reset timeout\n", sc->sc_dev.dv_xname);
652	return (-1);
653}
654
655
656/* ARGSUSED */
657int
658eso_open(void *hdl, int flags)
659{
660	return (0);
661}
662
663void
664eso_close(void *hdl)
665{
666}
667
668int
669eso_query_encoding(void *hdl, struct audio_encoding *fp)
670{
671	switch (fp->index) {
672	case 0:
673		strlcpy(fp->name, AudioEulinear, sizeof fp->name);
674		fp->encoding = AUDIO_ENCODING_ULINEAR;
675		fp->precision = 8;
676		fp->flags = 0;
677		break;
678	case 1:
679		strlcpy(fp->name, AudioEmulaw, sizeof fp->name);
680		fp->encoding = AUDIO_ENCODING_ULAW;
681		fp->precision = 8;
682		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
683		break;
684	case 2:
685		strlcpy(fp->name, AudioEalaw, sizeof fp->name);
686		fp->encoding = AUDIO_ENCODING_ALAW;
687		fp->precision = 8;
688		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
689		break;
690	case 3:
691		strlcpy(fp->name, AudioEslinear, sizeof fp->name);
692		fp->encoding = AUDIO_ENCODING_SLINEAR;
693		fp->precision = 8;
694		fp->flags = 0;
695		break;
696	case 4:
697		strlcpy(fp->name, AudioEslinear_le, sizeof fp->name);
698		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
699		fp->precision = 16;
700		fp->flags = 0;
701		break;
702	case 5:
703		strlcpy(fp->name, AudioEulinear_le, sizeof fp->name);
704		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
705		fp->precision = 16;
706		fp->flags = 0;
707		break;
708	case 6:
709		strlcpy(fp->name, AudioEslinear_be, sizeof fp->name);
710		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
711		fp->precision = 16;
712		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
713		break;
714	case 7:
715		strlcpy(fp->name, AudioEulinear_be, sizeof fp->name);
716		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
717		fp->precision = 16;
718		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
719		break;
720	default:
721		return (EINVAL);
722	}
723	fp->bps = AUDIO_BPS(fp->precision);
724	fp->msb = 1;
725
726	return (0);
727}
728
729void
730eso_get_default_params(void *addr, int mode, struct audio_params *params)
731{
732	params->sample_rate = 48000;
733	params->encoding = AUDIO_ENCODING_ULINEAR_LE;
734	params->precision = 16;
735	params->bps = 2;
736	params->msb = 1;
737	params->channels = 2;
738	params->sw_code = NULL;
739	params->factor = 1;
740}
741
742int
743eso_set_params(void *hdl, int setmode, int usemode,
744    struct audio_params *play, struct audio_params *rec)
745{
746	struct eso_softc *sc = hdl;
747	struct audio_params *p;
748	int mode, r[2], rd[2], ar[2], clk;
749	uint srg, fltdiv;
750
751	for (mode = AUMODE_RECORD; mode != -1;
752	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
753		if ((setmode & mode) == 0)
754			continue;
755
756		p = (mode == AUMODE_PLAY) ? play : rec;
757
758		if (p->sample_rate < ESO_MINRATE)
759			p->sample_rate = ESO_MINRATE;
760		if (p->sample_rate > ESO_MAXRATE)
761			p->sample_rate = ESO_MAXRATE;
762		if (p->precision > 16)
763			p->precision = 16;
764		if (p->channels > 2)
765			p->channels = 2;
766
767		p->factor = 1;
768		p->sw_code = NULL;
769		switch (p->encoding) {
770		case AUDIO_ENCODING_SLINEAR_BE:
771		case AUDIO_ENCODING_ULINEAR_BE:
772			if (p->precision == 16)
773				p->sw_code = swap_bytes;
774			break;
775		case AUDIO_ENCODING_SLINEAR_LE:
776		case AUDIO_ENCODING_ULINEAR_LE:
777			break;
778		case AUDIO_ENCODING_ULAW:
779			if (mode == AUMODE_PLAY) {
780				p->factor = 2;
781				p->sw_code = mulaw_to_ulinear16_le;
782			} else {
783				p->sw_code = ulinear8_to_mulaw;
784			}
785			break;
786		case AUDIO_ENCODING_ALAW:
787			if (mode == AUMODE_PLAY) {
788				p->factor = 2;
789				p->sw_code = alaw_to_ulinear16_le;
790			} else {
791				p->sw_code = ulinear8_to_alaw;
792			}
793			break;
794		default:
795			return (EINVAL);
796		}
797		p->bps = AUDIO_BPS(p->precision);
798		p->msb = 1;
799
800		/*
801		 * We'll compute both possible sample rate dividers and pick
802		 * the one with the least error.
803		 */
804#define ABS(x) ((x) < 0 ? -(x) : (x))
805		r[0] = ESO_CLK0 /
806		    (128 - (rd[0] = 128 - ESO_CLK0 / p->sample_rate));
807		r[1] = ESO_CLK1 /
808		    (128 - (rd[1] = 128 - ESO_CLK1 / p->sample_rate));
809
810		ar[0] = p->sample_rate - r[0];
811		ar[1] = p->sample_rate - r[1];
812		clk = ABS(ar[0]) > ABS(ar[1]) ? 1 : 0;
813		srg = rd[clk] | (clk == 1 ? ESO_CLK1_SELECT : 0x00);
814
815		/* Roll-off frequency of 87%, as in the ES1888 driver. */
816		fltdiv = 256 - 200279L / r[clk];
817
818		/* Update to reflect the possibly inexact rate. */
819		p->sample_rate = r[clk];
820
821		if (mode == AUMODE_RECORD) {
822			/* Audio 1 */
823			DPRINTF(("A1 srg 0x%02x fdiv 0x%02x\n", srg, fltdiv));
824			eso_write_ctlreg(sc, ESO_CTLREG_SRG, srg);
825			eso_write_ctlreg(sc, ESO_CTLREG_FLTDIV, fltdiv);
826		} else {
827			/* Audio 2 */
828			DPRINTF(("A2 srg 0x%02x fdiv 0x%02x\n", srg, fltdiv));
829			eso_write_mixreg(sc, ESO_MIXREG_A2SRG, srg);
830			eso_write_mixreg(sc, ESO_MIXREG_A2FLTDIV, fltdiv);
831		}
832#undef ABS
833
834	}
835
836	return (0);
837}
838
839int
840eso_round_blocksize(void *hdl, int blk)
841{
842	return ((blk + 31) & -32); /* keep good alignment; at least 16 req'd */
843}
844
845int
846eso_halt_output(void *hdl)
847{
848	struct eso_softc *sc = hdl;
849	int error, s;
850
851	DPRINTF(("%s: halt_output\n", sc->sc_dev.dv_xname));
852
853	/*
854	 * Disable auto-initialize DMA, allowing the FIFO to drain and then
855	 * stop.  The interrupt callback pointer is cleared at this
856	 * point so that an outstanding FIFO interrupt for the remaining data
857	 * will be acknowledged without further processing.
858	 *
859	 * This does not immediately `abort' an operation in progress (c.f.
860	 * audio(9)) but is the method to leave the FIFO behind in a clean
861	 * state with the least hair.  (Besides, that item needs to be
862	 * rephrased for trigger_*()-based DMA environments.)
863	 */
864	s = splaudio();
865	eso_write_mixreg(sc, ESO_MIXREG_A2C1,
866	    ESO_MIXREG_A2C1_FIFOENB | ESO_MIXREG_A2C1_DMAENB);
867	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM,
868	    ESO_IO_A2DMAM_DMAENB);
869
870	sc->sc_pintr = NULL;
871	error = tsleep(&sc->sc_pintr, PWAIT, "esoho", sc->sc_pdrain);
872	splx(s);
873
874	/* Shut down DMA completely. */
875	eso_write_mixreg(sc, ESO_MIXREG_A2C1, 0);
876	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM, 0);
877
878	return (error == EWOULDBLOCK ? 0 : error);
879}
880
881int
882eso_halt_input(void *hdl)
883{
884	struct eso_softc *sc = hdl;
885	int error, s;
886
887	DPRINTF(("%s: halt_input\n", sc->sc_dev.dv_xname));
888
889	/* Just like eso_halt_output(), but for Audio 1. */
890	s = splaudio();
891	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
892	    ESO_CTLREG_A1C2_READ | ESO_CTLREG_A1C2_ADC |
893	    ESO_CTLREG_A1C2_DMAENB);
894	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MODE,
895	    DMA37MD_WRITE | DMA37MD_DEMAND);
896
897	sc->sc_rintr = NULL;
898	error = tsleep(&sc->sc_rintr, PWAIT, "esohi", sc->sc_rdrain);
899	splx(s);
900
901	/* Shut down DMA completely. */
902	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
903	    ESO_CTLREG_A1C2_READ | ESO_CTLREG_A1C2_ADC);
904	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK,
905	    ESO_DMAC_MASK_MASK);
906
907	return (error == EWOULDBLOCK ? 0 : error);
908}
909
910int
911eso_getdev(void *hdl, struct audio_device *retp)
912{
913	struct eso_softc *sc = hdl;
914
915	strlcpy(retp->name, "ESS Solo-1", sizeof retp->name);
916	snprintf(retp->version, sizeof retp->version, "0x%02x",
917	    sc->sc_revision);
918	if (sc->sc_revision <
919	    sizeof (eso_rev2model) / sizeof (eso_rev2model[0]))
920		strlcpy(retp->config, eso_rev2model[sc->sc_revision],
921		    sizeof retp->config);
922	else
923		strlcpy(retp->config, "unknown", sizeof retp->config);
924
925	return (0);
926}
927
928int
929eso_set_port(void *hdl, mixer_ctrl_t *cp)
930{
931	struct eso_softc *sc = hdl;
932	uint lgain, rgain;
933	uint8_t tmp;
934
935	switch (cp->dev) {
936	case ESO_DAC_PLAY_VOL:
937	case ESO_MIC_PLAY_VOL:
938	case ESO_LINE_PLAY_VOL:
939	case ESO_SYNTH_PLAY_VOL:
940	case ESO_CD_PLAY_VOL:
941	case ESO_AUXB_PLAY_VOL:
942	case ESO_RECORD_VOL:
943	case ESO_DAC_REC_VOL:
944	case ESO_MIC_REC_VOL:
945	case ESO_LINE_REC_VOL:
946	case ESO_SYNTH_REC_VOL:
947	case ESO_CD_REC_VOL:
948	case ESO_AUXB_REC_VOL:
949		if (cp->type != AUDIO_MIXER_VALUE)
950			return (EINVAL);
951
952		/*
953		 * Stereo-capable mixer ports: if we get a single-channel
954		 * gain value passed in, then we duplicate it to both left
955		 * and right channels.
956		 */
957		switch (cp->un.value.num_channels) {
958		case 1:
959			lgain = rgain = ESO_GAIN_TO_4BIT(
960			    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
961			break;
962		case 2:
963			lgain = ESO_GAIN_TO_4BIT(
964			    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
965			rgain = ESO_GAIN_TO_4BIT(
966			    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
967			break;
968		default:
969			return (EINVAL);
970		}
971
972		sc->sc_gain[cp->dev][ESO_LEFT] = lgain;
973		sc->sc_gain[cp->dev][ESO_RIGHT] = rgain;
974		eso_set_gain(sc, cp->dev);
975		break;
976
977	case ESO_MASTER_VOL:
978		if (cp->type != AUDIO_MIXER_VALUE)
979			return (EINVAL);
980
981		/* Like above, but a precision of 6 bits. */
982		switch (cp->un.value.num_channels) {
983		case 1:
984			lgain = rgain = ESO_GAIN_TO_6BIT(
985			    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
986			break;
987		case 2:
988			lgain = ESO_GAIN_TO_6BIT(
989			    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
990			rgain = ESO_GAIN_TO_6BIT(
991			    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
992			break;
993		default:
994			return (EINVAL);
995		}
996
997		sc->sc_gain[cp->dev][ESO_LEFT] = lgain;
998		sc->sc_gain[cp->dev][ESO_RIGHT] = rgain;
999		eso_set_gain(sc, cp->dev);
1000		break;
1001
1002	case ESO_SPATIALIZER:
1003		if (cp->type != AUDIO_MIXER_VALUE ||
1004		    cp->un.value.num_channels != 1)
1005			return (EINVAL);
1006
1007		sc->sc_gain[cp->dev][ESO_LEFT] =
1008		    sc->sc_gain[cp->dev][ESO_RIGHT] =
1009		    ESO_GAIN_TO_6BIT(
1010			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1011		eso_set_gain(sc, cp->dev);
1012		break;
1013
1014	case ESO_MONO_PLAY_VOL:
1015	case ESO_MONO_REC_VOL:
1016		if (cp->type != AUDIO_MIXER_VALUE ||
1017		    cp->un.value.num_channels != 1)
1018			return (EINVAL);
1019
1020		sc->sc_gain[cp->dev][ESO_LEFT] =
1021		    sc->sc_gain[cp->dev][ESO_RIGHT] =
1022		    ESO_GAIN_TO_4BIT(
1023			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1024		eso_set_gain(sc, cp->dev);
1025		break;
1026
1027	case ESO_PCSPEAKER_VOL:
1028		if (cp->type != AUDIO_MIXER_VALUE ||
1029		    cp->un.value.num_channels != 1)
1030			return (EINVAL);
1031
1032		sc->sc_gain[cp->dev][ESO_LEFT] =
1033		    sc->sc_gain[cp->dev][ESO_RIGHT] =
1034		    ESO_GAIN_TO_3BIT(
1035			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1036		eso_set_gain(sc, cp->dev);
1037		break;
1038
1039	case ESO_SPATIALIZER_ENABLE:
1040		if (cp->type != AUDIO_MIXER_ENUM)
1041			return (EINVAL);
1042
1043		sc->sc_spatializer = (cp->un.ord != 0);
1044
1045		tmp = eso_read_mixreg(sc, ESO_MIXREG_SPAT);
1046		if (sc->sc_spatializer)
1047			tmp |= ESO_MIXREG_SPAT_ENB;
1048		else
1049			tmp &= ~ESO_MIXREG_SPAT_ENB;
1050		eso_write_mixreg(sc, ESO_MIXREG_SPAT,
1051		    tmp | ESO_MIXREG_SPAT_RSTREL);
1052		break;
1053
1054	case ESO_MASTER_MUTE:
1055		if (cp->type != AUDIO_MIXER_ENUM)
1056			return (EINVAL);
1057
1058		sc->sc_mvmute = (cp->un.ord != 0);
1059
1060		if (sc->sc_mvmute) {
1061			eso_write_mixreg(sc, ESO_MIXREG_LMVM,
1062			    eso_read_mixreg(sc, ESO_MIXREG_LMVM) |
1063			    ESO_MIXREG_LMVM_MUTE);
1064			eso_write_mixreg(sc, ESO_MIXREG_RMVM,
1065			    eso_read_mixreg(sc, ESO_MIXREG_RMVM) |
1066			    ESO_MIXREG_RMVM_MUTE);
1067		} else {
1068			eso_write_mixreg(sc, ESO_MIXREG_LMVM,
1069			    eso_read_mixreg(sc, ESO_MIXREG_LMVM) &
1070			    ~ESO_MIXREG_LMVM_MUTE);
1071			eso_write_mixreg(sc, ESO_MIXREG_RMVM,
1072			    eso_read_mixreg(sc, ESO_MIXREG_RMVM) &
1073			    ~ESO_MIXREG_RMVM_MUTE);
1074		}
1075		break;
1076
1077	case ESO_MONOOUT_SOURCE:
1078		if (cp->type != AUDIO_MIXER_ENUM)
1079			return (EINVAL);
1080
1081		return (eso_set_monooutsrc(sc, cp->un.ord));
1082
1083	case ESO_MONOIN_BYPASS:
1084		if (cp->type != AUDIO_MIXER_ENUM)
1085			return (EINVAL);
1086
1087		return (eso_set_monoinbypass(sc, cp->un.ord));
1088
1089	case ESO_RECORD_MONITOR:
1090		if (cp->type != AUDIO_MIXER_ENUM)
1091			return (EINVAL);
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			return (EINVAL);
1106
1107		return (eso_set_recsrc(sc, cp->un.ord));
1108
1109	case ESO_MIC_PREAMP:
1110		if (cp->type != AUDIO_MIXER_ENUM)
1111			return (EINVAL);
1112
1113		return (eso_set_preamp(sc, cp->un.ord));
1114
1115	default:
1116		return (EINVAL);
1117	}
1118
1119	return (0);
1120}
1121
1122int
1123eso_get_port(void *hdl, mixer_ctrl_t *cp)
1124{
1125	struct eso_softc *sc = hdl;
1126
1127	switch (cp->dev) {
1128	case ESO_MASTER_VOL:
1129		/* Reload from mixer after hardware volume control use. */
1130		if (sc->sc_gain[cp->dev][ESO_LEFT] == (uint8_t)~0)
1131			eso_reload_master_vol(sc);
1132		/* FALLTHROUGH */
1133	case ESO_DAC_PLAY_VOL:
1134	case ESO_MIC_PLAY_VOL:
1135	case ESO_LINE_PLAY_VOL:
1136	case ESO_SYNTH_PLAY_VOL:
1137	case ESO_CD_PLAY_VOL:
1138	case ESO_AUXB_PLAY_VOL:
1139	case ESO_RECORD_VOL:
1140	case ESO_DAC_REC_VOL:
1141	case ESO_MIC_REC_VOL:
1142	case ESO_LINE_REC_VOL:
1143	case ESO_SYNTH_REC_VOL:
1144	case ESO_CD_REC_VOL:
1145	case ESO_AUXB_REC_VOL:
1146		/*
1147		 * Stereo-capable ports: if a single-channel query is made,
1148		 * just return the left channel's value (since single-channel
1149		 * settings themselves are applied to both channels).
1150		 */
1151		switch (cp->un.value.num_channels) {
1152		case 1:
1153			cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1154			    sc->sc_gain[cp->dev][ESO_LEFT];
1155			break;
1156		case 2:
1157			cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1158			    sc->sc_gain[cp->dev][ESO_LEFT];
1159			cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1160			    sc->sc_gain[cp->dev][ESO_RIGHT];
1161			break;
1162		default:
1163			return (EINVAL);
1164		}
1165		break;
1166
1167	case ESO_MONO_PLAY_VOL:
1168	case ESO_PCSPEAKER_VOL:
1169	case ESO_MONO_REC_VOL:
1170	case ESO_SPATIALIZER:
1171		if (cp->un.value.num_channels != 1)
1172			return (EINVAL);
1173		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1174		    sc->sc_gain[cp->dev][ESO_LEFT];
1175		break;
1176
1177	case ESO_RECORD_MONITOR:
1178		cp->un.ord = sc->sc_recmon;
1179		break;
1180
1181	case ESO_RECORD_SOURCE:
1182		cp->un.ord = sc->sc_recsrc;
1183		break;
1184
1185	case ESO_MONOOUT_SOURCE:
1186		cp->un.ord = sc->sc_monooutsrc;
1187		break;
1188
1189	case ESO_MONOIN_BYPASS:
1190		cp->un.ord = sc->sc_monoinbypass;
1191		break;
1192
1193	case ESO_SPATIALIZER_ENABLE:
1194		cp->un.ord = sc->sc_spatializer;
1195		break;
1196
1197	case ESO_MIC_PREAMP:
1198		cp->un.ord = sc->sc_preamp;
1199		break;
1200
1201	case ESO_MASTER_MUTE:
1202		/* Reload from mixer after hardware volume control use. */
1203		if (sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] == (uint8_t)~0)
1204			eso_reload_master_vol(sc);
1205		cp->un.ord = sc->sc_mvmute;
1206		break;
1207
1208	default:
1209		return (EINVAL);
1210	}
1211
1212	return (0);
1213
1214}
1215
1216int
1217eso_query_devinfo(void *hdl, mixer_devinfo_t *dip)
1218{
1219	switch (dip->index) {
1220	case ESO_DAC_PLAY_VOL:
1221		dip->mixer_class = ESO_INPUT_CLASS;
1222		dip->next = dip->prev = AUDIO_MIXER_LAST;
1223		strlcpy(dip->label.name, AudioNdac, sizeof dip->label.name);
1224		dip->type = AUDIO_MIXER_VALUE;
1225		dip->un.v.num_channels = 2;
1226		strlcpy(dip->un.v.units.name, AudioNvolume,
1227		    sizeof dip->un.v.units.name);
1228		break;
1229	case ESO_MIC_PLAY_VOL:
1230		dip->mixer_class = ESO_INPUT_CLASS;
1231		dip->next = dip->prev = AUDIO_MIXER_LAST;
1232		strlcpy(dip->label.name, AudioNmicrophone,
1233		    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_LINE_PLAY_VOL:
1240		dip->mixer_class = ESO_INPUT_CLASS;
1241		dip->next = dip->prev = AUDIO_MIXER_LAST;
1242		strlcpy(dip->label.name, AudioNline, sizeof dip->label.name);
1243		dip->type = AUDIO_MIXER_VALUE;
1244		dip->un.v.num_channels = 2;
1245		strlcpy(dip->un.v.units.name, AudioNvolume,
1246		    sizeof dip->un.v.units.name);
1247		break;
1248	case ESO_SYNTH_PLAY_VOL:
1249		dip->mixer_class = ESO_INPUT_CLASS;
1250		dip->next = dip->prev = AUDIO_MIXER_LAST;
1251		strlcpy(dip->label.name, AudioNfmsynth,
1252		    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_MONO_PLAY_VOL:
1259		dip->mixer_class = ESO_INPUT_CLASS;
1260		dip->next = dip->prev = AUDIO_MIXER_LAST;
1261		strlcpy(dip->label.name, "mono_in", sizeof dip->label.name);
1262		dip->type = AUDIO_MIXER_VALUE;
1263		dip->un.v.num_channels = 1;
1264		strlcpy(dip->un.v.units.name, AudioNvolume,
1265		    sizeof dip->un.v.units.name);
1266		break;
1267	case ESO_CD_PLAY_VOL:
1268		dip->mixer_class = ESO_INPUT_CLASS;
1269		dip->next = dip->prev = AUDIO_MIXER_LAST;
1270		strlcpy(dip->label.name, AudioNcd, sizeof dip->label.name);
1271		dip->type = AUDIO_MIXER_VALUE;
1272		dip->un.v.num_channels = 2;
1273		strlcpy(dip->un.v.units.name, AudioNvolume,
1274		    sizeof dip->un.v.units.name);
1275		break;
1276	case ESO_AUXB_PLAY_VOL:
1277		dip->mixer_class = ESO_INPUT_CLASS;
1278		dip->next = dip->prev = AUDIO_MIXER_LAST;
1279		strlcpy(dip->label.name, "auxb", sizeof dip->label.name);
1280		dip->type = AUDIO_MIXER_VALUE;
1281		dip->un.v.num_channels = 2;
1282		strlcpy(dip->un.v.units.name, AudioNvolume,
1283		    sizeof dip->un.v.units.name);
1284		break;
1285	case ESO_MIC_PREAMP:
1286		dip->mixer_class = ESO_MICROPHONE_CLASS;
1287		dip->next = dip->prev = AUDIO_MIXER_LAST;
1288		strlcpy(dip->label.name, AudioNpreamp, sizeof dip->label.name);
1289		dip->type = AUDIO_MIXER_ENUM;
1290		dip->un.e.num_mem = 2;
1291		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1292		    sizeof dip->un.e.member[0].label.name);
1293		dip->un.e.member[0].ord = 0;
1294		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1295		    sizeof dip->un.e.member[1].label.name);
1296		dip->un.e.member[1].ord = 1;
1297		break;
1298	case ESO_MICROPHONE_CLASS:
1299		dip->mixer_class = ESO_MICROPHONE_CLASS;
1300		dip->next = dip->prev = AUDIO_MIXER_LAST;
1301		strlcpy(dip->label.name, AudioNmicrophone,
1302		    sizeof dip->label.name);
1303		dip->type = AUDIO_MIXER_CLASS;
1304		break;
1305	case ESO_INPUT_CLASS:
1306		dip->mixer_class = ESO_INPUT_CLASS;
1307		dip->next = dip->prev = AUDIO_MIXER_LAST;
1308		strlcpy(dip->label.name, AudioCinputs, sizeof dip->label.name);
1309		dip->type = AUDIO_MIXER_CLASS;
1310		break;
1311	case ESO_MASTER_VOL:
1312		dip->mixer_class = ESO_OUTPUT_CLASS;
1313		dip->prev = AUDIO_MIXER_LAST;
1314		dip->next = ESO_MASTER_MUTE;
1315		strlcpy(dip->label.name, AudioNmaster, sizeof dip->label.name);
1316		dip->type = AUDIO_MIXER_VALUE;
1317		dip->un.v.num_channels = 2;
1318		strlcpy(dip->un.v.units.name, AudioNvolume,
1319		    sizeof dip->un.v.units.name);
1320		break;
1321	case ESO_MASTER_MUTE:
1322		dip->mixer_class = ESO_OUTPUT_CLASS;
1323		dip->prev = ESO_MASTER_VOL;
1324		dip->next = AUDIO_MIXER_LAST;
1325		strlcpy(dip->label.name, AudioNmute, sizeof dip->label.name);
1326		dip->type = AUDIO_MIXER_ENUM;
1327		dip->un.e.num_mem = 2;
1328		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1329		    sizeof dip->un.e.member[0].label.name);
1330		dip->un.e.member[0].ord = 0;
1331		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1332		    sizeof dip->un.e.member[1].label.name);
1333		dip->un.e.member[1].ord = 1;
1334		break;
1335	case ESO_PCSPEAKER_VOL:
1336		dip->mixer_class = ESO_OUTPUT_CLASS;
1337		dip->next = dip->prev = AUDIO_MIXER_LAST;
1338		strlcpy(dip->label.name, "pc_speaker", sizeof dip->label.name);
1339		dip->type = AUDIO_MIXER_VALUE;
1340		dip->un.v.num_channels = 1;
1341		strlcpy(dip->un.v.units.name, AudioNvolume,
1342		    sizeof dip->un.v.units.name);
1343		break;
1344	case ESO_MONOOUT_SOURCE:
1345		dip->mixer_class = ESO_OUTPUT_CLASS;
1346		dip->next = dip->prev = AUDIO_MIXER_LAST;
1347		strlcpy(dip->label.name, "mono_out", sizeof dip->label.name);
1348		dip->type = AUDIO_MIXER_ENUM;
1349		dip->un.e.num_mem = 3;
1350		strlcpy(dip->un.e.member[0].label.name, AudioNmute,
1351		    sizeof dip->un.e.member[0].label.name);
1352		dip->un.e.member[0].ord = ESO_MIXREG_MPM_MOMUTE;
1353		strlcpy(dip->un.e.member[1].label.name, AudioNdac,
1354		    sizeof dip->un.e.member[1].label.name);
1355		dip->un.e.member[1].ord = ESO_MIXREG_MPM_MOA2R;
1356		strlcpy(dip->un.e.member[2].label.name, AudioNmixerout,
1357		    sizeof dip->un.e.member[2].label.name);
1358		dip->un.e.member[2].ord = ESO_MIXREG_MPM_MOREC;
1359		break;
1360	case ESO_MONOIN_BYPASS:
1361		dip->mixer_class = ESO_MONOIN_CLASS;
1362		dip->next = dip->prev = AUDIO_MIXER_LAST;
1363		strlcpy(dip->label.name, "bypass", sizeof dip->label.name);
1364		dip->type = AUDIO_MIXER_ENUM;
1365		dip->un.e.num_mem = 2;
1366		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1367		    sizeof dip->un.e.member[0].label.name);
1368		dip->un.e.member[0].ord = 0;
1369		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1370		    sizeof dip->un.e.member[1].label.name);
1371		dip->un.e.member[1].ord = 1;
1372		break;
1373	case ESO_MONOIN_CLASS:
1374		dip->mixer_class = ESO_MONOIN_CLASS;
1375		dip->next = dip->prev = AUDIO_MIXER_LAST;
1376		strlcpy(dip->label.name, "mono_in", sizeof dip->label.name);
1377		dip->type = AUDIO_MIXER_CLASS;
1378		break;
1379	case ESO_SPATIALIZER:
1380		dip->mixer_class = ESO_OUTPUT_CLASS;
1381		dip->prev = AUDIO_MIXER_LAST;
1382		dip->next = ESO_SPATIALIZER_ENABLE;
1383		strlcpy(dip->label.name, AudioNspatial,
1384		    sizeof dip->label.name);
1385		dip->type = AUDIO_MIXER_VALUE;
1386		dip->un.v.num_channels = 1;
1387		strlcpy(dip->un.v.units.name, "level",
1388		    sizeof dip->un.v.units.name);
1389		break;
1390	case ESO_SPATIALIZER_ENABLE:
1391		dip->mixer_class = ESO_OUTPUT_CLASS;
1392		dip->prev = ESO_SPATIALIZER;
1393		dip->next = AUDIO_MIXER_LAST;
1394		strlcpy(dip->label.name, "enable", sizeof dip->label.name);
1395		dip->type = AUDIO_MIXER_ENUM;
1396		dip->un.e.num_mem = 2;
1397		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1398		    sizeof dip->un.e.member[0].label.name);
1399		dip->un.e.member[0].ord = 0;
1400		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1401		    sizeof dip->un.e.member[1].label.name);
1402		dip->un.e.member[1].ord = 1;
1403		break;
1404	case ESO_OUTPUT_CLASS:
1405		dip->mixer_class = ESO_OUTPUT_CLASS;
1406		dip->next = dip->prev = AUDIO_MIXER_LAST;
1407		strlcpy(dip->label.name, AudioCoutputs,
1408		    sizeof dip->label.name);
1409		dip->type = AUDIO_MIXER_CLASS;
1410		break;
1411	case ESO_RECORD_MONITOR:
1412		dip->mixer_class = ESO_MONITOR_CLASS;
1413		dip->next = dip->prev = AUDIO_MIXER_LAST;
1414		strlcpy(dip->label.name, AudioNmute, sizeof dip->label.name);
1415		dip->type = AUDIO_MIXER_ENUM;
1416		dip->un.e.num_mem = 2;
1417		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1418		    sizeof dip->un.e.member[0].label.name);
1419		dip->un.e.member[0].ord = 0;
1420		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1421		    sizeof dip->un.e.member[1].label.name);
1422		dip->un.e.member[1].ord = 1;
1423		break;
1424	case ESO_MONITOR_CLASS:
1425		dip->mixer_class = ESO_MONITOR_CLASS;
1426		dip->next = dip->prev = AUDIO_MIXER_LAST;
1427		strlcpy(dip->label.name, AudioCmonitor,
1428		    sizeof dip->label.name);
1429		dip->type = AUDIO_MIXER_CLASS;
1430		break;
1431	case ESO_RECORD_VOL:
1432		dip->mixer_class = ESO_RECORD_CLASS;
1433		dip->next = dip->prev = AUDIO_MIXER_LAST;
1434		strlcpy(dip->label.name, AudioNrecord, sizeof dip->label.name);
1435		dip->type = AUDIO_MIXER_VALUE;
1436		strlcpy(dip->un.v.units.name, AudioNvolume,
1437		    sizeof dip->un.v.units.name);
1438		break;
1439	case ESO_RECORD_SOURCE:
1440		dip->mixer_class = ESO_RECORD_CLASS;
1441		dip->next = dip->prev = AUDIO_MIXER_LAST;
1442		strlcpy(dip->label.name, AudioNsource, sizeof dip->label.name);
1443		dip->type = AUDIO_MIXER_ENUM;
1444		dip->un.e.num_mem = 4;
1445		strlcpy(dip->un.e.member[0].label.name, AudioNmicrophone,
1446		    sizeof dip->un.e.member[0].label.name);
1447		dip->un.e.member[0].ord = ESO_MIXREG_ERS_MIC;
1448		strlcpy(dip->un.e.member[1].label.name, AudioNline,
1449		    sizeof dip->un.e.member[1].label.name);
1450		dip->un.e.member[1].ord = ESO_MIXREG_ERS_LINE;
1451		strlcpy(dip->un.e.member[2].label.name, AudioNcd,
1452		    sizeof dip->un.e.member[2].label.name);
1453		dip->un.e.member[2].ord = ESO_MIXREG_ERS_CD;
1454		strlcpy(dip->un.e.member[3].label.name, AudioNmixerout,
1455		    sizeof dip->un.e.member[3].label.name);
1456		dip->un.e.member[3].ord = ESO_MIXREG_ERS_MIXER;
1457		break;
1458	case ESO_DAC_REC_VOL:
1459		dip->mixer_class = ESO_RECORD_CLASS;
1460		dip->next = dip->prev = AUDIO_MIXER_LAST;
1461		strlcpy(dip->label.name, AudioNdac, sizeof dip->label.name);
1462		dip->type = AUDIO_MIXER_VALUE;
1463		dip->un.v.num_channels = 2;
1464		strlcpy(dip->un.v.units.name, AudioNvolume,
1465		    sizeof dip->un.v.units.name);
1466		break;
1467	case ESO_MIC_REC_VOL:
1468		dip->mixer_class = ESO_RECORD_CLASS;
1469		dip->next = dip->prev = AUDIO_MIXER_LAST;
1470		strlcpy(dip->label.name, AudioNmicrophone,
1471		    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_LINE_REC_VOL:
1478		dip->mixer_class = ESO_RECORD_CLASS;
1479		dip->next = dip->prev = AUDIO_MIXER_LAST;
1480		strlcpy(dip->label.name, AudioNline, sizeof dip->label.name);
1481		dip->type = AUDIO_MIXER_VALUE;
1482		dip->un.v.num_channels = 2;
1483		strlcpy(dip->un.v.units.name, AudioNvolume,
1484		    sizeof dip->un.v.units.name);
1485		break;
1486	case ESO_SYNTH_REC_VOL:
1487		dip->mixer_class = ESO_RECORD_CLASS;
1488		dip->next = dip->prev = AUDIO_MIXER_LAST;
1489		strlcpy(dip->label.name, AudioNfmsynth,
1490		    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_MONO_REC_VOL:
1497		dip->mixer_class = ESO_RECORD_CLASS;
1498		dip->next = dip->prev = AUDIO_MIXER_LAST;
1499		strlcpy(dip->label.name, "mono_in", sizeof dip->label.name);
1500		dip->type = AUDIO_MIXER_VALUE;
1501		dip->un.v.num_channels = 1; /* No lies */
1502		strlcpy(dip->un.v.units.name, AudioNvolume,
1503		    sizeof dip->un.v.units.name);
1504		break;
1505	case ESO_CD_REC_VOL:
1506		dip->mixer_class = ESO_RECORD_CLASS;
1507		dip->next = dip->prev = AUDIO_MIXER_LAST;
1508		strlcpy(dip->label.name, AudioNcd, sizeof dip->label.name);
1509		dip->type = AUDIO_MIXER_VALUE;
1510		dip->un.v.num_channels = 2;
1511		strlcpy(dip->un.v.units.name, AudioNvolume,
1512		    sizeof dip->un.v.units.name);
1513		break;
1514	case ESO_AUXB_REC_VOL:
1515		dip->mixer_class = ESO_RECORD_CLASS;
1516		dip->next = dip->prev = AUDIO_MIXER_LAST;
1517		strlcpy(dip->label.name, "auxb", sizeof dip->label.name);
1518		dip->type = AUDIO_MIXER_VALUE;
1519		dip->un.v.num_channels = 2;
1520		strlcpy(dip->un.v.units.name, AudioNvolume,
1521		    sizeof dip->un.v.units.name);
1522		break;
1523	case ESO_RECORD_CLASS:
1524		dip->mixer_class = ESO_RECORD_CLASS;
1525		dip->next = dip->prev = AUDIO_MIXER_LAST;
1526		strlcpy(dip->label.name, AudioCrecord, sizeof dip->label.name);
1527		dip->type = AUDIO_MIXER_CLASS;
1528		break;
1529	default:
1530		return (ENXIO);
1531	}
1532
1533	return (0);
1534}
1535
1536int
1537eso_allocmem(struct eso_softc *sc, size_t size, size_t align,
1538    size_t boundary, int flags, int direction, struct eso_dma *ed)
1539{
1540	int error, wait;
1541
1542	wait = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
1543	ed->ed_size = size;
1544
1545	error = bus_dmamem_alloc(ed->ed_dmat, ed->ed_size, align, boundary,
1546	    ed->ed_segs, sizeof (ed->ed_segs) / sizeof (ed->ed_segs[0]),
1547	    &ed->ed_nsegs, wait);
1548	if (error)
1549		goto out;
1550
1551	error = bus_dmamem_map(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs,
1552	    ed->ed_size, &ed->ed_addr, wait | BUS_DMA_COHERENT);
1553	if (error)
1554		goto free;
1555
1556	error = bus_dmamap_create(ed->ed_dmat, ed->ed_size, 1, ed->ed_size,
1557	    boundary,  wait, &ed->ed_map);
1558	if (error)
1559		goto unmap;
1560
1561	error = bus_dmamap_load(ed->ed_dmat, ed->ed_map, ed->ed_addr,
1562	    ed->ed_size, NULL, wait |
1563	    (direction == AUMODE_RECORD) ? BUS_DMA_READ : BUS_DMA_WRITE);
1564	if (error)
1565		goto destroy;
1566
1567	return (0);
1568
1569 destroy:
1570	bus_dmamap_destroy(ed->ed_dmat, ed->ed_map);
1571 unmap:
1572	bus_dmamem_unmap(ed->ed_dmat, ed->ed_addr, ed->ed_size);
1573 free:
1574	bus_dmamem_free(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs);
1575 out:
1576	return (error);
1577}
1578
1579void
1580eso_freemem(struct eso_dma *ed)
1581{
1582	bus_dmamap_unload(ed->ed_dmat, ed->ed_map);
1583	bus_dmamap_destroy(ed->ed_dmat, ed->ed_map);
1584	bus_dmamem_unmap(ed->ed_dmat, ed->ed_addr, ed->ed_size);
1585	bus_dmamem_free(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs);
1586}
1587
1588void *
1589eso_allocm(void *hdl, int direction, size_t size, int type, int flags)
1590{
1591	struct eso_softc *sc = hdl;
1592	struct eso_dma *ed;
1593	size_t boundary;
1594	int error;
1595
1596	if ((ed = malloc(sizeof (*ed), type, flags)) == NULL)
1597		return (NULL);
1598
1599	/*
1600	 * Apparently the Audio 1 DMA controller's current address
1601	 * register can't roll over a 64K address boundary, so we have to
1602	 * take care of that ourselves.  Similarly, the Audio 2 DMA
1603	 * controller needs a 1M address boundary.
1604	 */
1605	if (direction == AUMODE_RECORD)
1606		boundary = 0x10000;
1607	else
1608		boundary = 0x100000;
1609
1610	/*
1611	 * XXX Work around allocation problems for Audio 1, which
1612	 * XXX implements the 24 low address bits only, with
1613	 * XXX machine-specific DMA tag use.
1614	 */
1615#if defined(__alpha__)
1616	/*
1617	 * XXX Force allocation through the (ISA) SGMAP.
1618	 */
1619	if (direction == AUMODE_RECORD)
1620		ed->ed_dmat = alphabus_dma_get_tag(sc->sc_dmat, ALPHA_BUS_ISA);
1621	else
1622#elif defined(__amd64__) || defined(__i386__)
1623	/*
1624	 * XXX Force allocation through the ISA DMA tag.
1625	 */
1626	if (direction == AUMODE_RECORD)
1627		ed->ed_dmat = &isa_bus_dma_tag;
1628	else
1629#endif
1630		ed->ed_dmat = sc->sc_dmat;
1631
1632	error = eso_allocmem(sc, size, 32, boundary, flags, direction, ed);
1633	if (error) {
1634		free(ed, type);
1635		return (NULL);
1636	}
1637	ed->ed_next = sc->sc_dmas;
1638	sc->sc_dmas = ed;
1639
1640	return (KVADDR(ed));
1641}
1642
1643void
1644eso_freem(void *hdl, void *addr, int type)
1645{
1646	struct eso_softc *sc = hdl;
1647	struct eso_dma *p, **pp;
1648
1649	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->ed_next) {
1650		if (KVADDR(p) == addr) {
1651			eso_freemem(p);
1652			*pp = p->ed_next;
1653			free(p, type);
1654			return;
1655		}
1656	}
1657}
1658
1659size_t
1660eso_round_buffersize(void *hdl, int direction, size_t bufsize)
1661{
1662	size_t maxsize;
1663
1664	/*
1665	 * The playback DMA buffer size on the Solo-1 is limited to 0xfff0
1666	 * bytes.  This is because IO_A2DMAC is a two byte value
1667	 * indicating the literal byte count, and the 4 least significant
1668	 * bits are read-only.  Zero is not used as a special case for
1669	 * 0x10000.
1670	 *
1671	 * For recording, DMAC_DMAC is the byte count - 1, so 0x10000 can
1672	 * be represented.
1673	 */
1674	maxsize = (direction == AUMODE_PLAY) ? 0xfff0 : 0x10000;
1675
1676	if (bufsize > maxsize)
1677		bufsize = maxsize;
1678
1679	return (bufsize);
1680}
1681
1682paddr_t
1683eso_mappage(void *hdl, void *addr, off_t offs, int prot)
1684{
1685	struct eso_softc *sc = hdl;
1686	struct eso_dma *ed;
1687
1688	if (offs < 0)
1689		return (-1);
1690	for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != addr;
1691	     ed = ed->ed_next)
1692		;
1693	if (ed == NULL)
1694		return (-1);
1695
1696	return (bus_dmamem_mmap(ed->ed_dmat, ed->ed_segs, ed->ed_nsegs,
1697	    offs, prot, BUS_DMA_WAITOK));
1698}
1699
1700/* ARGSUSED */
1701int
1702eso_get_props(void *hdl)
1703{
1704	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1705	    AUDIO_PROP_FULLDUPLEX);
1706}
1707
1708int
1709eso_trigger_output(void *hdl, void *start, void *end, int blksize,
1710    void (*intr)(void *), void *arg, struct audio_params *param)
1711{
1712	struct eso_softc *sc = hdl;
1713	struct eso_dma *ed;
1714	uint8_t a2c1;
1715
1716	DPRINTF((
1717	    "%s: trigger_output: start %p, end %p, blksize %d, intr %p(%p)\n",
1718	    sc->sc_dev.dv_xname, start, end, blksize, intr, arg));
1719	DPRINTF(("%s: param: rate %lu, encoding %u, precision %u, channels %u, sw_code %p, factor %d\n",
1720	    sc->sc_dev.dv_xname, param->sample_rate, param->encoding,
1721	    param->precision, param->channels, param->sw_code, param->factor));
1722
1723	/* Find DMA buffer. */
1724	for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != start;
1725	     ed = ed->ed_next)
1726		;
1727	if (ed == NULL) {
1728		printf("%s: trigger_output: bad addr %p\n",
1729		    sc->sc_dev.dv_xname, start);
1730		return (EINVAL);
1731	}
1732	DPRINTF(("%s: output dmaaddr %lx\n",
1733	    sc->sc_dev.dv_xname, (unsigned long)DMAADDR(ed)));
1734
1735	sc->sc_pintr = intr;
1736	sc->sc_parg = arg;
1737
1738	/* Compute drain timeout. */
1739	sc->sc_pdrain = hz * (blksize * 3 / 2) /
1740	    (param->sample_rate * param->channels * param->bps * param->factor);
1741
1742	/* DMA transfer count (in `words'!) reload using 2's complement. */
1743	blksize = -(blksize >> 1);
1744	eso_write_mixreg(sc, ESO_MIXREG_A2TCRLO, blksize & 0xff);
1745	eso_write_mixreg(sc, ESO_MIXREG_A2TCRHI, blksize >> 8);
1746
1747	/* Update DAC to reflect DMA count and audio parameters. */
1748	/* Note: we cache A2C2 in order to avoid r/m/w at interrupt time. */
1749	if (param->precision * param->factor == 16)
1750		sc->sc_a2c2 |= ESO_MIXREG_A2C2_16BIT;
1751	else
1752		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_16BIT;
1753	if (param->channels == 2)
1754		sc->sc_a2c2 |= ESO_MIXREG_A2C2_STEREO;
1755	else
1756		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_STEREO;
1757	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1758	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1759		sc->sc_a2c2 |= ESO_MIXREG_A2C2_SIGNED;
1760	else
1761		sc->sc_a2c2 &= ~ESO_MIXREG_A2C2_SIGNED;
1762	/* Unmask IRQ. */
1763	sc->sc_a2c2 |= ESO_MIXREG_A2C2_IRQM;
1764	eso_write_mixreg(sc, ESO_MIXREG_A2C2, sc->sc_a2c2);
1765
1766	/* Set up DMA controller. */
1767	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAA, DMAADDR(ed));
1768	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAC,
1769	    (uint8_t *)end - (uint8_t *)start);
1770	bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM,
1771	    ESO_IO_A2DMAM_DMAENB | ESO_IO_A2DMAM_AUTO);
1772
1773	/* Start DMA. */
1774	a2c1 = eso_read_mixreg(sc, ESO_MIXREG_A2C1);
1775	a2c1 &= ~ESO_MIXREG_A2C1_RESV0; /* Paranoia? XXX bit 5 */
1776	a2c1 |= ESO_MIXREG_A2C1_FIFOENB | ESO_MIXREG_A2C1_DMAENB |
1777	    ESO_MIXREG_A2C1_AUTO;
1778	eso_write_mixreg(sc, ESO_MIXREG_A2C1, a2c1);
1779
1780	return (0);
1781}
1782
1783int
1784eso_trigger_input(void *hdl, void *start, void *end, int blksize,
1785    void (*intr)(void *), void *arg, struct audio_params *param)
1786{
1787	struct eso_softc *sc = hdl;
1788	struct eso_dma *ed;
1789	uint8_t actl, a1c1;
1790
1791	DPRINTF((
1792	    "%s: trigger_input: start %p, end %p, blksize %d, intr %p(%p)\n",
1793	    sc->sc_dev.dv_xname, start, end, blksize, intr, arg));
1794	DPRINTF(("%s: param: rate %lu, encoding %u, precision %u, channels %u, sw_code %p, factor %d\n",
1795	    sc->sc_dev.dv_xname, param->sample_rate, param->encoding,
1796	    param->precision, param->channels, param->sw_code, param->factor));
1797
1798	/*
1799	 * If we failed to configure the Audio 1 DMA controller, bail here
1800	 * while retaining availability of the DAC direction (in Audio 2).
1801	 */
1802	if (!sc->sc_dmac_configured)
1803		return (EIO);
1804
1805	/* Find DMA buffer. */
1806	for (ed = sc->sc_dmas; ed != NULL && KVADDR(ed) != start;
1807	     ed = ed->ed_next)
1808		;
1809	if (ed == NULL) {
1810		printf("%s: trigger_input: bad addr %p\n",
1811		    sc->sc_dev.dv_xname, start);
1812		return (EINVAL);
1813	}
1814	DPRINTF(("%s: input dmaaddr %lx\n",
1815	    sc->sc_dev.dv_xname, (unsigned long)DMAADDR(ed)));
1816
1817	sc->sc_rintr = intr;
1818	sc->sc_rarg = arg;
1819
1820	/* Compute drain timeout. */
1821	sc->sc_rdrain = hz * (blksize * 3 / 2) /
1822	    (param->sample_rate * param->channels * param->bps * param->factor);
1823
1824	/* Set up ADC DMA converter parameters. */
1825	actl = eso_read_ctlreg(sc, ESO_CTLREG_ACTL);
1826	if (param->channels == 2) {
1827		actl &= ~ESO_CTLREG_ACTL_MONO;
1828		actl |= ESO_CTLREG_ACTL_STEREO;
1829	} else {
1830		actl &= ~ESO_CTLREG_ACTL_STEREO;
1831		actl |= ESO_CTLREG_ACTL_MONO;
1832	}
1833	eso_write_ctlreg(sc, ESO_CTLREG_ACTL, actl);
1834
1835	/* Set up Transfer Type: maybe move to attach time? */
1836	eso_write_ctlreg(sc, ESO_CTLREG_A1TT, ESO_CTLREG_A1TT_DEMAND4);
1837
1838	/* DMA transfer count reload using 2's complement. */
1839	blksize = -blksize;
1840	eso_write_ctlreg(sc, ESO_CTLREG_A1TCRLO, blksize & 0xff);
1841	eso_write_ctlreg(sc, ESO_CTLREG_A1TCRHI, blksize >> 8);
1842
1843	/* Set up and enable Audio 1 DMA FIFO. */
1844	a1c1 = ESO_CTLREG_A1C1_RESV1 | ESO_CTLREG_A1C1_FIFOENB;
1845	if (param->precision * param->factor == 16)
1846		a1c1 |= ESO_CTLREG_A1C1_16BIT;
1847	if (param->channels == 2)
1848		a1c1 |= ESO_CTLREG_A1C1_STEREO;
1849	else
1850		a1c1 |= ESO_CTLREG_A1C1_MONO;
1851	if (param->encoding == AUDIO_ENCODING_SLINEAR_BE ||
1852	    param->encoding == AUDIO_ENCODING_SLINEAR_LE)
1853		a1c1 |= ESO_CTLREG_A1C1_SIGNED;
1854	eso_write_ctlreg(sc, ESO_CTLREG_A1C1, a1c1);
1855
1856	/* Set up ADC IRQ/DRQ parameters. */
1857	eso_write_ctlreg(sc, ESO_CTLREG_LAIC,
1858	    ESO_CTLREG_LAIC_PINENB | ESO_CTLREG_LAIC_EXTENB);
1859	eso_write_ctlreg(sc, ESO_CTLREG_DRQCTL,
1860	    ESO_CTLREG_DRQCTL_ENB1 | ESO_CTLREG_DRQCTL_EXTENB);
1861
1862	/* Set up and enable DMA controller. */
1863	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_CLEAR, 0);
1864	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK,
1865	    ESO_DMAC_MASK_MASK);
1866	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MODE,
1867	    DMA37MD_WRITE | DMA37MD_LOOP | DMA37MD_DEMAND);
1868	bus_space_write_4(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_DMAA,
1869	    DMAADDR(ed));
1870	bus_space_write_2(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_DMAC,
1871	    (uint8_t *)end - (uint8_t *)start - 1);
1872	bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh, ESO_DMAC_MASK, 0);
1873
1874	/* Start DMA. */
1875	eso_write_ctlreg(sc, ESO_CTLREG_A1C2,
1876	    ESO_CTLREG_A1C2_DMAENB | ESO_CTLREG_A1C2_READ |
1877	    ESO_CTLREG_A1C2_AUTO | ESO_CTLREG_A1C2_ADC);
1878
1879	return (0);
1880}
1881
1882/*
1883 * Mixer utility functions.
1884 */
1885int
1886eso_set_recsrc(struct eso_softc *sc, u_int recsrc)
1887{
1888	mixer_devinfo_t di;
1889	int i, error;
1890
1891	di.index = ESO_RECORD_SOURCE;
1892	error = eso_query_devinfo(sc, &di);
1893	if (error != 0) {
1894		printf("eso_set_recsrc: eso_query_devinfo failed");
1895		return (error);
1896	}
1897
1898	for (i = 0; i < di.un.e.num_mem; i++) {
1899		if (recsrc == di.un.e.member[i].ord) {
1900			eso_write_mixreg(sc, ESO_MIXREG_ERS, recsrc);
1901			sc->sc_recsrc = recsrc;
1902			return (0);
1903		}
1904	}
1905
1906	return (EINVAL);
1907}
1908
1909int
1910eso_set_monooutsrc(struct eso_softc *sc, uint monooutsrc)
1911{
1912	mixer_devinfo_t di;
1913	int i, error;
1914	uint8_t mpm;
1915
1916	di.index = ESO_MONOOUT_SOURCE;
1917	error = eso_query_devinfo(sc, &di);
1918	if (error != 0) {
1919		printf("eso_set_monooutsrc: eso_query_devinfo failed");
1920		return (error);
1921	}
1922
1923	for (i = 0; i < di.un.e.num_mem; i++) {
1924		if (monooutsrc == di.un.e.member[i].ord) {
1925			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1926			mpm &= ~ESO_MIXREG_MPM_MOMASK;
1927			mpm |= monooutsrc;
1928			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1929			sc->sc_monooutsrc = monooutsrc;
1930			return (0);
1931		}
1932	}
1933
1934	return (EINVAL);
1935}
1936
1937int
1938eso_set_monoinbypass(struct eso_softc *sc, uint monoinbypass)
1939{
1940	mixer_devinfo_t di;
1941	int i, error;
1942	uint8_t mpm;
1943
1944	di.index = ESO_MONOIN_BYPASS;
1945	error = eso_query_devinfo(sc, &di);
1946	if (error != 0) {
1947		printf("eso_set_monoinbypass: eso_query_devinfo failed");
1948		return (error);
1949	}
1950
1951	for (i = 0; i < di.un.e.num_mem; i++) {
1952		if (monoinbypass == di.un.e.member[i].ord) {
1953			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1954			mpm &= ~(ESO_MIXREG_MPM_MOMASK | ESO_MIXREG_MPM_RESV0);
1955			mpm |= (monoinbypass ? ESO_MIXREG_MPM_MIBYPASS : 0);
1956			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1957			sc->sc_monoinbypass = monoinbypass;
1958			return (0);
1959		}
1960	}
1961
1962	return (EINVAL);
1963}
1964
1965int
1966eso_set_preamp(struct eso_softc *sc, uint preamp)
1967{
1968	mixer_devinfo_t di;
1969	int i, error;
1970	uint8_t mpm;
1971
1972	di.index = ESO_MIC_PREAMP;
1973	error = eso_query_devinfo(sc, &di);
1974	if (error != 0) {
1975		printf("eso_set_preamp: eso_query_devinfo failed");
1976		return (error);
1977	}
1978
1979	for (i = 0; i < di.un.e.num_mem; i++) {
1980		if (preamp == di.un.e.member[i].ord) {
1981			mpm = eso_read_mixreg(sc, ESO_MIXREG_MPM);
1982			mpm &= ~(ESO_MIXREG_MPM_PREAMP | ESO_MIXREG_MPM_RESV0);
1983			mpm |= (preamp ? ESO_MIXREG_MPM_PREAMP : 0);
1984			eso_write_mixreg(sc, ESO_MIXREG_MPM, mpm);
1985			sc->sc_preamp = preamp;
1986			return (0);
1987		}
1988	}
1989
1990	return (EINVAL);
1991}
1992
1993/*
1994 * Reload Master Volume and Mute values in softc from mixer; used when
1995 * those have previously been invalidated by use of hardware volume controls.
1996 */
1997void
1998eso_reload_master_vol(struct eso_softc *sc)
1999{
2000	uint8_t mv;
2001
2002	mv = eso_read_mixreg(sc, ESO_MIXREG_LMVM);
2003	sc->sc_gain[ESO_MASTER_VOL][ESO_LEFT] =
2004	    (mv & ~ESO_MIXREG_LMVM_MUTE) << 2;
2005	mv = eso_read_mixreg(sc, ESO_MIXREG_LMVM);
2006	sc->sc_gain[ESO_MASTER_VOL][ESO_RIGHT] =
2007	    (mv & ~ESO_MIXREG_RMVM_MUTE) << 2;
2008	/* Currently both channels are muted simultaneously; either is OK. */
2009	sc->sc_mvmute = (mv & ESO_MIXREG_RMVM_MUTE) != 0;
2010}
2011
2012void
2013eso_set_gain(struct eso_softc *sc, uint port)
2014{
2015	uint8_t mixreg, tmp;
2016
2017	switch (port) {
2018	case ESO_DAC_PLAY_VOL:
2019		mixreg = ESO_MIXREG_PVR_A2;
2020		break;
2021	case ESO_MIC_PLAY_VOL:
2022		mixreg = ESO_MIXREG_PVR_MIC;
2023		break;
2024	case ESO_LINE_PLAY_VOL:
2025		mixreg = ESO_MIXREG_PVR_LINE;
2026		break;
2027	case ESO_SYNTH_PLAY_VOL:
2028		mixreg = ESO_MIXREG_PVR_SYNTH;
2029		break;
2030	case ESO_CD_PLAY_VOL:
2031		mixreg = ESO_MIXREG_PVR_CD;
2032		break;
2033	case ESO_AUXB_PLAY_VOL:
2034		mixreg = ESO_MIXREG_PVR_AUXB;
2035		break;
2036	case ESO_DAC_REC_VOL:
2037		mixreg = ESO_MIXREG_RVR_A2;
2038		break;
2039	case ESO_MIC_REC_VOL:
2040		mixreg = ESO_MIXREG_RVR_MIC;
2041		break;
2042	case ESO_LINE_REC_VOL:
2043		mixreg = ESO_MIXREG_RVR_LINE;
2044		break;
2045	case ESO_SYNTH_REC_VOL:
2046		mixreg = ESO_MIXREG_RVR_SYNTH;
2047		break;
2048	case ESO_CD_REC_VOL:
2049		mixreg = ESO_MIXREG_RVR_CD;
2050		break;
2051	case ESO_AUXB_REC_VOL:
2052		mixreg = ESO_MIXREG_RVR_AUXB;
2053		break;
2054	case ESO_MONO_PLAY_VOL:
2055		mixreg = ESO_MIXREG_PVR_MONO;
2056		break;
2057	case ESO_MONO_REC_VOL:
2058		mixreg = ESO_MIXREG_RVR_MONO;
2059		break;
2060	case ESO_PCSPEAKER_VOL:
2061		/* Special case - only 3-bit, mono, and reserved bits. */
2062		tmp = eso_read_mixreg(sc, ESO_MIXREG_PCSVR);
2063		tmp &= ESO_MIXREG_PCSVR_RESV;
2064		/* Map bits 7:5 -> 2:0. */
2065		tmp |= (sc->sc_gain[port][ESO_LEFT] >> 5);
2066		eso_write_mixreg(sc, ESO_MIXREG_PCSVR, tmp);
2067		return;
2068	case ESO_MASTER_VOL:
2069		/* Special case - separate regs, and 6-bit precision. */
2070		/* Map bits 7:2 -> 5:0, reflect mute settings. */
2071		eso_write_mixreg(sc, ESO_MIXREG_LMVM,
2072		    (sc->sc_gain[port][ESO_LEFT] >> 2) |
2073		    (sc->sc_mvmute ? ESO_MIXREG_LMVM_MUTE : 0x00));
2074		eso_write_mixreg(sc, ESO_MIXREG_RMVM,
2075		    (sc->sc_gain[port][ESO_RIGHT] >> 2) |
2076		    (sc->sc_mvmute ? ESO_MIXREG_RMVM_MUTE : 0x00));
2077		return;
2078	case ESO_SPATIALIZER:
2079		/* Special case - only `mono', and higher precision. */
2080		eso_write_mixreg(sc, ESO_MIXREG_SPATLVL,
2081		    sc->sc_gain[port][ESO_LEFT]);
2082		return;
2083	case ESO_RECORD_VOL:
2084		/* Very Special case, controller register. */
2085		eso_write_ctlreg(sc, ESO_CTLREG_RECLVL,ESO_4BIT_GAIN_TO_STEREO(
2086		   sc->sc_gain[port][ESO_LEFT], sc->sc_gain[port][ESO_RIGHT]));
2087		return;
2088	default:
2089#ifdef DIAGNOSTIC
2090		printf("eso_set_gain: bad port %u", port);
2091		return;
2092		/* NOTREACHED */
2093#else
2094		return;
2095#endif
2096		}
2097
2098	eso_write_mixreg(sc, mixreg, ESO_4BIT_GAIN_TO_STEREO(
2099	    sc->sc_gain[port][ESO_LEFT], sc->sc_gain[port][ESO_RIGHT]));
2100}
2101
2102int
2103eso_activate(struct device *self, int act)
2104{
2105	struct eso_softc *sc = (struct eso_softc *)self;
2106	uint8_t tmp;
2107	int rv = 0;
2108
2109	switch (act) {
2110	case DVACT_QUIESCE:
2111		rv = config_activate_children(self, act);
2112		tmp = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL);
2113		tmp &= ~(ESO_IO_IRQCTL_MASK);
2114		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_IRQCTL, tmp);
2115		break;
2116	case DVACT_SUSPEND:
2117		bus_space_write_1(sc->sc_iot, sc->sc_ioh, ESO_IO_A2DMAM, 0);
2118		bus_space_write_1(sc->sc_dmac_iot, sc->sc_dmac_ioh,
2119		    ESO_DMAC_CLEAR, 0);
2120		bus_space_write_1(sc->sc_sb_iot, sc->sc_sb_ioh,
2121		    ESO_SB_STATUSFLAGS, 3);
2122		/* shut down dma */
2123		pci_conf_write(sc->sc_pa.pa_pc, sc->sc_pa.pa_tag,
2124		    ESO_PCI_DDMAC, 0);
2125		break;
2126	case DVACT_RESUME:
2127		eso_setup(sc, 1, 1);
2128		pci_conf_write(sc->sc_pa.pa_pc, sc->sc_pa.pa_tag,
2129		    ESO_PCI_DDMAC, sc->sc_dmac_addr | ESO_PCI_DDMAC_DE);
2130		rv = config_activate_children(self, act);
2131		break;
2132	}
2133	return (rv);
2134}
2135