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