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