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