xref: /netbsd-src/sys/dev/pci/eap.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: eap.c,v 1.41 2000/06/26 04:56:24 simonb 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(pc, pa->pa_intrtag, pa->pa_intrpin,
621 	    pa->pa_intrline, &ih)) {
622 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
623 		return;
624 	}
625 	intrstr = pci_intr_string(pc, ih);
626 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
627 	if (sc->sc_ih == NULL) {
628 		printf("%s: couldn't establish interrupt",
629 		    sc->sc_dev.dv_xname);
630 		if (intrstr != NULL)
631 			printf(" at %s", intrstr);
632 		printf("\n");
633 		return;
634 	}
635 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
636 
637 	if (!sc->sc_1371) {
638 		/* Enable interrupts and looping mode. */
639 		/* enable the parts we need */
640 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
641 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
642 
643 		/* reset codec */
644 		/* normal operation */
645 		/* select codec clocks */
646 		eap1370_write_codec(sc, AK_RESET, AK_PD);
647 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
648 		eap1370_write_codec(sc, AK_CS, 0x0);
649 
650 		eap_hw_if = &eap1370_hw_if;
651 
652 		/* Enable all relevant mixer switches. */
653 		ctl.dev = EAP_OUTPUT_SELECT;
654 		ctl.type = AUDIO_MIXER_SET;
655 		ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
656 			1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
657 			1 << EAP_MIC_VOL;
658 		eap_hw_if->set_port(sc, &ctl);
659 
660 		ctl.type = AUDIO_MIXER_VALUE;
661 		ctl.un.value.num_channels = 1;
662 		for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
663 		     ctl.dev++) {
664 			ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
665 			eap_hw_if->set_port(sc, &ctl);
666 		}
667 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
668 		eap_hw_if->set_port(sc, &ctl);
669 		ctl.dev = EAP_MIC_PREAMP;
670 		ctl.type = AUDIO_MIXER_ENUM;
671 		ctl.un.ord = 0;
672 		eap_hw_if->set_port(sc, &ctl);
673 		ctl.dev = EAP_RECORD_SOURCE;
674 		ctl.type = AUDIO_MIXER_SET;
675 		ctl.un.mask = 1 << EAP_MIC_VOL;
676 		eap_hw_if->set_port(sc, &ctl);
677 	} else {
678 		/* clean slate */
679 
680                 EWRITE4(sc, EAP_SIC, 0);
681 		EWRITE4(sc, EAP_ICSC, 0);
682 		EWRITE4(sc, E1371_LEGACY, 0);
683 
684 		if (ct5880) {
685 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
686 			/* Let codec wake up */
687 			tsleep(sc, PRIBIO, "eapcdc", hz / 20);
688 		}
689 
690                 /* Reset from es1371's perspective */
691                 EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
692                 delay(20);
693                 EWRITE4(sc, EAP_ICSC, 0);
694 
695 		/*
696 		 * Must properly reprogram sample rate converter,
697 		 * or it locks up.  Set some defaults for the life of the
698 		 * machine, and set up a sb default sample rate.
699 		 */
700 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
701 		for (i = 0; i < 0x80; i++)
702 			eap1371_src_write(sc, i, 0);
703 		eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
704 		eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
705 		eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
706 		eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
707 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
708 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
709 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
710 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
711 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
712 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
713 		eap1371_set_adc_rate(sc, 22050);
714 		eap1371_set_dac_rate(sc, 22050, 1);
715 		eap1371_set_dac_rate(sc, 22050, 2);
716 
717 		EWRITE4(sc, E1371_SRC, 0);
718 
719 		/* Reset codec */
720 
721 		/* Interrupt enable */
722 		sc->host_if.arg = sc;
723 		sc->host_if.attach = eap1371_attach_codec;
724 		sc->host_if.read = eap1371_read_codec;
725 		sc->host_if.write = eap1371_write_codec;
726 		sc->host_if.reset = eap1371_reset_codec;
727 
728 		if (ac97_attach(&sc->host_if) == 0) {
729 			/* Interrupt enable */
730 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
731 		} else
732 			return;
733 
734 		eap_hw_if = &eap1371_hw_if;
735 
736 		/* Just enable the DAC and master volumes by default */
737 		ctl.type = AUDIO_MIXER_ENUM;
738 		ctl.un.ord = 0;  /* off */
739 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCoutputs,
740 		       AudioNmaster, AudioNmute);
741 		eap1371_mixer_set_port(sc, &ctl);
742 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCinputs,
743 		       AudioNdac, AudioNmute);
744 		eap1371_mixer_set_port(sc, &ctl);
745 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
746 		       AudioNvolume, AudioNmute);
747 		eap1371_mixer_set_port(sc, &ctl);
748 
749 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
750 		       AudioNsource, NULL);
751 		ctl.type = AUDIO_MIXER_ENUM;
752 		ctl.un.ord = 0;
753 		eap1371_mixer_set_port(sc, &ctl);
754 
755 	}
756 
757 	audio_attach_mi(eap_hw_if, sc, &sc->sc_dev);
758 
759 #if NMIDI > 0
760 	midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
761 #endif
762 }
763 
764 int
765 eap1371_attach_codec(sc_, codec_if)
766 	void *sc_;
767 	struct ac97_codec_if  *codec_if;
768 {
769 	struct eap_softc *sc = sc_;
770 
771 	sc->codec_if = codec_if;
772 	return (0);
773 }
774 
775 void
776 eap1371_reset_codec(sc_)
777 	void *sc_;
778 {
779 	struct eap_softc *sc = sc_;
780 	u_int32_t icsc;
781 	int s;
782 
783 	s = splaudio();
784 	icsc = EREAD4(sc, EAP_ICSC);
785 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
786 	delay(20);
787 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
788 	delay(1);
789 	splx(s);
790 
791 	return;
792 }
793 
794 int
795 eap_intr(p)
796 	void *p;
797 {
798 	struct eap_softc *sc = p;
799 	u_int32_t intr, sic;
800 
801 	intr = EREAD4(sc, EAP_ICSS);
802 	if (!(intr & EAP_INTR))
803 		return (0);
804 	sic = EREAD4(sc, EAP_SIC);
805 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
806 	if (intr & EAP_I_ADC) {
807 		/*
808 		 * XXX This is a hack!
809 		 * The EAP chip sometimes generates the recording interrupt
810 		 * while it is still transferring the data.  To make sure
811 		 * it has all arrived we busy wait until the count is right.
812 		 * The transfer we are waiting for is 8 longwords.
813 		 */
814 		int s, nw, n;
815 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
816 		s = EREAD4(sc, EAP_ADC_CSR);
817 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
818 		n = 0;
819 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
820 			delay(10);
821 			if (++n > 100) {
822 				printf("eapintr: dma fix timeout");
823 				break;
824 			}
825 		}
826 		/* Continue with normal interrupt handling. */
827 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
828 		EWRITE4(sc, EAP_SIC, sic);
829 		if (sc->sc_rintr)
830 			sc->sc_rintr(sc->sc_rarg);
831 	}
832 	if (intr & EAP_I_DAC2) {
833 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
834 		EWRITE4(sc, EAP_SIC, sic);
835 		if (sc->sc_pintr)
836 			sc->sc_pintr(sc->sc_parg);
837 	}
838 #if NMIDI > 0
839 	if ((intr & EAP_I_UART) && sc->sc_iintr != NULL) {
840 		u_int32_t data;
841 
842 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXINT) {
843 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
844 				data = EREAD1(sc, EAP_UART_DATA);
845 				sc->sc_iintr(sc->sc_arg, data);
846 			}
847 		}
848 	}
849 #endif
850 	return (1);
851 }
852 
853 int
854 eap_allocmem(sc, size, align, p)
855 	struct eap_softc *sc;
856 	size_t size;
857 	size_t align;
858 	struct eap_dma *p;
859 {
860 	int error;
861 
862 	p->size = size;
863 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
864 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
865 				 &p->nsegs, BUS_DMA_NOWAIT);
866 	if (error)
867 		return (error);
868 
869 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
870 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
871 	if (error)
872 		goto free;
873 
874 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
875 				  0, BUS_DMA_NOWAIT, &p->map);
876 	if (error)
877 		goto unmap;
878 
879 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
880 				BUS_DMA_NOWAIT);
881 	if (error)
882 		goto destroy;
883 	return (0);
884 
885 destroy:
886 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
887 unmap:
888 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
889 free:
890 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
891 	return (error);
892 }
893 
894 int
895 eap_freemem(sc, p)
896 	struct eap_softc *sc;
897 	struct eap_dma *p;
898 {
899 	bus_dmamap_unload(sc->sc_dmatag, p->map);
900 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
901 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
902 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
903 	return (0);
904 }
905 
906 int
907 eap_open(addr, flags)
908 	void *addr;
909 	int flags;
910 {
911 	return (0);
912 }
913 
914 /*
915  * Close function is called at splaudio().
916  */
917 void
918 eap_close(addr)
919 	void *addr;
920 {
921 	struct eap_softc *sc = addr;
922 
923 	eap_halt_output(sc);
924 	eap_halt_input(sc);
925 
926 	sc->sc_pintr = 0;
927 	sc->sc_rintr = 0;
928 }
929 
930 int
931 eap_query_encoding(addr, fp)
932 	void *addr;
933 	struct audio_encoding *fp;
934 {
935 	switch (fp->index) {
936 	case 0:
937 		strcpy(fp->name, AudioEulinear);
938 		fp->encoding = AUDIO_ENCODING_ULINEAR;
939 		fp->precision = 8;
940 		fp->flags = 0;
941 		return (0);
942 	case 1:
943 		strcpy(fp->name, AudioEmulaw);
944 		fp->encoding = AUDIO_ENCODING_ULAW;
945 		fp->precision = 8;
946 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
947 		return (0);
948 	case 2:
949 		strcpy(fp->name, AudioEalaw);
950 		fp->encoding = AUDIO_ENCODING_ALAW;
951 		fp->precision = 8;
952 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
953 		return (0);
954 	case 3:
955 		strcpy(fp->name, AudioEslinear);
956 		fp->encoding = AUDIO_ENCODING_SLINEAR;
957 		fp->precision = 8;
958 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
959 		return (0);
960 	case 4:
961 		strcpy(fp->name, AudioEslinear_le);
962 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
963 		fp->precision = 16;
964 		fp->flags = 0;
965 		return (0);
966 	case 5:
967 		strcpy(fp->name, AudioEulinear_le);
968 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
969 		fp->precision = 16;
970 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
971 		return (0);
972 	case 6:
973 		strcpy(fp->name, AudioEslinear_be);
974 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
975 		fp->precision = 16;
976 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
977 		return (0);
978 	case 7:
979 		strcpy(fp->name, AudioEulinear_be);
980 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
981 		fp->precision = 16;
982 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
983 		return (0);
984 	default:
985 		return (EINVAL);
986 	}
987 }
988 
989 int
990 eap_set_params(addr, setmode, usemode, play, rec)
991 	void *addr;
992 	int setmode, usemode;
993 	struct audio_params *play, *rec;
994 {
995 	struct eap_softc *sc = addr;
996 	struct audio_params *p;
997 	int mode;
998 	u_int32_t div;
999 
1000 	/*
1001 	 * The es1370 only has one clock, so make the sample rates match.
1002 	 */
1003 	if (!sc->sc_1371) {
1004 	    if (play->sample_rate != rec->sample_rate &&
1005 		usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
1006 	    	if (setmode == AUMODE_PLAY) {
1007 		    rec->sample_rate = play->sample_rate;
1008 		    setmode |= AUMODE_RECORD;
1009 		} else if (setmode == AUMODE_RECORD) {
1010 		    play->sample_rate = rec->sample_rate;
1011 		    setmode |= AUMODE_PLAY;
1012 		} else
1013 		    return (EINVAL);
1014 	    }
1015 	}
1016 
1017 	for (mode = AUMODE_RECORD; mode != -1;
1018 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
1019 		if ((setmode & mode) == 0)
1020 			continue;
1021 
1022 		p = mode == AUMODE_PLAY ? play : rec;
1023 
1024 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
1025 		    (p->precision != 8 && p->precision != 16) ||
1026 		    (p->channels != 1 && p->channels != 2))
1027 			return (EINVAL);
1028 
1029 		p->factor = 1;
1030 		p->sw_code = 0;
1031 		switch (p->encoding) {
1032 		case AUDIO_ENCODING_SLINEAR_BE:
1033 			if (p->precision == 16)
1034 				p->sw_code = swap_bytes;
1035 			else
1036 				p->sw_code = change_sign8;
1037 			break;
1038 		case AUDIO_ENCODING_SLINEAR_LE:
1039 			if (p->precision != 16)
1040 				p->sw_code = change_sign8;
1041 			break;
1042 		case AUDIO_ENCODING_ULINEAR_BE:
1043 			if (p->precision == 16) {
1044 				if (mode == AUMODE_PLAY)
1045 					p->sw_code = swap_bytes_change_sign16_le;
1046 				else
1047 					p->sw_code = change_sign16_swap_bytes_le;
1048 			}
1049 			break;
1050 		case AUDIO_ENCODING_ULINEAR_LE:
1051 			if (p->precision == 16)
1052 				p->sw_code = change_sign16_le;
1053 			break;
1054 		case AUDIO_ENCODING_ULAW:
1055 			if (mode == AUMODE_PLAY) {
1056 				p->factor = 2;
1057 				p->sw_code = mulaw_to_slinear16_le;
1058 			} else
1059 				p->sw_code = ulinear8_to_mulaw;
1060 			break;
1061 		case AUDIO_ENCODING_ALAW:
1062 			if (mode == AUMODE_PLAY) {
1063 				p->factor = 2;
1064 				p->sw_code = alaw_to_slinear16_le;
1065 			} else
1066 				p->sw_code = ulinear8_to_alaw;
1067 			break;
1068 		default:
1069 			return (EINVAL);
1070 		}
1071 	}
1072 
1073 	if (sc->sc_1371) {
1074 		eap1371_set_dac_rate(sc, play->sample_rate, 1);
1075 		eap1371_set_dac_rate(sc, play->sample_rate, 2);
1076 		eap1371_set_adc_rate(sc, rec->sample_rate);
1077 	} else {
1078 		/* Set the speed */
1079 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
1080 			     EREAD4(sc, EAP_ICSC)));
1081 		div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
1082 		/*
1083 		 * XXX
1084 		 * The -2 isn't documented, but seemed to make the wall
1085 		 * time match
1086 		 * what I expect.  - mycroft
1087 		 */
1088 		if (usemode == AUMODE_RECORD)
1089 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1090 				rec->sample_rate - 2);
1091 		else
1092 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1093 				play->sample_rate - 2);
1094 		div |= EAP_CCB_INTRM;
1095 		EWRITE4(sc, EAP_ICSC, div);
1096 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
1097 	}
1098 
1099 	return (0);
1100 }
1101 
1102 int
1103 eap_round_blocksize(addr, blk)
1104 	void *addr;
1105 	int blk;
1106 {
1107 	return (blk & -32);	/* keep good alignment */
1108 }
1109 
1110 int
1111 eap_trigger_output(addr, start, end, blksize, intr, arg, param)
1112 	void *addr;
1113 	void *start, *end;
1114 	int blksize;
1115 	void (*intr) __P((void *));
1116 	void *arg;
1117 	struct audio_params *param;
1118 {
1119 	struct eap_softc *sc = addr;
1120 	struct eap_dma *p;
1121 	u_int32_t icsc, sic;
1122 	int sampshift;
1123 
1124 #ifdef DIAGNOSTIC
1125 	if (sc->sc_prun)
1126 		panic("eap_trigger_output: already running");
1127 	sc->sc_prun = 1;
1128 #endif
1129 
1130 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
1131 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1132 	sc->sc_pintr = intr;
1133 	sc->sc_parg = arg;
1134 
1135 	icsc = EREAD4(sc, EAP_ICSC);
1136 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_DAC2_EN);
1137 
1138 	sic = EREAD4(sc, EAP_SIC);
1139 	sic &= ~(EAP_P2_S_EB | EAP_P2_S_MB | EAP_INC_BITS);
1140 	sic |= EAP_SET_P2_ST_INC(0) | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
1141 	sampshift = 0;
1142 	if (param->precision * param->factor == 16) {
1143 		sic |= EAP_P2_S_EB;
1144 		sampshift++;
1145 	}
1146 	if (param->channels == 2) {
1147 		sic |= EAP_P2_S_MB;
1148 		sampshift++;
1149 	}
1150 	EWRITE4(sc, EAP_SIC, sic);
1151 
1152 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1153 		;
1154 	if (!p) {
1155 		printf("eap_trigger_output: bad addr %p\n", start);
1156 		return (EINVAL);
1157 	}
1158 
1159 	DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
1160 		 (int)DMAADDR(p),
1161 		 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1162 	EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
1163 	EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
1164 	EWRITE4(sc, EAP_DAC2_SIZE,
1165 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1166 
1167 	EWRITE2(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
1168 
1169 	EWRITE4(sc, EAP_ICSC, icsc | EAP_DAC2_EN);
1170 
1171 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
1172 
1173 	return (0);
1174 }
1175 
1176 int
1177 eap_trigger_input(addr, start, end, blksize, intr, arg, param)
1178 	void *addr;
1179 	void *start, *end;
1180 	int blksize;
1181 	void (*intr) __P((void *));
1182 	void *arg;
1183 	struct audio_params *param;
1184 {
1185 	struct eap_softc *sc = addr;
1186 	struct eap_dma *p;
1187 	u_int32_t icsc, sic;
1188 	int sampshift;
1189 
1190 #ifdef DIAGNOSTIC
1191 	if (sc->sc_rrun)
1192 		panic("eap_trigger_input: already running");
1193 	sc->sc_rrun = 1;
1194 #endif
1195 
1196 	DPRINTFN(1, ("eap_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1197 	    addr, start, end, blksize, intr, arg));
1198 	sc->sc_rintr = intr;
1199 	sc->sc_rarg = arg;
1200 
1201 	icsc = EREAD4(sc, EAP_ICSC);
1202 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
1203 
1204 	sic = EREAD4(sc, EAP_SIC);
1205 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
1206 	sampshift = 0;
1207 	if (param->precision * param->factor == 16) {
1208 		sic |= EAP_R1_S_EB;
1209 		sampshift++;
1210 	}
1211 	if (param->channels == 2) {
1212 		sic |= EAP_R1_S_MB;
1213 		sampshift++;
1214 	}
1215 	EWRITE4(sc, EAP_SIC, sic);
1216 
1217 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1218 		;
1219 	if (!p) {
1220 		printf("eap_trigger_input: bad addr %p\n", start);
1221 		return (EINVAL);
1222 	}
1223 
1224 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
1225 		 (int)DMAADDR(p),
1226 		 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1227 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
1228 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
1229 	EWRITE4(sc, EAP_ADC_SIZE,
1230 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1231 
1232 	EWRITE2(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
1233 
1234 	EWRITE4(sc, EAP_ICSC, icsc | EAP_ADC_EN);
1235 
1236 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
1237 
1238 	return (0);
1239 }
1240 
1241 int
1242 eap_halt_output(addr)
1243 	void *addr;
1244 {
1245 	struct eap_softc *sc = addr;
1246 	u_int32_t icsc;
1247 
1248 	DPRINTF(("eap: eap_halt_output\n"));
1249 	icsc = EREAD4(sc, EAP_ICSC);
1250 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_DAC2_EN);
1251 #ifdef DIAGNOSTIC
1252 	sc->sc_prun = 0;
1253 #endif
1254 	return (0);
1255 }
1256 
1257 int
1258 eap_halt_input(addr)
1259 	void *addr;
1260 {
1261 	struct eap_softc *sc = addr;
1262 	u_int32_t icsc;
1263 
1264 	DPRINTF(("eap: eap_halt_input\n"));
1265 	icsc = EREAD4(sc, EAP_ICSC);
1266 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
1267 #ifdef DIAGNOSTIC
1268 	sc->sc_rrun = 0;
1269 #endif
1270 	return (0);
1271 }
1272 
1273 int
1274 eap_getdev(addr, retp)
1275 	void *addr;
1276 	struct audio_device *retp;
1277 {
1278 	*retp = eap_device;
1279 	return (0);
1280 }
1281 
1282 int
1283 eap1371_mixer_set_port(addr, cp)
1284 	void *addr;
1285 	mixer_ctrl_t *cp;
1286 {
1287 	struct eap_softc *sc = addr;
1288 
1289 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
1290 }
1291 
1292 int
1293 eap1371_mixer_get_port(addr, cp)
1294 	void *addr;
1295 	mixer_ctrl_t *cp;
1296 {
1297 	struct eap_softc *sc = addr;
1298 
1299 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
1300 }
1301 
1302 int
1303 eap1371_query_devinfo(addr, dip)
1304 	void *addr;
1305 	mixer_devinfo_t *dip;
1306 {
1307 	struct eap_softc *sc = addr;
1308 
1309 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
1310 }
1311 
1312 int
1313 eap1371_get_portnum_by_name(sc, class, device, qualifier)
1314 	struct eap_softc *sc;
1315 	char *class, *device, *qualifier;
1316 {
1317 	return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
1318 	     device, qualifier));
1319 }
1320 
1321 void
1322 eap1370_set_mixer(sc, a, d)
1323 	struct eap_softc *sc;
1324 	int a, d;
1325 {
1326 	eap1370_write_codec(sc, a, d);
1327 
1328 	sc->sc_port[a] = d;
1329 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
1330 }
1331 
1332 int
1333 eap1370_mixer_set_port(addr, cp)
1334 	void *addr;
1335 	mixer_ctrl_t *cp;
1336 {
1337 	struct eap_softc *sc = addr;
1338 	int lval, rval, l, r, la, ra;
1339 	int l1, r1, l2, r2, m, o1, o2;
1340 
1341 	if (cp->dev == EAP_RECORD_SOURCE) {
1342 		if (cp->type != AUDIO_MIXER_SET)
1343 			return (EINVAL);
1344 		m = sc->sc_record_source = cp->un.mask;
1345 		l1 = l2 = r1 = r2 = 0;
1346 		if (m & (1 << EAP_VOICE_VOL))
1347 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
1348 		if (m & (1 << EAP_FM_VOL))
1349 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
1350 		if (m & (1 << EAP_CD_VOL))
1351 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
1352 		if (m & (1 << EAP_LINE_VOL))
1353 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
1354 		if (m & (1 << EAP_AUX_VOL))
1355 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
1356 		if (m & (1 << EAP_MIC_VOL))
1357 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
1358 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
1359 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
1360 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
1361 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
1362 		return (0);
1363 	}
1364 	if (cp->dev == EAP_OUTPUT_SELECT) {
1365 		if (cp->type != AUDIO_MIXER_SET)
1366 			return (EINVAL);
1367 		m = sc->sc_output_source = cp->un.mask;
1368 		o1 = o2 = 0;
1369 		if (m & (1 << EAP_VOICE_VOL))
1370 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
1371 		if (m & (1 << EAP_FM_VOL))
1372 			o1 |= AK_M_FM_L | AK_M_FM_R;
1373 		if (m & (1 << EAP_CD_VOL))
1374 			o1 |= AK_M_CD_L | AK_M_CD_R;
1375 		if (m & (1 << EAP_LINE_VOL))
1376 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
1377 		if (m & (1 << EAP_AUX_VOL))
1378 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
1379 		if (m & (1 << EAP_MIC_VOL))
1380 			o1 |= AK_M_MIC;
1381 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
1382 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
1383 		return (0);
1384 	}
1385 	if (cp->dev == EAP_MIC_PREAMP) {
1386 		if (cp->type != AUDIO_MIXER_ENUM)
1387 			return (EINVAL);
1388 		if (cp->un.ord != 0 && cp->un.ord != 1)
1389 			return (EINVAL);
1390 		sc->sc_mic_preamp = cp->un.ord;
1391 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
1392 		return (0);
1393 	}
1394 	if (cp->type != AUDIO_MIXER_VALUE)
1395 		return (EINVAL);
1396 	if (cp->un.value.num_channels == 1)
1397 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1398 	else if (cp->un.value.num_channels == 2) {
1399 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1400 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1401 	} else
1402 		return (EINVAL);
1403 	ra = -1;
1404 	switch (cp->dev) {
1405 	case EAP_MASTER_VOL:
1406 		l = VOL_TO_ATT5(lval);
1407 		r = VOL_TO_ATT5(rval);
1408 		la = AK_MASTER_L;
1409 		ra = AK_MASTER_R;
1410 		break;
1411 	case EAP_MIC_VOL:
1412 		if (cp->un.value.num_channels != 1)
1413 			return (EINVAL);
1414 		la = AK_MIC;
1415 		goto lr;
1416 	case EAP_VOICE_VOL:
1417 		la = AK_VOICE_L;
1418 		ra = AK_VOICE_R;
1419 		goto lr;
1420 	case EAP_FM_VOL:
1421 		la = AK_FM_L;
1422 		ra = AK_FM_R;
1423 		goto lr;
1424 	case EAP_CD_VOL:
1425 		la = AK_CD_L;
1426 		ra = AK_CD_R;
1427 		goto lr;
1428 	case EAP_LINE_VOL:
1429 		la = AK_LINE_L;
1430 		ra = AK_LINE_R;
1431 		goto lr;
1432 	case EAP_AUX_VOL:
1433 		la = AK_AUX_L;
1434 		ra = AK_AUX_R;
1435 	lr:
1436 		l = VOL_TO_GAIN5(lval);
1437 		r = VOL_TO_GAIN5(rval);
1438 		break;
1439 	default:
1440 		return (EINVAL);
1441 	}
1442 	eap1370_set_mixer(sc, la, l);
1443 	if (ra >= 0) {
1444 		eap1370_set_mixer(sc, ra, r);
1445 	}
1446 	return (0);
1447 }
1448 
1449 int
1450 eap1370_mixer_get_port(addr, cp)
1451 	void *addr;
1452 	mixer_ctrl_t *cp;
1453 {
1454 	struct eap_softc *sc = addr;
1455 	int la, ra, l, r;
1456 
1457 	switch (cp->dev) {
1458 	case EAP_RECORD_SOURCE:
1459 		if (cp->type != AUDIO_MIXER_SET)
1460 			return (EINVAL);
1461 		cp->un.mask = sc->sc_record_source;
1462 		return (0);
1463 	case EAP_OUTPUT_SELECT:
1464 		if (cp->type != AUDIO_MIXER_SET)
1465 			return (EINVAL);
1466 		cp->un.mask = sc->sc_output_source;
1467 		return (0);
1468 	case EAP_MIC_PREAMP:
1469 		if (cp->type != AUDIO_MIXER_ENUM)
1470 			return (EINVAL);
1471 		cp->un.ord = sc->sc_mic_preamp;
1472 		return (0);
1473 	case EAP_MASTER_VOL:
1474 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
1475 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
1476 		break;
1477 	case EAP_MIC_VOL:
1478 		if (cp->un.value.num_channels != 1)
1479 			return (EINVAL);
1480 		la = ra = AK_MIC;
1481 		goto lr;
1482 	case EAP_VOICE_VOL:
1483 		la = AK_VOICE_L;
1484 		ra = AK_VOICE_R;
1485 		goto lr;
1486 	case EAP_FM_VOL:
1487 		la = AK_FM_L;
1488 		ra = AK_FM_R;
1489 		goto lr;
1490 	case EAP_CD_VOL:
1491 		la = AK_CD_L;
1492 		ra = AK_CD_R;
1493 		goto lr;
1494 	case EAP_LINE_VOL:
1495 		la = AK_LINE_L;
1496 		ra = AK_LINE_R;
1497 		goto lr;
1498 	case EAP_AUX_VOL:
1499 		la = AK_AUX_L;
1500 		ra = AK_AUX_R;
1501 	lr:
1502 		l = GAIN5_TO_VOL(sc->sc_port[la]);
1503 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
1504 		break;
1505 	default:
1506 		return (EINVAL);
1507 	}
1508 	if (cp->un.value.num_channels == 1)
1509 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
1510 	else if (cp->un.value.num_channels == 2) {
1511 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
1512 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1513 	} else
1514 		return (EINVAL);
1515 	return (0);
1516 }
1517 
1518 int
1519 eap1370_query_devinfo(addr, dip)
1520 	void *addr;
1521 	mixer_devinfo_t *dip;
1522 {
1523 	switch (dip->index) {
1524 	case EAP_MASTER_VOL:
1525 		dip->type = AUDIO_MIXER_VALUE;
1526 		dip->mixer_class = EAP_OUTPUT_CLASS;
1527 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1528 		strcpy(dip->label.name, AudioNmaster);
1529 		dip->un.v.num_channels = 2;
1530 		strcpy(dip->un.v.units.name, AudioNvolume);
1531 		return (0);
1532 	case EAP_VOICE_VOL:
1533 		dip->type = AUDIO_MIXER_VALUE;
1534 		dip->mixer_class = EAP_INPUT_CLASS;
1535 		dip->prev = AUDIO_MIXER_LAST;
1536 		dip->next = AUDIO_MIXER_LAST;
1537 		strcpy(dip->label.name, AudioNdac);
1538 		dip->un.v.num_channels = 2;
1539 		strcpy(dip->un.v.units.name, AudioNvolume);
1540 		return (0);
1541 	case EAP_FM_VOL:
1542 		dip->type = AUDIO_MIXER_VALUE;
1543 		dip->mixer_class = EAP_INPUT_CLASS;
1544 		dip->prev = AUDIO_MIXER_LAST;
1545 		dip->next = AUDIO_MIXER_LAST;
1546 		strcpy(dip->label.name, AudioNfmsynth);
1547 		dip->un.v.num_channels = 2;
1548 		strcpy(dip->un.v.units.name, AudioNvolume);
1549 		return (0);
1550 	case EAP_CD_VOL:
1551 		dip->type = AUDIO_MIXER_VALUE;
1552 		dip->mixer_class = EAP_INPUT_CLASS;
1553 		dip->prev = AUDIO_MIXER_LAST;
1554 		dip->next = AUDIO_MIXER_LAST;
1555 		strcpy(dip->label.name, AudioNcd);
1556 		dip->un.v.num_channels = 2;
1557 		strcpy(dip->un.v.units.name, AudioNvolume);
1558 		return (0);
1559 	case EAP_LINE_VOL:
1560 		dip->type = AUDIO_MIXER_VALUE;
1561 		dip->mixer_class = EAP_INPUT_CLASS;
1562 		dip->prev = AUDIO_MIXER_LAST;
1563 		dip->next = AUDIO_MIXER_LAST;
1564 		strcpy(dip->label.name, AudioNline);
1565 		dip->un.v.num_channels = 2;
1566 		strcpy(dip->un.v.units.name, AudioNvolume);
1567 		return (0);
1568 	case EAP_AUX_VOL:
1569 		dip->type = AUDIO_MIXER_VALUE;
1570 		dip->mixer_class = EAP_INPUT_CLASS;
1571 		dip->prev = AUDIO_MIXER_LAST;
1572 		dip->next = AUDIO_MIXER_LAST;
1573 		strcpy(dip->label.name, AudioNaux);
1574 		dip->un.v.num_channels = 2;
1575 		strcpy(dip->un.v.units.name, AudioNvolume);
1576 		return (0);
1577 	case EAP_MIC_VOL:
1578 		dip->type = AUDIO_MIXER_VALUE;
1579 		dip->mixer_class = EAP_INPUT_CLASS;
1580 		dip->prev = AUDIO_MIXER_LAST;
1581 		dip->next = EAP_MIC_PREAMP;
1582 		strcpy(dip->label.name, AudioNmicrophone);
1583 		dip->un.v.num_channels = 1;
1584 		strcpy(dip->un.v.units.name, AudioNvolume);
1585 		return (0);
1586 	case EAP_RECORD_SOURCE:
1587 		dip->mixer_class = EAP_RECORD_CLASS;
1588 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1589 		strcpy(dip->label.name, AudioNsource);
1590 		dip->type = AUDIO_MIXER_SET;
1591 		dip->un.s.num_mem = 6;
1592 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1593 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1594 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
1595 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1596 		strcpy(dip->un.s.member[2].label.name, AudioNline);
1597 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1598 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1599 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1600 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
1601 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1602 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
1603 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1604 		return (0);
1605 	case EAP_OUTPUT_SELECT:
1606 		dip->mixer_class = EAP_OUTPUT_CLASS;
1607 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1608 		strcpy(dip->label.name, AudioNselect);
1609 		dip->type = AUDIO_MIXER_SET;
1610 		dip->un.s.num_mem = 6;
1611 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1612 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1613 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
1614 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1615 		strcpy(dip->un.s.member[2].label.name, AudioNline);
1616 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1617 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1618 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1619 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
1620 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1621 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
1622 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1623 		return (0);
1624 	case EAP_MIC_PREAMP:
1625 		dip->type = AUDIO_MIXER_ENUM;
1626 		dip->mixer_class = EAP_INPUT_CLASS;
1627 		dip->prev = EAP_MIC_VOL;
1628 		dip->next = AUDIO_MIXER_LAST;
1629 		strcpy(dip->label.name, AudioNpreamp);
1630 		dip->un.e.num_mem = 2;
1631 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
1632 		dip->un.e.member[0].ord = 0;
1633 		strcpy(dip->un.e.member[1].label.name, AudioNon);
1634 		dip->un.e.member[1].ord = 1;
1635 		return (0);
1636 	case EAP_OUTPUT_CLASS:
1637 		dip->type = AUDIO_MIXER_CLASS;
1638 		dip->mixer_class = EAP_OUTPUT_CLASS;
1639 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1640 		strcpy(dip->label.name, AudioCoutputs);
1641 		return (0);
1642 	case EAP_RECORD_CLASS:
1643 		dip->type = AUDIO_MIXER_CLASS;
1644 		dip->mixer_class = EAP_RECORD_CLASS;
1645 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1646 		strcpy(dip->label.name, AudioCrecord);
1647 		return (0);
1648 	case EAP_INPUT_CLASS:
1649 		dip->type = AUDIO_MIXER_CLASS;
1650 		dip->mixer_class = EAP_INPUT_CLASS;
1651 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1652 		strcpy(dip->label.name, AudioCinputs);
1653 		return (0);
1654 	}
1655 	return (ENXIO);
1656 }
1657 
1658 void *
1659 eap_malloc(addr, direction, size, pool, flags)
1660 	void *addr;
1661 	int direction;
1662 	size_t size;
1663 	int pool, flags;
1664 {
1665 	struct eap_softc *sc = addr;
1666 	struct eap_dma *p;
1667 	int error;
1668 
1669 	p = malloc(sizeof(*p), pool, flags);
1670 	if (!p)
1671 		return (0);
1672 	error = eap_allocmem(sc, size, 16, p);
1673 	if (error) {
1674 		free(p, pool);
1675 		return (0);
1676 	}
1677 	p->next = sc->sc_dmas;
1678 	sc->sc_dmas = p;
1679 	return (KERNADDR(p));
1680 }
1681 
1682 void
1683 eap_free(addr, ptr, pool)
1684 	void *addr;
1685 	void *ptr;
1686 	int pool;
1687 {
1688 	struct eap_softc *sc = addr;
1689 	struct eap_dma **pp, *p;
1690 
1691 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1692 		if (KERNADDR(p) == ptr) {
1693 			eap_freemem(sc, p);
1694 			*pp = p->next;
1695 			free(p, pool);
1696 			return;
1697 		}
1698 	}
1699 }
1700 
1701 size_t
1702 eap_round_buffersize(addr, direction, size)
1703 	void *addr;
1704 	int direction;
1705 	size_t size;
1706 {
1707 	return (size);
1708 }
1709 
1710 paddr_t
1711 eap_mappage(addr, mem, off, prot)
1712 	void *addr;
1713 	void *mem;
1714 	off_t off;
1715 	int prot;
1716 {
1717 	struct eap_softc *sc = addr;
1718 	struct eap_dma *p;
1719 
1720 	if (off < 0)
1721 		return (-1);
1722 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1723 		;
1724 	if (!p)
1725 		return (-1);
1726 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1727 				off, prot, BUS_DMA_WAITOK));
1728 }
1729 
1730 int
1731 eap_get_props(addr)
1732 	void *addr;
1733 {
1734 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1735 		AUDIO_PROP_FULLDUPLEX);
1736 }
1737 
1738 #if NMIDI > 0
1739 int
1740 eap_midi_open(addr, flags, iintr, ointr, arg)
1741 	void *addr;
1742 	int flags;
1743 	void (*iintr)__P((void *, int));
1744 	void (*ointr)__P((void *));
1745 	void *arg;
1746 {
1747 	struct eap_softc *sc = addr;
1748 	u_int32_t uctrl;
1749 
1750 	sc->sc_iintr = iintr;
1751 	sc->sc_ointr = ointr;
1752 	sc->sc_arg = arg;
1753 
1754 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
1755 	uctrl = 0;
1756 	if (flags & FREAD)
1757 		uctrl |= EAP_UC_RXINTEN;
1758 #if 0
1759 	/* I don't understand ../midi.c well enough to use output interrupts */
1760 	if (flags & FWRITE)
1761 		uctrl |= EAP_UC_TXINTEN; */
1762 #endif
1763 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
1764 
1765 	return (0);
1766 }
1767 
1768 void
1769 eap_midi_close(addr)
1770 	void *addr;
1771 {
1772 	struct eap_softc *sc = addr;
1773 
1774 	EWRITE1(sc, EAP_UART_CONTROL, 0);
1775 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
1776 
1777 	sc->sc_iintr = 0;
1778 	sc->sc_ointr = 0;
1779 }
1780 
1781 int
1782 eap_midi_output(addr, d)
1783 	void *addr;
1784 	int d;
1785 {
1786 	struct eap_softc *sc = addr;
1787 	int x;
1788 
1789 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1790 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
1791 			EWRITE1(sc, EAP_UART_DATA, d);
1792 			return (0);
1793 		}
1794 		delay(MIDI_BUSY_DELAY);
1795 	}
1796 	return (EIO);
1797 }
1798 
1799 void
1800 eap_midi_getinfo(addr, mi)
1801 	void *addr;
1802 	struct midi_info *mi;
1803 {
1804 	mi->name = "AudioPCI MIDI UART";
1805 	mi->props = MIDI_PROP_CAN_INPUT;
1806 }
1807 
1808 #endif
1809