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