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