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