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