xref: /netbsd-src/sys/dev/pci/eap.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: eap.c,v 1.94 2011/11/24 03:35:58 mrg 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.94 2011/11/24 03:35:58 mrg 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 
537 	sc = device_private(self);
538 	sc->sc_dev = self;
539 	pa = (struct pci_attach_args *)aux;
540 	pc = pa->pa_pc;
541 	revstr = "";
542 	aprint_naive(": Audio controller\n");
543 
544 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
545 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
546 
547 	/* Stash this away for detach */
548 	sc->sc_pc = pc;
549 
550 	/* Flag if we're "creative" */
551 	sc->sc_1371 = !(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
552 			PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI);
553 
554 	/*
555 	 * The vendor and product ID's are quite "interesting". Just
556 	 * trust the following and be happy.
557 	 */
558 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
559 	revision = PCI_REVISION(pa->pa_class);
560 	ct5880 = 0;
561 	if (sc->sc_1371) {
562 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
563 		    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_CT5880) {
564 			ct5880 = 1;
565 			switch (revision) {
566 			case EAP_CT5880_C: revstr = "CT5880-C "; break;
567 			case EAP_CT5880_D: revstr = "CT5880-D "; break;
568 			case EAP_CT5880_E: revstr = "CT5880-E "; break;
569 			}
570 		} else {
571 			switch (revision) {
572 			case EAP_EV1938_A: revstr = "EV1938-A "; break;
573 			case EAP_ES1373_A: revstr = "ES1373-A "; break;
574 			case EAP_ES1373_B: revstr = "ES1373-B "; break;
575 			case EAP_CT5880_A: revstr = "CT5880-A "; ct5880=1;break;
576 			case EAP_ES1373_8: revstr = "ES1373-8" ; ct5880=1;break;
577 			case EAP_ES1371_B: revstr = "ES1371-B "; break;
578 			}
579 		}
580 	}
581 	aprint_normal(": %s %s(rev. 0x%02x)\n", devinfo, revstr, revision);
582 
583 	/* Map I/O register */
584 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
585 	      &sc->iot, &sc->ioh, NULL, &sc->iosz)) {
586 		aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
587 		return;
588 	}
589 
590 	sc->sc_dmatag = pa->pa_dmat;
591 
592 	/* Enable the device. */
593 	csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
594 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
595 		       csr | PCI_COMMAND_MASTER_ENABLE);
596 
597 	/* Map and establish the interrupt. */
598 	if (pci_intr_map(pa, &ih)) {
599 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
600 		return;
601 	}
602 	intrstr = pci_intr_string(pc, ih);
603 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
604 	if (sc->sc_ih == NULL) {
605 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
606 		if (intrstr != NULL)
607 			aprint_error(" at %s", intrstr);
608 		aprint_error("\n");
609 		return;
610 	}
611 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
612 
613 	sc->sc_ei[EAP_I1].parent = self;
614 	sc->sc_ei[EAP_I1].index = EAP_DAC2;
615 	sc->sc_ei[EAP_I2].parent = self;
616 	sc->sc_ei[EAP_I2].index = EAP_DAC1;
617 
618 	if (!sc->sc_1371) {
619 		/* Enable interrupts and looping mode. */
620 		/* enable the parts we need */
621 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
622 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
623 
624 		/* reset codec */
625 		/* normal operation */
626 		/* select codec clocks */
627 		eap1370_write_codec(sc, AK_RESET, AK_PD);
628 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
629 		eap1370_write_codec(sc, AK_CS, 0x0);
630 
631 		eap_hw_if = &eap1370_hw_if;
632 
633 		/* Enable all relevant mixer switches. */
634 		ctl.dev = EAP_INPUT_SOURCE;
635 		ctl.type = AUDIO_MIXER_SET;
636 		ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
637 			1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
638 			1 << EAP_MIC_VOL;
639 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
640 
641 		ctl.type = AUDIO_MIXER_VALUE;
642 		ctl.un.value.num_channels = 1;
643 		for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
644 		     ctl.dev++) {
645 			ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
646 			eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
647 		}
648 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
649 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
650 		ctl.dev = EAP_MIC_PREAMP;
651 		ctl.type = AUDIO_MIXER_ENUM;
652 		ctl.un.ord = 0;
653 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
654 		ctl.dev = EAP_RECORD_SOURCE;
655 		ctl.type = AUDIO_MIXER_SET;
656 		ctl.un.mask = 1 << EAP_MIC_VOL;
657 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
658 	} else {
659 		/* clean slate */
660 
661 		EWRITE4(sc, EAP_SIC, 0);
662 		EWRITE4(sc, EAP_ICSC, 0);
663 		EWRITE4(sc, E1371_LEGACY, 0);
664 
665 		if (ct5880) {
666 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
667 			/* Let codec wake up */
668 			delay(20000);
669 		}
670 
671 		/* Reset from es1371's perspective */
672 		EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
673 		delay(20);
674 		EWRITE4(sc, EAP_ICSC, 0);
675 
676 		/*
677 		 * Must properly reprogram sample rate converter,
678 		 * or it locks up.  Set some defaults for the life of the
679 		 * machine, and set up a sb default sample rate.
680 		 */
681 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
682 		for (i = 0; i < 0x80; i++)
683 			eap1371_src_write(sc, i, 0);
684 		eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
685 		eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
686 		eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
687 		eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
688 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
689 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
690 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
691 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
692 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
693 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
694 		eap1371_set_adc_rate(sc, 22050);
695 		eap1371_set_dac_rate(&sc->sc_ei[0], 22050);
696 		eap1371_set_dac_rate(&sc->sc_ei[1], 22050);
697 
698 		EWRITE4(sc, E1371_SRC, 0);
699 
700 		/* Reset codec */
701 
702 		/* Interrupt enable */
703 		sc->host_if.arg = sc;
704 		sc->host_if.attach = eap1371_attach_codec;
705 		sc->host_if.read = eap1371_read_codec;
706 		sc->host_if.write = eap1371_write_codec;
707 		sc->host_if.reset = eap1371_reset_codec;
708 
709 		if (ac97_attach(&sc->host_if, self, &sc->sc_lock) == 0) {
710 			/* Interrupt enable */
711 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
712 		} else
713 			return;
714 
715 		eap_hw_if = &eap1371_hw_if;
716 	}
717 
718 	sc->sc_ei[EAP_I1].ei_audiodev =
719 	    audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I1], sc->sc_dev);
720 
721 #ifdef EAP_USE_BOTH_DACS
722 	aprint_normal_dev(self, "attaching secondary DAC\n");
723 	sc->sc_ei[EAP_I2].ei_audiodev =
724 	    audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I2], sc->sc_dev);
725 #endif
726 
727 #if NMIDI > 0
728 	sc->sc_mididev = midi_attach_mi(&eap_midi_hw_if, sc, sc->sc_dev);
729 #endif
730 
731 #if NJOY_EAP > 0
732 	if (sc->sc_1371) {
733 		gpargs.gpa_iot = sc->iot;
734 		gpargs.gpa_ioh = sc->ioh;
735 		sc->sc_gameport = eap_joy_attach(sc->sc_dev, &gpargs);
736 	}
737 #endif
738 }
739 
740 static int
741 eap_detach(device_t self, int flags)
742 {
743 	struct eap_softc *sc;
744 	int res;
745 #if NJOY_EAP > 0
746 	struct eap_gameport_args gpargs;
747 
748 	sc = device_private(self);
749 	if (sc->sc_gameport) {
750 		gpargs.gpa_iot = sc->iot;
751 		gpargs.gpa_ioh = sc->ioh;
752 		res = eap_joy_detach(sc->sc_gameport, &gpargs);
753 		if (res)
754 			return res;
755 	}
756 #else
757 	sc = device_private(self);
758 #endif
759 #if NMIDI > 0
760 	if (sc->sc_mididev != NULL) {
761 		res = config_detach(sc->sc_mididev, 0);
762 		if (res)
763 			return res;
764 	}
765 #endif
766 #ifdef EAP_USE_BOTH_DACS
767 	if (sc->sc_ei[EAP_I2].ei_audiodev != NULL) {
768 		res = config_detach(sc->sc_ei[EAP_I2].ei_audiodev, 0);
769 		if (res)
770 			return res;
771 	}
772 #endif
773 	if (sc->sc_ei[EAP_I1].ei_audiodev != NULL) {
774 		res = config_detach(sc->sc_ei[EAP_I1].ei_audiodev, 0);
775 		if (res)
776 			return res;
777 	}
778 
779 	bus_space_unmap(sc->iot, sc->ioh, sc->iosz);
780 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
781 	mutex_destroy(&sc->sc_lock);
782 	mutex_destroy(&sc->sc_intr_lock);
783 
784 	return 0;
785 }
786 
787 static int
788 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
789 {
790 	struct eap_softc *sc;
791 
792 	sc = sc_;
793 	sc->codec_if = codec_if;
794 	return 0;
795 }
796 
797 static int
798 eap1371_reset_codec(void *sc_)
799 {
800 	struct eap_softc *sc;
801 	uint32_t icsc;
802 
803 	sc = sc_;
804 	mutex_spin_enter(&sc->sc_intr_lock);
805 	icsc = EREAD4(sc, EAP_ICSC);
806 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
807 	delay(20);
808 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
809 	delay(1);
810 	mutex_spin_exit(&sc->sc_intr_lock);
811 
812 	return 0;
813 }
814 
815 static int
816 eap_intr(void *p)
817 {
818 	struct eap_softc *sc;
819 	uint32_t intr, sic;
820 
821 	sc = p;
822 	mutex_spin_enter(&sc->sc_intr_lock);
823 	intr = EREAD4(sc, EAP_ICSS);
824 	if (!(intr & EAP_INTR)) {
825 		mutex_spin_exit(&sc->sc_intr_lock);
826 		return 0;
827 	}
828 	sic = EREAD4(sc, EAP_SIC);
829 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
830 	if (intr & EAP_I_ADC) {
831 #if 0
832 		/*
833 		 * XXX This is a hack!
834 		 * The EAP chip sometimes generates the recording interrupt
835 		 * while it is still transferring the data.  To make sure
836 		 * it has all arrived we busy wait until the count is right.
837 		 * The transfer we are waiting for is 8 longwords.
838 		 */
839 		int s, nw, n;
840 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
841 		s = EREAD4(sc, EAP_ADC_CSR);
842 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
843 		n = 0;
844 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
845 			delay(10);
846 			if (++n > 100) {
847 				printf("eapintr: DMA fix timeout");
848 				break;
849 			}
850 		}
851 		/* Continue with normal interrupt handling. */
852 #endif
853 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
854 		EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
855 		if (sc->sc_rintr)
856 			sc->sc_rintr(sc->sc_rarg);
857 	}
858 
859 	if (intr & EAP_I_DAC2) {
860 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
861 		EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
862 		if (sc->sc_ei[EAP_DAC2].ei_pintr)
863 			sc->sc_ei[EAP_DAC2].ei_pintr(sc->sc_ei[EAP_DAC2].ei_parg);
864 	}
865 
866 	if (intr & EAP_I_DAC1) {
867 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P1_INTR_EN);
868 		EWRITE4(sc, EAP_SIC, sic | EAP_P1_INTR_EN);
869 		if (sc->sc_ei[EAP_DAC1].ei_pintr)
870 			sc->sc_ei[EAP_DAC1].ei_pintr(sc->sc_ei[EAP_DAC1].ei_parg);
871 	}
872 
873 	if (intr & EAP_I_MCCB)
874 		panic("eap_intr: unexpected MCCB interrupt");
875 #if NMIDI > 0
876 	if (intr & EAP_I_UART) {
877 		uint8_t ustat;
878 		uint32_t data;
879 
880 		ustat = EREAD1(sc, EAP_UART_STATUS);
881 
882 		if (ustat & EAP_US_RXINT) {
883 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
884 				data = EREAD1(sc, EAP_UART_DATA);
885 				sc->sc_iintr(sc->sc_arg, data);
886 			}
887 		}
888 
889 		if (ustat & EAP_US_TXINT)
890 			eap_uart_txrdy(sc);
891 	}
892 #endif
893 	mutex_spin_exit(&sc->sc_intr_lock);
894 	return 1;
895 }
896 
897 static int
898 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
899 {
900 	int error;
901 
902 	p->size = size;
903 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
904 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
905 				 &p->nsegs, BUS_DMA_WAITOK);
906 	if (error)
907 		return error;
908 
909 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
910 			       &p->addr, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
911 	if (error)
912 		goto free;
913 
914 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
915 				  0, BUS_DMA_WAITOK, &p->map);
916 	if (error)
917 		goto unmap;
918 
919 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
920 				BUS_DMA_WAITOK);
921 	if (error)
922 		goto destroy;
923 	return (0);
924 
925 destroy:
926 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
927 unmap:
928 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
929 free:
930 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
931 	return error;
932 }
933 
934 static int
935 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
936 {
937 
938 	bus_dmamap_unload(sc->sc_dmatag, p->map);
939 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
940 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
941 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
942 	return 0;
943 }
944 
945 static int
946 eap_open(void *addr, int flags)
947 {
948 	struct eap_instance *ei;
949 
950 	ei = addr;
951 	/* there is only one ADC */
952 	if (ei->index == EAP_I2 && flags & FREAD)
953 		return EOPNOTSUPP;
954 
955 	return 0;
956 }
957 
958 static int
959 eap_query_encoding(void *addr, struct audio_encoding *fp)
960 {
961 
962 	switch (fp->index) {
963 	case 0:
964 		strcpy(fp->name, AudioEulinear);
965 		fp->encoding = AUDIO_ENCODING_ULINEAR;
966 		fp->precision = 8;
967 		fp->flags = 0;
968 		return 0;
969 	case 1:
970 		strcpy(fp->name, AudioEmulaw);
971 		fp->encoding = AUDIO_ENCODING_ULAW;
972 		fp->precision = 8;
973 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
974 		return 0;
975 	case 2:
976 		strcpy(fp->name, AudioEalaw);
977 		fp->encoding = AUDIO_ENCODING_ALAW;
978 		fp->precision = 8;
979 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
980 		return 0;
981 	case 3:
982 		strcpy(fp->name, AudioEslinear);
983 		fp->encoding = AUDIO_ENCODING_SLINEAR;
984 		fp->precision = 8;
985 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
986 		return 0;
987 	case 4:
988 		strcpy(fp->name, AudioEslinear_le);
989 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
990 		fp->precision = 16;
991 		fp->flags = 0;
992 		return 0;
993 	case 5:
994 		strcpy(fp->name, AudioEulinear_le);
995 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
996 		fp->precision = 16;
997 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
998 		return 0;
999 	case 6:
1000 		strcpy(fp->name, AudioEslinear_be);
1001 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
1002 		fp->precision = 16;
1003 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1004 		return 0;
1005 	case 7:
1006 		strcpy(fp->name, AudioEulinear_be);
1007 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
1008 		fp->precision = 16;
1009 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1010 		return 0;
1011 	default:
1012 		return EINVAL;
1013 	}
1014 }
1015 
1016 static int
1017 eap_set_params(void *addr, int setmode, int usemode,
1018 	       audio_params_t *play, audio_params_t *rec,
1019 	       stream_filter_list_t *pfil, stream_filter_list_t *rfil)
1020 {
1021 	struct eap_instance *ei;
1022 	struct eap_softc *sc;
1023 	struct audio_params *p;
1024 	stream_filter_list_t *fil;
1025 	int mode, i;
1026 	uint32_t div;
1027 
1028 	ei = addr;
1029 	sc = device_private(ei->parent);
1030 	/*
1031 	 * The es1370 only has one clock, so make the sample rates match.
1032 	 * This only applies for ADC/DAC2. The FM DAC is handled below.
1033 	 */
1034 	if (!sc->sc_1371 && ei->index == EAP_DAC2) {
1035 		if (play->sample_rate != rec->sample_rate &&
1036 		    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
1037 			if (setmode == AUMODE_PLAY) {
1038 				rec->sample_rate = play->sample_rate;
1039 				setmode |= AUMODE_RECORD;
1040 			} else if (setmode == AUMODE_RECORD) {
1041 				play->sample_rate = rec->sample_rate;
1042 				setmode |= AUMODE_PLAY;
1043 			} else
1044 				return EINVAL;
1045 		}
1046 	}
1047 
1048 	for (mode = AUMODE_RECORD; mode != -1;
1049 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
1050 		if ((setmode & mode) == 0)
1051 			continue;
1052 
1053 		p = mode == AUMODE_PLAY ? play : rec;
1054 
1055 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
1056 		    (p->precision != 8 && p->precision != 16) ||
1057 		    (p->channels != 1 && p->channels != 2))
1058 			return EINVAL;
1059 
1060 		fil = mode == AUMODE_PLAY ? pfil : rfil;
1061 		i = auconv_set_converter(eap_formats, EAP_NFORMATS,
1062 					 mode, p, FALSE, fil);
1063 		if (i < 0)
1064 			return EINVAL;
1065 	}
1066 
1067 	if (sc->sc_1371) {
1068 		eap1371_set_dac_rate(ei, play->sample_rate);
1069 		eap1371_set_adc_rate(sc, rec->sample_rate);
1070 	} else if (ei->index == EAP_DAC2) {
1071 		/* Set the speed */
1072 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
1073 			     EREAD4(sc, EAP_ICSC)));
1074 		div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
1075 		/*
1076 		 * XXX
1077 		 * The -2 isn't documented, but seemed to make the wall
1078 		 * time match
1079 		 * what I expect.  - mycroft
1080 		 */
1081 		if (usemode == AUMODE_RECORD)
1082 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1083 				rec->sample_rate - 2);
1084 		else
1085 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1086 				play->sample_rate - 2);
1087 #if 0
1088 		div |= EAP_CCB_INTRM;
1089 #else
1090 		/*
1091 		 * It is not obvious how to acknowledge MCCB interrupts, so
1092 		 * we had better not enable them.
1093 		 */
1094 #endif
1095 		EWRITE4(sc, EAP_ICSC, div);
1096 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
1097 	} else {
1098 		/*
1099 		 * The FM DAC has only a few fixed-frequency choises, so
1100 		 * pick out the best candidate.
1101 		 */
1102 		div = EREAD4(sc, EAP_ICSC);
1103 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n", div));
1104 
1105 		div &= ~EAP_WTSRSEL;
1106 		if (play->sample_rate < 8268)
1107 			div |= EAP_WTSRSEL_5;
1108 		else if (play->sample_rate < 16537)
1109 			div |= EAP_WTSRSEL_11;
1110 		else if (play->sample_rate < 33075)
1111 			div |= EAP_WTSRSEL_22;
1112 		else
1113 			div |= EAP_WTSRSEL_44;
1114 
1115 		EWRITE4(sc, EAP_ICSC, div);
1116 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
1117 	}
1118 
1119 	return 0;
1120 }
1121 
1122 static int
1123 eap_round_blocksize(void *addr, int blk, int mode,
1124     const audio_params_t *param)
1125 {
1126 
1127 	return blk & -32;	/* keep good alignment */
1128 }
1129 
1130 static int
1131 eap_trigger_output(
1132 	void *addr,
1133 	void *start,
1134 	void *end,
1135 	int blksize,
1136 	void (*intr)(void *),
1137 	void *arg,
1138 	const audio_params_t *param)
1139 {
1140 	struct eap_instance *ei;
1141 	struct eap_softc *sc;
1142 	struct eap_dma *p;
1143 	uint32_t icsc, sic;
1144 	int sampshift;
1145 
1146 	ei = addr;
1147 	sc = device_private(ei->parent);
1148 #ifdef DIAGNOSTIC
1149 	if (ei->ei_prun)
1150 		panic("eap_trigger_output: already running");
1151 	ei->ei_prun = 1;
1152 #endif
1153 
1154 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
1155 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1156 	ei->ei_pintr = intr;
1157 	ei->ei_parg = arg;
1158 
1159 	sic = EREAD4(sc, EAP_SIC);
1160 	sic &= ~(EAP_S_EB(ei->index) | EAP_S_MB(ei->index) | EAP_INC_BITS);
1161 
1162 	if (ei->index == EAP_DAC2)
1163 		sic |= EAP_SET_P2_ST_INC(0)
1164 		    | EAP_SET_P2_END_INC(param->precision / 8);
1165 
1166 	sampshift = 0;
1167 	if (param->precision == 16) {
1168 		sic |= EAP_S_EB(ei->index);
1169 		sampshift++;
1170 	}
1171 	if (param->channels == 2) {
1172 		sic |= EAP_S_MB(ei->index);
1173 		sampshift++;
1174 	}
1175 	EWRITE4(sc, EAP_SIC, sic & ~EAP_P_INTR_EN(ei->index));
1176 	EWRITE4(sc, EAP_SIC, sic | EAP_P_INTR_EN(ei->index));
1177 
1178 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1179 		continue;
1180 	if (!p) {
1181 		printf("eap_trigger_output: bad addr %p\n", start);
1182 		return EINVAL;
1183 	}
1184 
1185 	if (ei->index == EAP_DAC2) {
1186 		DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
1187 			 (int)DMAADDR(p),
1188 			 (int)EAP_SET_SIZE(0,
1189 			 (((char *)end - (char *)start) >> 2) - 1)));
1190 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
1191 		EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
1192 		EWRITE4(sc, EAP_DAC2_SIZE,
1193 			EAP_SET_SIZE(0,
1194 			((char *)end - (char *)start) >> 2) - 1);
1195 		EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
1196 	} else if (ei->index == EAP_DAC1) {
1197 		DPRINTF(("eap_trigger_output: DAC1_ADDR=0x%x, DAC1_SIZE=0x%x\n",
1198 			 (int)DMAADDR(p),
1199 			 (int)EAP_SET_SIZE(0,
1200 			 (((char *)end - (char *)start) >> 2) - 1)));
1201 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
1202 		EWRITE4(sc, EAP_DAC1_ADDR, DMAADDR(p));
1203 		EWRITE4(sc, EAP_DAC1_SIZE,
1204 			EAP_SET_SIZE(0,
1205 			((char *)end - (char *)start) >> 2) - 1);
1206 		EWRITE4(sc, EAP_DAC1_CSR, (blksize >> sampshift) - 1);
1207 	}
1208 #ifdef DIAGNOSTIC
1209 	else
1210 		panic("eap_trigger_output: impossible instance %d", ei->index);
1211 #endif
1212 
1213 	if (sc->sc_1371)
1214 		EWRITE4(sc, E1371_SRC, 0);
1215 
1216 	icsc = EREAD4(sc, EAP_ICSC);
1217 	icsc |= EAP_DAC_EN(ei->index);
1218 	EWRITE4(sc, EAP_ICSC, icsc);
1219 
1220 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
1221 
1222 	return 0;
1223 }
1224 
1225 static int
1226 eap_trigger_input(
1227 	void *addr,
1228 	void *start,
1229 	void *end,
1230 	int blksize,
1231 	void (*intr)(void *),
1232 	void *arg,
1233 	const audio_params_t *param)
1234 {
1235 	struct eap_instance *ei;
1236 	struct eap_softc *sc;
1237 	struct eap_dma *p;
1238 	uint32_t icsc, sic;
1239 	int sampshift;
1240 
1241 	ei = addr;
1242 	sc = device_private(ei->parent);
1243 #ifdef DIAGNOSTIC
1244 	if (sc->sc_rrun)
1245 		panic("eap_trigger_input: already running");
1246 	sc->sc_rrun = 1;
1247 #endif
1248 
1249 	DPRINTFN(1, ("eap_trigger_input: ei=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1250 	    addr, start, end, blksize, intr, arg));
1251 	sc->sc_rintr = intr;
1252 	sc->sc_rarg = arg;
1253 
1254 	sic = EREAD4(sc, EAP_SIC);
1255 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
1256 	sampshift = 0;
1257 	if (param->precision == 16) {
1258 		sic |= EAP_R1_S_EB;
1259 		sampshift++;
1260 	}
1261 	if (param->channels == 2) {
1262 		sic |= EAP_R1_S_MB;
1263 		sampshift++;
1264 	}
1265 	EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
1266 	EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
1267 
1268 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1269 		continue;
1270 	if (!p) {
1271 		printf("eap_trigger_input: bad addr %p\n", start);
1272 		return (EINVAL);
1273 	}
1274 
1275 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
1276 		 (int)DMAADDR(p),
1277 		 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1278 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
1279 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
1280 	EWRITE4(sc, EAP_ADC_SIZE,
1281 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1282 
1283 	EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
1284 
1285 	if (sc->sc_1371)
1286 		EWRITE4(sc, E1371_SRC, 0);
1287 
1288 	icsc = EREAD4(sc, EAP_ICSC);
1289 	icsc |= EAP_ADC_EN;
1290 	EWRITE4(sc, EAP_ICSC, icsc);
1291 
1292 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
1293 
1294 	return 0;
1295 }
1296 
1297 static int
1298 eap_halt_output(void *addr)
1299 {
1300 	struct eap_instance *ei;
1301 	struct eap_softc *sc;
1302 	uint32_t icsc;
1303 
1304 	DPRINTF(("eap: eap_halt_output\n"));
1305 	ei = addr;
1306 	sc = device_private(ei->parent);
1307 	icsc = EREAD4(sc, EAP_ICSC);
1308 	EWRITE4(sc, EAP_ICSC, icsc & ~(EAP_DAC_EN(ei->index)));
1309 	ei->ei_pintr = 0;
1310 #ifdef DIAGNOSTIC
1311 	ei->ei_prun = 0;
1312 #endif
1313 
1314 	return 0;
1315 }
1316 
1317 static int
1318 eap_halt_input(void *addr)
1319 {
1320 	struct eap_instance *ei;
1321 	struct eap_softc *sc;
1322 	uint32_t icsc;
1323 
1324 #define EAP_USE_FMDAC_ALSO
1325 	DPRINTF(("eap: eap_halt_input\n"));
1326 	ei = addr;
1327 	sc = device_private(ei->parent);
1328 	icsc = EREAD4(sc, EAP_ICSC);
1329 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
1330 	sc->sc_rintr = 0;
1331 #ifdef DIAGNOSTIC
1332 	sc->sc_rrun = 0;
1333 #endif
1334 
1335 	return 0;
1336 }
1337 
1338 static int
1339 eap_getdev(void *addr, struct audio_device *retp)
1340 {
1341 
1342 	*retp = eap_device;
1343 	return 0;
1344 }
1345 
1346 static int
1347 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1348 {
1349 	struct eap_instance *ei;
1350 	struct eap_softc *sc;
1351 
1352 	ei = addr;
1353 	sc = device_private(ei->parent);
1354 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
1355 }
1356 
1357 static int
1358 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1359 {
1360 	struct eap_instance *ei;
1361 	struct eap_softc *sc;
1362 
1363 	ei = addr;
1364 	sc = device_private(ei->parent);
1365 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
1366 }
1367 
1368 static int
1369 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
1370 {
1371 	struct eap_instance *ei;
1372 	struct eap_softc *sc;
1373 
1374 	ei = addr;
1375 	sc = device_private(ei->parent);
1376 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
1377 }
1378 
1379 static void
1380 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
1381 {
1382 	eap1370_write_codec(sc, a, d);
1383 
1384 	sc->sc_port[a] = d;
1385 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
1386 }
1387 
1388 static int
1389 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1390 {
1391 	struct eap_instance *ei;
1392 	struct eap_softc *sc;
1393 	int lval, rval, l, r, la, ra;
1394 	int l1, r1, l2, r2, m, o1, o2;
1395 
1396 	ei = addr;
1397 	sc = device_private(ei->parent);
1398 	if (cp->dev == EAP_RECORD_SOURCE) {
1399 		if (cp->type != AUDIO_MIXER_SET)
1400 			return EINVAL;
1401 		m = sc->sc_record_source = cp->un.mask;
1402 		l1 = l2 = r1 = r2 = 0;
1403 		if (m & (1 << EAP_VOICE_VOL))
1404 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
1405 		if (m & (1 << EAP_FM_VOL))
1406 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
1407 		if (m & (1 << EAP_CD_VOL))
1408 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
1409 		if (m & (1 << EAP_LINE_VOL))
1410 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
1411 		if (m & (1 << EAP_AUX_VOL))
1412 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
1413 		if (m & (1 << EAP_MIC_VOL))
1414 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
1415 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
1416 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
1417 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
1418 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
1419 		return 0;
1420 	}
1421 	if (cp->dev == EAP_INPUT_SOURCE) {
1422 		if (cp->type != AUDIO_MIXER_SET)
1423 			return EINVAL;
1424 		m = sc->sc_input_source = cp->un.mask;
1425 		o1 = o2 = 0;
1426 		if (m & (1 << EAP_VOICE_VOL))
1427 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
1428 		if (m & (1 << EAP_FM_VOL))
1429 			o1 |= AK_M_FM_L | AK_M_FM_R;
1430 		if (m & (1 << EAP_CD_VOL))
1431 			o1 |= AK_M_CD_L | AK_M_CD_R;
1432 		if (m & (1 << EAP_LINE_VOL))
1433 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
1434 		if (m & (1 << EAP_AUX_VOL))
1435 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
1436 		if (m & (1 << EAP_MIC_VOL))
1437 			o1 |= AK_M_MIC;
1438 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
1439 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
1440 		return 0;
1441 	}
1442 	if (cp->dev == EAP_MIC_PREAMP) {
1443 		if (cp->type != AUDIO_MIXER_ENUM)
1444 			return EINVAL;
1445 		if (cp->un.ord != 0 && cp->un.ord != 1)
1446 			return EINVAL;
1447 		sc->sc_mic_preamp = cp->un.ord;
1448 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
1449 		return 0;
1450 	}
1451 	if (cp->type != AUDIO_MIXER_VALUE)
1452 		return EINVAL;
1453 	if (cp->un.value.num_channels == 1)
1454 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1455 	else if (cp->un.value.num_channels == 2) {
1456 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1457 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1458 	} else
1459 		return EINVAL;
1460 	ra = -1;
1461 	switch (cp->dev) {
1462 	case EAP_MASTER_VOL:
1463 		l = VOL_TO_ATT5(lval);
1464 		r = VOL_TO_ATT5(rval);
1465 		la = AK_MASTER_L;
1466 		ra = AK_MASTER_R;
1467 		break;
1468 	case EAP_MIC_VOL:
1469 		if (cp->un.value.num_channels != 1)
1470 			return EINVAL;
1471 		la = AK_MIC;
1472 		goto lr;
1473 	case EAP_VOICE_VOL:
1474 		la = AK_VOICE_L;
1475 		ra = AK_VOICE_R;
1476 		goto lr;
1477 	case EAP_FM_VOL:
1478 		la = AK_FM_L;
1479 		ra = AK_FM_R;
1480 		goto lr;
1481 	case EAP_CD_VOL:
1482 		la = AK_CD_L;
1483 		ra = AK_CD_R;
1484 		goto lr;
1485 	case EAP_LINE_VOL:
1486 		la = AK_LINE_L;
1487 		ra = AK_LINE_R;
1488 		goto lr;
1489 	case EAP_AUX_VOL:
1490 		la = AK_AUX_L;
1491 		ra = AK_AUX_R;
1492 	lr:
1493 		l = VOL_TO_GAIN5(lval);
1494 		r = VOL_TO_GAIN5(rval);
1495 		break;
1496 	default:
1497 		return EINVAL;
1498 	}
1499 	eap1370_set_mixer(sc, la, l);
1500 	if (ra >= 0) {
1501 		eap1370_set_mixer(sc, ra, r);
1502 	}
1503 	return 0;
1504 }
1505 
1506 static int
1507 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1508 {
1509 	struct eap_instance *ei;
1510 	struct eap_softc *sc;
1511 	int la, ra, l, r;
1512 
1513 	ei = addr;
1514 	sc = device_private(ei->parent);
1515 	switch (cp->dev) {
1516 	case EAP_RECORD_SOURCE:
1517 		if (cp->type != AUDIO_MIXER_SET)
1518 			return EINVAL;
1519 		cp->un.mask = sc->sc_record_source;
1520 		return 0;
1521 	case EAP_INPUT_SOURCE:
1522 		if (cp->type != AUDIO_MIXER_SET)
1523 			return EINVAL;
1524 		cp->un.mask = sc->sc_input_source;
1525 		return 0;
1526 	case EAP_MIC_PREAMP:
1527 		if (cp->type != AUDIO_MIXER_ENUM)
1528 			return EINVAL;
1529 		cp->un.ord = sc->sc_mic_preamp;
1530 		return 0;
1531 	case EAP_MASTER_VOL:
1532 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
1533 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
1534 		break;
1535 	case EAP_MIC_VOL:
1536 		if (cp->un.value.num_channels != 1)
1537 			return EINVAL;
1538 		la = ra = AK_MIC;
1539 		goto lr;
1540 	case EAP_VOICE_VOL:
1541 		la = AK_VOICE_L;
1542 		ra = AK_VOICE_R;
1543 		goto lr;
1544 	case EAP_FM_VOL:
1545 		la = AK_FM_L;
1546 		ra = AK_FM_R;
1547 		goto lr;
1548 	case EAP_CD_VOL:
1549 		la = AK_CD_L;
1550 		ra = AK_CD_R;
1551 		goto lr;
1552 	case EAP_LINE_VOL:
1553 		la = AK_LINE_L;
1554 		ra = AK_LINE_R;
1555 		goto lr;
1556 	case EAP_AUX_VOL:
1557 		la = AK_AUX_L;
1558 		ra = AK_AUX_R;
1559 	lr:
1560 		l = GAIN5_TO_VOL(sc->sc_port[la]);
1561 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
1562 		break;
1563 	default:
1564 		return EINVAL;
1565 	}
1566 	if (cp->un.value.num_channels == 1)
1567 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
1568 	else if (cp->un.value.num_channels == 2) {
1569 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
1570 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1571 	} else
1572 		return EINVAL;
1573 	return 0;
1574 }
1575 
1576 static int
1577 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
1578 {
1579 
1580 	switch (dip->index) {
1581 	case EAP_MASTER_VOL:
1582 		dip->type = AUDIO_MIXER_VALUE;
1583 		dip->mixer_class = EAP_OUTPUT_CLASS;
1584 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1585 		strcpy(dip->label.name, AudioNmaster);
1586 		dip->un.v.num_channels = 2;
1587 		dip->un.v.delta = 8;
1588 		strcpy(dip->un.v.units.name, AudioNvolume);
1589 		return 0;
1590 	case EAP_VOICE_VOL:
1591 		dip->type = AUDIO_MIXER_VALUE;
1592 		dip->mixer_class = EAP_INPUT_CLASS;
1593 		dip->prev = AUDIO_MIXER_LAST;
1594 		dip->next = AUDIO_MIXER_LAST;
1595 		strcpy(dip->label.name, AudioNdac);
1596 		dip->un.v.num_channels = 2;
1597 		dip->un.v.delta = 8;
1598 		strcpy(dip->un.v.units.name, AudioNvolume);
1599 		return 0;
1600 	case EAP_FM_VOL:
1601 		dip->type = AUDIO_MIXER_VALUE;
1602 		dip->mixer_class = EAP_INPUT_CLASS;
1603 		dip->prev = AUDIO_MIXER_LAST;
1604 		dip->next = AUDIO_MIXER_LAST;
1605 		strcpy(dip->label.name, AudioNfmsynth);
1606 		dip->un.v.num_channels = 2;
1607 		dip->un.v.delta = 8;
1608 		strcpy(dip->un.v.units.name, AudioNvolume);
1609 		return 0;
1610 	case EAP_CD_VOL:
1611 		dip->type = AUDIO_MIXER_VALUE;
1612 		dip->mixer_class = EAP_INPUT_CLASS;
1613 		dip->prev = AUDIO_MIXER_LAST;
1614 		dip->next = AUDIO_MIXER_LAST;
1615 		strcpy(dip->label.name, AudioNcd);
1616 		dip->un.v.num_channels = 2;
1617 		dip->un.v.delta = 8;
1618 		strcpy(dip->un.v.units.name, AudioNvolume);
1619 		return 0;
1620 	case EAP_LINE_VOL:
1621 		dip->type = AUDIO_MIXER_VALUE;
1622 		dip->mixer_class = EAP_INPUT_CLASS;
1623 		dip->prev = AUDIO_MIXER_LAST;
1624 		dip->next = AUDIO_MIXER_LAST;
1625 		strcpy(dip->label.name, AudioNline);
1626 		dip->un.v.num_channels = 2;
1627 		dip->un.v.delta = 8;
1628 		strcpy(dip->un.v.units.name, AudioNvolume);
1629 		return 0;
1630 	case EAP_AUX_VOL:
1631 		dip->type = AUDIO_MIXER_VALUE;
1632 		dip->mixer_class = EAP_INPUT_CLASS;
1633 		dip->prev = AUDIO_MIXER_LAST;
1634 		dip->next = AUDIO_MIXER_LAST;
1635 		strcpy(dip->label.name, AudioNaux);
1636 		dip->un.v.num_channels = 2;
1637 		dip->un.v.delta = 8;
1638 		strcpy(dip->un.v.units.name, AudioNvolume);
1639 		return 0;
1640 	case EAP_MIC_VOL:
1641 		dip->type = AUDIO_MIXER_VALUE;
1642 		dip->mixer_class = EAP_INPUT_CLASS;
1643 		dip->prev = AUDIO_MIXER_LAST;
1644 		dip->next = EAP_MIC_PREAMP;
1645 		strcpy(dip->label.name, AudioNmicrophone);
1646 		dip->un.v.num_channels = 1;
1647 		dip->un.v.delta = 8;
1648 		strcpy(dip->un.v.units.name, AudioNvolume);
1649 		return 0;
1650 	case EAP_RECORD_SOURCE:
1651 		dip->mixer_class = EAP_RECORD_CLASS;
1652 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1653 		strcpy(dip->label.name, AudioNsource);
1654 		dip->type = AUDIO_MIXER_SET;
1655 		dip->un.s.num_mem = 6;
1656 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1657 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1658 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
1659 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1660 		strcpy(dip->un.s.member[2].label.name, AudioNline);
1661 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1662 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1663 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1664 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
1665 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1666 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
1667 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1668 		return 0;
1669 	case EAP_INPUT_SOURCE:
1670 		dip->mixer_class = EAP_INPUT_CLASS;
1671 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1672 		strcpy(dip->label.name, AudioNsource);
1673 		dip->type = AUDIO_MIXER_SET;
1674 		dip->un.s.num_mem = 6;
1675 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1676 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1677 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
1678 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1679 		strcpy(dip->un.s.member[2].label.name, AudioNline);
1680 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1681 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1682 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1683 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
1684 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1685 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
1686 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1687 		return 0;
1688 	case EAP_MIC_PREAMP:
1689 		dip->type = AUDIO_MIXER_ENUM;
1690 		dip->mixer_class = EAP_INPUT_CLASS;
1691 		dip->prev = EAP_MIC_VOL;
1692 		dip->next = AUDIO_MIXER_LAST;
1693 		strcpy(dip->label.name, AudioNpreamp);
1694 		dip->un.e.num_mem = 2;
1695 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
1696 		dip->un.e.member[0].ord = 0;
1697 		strcpy(dip->un.e.member[1].label.name, AudioNon);
1698 		dip->un.e.member[1].ord = 1;
1699 		return 0;
1700 	case EAP_OUTPUT_CLASS:
1701 		dip->type = AUDIO_MIXER_CLASS;
1702 		dip->mixer_class = EAP_OUTPUT_CLASS;
1703 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1704 		strcpy(dip->label.name, AudioCoutputs);
1705 		return 0;
1706 	case EAP_RECORD_CLASS:
1707 		dip->type = AUDIO_MIXER_CLASS;
1708 		dip->mixer_class = EAP_RECORD_CLASS;
1709 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1710 		strcpy(dip->label.name, AudioCrecord);
1711 		return 0;
1712 	case EAP_INPUT_CLASS:
1713 		dip->type = AUDIO_MIXER_CLASS;
1714 		dip->mixer_class = EAP_INPUT_CLASS;
1715 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1716 		strcpy(dip->label.name, AudioCinputs);
1717 		return 0;
1718 	}
1719 	return ENXIO;
1720 }
1721 
1722 static void *
1723 eap_malloc(void *addr, int direction, size_t size)
1724 {
1725 	struct eap_instance *ei;
1726 	struct eap_softc *sc;
1727 	struct eap_dma *p;
1728 	int error;
1729 
1730 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
1731 	if (!p)
1732 		return NULL;
1733 	ei = addr;
1734 	sc = device_private(ei->parent);
1735 	error = eap_allocmem(sc, size, 16, p);
1736 	if (error) {
1737 		kmem_free(p, sizeof(*p));
1738 		return NULL;
1739 	}
1740 	p->next = sc->sc_dmas;
1741 	sc->sc_dmas = p;
1742 	return KERNADDR(p);
1743 }
1744 
1745 static void
1746 eap_free(void *addr, void *ptr, size_t size)
1747 {
1748 	struct eap_instance *ei;
1749 	struct eap_softc *sc;
1750 	struct eap_dma **pp, *p;
1751 
1752 	ei = addr;
1753 	sc = device_private(ei->parent);
1754 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1755 		if (KERNADDR(p) == ptr) {
1756 			eap_freemem(sc, p);
1757 			*pp = p->next;
1758 			kmem_free(p, sizeof(*p));
1759 			return;
1760 		}
1761 	}
1762 }
1763 
1764 static size_t
1765 eap_round_buffersize(void *addr, int direction, size_t size)
1766 {
1767 
1768 	return size;
1769 }
1770 
1771 static paddr_t
1772 eap_mappage(void *addr, void *mem, off_t off, int prot)
1773 {
1774 	struct eap_instance *ei;
1775 	struct eap_softc *sc;
1776 	struct eap_dma *p;
1777 
1778 	if (off < 0)
1779 		return -1;
1780 	ei = addr;
1781 	sc = device_private(ei->parent);
1782 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1783 		continue;
1784 	if (!p)
1785 		return -1;
1786 
1787 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1788 			       off, prot, BUS_DMA_WAITOK);
1789 }
1790 
1791 static int
1792 eap_get_props(void *addr)
1793 {
1794 
1795 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1796 	    AUDIO_PROP_FULLDUPLEX;
1797 }
1798 
1799 static void
1800 eap_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
1801 {
1802 	struct eap_instance *ei;
1803 	struct eap_softc *sc;
1804 
1805 	ei = addr;
1806 	sc = device_private(ei->parent);
1807 	*intr = &sc->sc_intr_lock;
1808 	*thread = &sc->sc_lock;
1809 }
1810 
1811 #if NMIDI > 0
1812 static int
1813 eap_midi_open(void *addr, int flags,
1814 	      void (*iintr)(void *, int),
1815 	      void (*ointr)(void *),
1816 	      void *arg)
1817 {
1818 	struct eap_softc *sc;
1819 	uint8_t uctrl;
1820 
1821 	sc = addr;
1822 	sc->sc_arg = arg;
1823 
1824 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
1825 	uctrl = 0;
1826 	if (flags & FREAD) {
1827 		uctrl |= EAP_UC_RXINTEN;
1828 		sc->sc_iintr = iintr;
1829 	}
1830 	if (flags & FWRITE)
1831 		sc->sc_ointr = ointr;
1832 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
1833 
1834 	return 0;
1835 }
1836 
1837 static void
1838 eap_midi_close(void *addr)
1839 {
1840 	struct eap_softc *sc;
1841 
1842 	sc = addr;
1843 	/* give uart a chance to drain */
1844 	(void)kpause("eapclm", false, hz/10, &sc->sc_intr_lock);
1845 	EWRITE1(sc, EAP_UART_CONTROL, 0);
1846 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
1847 
1848 	sc->sc_iintr = 0;
1849 	sc->sc_ointr = 0;
1850 }
1851 
1852 static int
1853 eap_midi_output(void *addr, int d)
1854 {
1855 	struct eap_softc *sc;
1856 	uint8_t uctrl;
1857 
1858 	sc = addr;
1859 	EWRITE1(sc, EAP_UART_DATA, d);
1860 
1861 	uctrl = EAP_UC_TXINTEN;
1862 	if (sc->sc_iintr)
1863 		uctrl |= EAP_UC_RXINTEN;
1864 	/*
1865 	 * This is a write-only register, so we have to remember the right
1866 	 * value of RXINTEN as well as setting TXINTEN. But if we are open
1867 	 * for reading, it will always be correct to set RXINTEN here; only
1868 	 * during service of a receive interrupt could it be momentarily
1869 	 * toggled off, and whether we got here from the top half or from
1870 	 * an interrupt, that won't be the current state.
1871 	 */
1872 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
1873 	return 0;
1874 }
1875 
1876 static void
1877 eap_midi_getinfo(void *addr, struct midi_info *mi)
1878 {
1879 	mi->name = "AudioPCI MIDI UART";
1880 	mi->props = MIDI_PROP_CAN_INPUT | MIDI_PROP_OUT_INTR;
1881 }
1882 
1883 static void
1884 eap_uart_txrdy(struct eap_softc *sc)
1885 {
1886 	uint8_t uctrl;
1887 	uctrl = 0;
1888 	if (sc->sc_iintr)
1889 		uctrl = EAP_UC_RXINTEN;
1890 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
1891 	sc->sc_ointr(sc->sc_arg);
1892 }
1893 
1894 #endif
1895