xref: /openbsd-src/sys/dev/pci/eap.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*      $OpenBSD: eap.c,v 1.43 2011/07/03 15:47:17 matthew Exp $ */
2 /*	$NetBSD: eap.c,v 1.46 2001/09/03 15:07:37 reinoud Exp $ */
3 
4 /*
5  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson <augustss@netbsd.org> and Charles M. Hannum.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Debugging:   Andreas Gustafsson <gson@araneus.fi>
35  * Testing:     Chuck Cranor       <chuck@maria.wustl.edu>
36  *              Phil Nelson        <phil@cs.wwu.edu>
37  *
38  * ES1371/AC97:	Ezra Story         <ezy@panix.com>
39  */
40 
41 /*
42  * Ensoniq ES1370 + AK4531 and ES1371/ES1373 + AC97
43  *
44  * Documentation links:
45  *
46  * ftp://ftp.alsa-project.org/pub/manuals/ensoniq/
47  * ftp://ftp.alsa-project.org/pub/manuals/asahi_kasei/4531.pdf
48  */
49 
50 #include "midi.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/fcntl.h>
56 #include <sys/malloc.h>
57 #include <sys/device.h>
58 
59 #include <dev/pci/pcidevs.h>
60 #include <dev/pci/pcivar.h>
61 
62 #include <sys/audioio.h>
63 #include <dev/audio_if.h>
64 #include <dev/midi_if.h>
65 #include <dev/mulaw.h>
66 #include <dev/auconv.h>
67 #include <dev/ic/ac97.h>
68 
69 #include <machine/bus.h>
70 
71 #include <dev/pci/eapreg.h>
72 
73 struct        cfdriver eap_cd = {
74       NULL, "eap", DV_DULL
75 };
76 
77 #define	PCI_CBIO		0x10
78 
79 /* Debug */
80 #ifdef AUDIO_DEBUG
81 #define DPRINTF(x)	if (eapdebug) printf x
82 #define DPRINTFN(n,x)	if (eapdebug>(n)) printf x
83 int	eapdebug = 1;
84 #else
85 #define DPRINTF(x)
86 #define DPRINTFN(n,x)
87 #endif
88 
89 int	eap_match(struct device *, void *, void *);
90 void	eap_attach(struct device *, struct device *, void *);
91 int	eap_activate(struct device *, int);
92 int	eap_intr(void *);
93 
94 struct eap_dma {
95 	bus_dmamap_t map;
96 	caddr_t addr;
97 	bus_dma_segment_t segs[1];
98 	int nsegs;
99 	size_t size;
100 	struct eap_dma *next;
101 };
102 
103 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
104 #define KERNADDR(p) ((void *)((p)->addr))
105 
106 struct eap_softc {
107 	struct device sc_dev;		/* base device */
108 	void *sc_ih;			/* interrupt vectoring */
109 	bus_space_tag_t iot;
110 	bus_space_handle_t ioh;
111 	bus_dma_tag_t sc_dmatag;	/* DMA tag */
112 
113 	struct eap_dma *sc_dmas;
114 
115 	void	(*sc_pintr)(void *);	/* dma completion intr handler */
116 	void	*sc_parg;		/* arg for sc_intr() */
117 #ifdef DIAGNOSTIC
118 	char	sc_prun;
119 #endif
120 
121 	void	(*sc_rintr)(void *);	/* dma completion intr handler */
122 	void	*sc_rarg;		/* arg for sc_intr() */
123 #ifdef DIAGNOSTIC
124 	char	sc_rrun;
125 #endif
126 
127 #if NMIDI > 0
128 	void	(*sc_iintr)(void *, int); /* midi input ready handler */
129 	void	(*sc_ointr)(void *);	/* midi output ready handler */
130 	void	*sc_arg;
131 	struct device *sc_mididev;
132 #endif
133 
134 	u_short	sc_port[AK_NPORTS];	/* mirror of the hardware setting */
135 	u_int	sc_record_source;	/* recording source mask */
136 	u_int	sc_input_source;	/* input source mask */
137 	u_int	sc_mic_preamp;
138 	char    sc_1371;		/* Using ES1371/AC97 codec */
139 	char    sc_ct5880;		/* CT5880 chip */
140 
141 	struct ac97_codec_if *codec_if;
142 	struct ac97_host_if host_if;
143 
144 	int flags;
145 };
146 
147 enum	ac97_host_flags eap_flags_codec(void *);
148 int	eap_allocmem(struct eap_softc *, size_t, size_t, struct eap_dma *);
149 int	eap_freemem(struct eap_softc *, struct eap_dma *);
150 
151 #define EWRITE1(sc, r, x) bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x))
152 #define EWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
153 #define EWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
154 #define EREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
155 #define EREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
156 #define EREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
157 
158 struct cfattach eap_ca = {
159 	sizeof(struct eap_softc), eap_match, eap_attach, NULL, eap_activate
160 };
161 
162 int	eap_open(void *, int);
163 void	eap_close(void *);
164 int	eap_query_encoding(void *, struct audio_encoding *);
165 int	eap_set_params(void *, int, int, struct audio_params *, struct audio_params *);
166 int	eap_round_blocksize(void *, int);
167 int	eap_trigger_output(void *, void *, void *, int, void (*)(void *),
168 	    void *, struct audio_params *);
169 int	eap_trigger_input(void *, void *, void *, int, void (*)(void *),
170 	    void *, struct audio_params *);
171 int	eap_halt_output(void *);
172 int	eap_halt_input(void *);
173 void	eap_get_default_params(void *, int, struct audio_params *);
174 int	eap_resume(struct eap_softc *);
175 void    eap1370_write_codec(struct eap_softc *, int, int);
176 int	eap_getdev(void *, struct audio_device *);
177 int	eap1370_mixer_set_port(void *, mixer_ctrl_t *);
178 int	eap1370_mixer_get_port(void *, mixer_ctrl_t *);
179 int	eap1371_mixer_set_port(void *, mixer_ctrl_t *);
180 int	eap1371_mixer_get_port(void *, mixer_ctrl_t *);
181 int	eap1370_query_devinfo(void *, mixer_devinfo_t *);
182 void   *eap_malloc(void *, int, size_t, int, int);
183 void	eap_free(void *, void *, int);
184 paddr_t	eap_mappage(void *, void *, off_t, int);
185 int	eap_get_props(void *);
186 void	eap1370_set_mixer(struct eap_softc *sc, int a, int d);
187 u_int32_t eap1371_src_wait(struct eap_softc *sc);
188 void	eap1371_src_write(struct eap_softc *sc, int a, int d);
189 int	eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip);
190 
191 int     eap1371_attach_codec(void *sc, struct ac97_codec_if *);
192 int	eap1371_read_codec(void *sc, u_int8_t a, u_int16_t *d);
193 int	eap1371_write_codec(void *sc, u_int8_t a, u_int16_t d);
194 void    eap1371_reset_codec(void *sc);
195 #if NMIDI > 0
196 void	eap_midi_close(void *);
197 void	eap_midi_getinfo(void *, struct midi_info *);
198 int	eap_midi_open(void *, int, void (*)(void *, int),
199 	    void (*)(void *), void *);
200 int	eap_midi_output(void *, int);
201 #endif
202 
203 struct audio_hw_if eap1370_hw_if = {
204 	eap_open,
205 	eap_close,
206 	NULL,
207 	eap_query_encoding,
208 	eap_set_params,
209 	eap_round_blocksize,
210 	NULL,
211 	NULL,
212 	NULL,
213 	NULL,
214 	NULL,
215 	eap_halt_output,
216 	eap_halt_input,
217 	NULL,
218 	eap_getdev,
219 	NULL,
220 	eap1370_mixer_set_port,
221 	eap1370_mixer_get_port,
222 	eap1370_query_devinfo,
223 	eap_malloc,
224 	eap_free,
225 	NULL,
226 	eap_mappage,
227 	eap_get_props,
228 	eap_trigger_output,
229 	eap_trigger_input,
230 	eap_get_default_params
231 };
232 
233 struct audio_hw_if eap1371_hw_if = {
234 	eap_open,
235 	eap_close,
236 	NULL,
237 	eap_query_encoding,
238 	eap_set_params,
239 	eap_round_blocksize,
240 	NULL,
241 	NULL,
242 	NULL,
243 	NULL,
244 	NULL,
245 	eap_halt_output,
246 	eap_halt_input,
247 	NULL,
248 	eap_getdev,
249 	NULL,
250 	eap1371_mixer_set_port,
251 	eap1371_mixer_get_port,
252 	eap1371_query_devinfo,
253 	eap_malloc,
254 	eap_free,
255 	NULL,
256 	eap_mappage,
257 	eap_get_props,
258 	eap_trigger_output,
259 	eap_trigger_input,
260 	eap_get_default_params
261 };
262 
263 #if NMIDI > 0
264 struct midi_hw_if eap_midi_hw_if = {
265 	eap_midi_open,
266 	eap_midi_close,
267 	eap_midi_output,
268 	0,				/* flush */
269 	eap_midi_getinfo,
270 	0,				/* ioctl */
271 };
272 #endif
273 
274 struct audio_device eap_device = {
275 	"Ensoniq AudioPCI",
276 	"",
277 	"eap"
278 };
279 
280 const struct pci_matchid eap_devices[] = {
281 	{ PCI_VENDOR_CREATIVELABS, PCI_PRODUCT_CREATIVELABS_EV1938 },
282 	{ PCI_VENDOR_ENSONIQ, PCI_PRODUCT_ENSONIQ_AUDIOPCI },
283 	{ PCI_VENDOR_ENSONIQ, PCI_PRODUCT_ENSONIQ_AUDIOPCI97 },
284 	{ PCI_VENDOR_ENSONIQ, PCI_PRODUCT_ENSONIQ_CT5880 },
285 };
286 
287 int
288 eap_match(struct device *parent, void *match, void *aux)
289 {
290 	return (pci_matchbyid((struct pci_attach_args *)aux, eap_devices,
291 	    nitems(eap_devices)));
292 }
293 
294 int
295 eap_activate(struct device *self, int act)
296 {
297 	struct eap_softc *sc = (struct eap_softc *)self;
298 	int rv = 0;
299 
300 	switch (act) {
301 	case DVACT_QUIESCE:
302 		rv = config_activate_children(self, act);
303 		break;
304 	case DVACT_SUSPEND:
305 		break;
306 	case DVACT_RESUME:
307 		eap_resume(sc);
308 		rv = config_activate_children(self, act);
309 		break;
310 	case DVACT_DEACTIVATE:
311 		break;
312 	}
313 	return (rv);
314 }
315 
316 void
317 eap1370_write_codec(struct eap_softc *sc, int a, int d)
318 {
319 	int icss, to;
320 
321 	to = EAP_WRITE_TIMEOUT;
322 	do {
323 		icss = EREAD4(sc, EAP_ICSS);
324 		DPRINTFN(5,("eap: codec %d prog: icss=0x%08x\n", a, icss));
325 		if (!to--) {
326 			printf("%s: timeout writing to codec\n",
327 			    sc->sc_dev.dv_xname);
328 			return;
329 		}
330 	} while (icss & EAP_CWRIP);  /* XXX could use CSTAT here */
331 	EWRITE4(sc, EAP_CODEC, EAP_SET_CODEC(a, d));
332 }
333 
334 /*
335  * Reading and writing the CODEC is very convoluted.  This mimics the
336  * FreeBSD and Linux drivers.
337  */
338 
339 static __inline void
340 eap1371_ready_codec(struct eap_softc *sc, u_int8_t a, u_int32_t wd)
341 {
342 	int to, s;
343 	u_int32_t src, t;
344 
345 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
346 		if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
347 			break;
348 		delay(1);
349 	}
350 	if (to == EAP_WRITE_TIMEOUT)
351 		printf("%s: eap1371_ready_codec timeout 1\n",
352 		    sc->sc_dev.dv_xname);
353 
354 	s = splaudio();
355 	src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
356 	EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
357 
358 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
359 		t = EREAD4(sc, E1371_SRC);
360 		if ((t & E1371_SRC_STATE_MASK) == 0)
361 			break;
362 		delay(1);
363 	}
364 	if (to == EAP_READ_TIMEOUT)
365 		printf("%s: eap1371_ready_codec timeout 2\n",
366 		    sc->sc_dev.dv_xname);
367 
368 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
369 		t = EREAD4(sc, E1371_SRC);
370 		if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
371 			break;
372 		delay(1);
373 	}
374 	if (to == EAP_READ_TIMEOUT)
375 		printf("%s: eap1371_ready_codec timeout 3\n",
376 		    sc->sc_dev.dv_xname);
377 
378 	EWRITE4(sc, E1371_CODEC, wd);
379 
380 	eap1371_src_wait(sc);
381 	EWRITE4(sc, E1371_SRC, src);
382 
383 	splx(s);
384 }
385 
386 int
387 eap1371_read_codec(void *sc_, u_int8_t a, u_int16_t *d)
388 {
389 	struct eap_softc *sc = sc_;
390 	int to;
391 	u_int32_t t;
392 
393 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, 0) | E1371_CODEC_READ);
394 
395 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
396 		if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
397 			break;
398 		delay(1);
399 	}
400 	if (to == EAP_WRITE_TIMEOUT)
401 		printf("%s: eap1371_read_codec timeout 1\n",
402 		    sc->sc_dev.dv_xname);
403 
404 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
405 		t = EREAD4(sc, E1371_CODEC);
406 		if (t & E1371_CODEC_VALID)
407 			break;
408 		delay(1);
409 	}
410 	if (to == EAP_WRITE_TIMEOUT)
411 		printf("%s: eap1371_read_codec timeout 2\n",
412 		    sc->sc_dev.dv_xname);
413 
414 	*d = (u_int16_t)t;
415 
416 	DPRINTFN(10, ("eap1371: reading codec (%x) = %x\n", a, *d));
417 
418 	return (0);
419 }
420 
421 int
422 eap1371_write_codec(void *sc_, u_int8_t a, u_int16_t d)
423 {
424 	struct eap_softc *sc = sc_;
425 
426 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, d));
427 
428         DPRINTFN(10, ("eap1371: writing codec %x --> %x\n", d, a));
429 
430 	return (0);
431 }
432 
433 u_int32_t
434 eap1371_src_wait(struct eap_softc *sc)
435 {
436 	int to;
437 	u_int32_t src = 0;
438 
439 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
440 		src = EREAD4(sc, E1371_SRC);
441 		if (!(src & E1371_SRC_RBUSY))
442 			return (src);
443 		delay(1);
444 	}
445 	printf("%s: eap1371_src_wait timeout\n", sc->sc_dev.dv_xname);
446 	return (src);
447 }
448 
449 void
450 eap1371_src_write(struct eap_softc *sc, int a, int d)
451 {
452 	u_int32_t r;
453 
454 	r = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
455 	r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
456 	EWRITE4(sc, E1371_SRC, r);
457 }
458 
459 void
460 eap_attach(struct device *parent, struct device *self, void *aux)
461 {
462 	struct eap_softc *sc = (struct eap_softc *)self;
463 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
464 	pci_chipset_tag_t pc = pa->pa_pc;
465 	struct audio_hw_if *eap_hw_if;
466 	char const *intrstr;
467 	pci_intr_handle_t ih;
468 	mixer_ctrl_t ctl;
469 	int i;
470 	int revision;
471 
472 	/* Flag if we're "creative" */
473 	sc->sc_1371 = !(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
474 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI);
475 
476 	revision = PCI_REVISION(pa->pa_class);
477 	if (sc->sc_1371) {
478 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
479 		    ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI97 &&
480 		    (revision == EAP_ES1373_8 || revision == EAP_CT5880_A)) ||
481 		    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_CT5880))
482 			sc->sc_ct5880 = 1;
483 	}
484 
485 	/* Map I/O register */
486 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
487 	    &sc->iot, &sc->ioh, NULL, NULL, 0)) {
488 		return;
489 	}
490 
491 	sc->sc_dmatag = pa->pa_dmat;
492 
493 	/* Map and establish the interrupt. */
494 	if (pci_intr_map(pa, &ih)) {
495 		printf(": couldn't map interrupt\n");
496 		return;
497 	}
498 	intrstr = pci_intr_string(pc, ih);
499 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc,
500 	    sc->sc_dev.dv_xname);
501 	if (sc->sc_ih == NULL) {
502 		printf(": couldn't establish interrupt");
503 		if (intrstr != NULL)
504 			printf(" at %s", intrstr);
505 		printf("\n");
506 		return;
507 	}
508 	printf(": %s\n", intrstr);
509 
510 	if (!sc->sc_1371) {
511 		/* Enable interrupts and looping mode. */
512 		/* enable the parts we need */
513 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
514 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
515 
516 		/* reset codec */
517 		/* normal operation */
518 		/* select codec clocks */
519 		eap1370_write_codec(sc, AK_RESET, AK_PD);
520 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
521 		eap1370_write_codec(sc, AK_CS, 0x0);
522 
523 		eap_hw_if = &eap1370_hw_if;
524 
525 		/* Enable all relevant mixer switches. */
526 		ctl.dev = EAP_INPUT_SOURCE;
527 		ctl.type = AUDIO_MIXER_SET;
528 		ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
529 		    1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
530 		    1 << EAP_MIC_VOL;
531 		eap_hw_if->set_port(sc, &ctl);
532 
533 		ctl.type = AUDIO_MIXER_VALUE;
534 		ctl.un.value.num_channels = 1;
535 		for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
536 		     ctl.dev++) {
537 			ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
538 			eap_hw_if->set_port(sc, &ctl);
539 		}
540 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
541 		eap_hw_if->set_port(sc, &ctl);
542 		ctl.dev = EAP_MIC_PREAMP;
543 		ctl.type = AUDIO_MIXER_ENUM;
544 		ctl.un.ord = 0;
545 		eap_hw_if->set_port(sc, &ctl);
546 		ctl.dev = EAP_RECORD_SOURCE;
547 		ctl.type = AUDIO_MIXER_SET;
548 		ctl.un.mask = 1 << EAP_MIC_VOL;
549 		eap_hw_if->set_port(sc, &ctl);
550 	} else {
551 		/* clean slate */
552 
553                 EWRITE4(sc, EAP_SIC, 0);
554 		EWRITE4(sc, EAP_ICSC, 0);
555 		EWRITE4(sc, E1371_LEGACY, 0);
556 
557 		if (sc->sc_ct5880) {
558 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
559 			/* Let codec wake up */
560 			delay(20000);
561 		}
562 
563                 /* Reset from es1371's perspective */
564                 EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
565                 delay(20);
566                 EWRITE4(sc, EAP_ICSC, 0);
567 
568 		/*
569 		 * Must properly reprogram sample rate converter,
570 		 * or it locks up.
571 		 *
572 		 * We don't know how to program it (no documentation),
573 		 * and the linux/oss magic receipe doesn't work (breaks
574 		 * full-duplex, by selecting different play and record
575 		 * rates). On the other hand, the sample rate converter
576 		 * can't be disabled (disabling it would disable DMA),
577 		 * so we use these magic defaults that make it "resample"
578 		 * 48kHz to 48kHz without breaking full-duplex.
579 		 */
580 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
581 		for (i = 0; i < 0x80; i++)
582 			eap1371_src_write(sc, i, 0);
583 		eap1371_src_write(sc, ESRC_ADC + ESRC_TRUNC_N, ESRC_SET_N(16));
584 		eap1371_src_write(sc, ESRC_ADC + ESRC_IREGS, ESRC_SET_VFI(16));
585 		eap1371_src_write(sc, ESRC_ADC + ESRC_VFF, 0);
586 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
587 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
588 		eap1371_src_write(sc, ESRC_DAC1 + ESRC_TRUNC_N, ESRC_SET_N(16));
589 		eap1371_src_write(sc, ESRC_DAC1 + ESRC_IREGS, ESRC_SET_VFI(16));
590 		eap1371_src_write(sc, ESRC_DAC1 + ESRC_VFF, 0);
591 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
592 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
593 		eap1371_src_write(sc, ESRC_DAC2 + ESRC_IREGS, ESRC_SET_VFI(16));
594 		eap1371_src_write(sc, ESRC_DAC2 + ESRC_TRUNC_N, ESRC_SET_N(16));
595 		eap1371_src_write(sc, ESRC_DAC2 + ESRC_VFF, 0);
596 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
597 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
598 		EWRITE4(sc, E1371_SRC, 0);
599 
600 		/* Reset codec */
601 
602 		/* Interrupt enable */
603 		sc->host_if.arg = sc;
604 		sc->host_if.attach = eap1371_attach_codec;
605 		sc->host_if.read = eap1371_read_codec;
606 		sc->host_if.write = eap1371_write_codec;
607 		sc->host_if.reset = eap1371_reset_codec;
608 		sc->host_if.flags = eap_flags_codec;
609 		sc->flags = AC97_HOST_DONT_READ;
610 
611 		if (ac97_attach(&sc->host_if) == 0) {
612 			/* Interrupt enable */
613 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
614 		} else
615 			return;
616 
617 		eap_hw_if = &eap1371_hw_if;
618 	}
619 
620 	audio_attach_mi(eap_hw_if, sc, &sc->sc_dev);
621 #if NMIDI > 0
622 	sc->sc_mididev = midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
623 #endif
624 }
625 
626 int
627 eap_resume(struct eap_softc *sc)
628 {
629 	mixer_ctrl_t ctl;
630 	int i;
631 
632 	if (!sc->sc_1371) {
633 		/* Enable interrupts and looping mode. */
634 		/* enable the parts we need */
635 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
636 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
637 
638 		/* reset codec */
639 		/* normal operation */
640 		/* select codec clocks */
641 		eap1370_write_codec(sc, AK_RESET, AK_PD);
642 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
643 		eap1370_write_codec(sc, AK_CS, 0x0);
644 
645 		bzero(&ctl, sizeof(ctl));
646 
647 		ctl.dev = EAP_RECORD_SOURCE;
648 		ctl.type = AUDIO_MIXER_SET;
649 		ctl.un.mask = sc->sc_record_source;
650 		eap1370_hw_if.set_port(sc, &ctl);
651 
652 		ctl.dev = EAP_INPUT_SOURCE;
653 		ctl.type = AUDIO_MIXER_SET;
654 		ctl.un.mask = sc->sc_input_source;
655 		eap1370_hw_if.set_port(sc, &ctl);
656 
657 		eap1370_set_mixer(sc, AK_MGAIN, sc->sc_mic_preamp);
658 
659 		for (i = EAP_MASTER_VOL; i < EAP_MIC_VOL; i++)
660 			eap1370_write_codec(sc, i, sc->sc_port[i]);
661 
662 	} else {
663 		/* clean slate */
664 
665 		EWRITE4(sc, EAP_SIC, 0);
666 		EWRITE4(sc, EAP_ICSC, 0);
667 		EWRITE4(sc, E1371_LEGACY, 0);
668 
669 		if (sc->sc_ct5880) {
670 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
671 			/* Let codec wake up */
672 			delay(20000);
673 		}
674 
675 		ac97_resume(&sc->host_if, sc->codec_if);
676 
677 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
678 		for (i = 0; i < 0x80; i++)
679 			eap1371_src_write(sc, i, 0);
680 		eap1371_src_write(sc, ESRC_ADC + ESRC_TRUNC_N, ESRC_SET_N(16));
681 		eap1371_src_write(sc, ESRC_ADC + ESRC_IREGS, ESRC_SET_VFI(16));
682 		eap1371_src_write(sc, ESRC_ADC + ESRC_VFF, 0);
683 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
684 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
685 		eap1371_src_write(sc, ESRC_DAC1 + ESRC_TRUNC_N, ESRC_SET_N(16));
686 		eap1371_src_write(sc, ESRC_DAC1 + ESRC_IREGS, ESRC_SET_VFI(16));
687 		eap1371_src_write(sc, ESRC_DAC1 + ESRC_VFF, 0);
688 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
689 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
690 		eap1371_src_write(sc, ESRC_DAC2 + ESRC_IREGS, ESRC_SET_VFI(16));
691 		eap1371_src_write(sc, ESRC_DAC2 + ESRC_TRUNC_N, ESRC_SET_N(16));
692 		eap1371_src_write(sc, ESRC_DAC2 + ESRC_VFF, 0);
693 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
694 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
695 		EWRITE4(sc, E1371_SRC, 0);
696 
697 		/* Interrupt enable */
698 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
699 	}
700 
701 	return (0);
702 }
703 
704 
705 int
706 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
707 {
708 	struct eap_softc *sc = sc_;
709 
710 	sc->codec_if = codec_if;
711 	return (0);
712 }
713 
714 void
715 eap1371_reset_codec(void *sc_)
716 {
717 	struct eap_softc *sc = sc_;
718 	u_int32_t icsc;
719 	int s;
720 
721 	s = splaudio();
722 	icsc = EREAD4(sc, EAP_ICSC);
723 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
724 	delay(20);
725 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
726 	delay(1);
727 	splx(s);
728 
729 	return;
730 }
731 
732 int
733 eap_intr(void *p)
734 {
735 	struct eap_softc *sc = p;
736 	u_int32_t intr, sic;
737 
738 	intr = EREAD4(sc, EAP_ICSS);
739 	if (!(intr & EAP_INTR))
740 		return (0);
741 	sic = EREAD4(sc, EAP_SIC);
742 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
743 	if (intr & EAP_I_ADC) {
744 #if 0
745 		/*
746 		 * XXX This is a hack!
747 		 * The EAP chip sometimes generates the recording interrupt
748 		 * while it is still transferring the data.  To make sure
749 		 * it has all arrived we busy wait until the count is right.
750 		 * The transfer we are waiting for is 8 longwords.
751 		 */
752 		int s, nw, n;
753 
754 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
755 		s = EREAD4(sc, EAP_ADC_CSR);
756 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
757 		n = 0;
758 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
759 			delay(10);
760 			if (++n > 100) {
761 				printf("eapintr: dma fix timeout");
762 				break;
763 			}
764 		}
765 		/* Continue with normal interrupt handling. */
766 #endif
767 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
768 		EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
769 		if (sc->sc_rintr)
770 			sc->sc_rintr(sc->sc_rarg);
771 	}
772 	if (intr & EAP_I_DAC2) {
773 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
774 		EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
775 		if (sc->sc_pintr)
776 			sc->sc_pintr(sc->sc_parg);
777 	}
778 #if NMIDI > 0
779 	if ((intr & EAP_I_UART) && sc->sc_iintr != NULL) {
780 		u_int32_t data;
781 
782 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXINT) {
783 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
784 				data = EREAD1(sc, EAP_UART_DATA);
785 				sc->sc_iintr(sc->sc_arg, data);
786 			}
787 		}
788 	}
789 #endif
790 	return (1);
791 }
792 
793 int
794 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
795 {
796 	int error;
797 
798 	p->size = size;
799 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
800 	    p->segs, nitems(p->segs),
801 	    &p->nsegs, BUS_DMA_NOWAIT);
802 	if (error)
803 		return (error);
804 
805 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
806 	    &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
807 	if (error)
808 		goto free;
809 
810 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
811 	    0, BUS_DMA_NOWAIT, &p->map);
812 	if (error)
813 		goto unmap;
814 
815 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
816 	    BUS_DMA_NOWAIT);
817 	if (error)
818 		goto destroy;
819 	return (0);
820 
821 destroy:
822 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
823 unmap:
824 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
825 free:
826 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
827 	return (error);
828 }
829 
830 int
831 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
832 {
833 	bus_dmamap_unload(sc->sc_dmatag, p->map);
834 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
835 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
836 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
837 	return (0);
838 }
839 
840 int
841 eap_open(void *addr, int flags)
842 {
843 	return (0);
844 }
845 
846 /*
847  * Close function is called at splaudio().
848  */
849 void
850 eap_close(void *addr)
851 {
852 	struct eap_softc *sc = addr;
853 
854 	eap_halt_output(sc);
855 	eap_halt_input(sc);
856 
857 	sc->sc_pintr = 0;
858 	sc->sc_rintr = 0;
859 }
860 
861 int
862 eap_query_encoding(void *addr, struct audio_encoding *fp)
863 {
864 	switch (fp->index) {
865 	case 0:
866 		strlcpy(fp->name, AudioEulinear, sizeof fp->name);
867 		fp->encoding = AUDIO_ENCODING_ULINEAR;
868 		fp->precision = 8;
869 		fp->flags = 0;
870 		break;
871 	case 1:
872 		strlcpy(fp->name, AudioEmulaw, sizeof fp->name);
873 		fp->encoding = AUDIO_ENCODING_ULAW;
874 		fp->precision = 8;
875 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
876 		break;
877 	case 2:
878 		strlcpy(fp->name, AudioEalaw, sizeof fp->name);
879 		fp->encoding = AUDIO_ENCODING_ALAW;
880 		fp->precision = 8;
881 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
882 		break;
883 	case 3:
884 		strlcpy(fp->name, AudioEslinear, sizeof fp->name);
885 		fp->encoding = AUDIO_ENCODING_SLINEAR;
886 		fp->precision = 8;
887 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
888 		break;
889 	case 4:
890 		strlcpy(fp->name, AudioEslinear_le, sizeof fp->name);
891 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
892 		fp->precision = 16;
893 		fp->flags = 0;
894 		break;
895 	case 5:
896 		strlcpy(fp->name, AudioEulinear_le, sizeof fp->name);
897 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
898 		fp->precision = 16;
899 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
900 		break;
901 	case 6:
902 		strlcpy(fp->name, AudioEslinear_be, sizeof fp->name);
903 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
904 		fp->precision = 16;
905 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
906 		break;
907 	case 7:
908 		strlcpy(fp->name, AudioEulinear_be, sizeof fp->name);
909 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
910 		fp->precision = 16;
911 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
912 		break;
913 	default:
914 		return (EINVAL);
915 	}
916 	fp->bps = AUDIO_BPS(fp->precision);
917 	fp->msb = 1;
918 
919 	return (0);
920 }
921 
922 void
923 eap_get_default_params(void *addr, int mode, struct audio_params *params)
924 {
925 	ac97_get_default_params(params);
926 }
927 
928 int
929 eap_set_params(void *addr, int setmode, int usemode,
930     struct audio_params *play, struct audio_params *rec)
931 {
932 	struct eap_softc *sc = addr;
933 	struct audio_params *p;
934 	int mode;
935 	u_int32_t div;
936 
937 	/*
938 	 * The es1370 only has one clock, so make the sample rates match.
939 	 */
940 	if (!sc->sc_1371) {
941 		if (play->sample_rate != rec->sample_rate &&
942 		    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
943 			if (setmode == AUMODE_PLAY) {
944 				rec->sample_rate = play->sample_rate;
945 				setmode |= AUMODE_RECORD;
946 			} else if (setmode == AUMODE_RECORD) {
947 				play->sample_rate = rec->sample_rate;
948 				setmode |= AUMODE_PLAY;
949 			} else
950 				return (EINVAL);
951 		}
952 	}
953 
954 	for (mode = AUMODE_RECORD; mode != -1;
955 	    mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
956 		if ((setmode & mode) == 0)
957 			continue;
958 
959 		p = mode == AUMODE_PLAY ? play : rec;
960 
961 		if (sc->sc_1371)
962 			p->sample_rate = 48000;
963 		if (p->sample_rate < 4000)
964 			p->sample_rate = 4000;
965 		if (p->sample_rate > 48000)
966 			p->sample_rate = 48000;
967 		if (p->precision > 16)
968 			p->precision = 16;
969 		if (p->channels > 2)
970 			p->channels = 2;
971 		p->factor = 1;
972 		p->sw_code = 0;
973 		switch (p->encoding) {
974 		case AUDIO_ENCODING_SLINEAR_BE:
975 			if (p->precision == 16)
976 				p->sw_code = swap_bytes;
977 			else
978 				p->sw_code = change_sign8;
979 			break;
980 		case AUDIO_ENCODING_SLINEAR_LE:
981 			if (p->precision != 16)
982 				p->sw_code = change_sign8;
983 			break;
984 		case AUDIO_ENCODING_ULINEAR_BE:
985 			if (p->precision == 16) {
986 				if (mode == AUMODE_PLAY)
987 					p->sw_code = swap_bytes_change_sign16_le;
988 				else
989 					p->sw_code = change_sign16_swap_bytes_le;
990 			}
991 			break;
992 		case AUDIO_ENCODING_ULINEAR_LE:
993 			if (p->precision == 16)
994 				p->sw_code = change_sign16_le;
995 			break;
996 		case AUDIO_ENCODING_ULAW:
997 			if (mode == AUMODE_PLAY) {
998 				p->factor = 2;
999 				p->sw_code = mulaw_to_slinear16_le;
1000 			} else
1001 				p->sw_code = ulinear8_to_mulaw;
1002 			break;
1003 		case AUDIO_ENCODING_ALAW:
1004 			if (mode == AUMODE_PLAY) {
1005 				p->factor = 2;
1006 				p->sw_code = alaw_to_slinear16_le;
1007 			} else
1008 				p->sw_code = ulinear8_to_alaw;
1009 			break;
1010 		default:
1011 			return (EINVAL);
1012 		}
1013 		p->bps = AUDIO_BPS(p->precision);
1014 		p->msb = 1;
1015 	}
1016 
1017 	if (!sc->sc_1371) {
1018 		/* Set the speed */
1019 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
1020 		    EREAD4(sc, EAP_ICSC)));
1021 		div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
1022 		/*
1023 		 * XXX
1024 		 * The -2 isn't documented, but seemed to make the wall
1025 		 * time match
1026 		 * what I expect.  - mycroft
1027 		 */
1028 		if (usemode == AUMODE_RECORD)
1029 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1030 			    rec->sample_rate - 2);
1031 		else
1032 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1033 			    play->sample_rate - 2);
1034 		div |= EAP_CCB_INTRM;
1035 		EWRITE4(sc, EAP_ICSC, div);
1036 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
1037 	}
1038 
1039 	return (0);
1040 }
1041 
1042 int
1043 eap_round_blocksize(void *addr, int blk)
1044 {
1045 	return ((blk + 31) & -32);	/* keep good alignment */
1046 }
1047 
1048 int
1049 eap_trigger_output(
1050 	void *addr,
1051 	void *start,
1052 	void *end,
1053 	int blksize,
1054 	void (*intr)(void *),
1055 	void *arg,
1056 	struct audio_params *param)
1057 {
1058 	struct eap_softc *sc = addr;
1059 	struct eap_dma *p;
1060 	u_int32_t icsc, sic;
1061 	int sampshift;
1062 
1063 #ifdef DIAGNOSTIC
1064 	if (sc->sc_prun)
1065 		panic("eap_trigger_output: already running");
1066 	sc->sc_prun = 1;
1067 #endif
1068 
1069 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
1070 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1071 	sc->sc_pintr = intr;
1072 	sc->sc_parg = arg;
1073 
1074 	sic = EREAD4(sc, EAP_SIC);
1075 	sic &= ~(EAP_P2_S_EB | EAP_P2_S_MB | EAP_INC_BITS);
1076 	sic |= EAP_SET_P2_ST_INC(0) | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
1077 	sampshift = 0;
1078 	if (param->precision * param->factor == 16) {
1079 		sic |= EAP_P2_S_EB;
1080 		sampshift++;
1081 	}
1082 	if (param->channels == 2) {
1083 		sic |= EAP_P2_S_MB;
1084 		sampshift++;
1085 	}
1086 	EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
1087 	EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
1088 
1089 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1090 		;
1091 	if (!p) {
1092 		printf("eap_trigger_output: bad addr %p\n", start);
1093 		return (EINVAL);
1094 	}
1095 
1096 	DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
1097 	    (int)DMAADDR(p),
1098 	    (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1099 	EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
1100 	EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
1101 	EWRITE4(sc, EAP_DAC2_SIZE,
1102 	    EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1103 
1104 	EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
1105 
1106 	if (sc->sc_1371)
1107 		EWRITE4(sc, E1371_SRC, 0);
1108 
1109 	icsc = EREAD4(sc, EAP_ICSC);
1110 	EWRITE4(sc, EAP_ICSC, icsc | EAP_DAC2_EN);
1111 
1112 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
1113 
1114 	return (0);
1115 }
1116 
1117 int
1118 eap_trigger_input(
1119 	void *addr,
1120 	void *start,
1121 	void *end,
1122 	int blksize,
1123 	void (*intr)(void *),
1124 	void *arg,
1125 	struct audio_params *param)
1126 {
1127 	struct eap_softc *sc = addr;
1128 	struct eap_dma *p;
1129 	u_int32_t icsc, sic;
1130 	int sampshift;
1131 
1132 #ifdef DIAGNOSTIC
1133 	if (sc->sc_rrun)
1134 		panic("eap_trigger_input: already running");
1135 	sc->sc_rrun = 1;
1136 #endif
1137 
1138 	DPRINTFN(1, ("eap_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1139 	    addr, start, end, blksize, intr, arg));
1140 	sc->sc_rintr = intr;
1141 	sc->sc_rarg = arg;
1142 
1143 	sic = EREAD4(sc, EAP_SIC);
1144 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
1145 	sampshift = 0;
1146 	if (param->precision * param->factor == 16) {
1147 		sic |= EAP_R1_S_EB;
1148 		sampshift++;
1149 	}
1150 	if (param->channels == 2) {
1151 		sic |= EAP_R1_S_MB;
1152 		sampshift++;
1153 	}
1154 	EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
1155 	EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
1156 
1157 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1158 		;
1159 	if (!p) {
1160 		printf("eap_trigger_input: bad addr %p\n", start);
1161 		return (EINVAL);
1162 	}
1163 
1164 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
1165 	    (int)DMAADDR(p),
1166 	    (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1167 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
1168 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
1169 	EWRITE4(sc, EAP_ADC_SIZE,
1170 	    EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1171 
1172 	EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
1173 
1174 	if (sc->sc_1371)
1175 		EWRITE4(sc, E1371_SRC, 0);
1176 
1177 	icsc = EREAD4(sc, EAP_ICSC);
1178 	EWRITE4(sc, EAP_ICSC, icsc | EAP_ADC_EN);
1179 
1180 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
1181 
1182 	return (0);
1183 }
1184 
1185 int
1186 eap_halt_output(void *addr)
1187 {
1188 	struct eap_softc *sc = addr;
1189 	u_int32_t icsc;
1190 
1191 	DPRINTF(("eap: eap_halt_output\n"));
1192 	icsc = EREAD4(sc, EAP_ICSC);
1193 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_DAC2_EN);
1194 #ifdef DIAGNOSTIC
1195 	sc->sc_prun = 0;
1196 #endif
1197 	return (0);
1198 }
1199 
1200 int
1201 eap_halt_input(void *addr)
1202 {
1203 	struct eap_softc *sc = addr;
1204 	u_int32_t icsc;
1205 
1206 	DPRINTF(("eap: eap_halt_input\n"));
1207 	icsc = EREAD4(sc, EAP_ICSC);
1208 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
1209 #ifdef DIAGNOSTIC
1210 	sc->sc_rrun = 0;
1211 #endif
1212 	return (0);
1213 }
1214 
1215 int
1216 eap_getdev(void *addr, struct audio_device *retp)
1217 {
1218 	*retp = eap_device;
1219 	return (0);
1220 }
1221 
1222 int
1223 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1224 {
1225 	struct eap_softc *sc = addr;
1226 
1227 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
1228 }
1229 
1230 int
1231 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1232 {
1233 	struct eap_softc *sc = addr;
1234 
1235 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
1236 }
1237 
1238 int
1239 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
1240 {
1241 	struct eap_softc *sc = addr;
1242 
1243 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
1244 }
1245 
1246 void
1247 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
1248 {
1249 	eap1370_write_codec(sc, a, d);
1250 
1251 	sc->sc_port[a] = d;
1252 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
1253 }
1254 
1255 int
1256 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1257 {
1258 	struct eap_softc *sc = addr;
1259 	int lval, rval, l, r, la, ra;
1260 	int l1, r1, l2, r2, m, o1, o2;
1261 
1262 	if (cp->dev == EAP_RECORD_SOURCE) {
1263 		if (cp->type != AUDIO_MIXER_SET)
1264 			return (EINVAL);
1265 		m = sc->sc_record_source = cp->un.mask;
1266 		l1 = l2 = r1 = r2 = 0;
1267 		if (m & (1 << EAP_VOICE_VOL))
1268 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
1269 		if (m & (1 << EAP_FM_VOL))
1270 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
1271 		if (m & (1 << EAP_CD_VOL))
1272 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
1273 		if (m & (1 << EAP_LINE_VOL))
1274 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
1275 		if (m & (1 << EAP_AUX_VOL))
1276 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
1277 		if (m & (1 << EAP_MIC_VOL))
1278 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
1279 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
1280 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
1281 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
1282 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
1283 		return (0);
1284 	}
1285 	if (cp->dev == EAP_INPUT_SOURCE) {
1286 		if (cp->type != AUDIO_MIXER_SET)
1287 			return (EINVAL);
1288 		m = sc->sc_input_source = cp->un.mask;
1289 		o1 = o2 = 0;
1290 		if (m & (1 << EAP_VOICE_VOL))
1291 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
1292 		if (m & (1 << EAP_FM_VOL))
1293 			o1 |= AK_M_FM_L | AK_M_FM_R;
1294 		if (m & (1 << EAP_CD_VOL))
1295 			o1 |= AK_M_CD_L | AK_M_CD_R;
1296 		if (m & (1 << EAP_LINE_VOL))
1297 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
1298 		if (m & (1 << EAP_AUX_VOL))
1299 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
1300 		if (m & (1 << EAP_MIC_VOL))
1301 			o1 |= AK_M_MIC;
1302 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
1303 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
1304 		return (0);
1305 	}
1306 	if (cp->dev == EAP_MIC_PREAMP) {
1307 		if (cp->type != AUDIO_MIXER_ENUM)
1308 			return (EINVAL);
1309 		if (cp->un.ord != 0 && cp->un.ord != 1)
1310 			return (EINVAL);
1311 		sc->sc_mic_preamp = cp->un.ord;
1312 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
1313 		return (0);
1314 	}
1315 	if (cp->type != AUDIO_MIXER_VALUE)
1316 		return (EINVAL);
1317 	if (cp->un.value.num_channels == 1)
1318 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1319 	else if (cp->un.value.num_channels == 2) {
1320 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1321 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1322 	} else
1323 		return (EINVAL);
1324 	ra = -1;
1325 	switch (cp->dev) {
1326 	case EAP_MASTER_VOL:
1327 		l = VOL_TO_ATT5(lval);
1328 		r = VOL_TO_ATT5(rval);
1329 		la = AK_MASTER_L;
1330 		ra = AK_MASTER_R;
1331 		break;
1332 	case EAP_MIC_VOL:
1333 		if (cp->un.value.num_channels != 1)
1334 			return (EINVAL);
1335 		la = AK_MIC;
1336 		goto lr;
1337 	case EAP_VOICE_VOL:
1338 		la = AK_VOICE_L;
1339 		ra = AK_VOICE_R;
1340 		goto lr;
1341 	case EAP_FM_VOL:
1342 		la = AK_FM_L;
1343 		ra = AK_FM_R;
1344 		goto lr;
1345 	case EAP_CD_VOL:
1346 		la = AK_CD_L;
1347 		ra = AK_CD_R;
1348 		goto lr;
1349 	case EAP_LINE_VOL:
1350 		la = AK_LINE_L;
1351 		ra = AK_LINE_R;
1352 		goto lr;
1353 	case EAP_AUX_VOL:
1354 		la = AK_AUX_L;
1355 		ra = AK_AUX_R;
1356 	lr:
1357 		l = VOL_TO_GAIN5(lval);
1358 		r = VOL_TO_GAIN5(rval);
1359 		break;
1360 	default:
1361 		return (EINVAL);
1362 	}
1363 	eap1370_set_mixer(sc, la, l);
1364 	if (ra >= 0) {
1365 		eap1370_set_mixer(sc, ra, r);
1366 	}
1367 	return (0);
1368 }
1369 
1370 int
1371 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1372 {
1373 	struct eap_softc *sc = addr;
1374 	int la, ra, l, r;
1375 
1376 	switch (cp->dev) {
1377 	case EAP_RECORD_SOURCE:
1378 		if (cp->type != AUDIO_MIXER_SET)
1379 			return (EINVAL);
1380 		cp->un.mask = sc->sc_record_source;
1381 		return (0);
1382 	case EAP_INPUT_SOURCE:
1383 		if (cp->type != AUDIO_MIXER_SET)
1384 			return (EINVAL);
1385 		cp->un.mask = sc->sc_input_source;
1386 		return (0);
1387 	case EAP_MIC_PREAMP:
1388 		if (cp->type != AUDIO_MIXER_ENUM)
1389 			return (EINVAL);
1390 		cp->un.ord = sc->sc_mic_preamp;
1391 		return (0);
1392 	case EAP_MASTER_VOL:
1393 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
1394 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
1395 		break;
1396 	case EAP_MIC_VOL:
1397 		if (cp->un.value.num_channels != 1)
1398 			return (EINVAL);
1399 		la = ra = AK_MIC;
1400 		goto lr;
1401 	case EAP_VOICE_VOL:
1402 		la = AK_VOICE_L;
1403 		ra = AK_VOICE_R;
1404 		goto lr;
1405 	case EAP_FM_VOL:
1406 		la = AK_FM_L;
1407 		ra = AK_FM_R;
1408 		goto lr;
1409 	case EAP_CD_VOL:
1410 		la = AK_CD_L;
1411 		ra = AK_CD_R;
1412 		goto lr;
1413 	case EAP_LINE_VOL:
1414 		la = AK_LINE_L;
1415 		ra = AK_LINE_R;
1416 		goto lr;
1417 	case EAP_AUX_VOL:
1418 		la = AK_AUX_L;
1419 		ra = AK_AUX_R;
1420 	lr:
1421 		l = GAIN5_TO_VOL(sc->sc_port[la]);
1422 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
1423 		break;
1424 	default:
1425 		return (EINVAL);
1426 	}
1427 	if (cp->un.value.num_channels == 1)
1428 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
1429 	else if (cp->un.value.num_channels == 2) {
1430 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
1431 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1432 	} else
1433 		return (EINVAL);
1434 	return (0);
1435 }
1436 
1437 int
1438 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
1439 {
1440 	switch (dip->index) {
1441 	case EAP_MASTER_VOL:
1442 		dip->type = AUDIO_MIXER_VALUE;
1443 		dip->mixer_class = EAP_OUTPUT_CLASS;
1444 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1445 		strlcpy(dip->label.name, AudioNmaster, sizeof dip->label.name);
1446 		dip->un.v.num_channels = 2;
1447 		strlcpy(dip->un.v.units.name, AudioNvolume,
1448 		    sizeof dip->un.v.units.name);
1449 		return (0);
1450 	case EAP_VOICE_VOL:
1451 		dip->type = AUDIO_MIXER_VALUE;
1452 		dip->mixer_class = EAP_INPUT_CLASS;
1453 		dip->prev = AUDIO_MIXER_LAST;
1454 		dip->next = AUDIO_MIXER_LAST;
1455 		strlcpy(dip->label.name, AudioNdac, sizeof dip->label.name);
1456 		dip->un.v.num_channels = 2;
1457 		strlcpy(dip->un.v.units.name, AudioNvolume,
1458 		    sizeof dip->un.v.units.name);
1459 		return (0);
1460 	case EAP_FM_VOL:
1461 		dip->type = AUDIO_MIXER_VALUE;
1462 		dip->mixer_class = EAP_INPUT_CLASS;
1463 		dip->prev = AUDIO_MIXER_LAST;
1464 		dip->next = AUDIO_MIXER_LAST;
1465 		strlcpy(dip->label.name, AudioNfmsynth,
1466 		    sizeof dip->label.name);
1467 		dip->un.v.num_channels = 2;
1468 		strlcpy(dip->un.v.units.name, AudioNvolume,
1469 		    sizeof dip->un.v.units.name);
1470 		return (0);
1471 	case EAP_CD_VOL:
1472 		dip->type = AUDIO_MIXER_VALUE;
1473 		dip->mixer_class = EAP_INPUT_CLASS;
1474 		dip->prev = AUDIO_MIXER_LAST;
1475 		dip->next = AUDIO_MIXER_LAST;
1476 		strlcpy(dip->label.name, AudioNcd, sizeof dip->label.name);
1477 		dip->un.v.num_channels = 2;
1478 		strlcpy(dip->un.v.units.name, AudioNvolume,
1479 		    sizeof dip->un.v.units.name);
1480 		return (0);
1481 	case EAP_LINE_VOL:
1482 		dip->type = AUDIO_MIXER_VALUE;
1483 		dip->mixer_class = EAP_INPUT_CLASS;
1484 		dip->prev = AUDIO_MIXER_LAST;
1485 		dip->next = AUDIO_MIXER_LAST;
1486 		strlcpy(dip->label.name, AudioNline, sizeof dip->label.name);
1487 		dip->un.v.num_channels = 2;
1488 		strlcpy(dip->un.v.units.name, AudioNvolume,
1489 		    sizeof dip->un.v.units.name);
1490 		return (0);
1491 	case EAP_AUX_VOL:
1492 		dip->type = AUDIO_MIXER_VALUE;
1493 		dip->mixer_class = EAP_INPUT_CLASS;
1494 		dip->prev = AUDIO_MIXER_LAST;
1495 		dip->next = AUDIO_MIXER_LAST;
1496 		strlcpy(dip->label.name, AudioNaux, sizeof dip->label.name);
1497 		dip->un.v.num_channels = 2;
1498 		strlcpy(dip->un.v.units.name, AudioNvolume,
1499 		    sizeof dip->un.v.units.name);
1500 		return (0);
1501 	case EAP_MIC_VOL:
1502 		dip->type = AUDIO_MIXER_VALUE;
1503 		dip->mixer_class = EAP_INPUT_CLASS;
1504 		dip->prev = AUDIO_MIXER_LAST;
1505 		dip->next = EAP_MIC_PREAMP;
1506 		strlcpy(dip->label.name, AudioNmicrophone,
1507 		    sizeof dip->label.name);
1508 		dip->un.v.num_channels = 1;
1509 		strlcpy(dip->un.v.units.name, AudioNvolume,
1510 		    sizeof dip->un.v.units.name);
1511 		return (0);
1512 	case EAP_RECORD_SOURCE:
1513 		dip->mixer_class = EAP_RECORD_CLASS;
1514 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1515 		strlcpy(dip->label.name, AudioNsource, sizeof dip->label.name);
1516 		dip->type = AUDIO_MIXER_SET;
1517 		dip->un.s.num_mem = 6;
1518 		strlcpy(dip->un.s.member[0].label.name, AudioNmicrophone,
1519 		    sizeof dip->un.s.member[0].label.name);
1520 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1521 		strlcpy(dip->un.s.member[1].label.name, AudioNcd,
1522 		    sizeof dip->un.s.member[1].label.name);
1523 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1524 		strlcpy(dip->un.s.member[2].label.name, AudioNline,
1525 		    sizeof dip->un.s.member[2].label.name);
1526 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1527 		strlcpy(dip->un.s.member[3].label.name, AudioNfmsynth,
1528 		    sizeof dip->un.s.member[3].label.name);
1529 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1530 		strlcpy(dip->un.s.member[4].label.name, AudioNaux,
1531 		    sizeof dip->un.s.member[4].label.name);
1532 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1533 		strlcpy(dip->un.s.member[5].label.name, AudioNdac,
1534 		    sizeof dip->un.s.member[5].label.name);
1535 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1536 		return (0);
1537 	case EAP_INPUT_SOURCE:
1538 		dip->mixer_class = EAP_INPUT_CLASS;
1539 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1540 		strlcpy(dip->label.name, AudioNsource, sizeof dip->label.name);
1541 		dip->type = AUDIO_MIXER_SET;
1542 		dip->un.s.num_mem = 6;
1543 		strlcpy(dip->un.s.member[0].label.name, AudioNmicrophone,
1544 		    sizeof dip->un.s.member[0].label.name);
1545 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1546 		strlcpy(dip->un.s.member[1].label.name, AudioNcd,
1547 		    sizeof dip->un.s.member[1].label.name);
1548 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1549 		strlcpy(dip->un.s.member[2].label.name, AudioNline,
1550 		    sizeof dip->un.s.member[2].label.name);
1551 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1552 		strlcpy(dip->un.s.member[3].label.name, AudioNfmsynth,
1553 		    sizeof dip->un.s.member[3].label.name);
1554 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1555 		strlcpy(dip->un.s.member[4].label.name, AudioNaux,
1556 		    sizeof dip->un.s.member[4].label.name);
1557 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1558 		strlcpy(dip->un.s.member[5].label.name, AudioNdac,
1559 		    sizeof dip->un.s.member[5].label.name);
1560 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1561 		return (0);
1562 	case EAP_MIC_PREAMP:
1563 		dip->type = AUDIO_MIXER_ENUM;
1564 		dip->mixer_class = EAP_INPUT_CLASS;
1565 		dip->prev = EAP_MIC_VOL;
1566 		dip->next = AUDIO_MIXER_LAST;
1567 		strlcpy(dip->label.name, AudioNpreamp, sizeof dip->label.name);
1568 		dip->un.e.num_mem = 2;
1569 		strlcpy(dip->un.e.member[0].label.name, AudioNoff,
1570 		    sizeof dip->un.e.member[0].label.name);
1571 		dip->un.e.member[0].ord = 0;
1572 		strlcpy(dip->un.e.member[1].label.name, AudioNon,
1573 		    sizeof dip->un.e.member[1].label.name);
1574 		dip->un.e.member[1].ord = 1;
1575 		return (0);
1576 	case EAP_OUTPUT_CLASS:
1577 		dip->type = AUDIO_MIXER_CLASS;
1578 		dip->mixer_class = EAP_OUTPUT_CLASS;
1579 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1580 		strlcpy(dip->label.name, AudioCoutputs,
1581 		    sizeof dip->label.name);
1582 		return (0);
1583 	case EAP_RECORD_CLASS:
1584 		dip->type = AUDIO_MIXER_CLASS;
1585 		dip->mixer_class = EAP_RECORD_CLASS;
1586 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1587 		strlcpy(dip->label.name, AudioCrecord, sizeof dip->label.name);
1588 		return (0);
1589 	case EAP_INPUT_CLASS:
1590 		dip->type = AUDIO_MIXER_CLASS;
1591 		dip->mixer_class = EAP_INPUT_CLASS;
1592 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1593 		strlcpy(dip->label.name, AudioCinputs, sizeof dip->label.name);
1594 		return (0);
1595 	}
1596 	return (ENXIO);
1597 }
1598 
1599 void *
1600 eap_malloc(void *addr, int direction, size_t size, int pool, int flags)
1601 {
1602 	struct eap_softc *sc = addr;
1603 	struct eap_dma *p;
1604 	int error;
1605 
1606 	p = malloc(sizeof(*p), pool, flags);
1607 	if (!p)
1608 		return (0);
1609 	error = eap_allocmem(sc, size, 16, p);
1610 	if (error) {
1611 		free(p, pool);
1612 		return (0);
1613 	}
1614 	p->next = sc->sc_dmas;
1615 	sc->sc_dmas = p;
1616 	return (KERNADDR(p));
1617 }
1618 
1619 void
1620 eap_free(void *addr, void *ptr, int pool)
1621 {
1622 	struct eap_softc *sc = addr;
1623 	struct eap_dma **pp, *p;
1624 
1625 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1626 		if (KERNADDR(p) == ptr) {
1627 			eap_freemem(sc, p);
1628 			*pp = p->next;
1629 			free(p, pool);
1630 			return;
1631 		}
1632 	}
1633 }
1634 
1635 paddr_t
1636 eap_mappage(void *addr, void *mem, off_t off, int prot)
1637 {
1638 	struct eap_softc *sc = addr;
1639 	struct eap_dma *p;
1640 
1641 	if (off < 0)
1642 		return (-1);
1643 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1644 		;
1645 	if (!p)
1646 		return (-1);
1647 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1648 	    off, prot, BUS_DMA_WAITOK));
1649 }
1650 
1651 int
1652 eap_get_props(void *addr)
1653 {
1654 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1655 	    AUDIO_PROP_FULLDUPLEX);
1656 }
1657 
1658 enum ac97_host_flags
1659 eap_flags_codec(void *v)
1660 {
1661       struct eap_softc *sc = v;
1662 
1663       return (sc->flags);
1664 }
1665 #if NMIDI > 0
1666 int
1667 eap_midi_open(void *addr, int flags,
1668     void (*iintr)(void *, int),
1669     void (*ointr)(void *),
1670     void *arg)
1671 {
1672 	struct eap_softc *sc = addr;
1673 	u_int32_t uctrl;
1674 
1675 	sc->sc_iintr = iintr;
1676 	sc->sc_ointr = ointr;
1677 	sc->sc_arg = arg;
1678 
1679 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
1680 	uctrl = 0;
1681 	if (flags & FREAD)
1682 		uctrl |= EAP_UC_RXINTEN;
1683 #if 0
1684 	/* I don't understand ../midi.c well enough to use output interrupts */
1685 	if (flags & FWRITE)
1686 		uctrl |= EAP_UC_TXINTEN; */
1687 #endif
1688 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
1689 
1690 	return (0);
1691 }
1692 
1693 void
1694 eap_midi_close(void *addr)
1695 {
1696 	struct eap_softc *sc = addr;
1697 
1698 	tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
1699 	EWRITE1(sc, EAP_UART_CONTROL, 0);
1700 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
1701 
1702 	sc->sc_iintr = 0;
1703 	sc->sc_ointr = 0;
1704 }
1705 
1706 int
1707 eap_midi_output(void *addr, int d)
1708 {
1709 	struct eap_softc *sc = addr;
1710 	int x;
1711 
1712 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1713 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
1714 			EWRITE1(sc, EAP_UART_DATA, d);
1715 			return (0);
1716 		}
1717 		delay(MIDI_BUSY_DELAY);
1718 	}
1719 	return (EIO);
1720 }
1721 
1722 void
1723 eap_midi_getinfo(void *addr, struct midi_info *mi)
1724 {
1725 	mi->name = "AudioPCI MIDI UART";
1726 	mi->props = MIDI_PROP_CAN_INPUT;
1727 }
1728 
1729 #endif
1730