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