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