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