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