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