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