eap.c revision 1.44.2.9 1 /* $NetBSD: eap.c,v 1.44.2.9 2002/12/19 00:48:09 thorpej 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 (at) netbsd.org>, Charles M. Hannum, and
10 * Antti Kantee <pooka (at) 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 (at) araneus.fi>
43 * Testing: Chuck Cranor <chuck (at) maria.wustl.edu>
44 * Phil Nelson <phil (at) cs.wwu.edu>
45 *
46 * ES1371/AC97: Ezra Story <ezy (at) 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.44.2.9 2002/12/19 00:48:09 thorpej 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 /*
588 * The vendor and product ID's are quite "interesting". Just
589 * trust the following and be happy.
590 */
591 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
592 revision = PCI_REVISION(pa->pa_class);
593 ct5880 = 0;
594 if (sc->sc_1371) {
595 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
596 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_CT5880) {
597 ct5880 = 1;
598 switch (revision) {
599 case EAP_CT5880_C: revstr = "CT5880-C "; break;
600 case EAP_CT5880_D: revstr = "CT5880-D "; break;
601 case EAP_CT5880_E: revstr = "CT5880-E "; break;
602 }
603 } else {
604 switch (revision) {
605 case EAP_EV1938_A: revstr = "EV1938-A "; break;
606 case EAP_ES1373_A: revstr = "ES1373-A "; break;
607 case EAP_ES1373_B: revstr = "ES1373-B "; break;
608 case EAP_CT5880_A: revstr = "CT5880-A "; ct5880=1;break;
609 case EAP_ES1373_8: revstr = "ES1373-8" ; ct5880=1;break;
610 case EAP_ES1371_B: revstr = "ES1371-B "; break;
611 }
612 }
613 }
614 printf(": %s %s(rev. 0x%02x)\n", devinfo, revstr, revision);
615
616 /* Map I/O register */
617 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
618 &sc->iot, &sc->ioh, NULL, NULL)) {
619 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
620 return;
621 }
622
623 sc->sc_dmatag = pa->pa_dmat;
624
625 /* Enable the device. */
626 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
627 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
628 csr | PCI_COMMAND_MASTER_ENABLE);
629
630 /* Map and establish the interrupt. */
631 if (pci_intr_map(pa, &ih)) {
632 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
633 return;
634 }
635 intrstr = pci_intr_string(pc, ih);
636 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
637 if (sc->sc_ih == NULL) {
638 printf("%s: couldn't establish interrupt",
639 sc->sc_dev.dv_xname);
640 if (intrstr != NULL)
641 printf(" at %s", intrstr);
642 printf("\n");
643 return;
644 }
645 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
646
647 sc->sc_ei[EAP_I1].parent = (struct device *)sc;
648 sc->sc_ei[EAP_I1].index = EAP_DAC2;
649 sc->sc_ei[EAP_I2].parent = (struct device *)sc;
650 sc->sc_ei[EAP_I2].index = EAP_DAC1;
651
652 if (!sc->sc_1371) {
653 /* Enable interrupts and looping mode. */
654 /* enable the parts we need */
655 EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
656 EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
657
658 /* reset codec */
659 /* normal operation */
660 /* select codec clocks */
661 eap1370_write_codec(sc, AK_RESET, AK_PD);
662 eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
663 eap1370_write_codec(sc, AK_CS, 0x0);
664
665 eap_hw_if = &eap1370_hw_if;
666
667 /* Enable all relevant mixer switches. */
668 ctl.dev = EAP_OUTPUT_SELECT;
669 ctl.type = AUDIO_MIXER_SET;
670 ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
671 1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
672 1 << EAP_MIC_VOL;
673 eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
674
675 ctl.type = AUDIO_MIXER_VALUE;
676 ctl.un.value.num_channels = 1;
677 for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
678 ctl.dev++) {
679 ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
680 eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
681 }
682 ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
683 eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
684 ctl.dev = EAP_MIC_PREAMP;
685 ctl.type = AUDIO_MIXER_ENUM;
686 ctl.un.ord = 0;
687 eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
688 ctl.dev = EAP_RECORD_SOURCE;
689 ctl.type = AUDIO_MIXER_SET;
690 ctl.un.mask = 1 << EAP_MIC_VOL;
691 eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
692 } else {
693 /* clean slate */
694
695 EWRITE4(sc, EAP_SIC, 0);
696 EWRITE4(sc, EAP_ICSC, 0);
697 EWRITE4(sc, E1371_LEGACY, 0);
698
699 if (ct5880) {
700 EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
701 /* Let codec wake up */
702 delay(20000);
703 }
704
705 /* Reset from es1371's perspective */
706 EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
707 delay(20);
708 EWRITE4(sc, EAP_ICSC, 0);
709
710 /*
711 * Must properly reprogram sample rate converter,
712 * or it locks up. Set some defaults for the life of the
713 * machine, and set up a sb default sample rate.
714 */
715 EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
716 for (i = 0; i < 0x80; i++)
717 eap1371_src_write(sc, i, 0);
718 eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
719 eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
720 eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
721 eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
722 eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
723 eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
724 eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
725 eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
726 eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
727 eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
728 eap1371_set_adc_rate(sc, 22050);
729 eap1371_set_dac_rate(&sc->sc_ei[0], 22050);
730 eap1371_set_dac_rate(&sc->sc_ei[1], 22050);
731
732 EWRITE4(sc, E1371_SRC, 0);
733
734 /* Reset codec */
735
736 /* Interrupt enable */
737 sc->host_if.arg = sc;
738 sc->host_if.attach = eap1371_attach_codec;
739 sc->host_if.read = eap1371_read_codec;
740 sc->host_if.write = eap1371_write_codec;
741 sc->host_if.reset = eap1371_reset_codec;
742
743 if (ac97_attach(&sc->host_if) == 0) {
744 /* Interrupt enable */
745 EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
746 } else
747 return;
748
749 eap_hw_if = &eap1371_hw_if;
750
751 /* Just enable the DAC and master volumes by default */
752 ctl.type = AUDIO_MIXER_ENUM;
753 ctl.un.ord = 0; /* off */
754 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCoutputs,
755 AudioNmaster, AudioNmute);
756 eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
757 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCinputs,
758 AudioNdac, AudioNmute);
759 eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
760 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
761 AudioNvolume, AudioNmute);
762 eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
763
764 ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
765 AudioNsource, NULL);
766 ctl.type = AUDIO_MIXER_ENUM;
767 ctl.un.ord = 0;
768 eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
769
770 }
771
772 audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I1], &sc->sc_dev);
773
774 #ifdef EAP_USE_BOTH_DACS
775 printf("%s: attaching secondary DAC\n", sc->sc_dev.dv_xname);
776 audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I2], &sc->sc_dev);
777 #endif
778
779 #if NMIDI > 0
780 midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
781 #endif
782 }
783
784 int
785 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
786 {
787 struct eap_softc *sc = sc_;
788
789 sc->codec_if = codec_if;
790 return (0);
791 }
792
793 void
794 eap1371_reset_codec(void *sc_)
795 {
796 struct eap_softc *sc = sc_;
797 u_int32_t icsc;
798 int s;
799
800 s = splaudio();
801 icsc = EREAD4(sc, EAP_ICSC);
802 EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
803 delay(20);
804 EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
805 delay(1);
806 splx(s);
807
808 return;
809 }
810
811 int
812 eap_intr(void *p)
813 {
814 struct eap_softc *sc = p;
815 u_int32_t intr, sic;
816
817 intr = EREAD4(sc, EAP_ICSS);
818 if (!(intr & EAP_INTR))
819 return (0);
820 sic = EREAD4(sc, EAP_SIC);
821 DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
822 if (intr & EAP_I_ADC) {
823 #if 0
824 /*
825 * XXX This is a hack!
826 * The EAP chip sometimes generates the recording interrupt
827 * while it is still transferring the data. To make sure
828 * it has all arrived we busy wait until the count is right.
829 * The transfer we are waiting for is 8 longwords.
830 */
831 int s, nw, n;
832 EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
833 s = EREAD4(sc, EAP_ADC_CSR);
834 nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
835 n = 0;
836 while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
837 delay(10);
838 if (++n > 100) {
839 printf("eapintr: dma fix timeout");
840 break;
841 }
842 }
843 /* Continue with normal interrupt handling. */
844 #endif
845 EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
846 EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
847 if (sc->sc_rintr)
848 sc->sc_rintr(sc->sc_rarg);
849 }
850
851 if (intr & EAP_I_DAC2) {
852 EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
853 EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
854 if (sc->sc_ei[EAP_DAC2].ei_pintr)
855 sc->sc_ei[EAP_DAC2].ei_pintr(sc->sc_ei[EAP_DAC2].ei_parg);
856 }
857
858 if (intr & EAP_I_DAC1) {
859 EWRITE4(sc, EAP_SIC, sic & ~EAP_P1_INTR_EN);
860 EWRITE4(sc, EAP_SIC, sic | EAP_P1_INTR_EN);
861 if (sc->sc_ei[EAP_DAC1].ei_pintr)
862 sc->sc_ei[EAP_DAC1].ei_pintr(sc->sc_ei[EAP_DAC1].ei_parg);
863 }
864
865 if (intr & EAP_I_MCCB)
866 panic("eap_intr: unexpected MCCB interrupt");
867 #if NMIDI > 0
868 if ((intr & EAP_I_UART) && sc->sc_iintr != NULL) {
869 u_int32_t data;
870
871 if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXINT) {
872 while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
873 data = EREAD1(sc, EAP_UART_DATA);
874 sc->sc_iintr(sc->sc_arg, data);
875 }
876 }
877 }
878 #endif
879 return (1);
880 }
881
882 int
883 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
884 {
885 int error;
886
887 p->size = size;
888 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
889 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
890 &p->nsegs, BUS_DMA_NOWAIT);
891 if (error)
892 return (error);
893
894 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
895 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
896 if (error)
897 goto free;
898
899 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
900 0, BUS_DMA_NOWAIT, &p->map);
901 if (error)
902 goto unmap;
903
904 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
905 BUS_DMA_NOWAIT);
906 if (error)
907 goto destroy;
908 return (0);
909
910 destroy:
911 bus_dmamap_destroy(sc->sc_dmatag, p->map);
912 unmap:
913 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
914 free:
915 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
916 return (error);
917 }
918
919 int
920 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
921 {
922 bus_dmamap_unload(sc->sc_dmatag, p->map);
923 bus_dmamap_destroy(sc->sc_dmatag, p->map);
924 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
925 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
926 return (0);
927 }
928
929 int
930 eap_open(void *addr, int flags)
931 {
932 struct eap_instance *ei = addr;
933
934 /* there is only one ADC */
935 if (ei->index == EAP_I2 && flags & AUOPEN_READ)
936 return (EOPNOTSUPP);
937
938 return (0);
939 }
940
941 /*
942 * Close function is called at splaudio().
943 */
944 void
945 eap_close(void *addr)
946 {
947 struct eap_instance *ei = addr;
948 struct eap_softc *sc = (struct eap_softc *)ei->parent;
949
950 eap_halt_output(ei);
951 if (ei->index == EAP_I1) {
952 eap_halt_input(ei);
953 sc->sc_rintr = 0;
954 }
955
956 ei->ei_pintr = 0;
957 }
958
959 int
960 eap_query_encoding(void *addr, struct audio_encoding *fp)
961 {
962 switch (fp->index) {
963 case 0:
964 strcpy(fp->name, AudioEulinear);
965 fp->encoding = AUDIO_ENCODING_ULINEAR;
966 fp->precision = 8;
967 fp->flags = 0;
968 return (0);
969 case 1:
970 strcpy(fp->name, AudioEmulaw);
971 fp->encoding = AUDIO_ENCODING_ULAW;
972 fp->precision = 8;
973 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
974 return (0);
975 case 2:
976 strcpy(fp->name, AudioEalaw);
977 fp->encoding = AUDIO_ENCODING_ALAW;
978 fp->precision = 8;
979 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
980 return (0);
981 case 3:
982 strcpy(fp->name, AudioEslinear);
983 fp->encoding = AUDIO_ENCODING_SLINEAR;
984 fp->precision = 8;
985 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
986 return (0);
987 case 4:
988 strcpy(fp->name, AudioEslinear_le);
989 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
990 fp->precision = 16;
991 fp->flags = 0;
992 return (0);
993 case 5:
994 strcpy(fp->name, AudioEulinear_le);
995 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
996 fp->precision = 16;
997 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
998 return (0);
999 case 6:
1000 strcpy(fp->name, AudioEslinear_be);
1001 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
1002 fp->precision = 16;
1003 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1004 return (0);
1005 case 7:
1006 strcpy(fp->name, AudioEulinear_be);
1007 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
1008 fp->precision = 16;
1009 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1010 return (0);
1011 default:
1012 return (EINVAL);
1013 }
1014 }
1015
1016 int
1017 eap_set_params(void *addr, int setmode, int usemode,
1018 struct audio_params *play, struct audio_params *rec)
1019 {
1020 struct eap_instance *ei = addr;
1021 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1022 struct audio_params *p;
1023 int mode;
1024 u_int32_t div;
1025
1026 /*
1027 * The es1370 only has one clock, so make the sample rates match.
1028 */
1029 if (!sc->sc_1371) {
1030 if (play->sample_rate != rec->sample_rate &&
1031 usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
1032 if (setmode == AUMODE_PLAY) {
1033 rec->sample_rate = play->sample_rate;
1034 setmode |= AUMODE_RECORD;
1035 } else if (setmode == AUMODE_RECORD) {
1036 play->sample_rate = rec->sample_rate;
1037 setmode |= AUMODE_PLAY;
1038 } else
1039 return (EINVAL);
1040 }
1041 }
1042
1043 for (mode = AUMODE_RECORD; mode != -1;
1044 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
1045 if ((setmode & mode) == 0)
1046 continue;
1047
1048 p = mode == AUMODE_PLAY ? play : rec;
1049
1050 if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
1051 (p->precision != 8 && p->precision != 16) ||
1052 (p->channels != 1 && p->channels != 2))
1053 return (EINVAL);
1054
1055 p->factor = 1;
1056 p->sw_code = 0;
1057 switch (p->encoding) {
1058 case AUDIO_ENCODING_SLINEAR_BE:
1059 if (p->precision == 16)
1060 p->sw_code = swap_bytes;
1061 else
1062 p->sw_code = change_sign8;
1063 break;
1064 case AUDIO_ENCODING_SLINEAR_LE:
1065 if (p->precision != 16)
1066 p->sw_code = change_sign8;
1067 break;
1068 case AUDIO_ENCODING_ULINEAR_BE:
1069 if (p->precision == 16) {
1070 if (mode == AUMODE_PLAY)
1071 p->sw_code = swap_bytes_change_sign16_le;
1072 else
1073 p->sw_code = change_sign16_swap_bytes_le;
1074 }
1075 break;
1076 case AUDIO_ENCODING_ULINEAR_LE:
1077 if (p->precision == 16)
1078 p->sw_code = change_sign16_le;
1079 break;
1080 case AUDIO_ENCODING_ULAW:
1081 if (mode == AUMODE_PLAY) {
1082 p->factor = 2;
1083 p->sw_code = mulaw_to_slinear16_le;
1084 } else
1085 p->sw_code = ulinear8_to_mulaw;
1086 break;
1087 case AUDIO_ENCODING_ALAW:
1088 if (mode == AUMODE_PLAY) {
1089 p->factor = 2;
1090 p->sw_code = alaw_to_slinear16_le;
1091 } else
1092 p->sw_code = ulinear8_to_alaw;
1093 break;
1094 default:
1095 return (EINVAL);
1096 }
1097 }
1098
1099 if (sc->sc_1371) {
1100 eap1371_set_dac_rate(ei, play->sample_rate);
1101 eap1371_set_adc_rate(sc, rec->sample_rate);
1102 } else {
1103 /* Set the speed */
1104 DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
1105 EREAD4(sc, EAP_ICSC)));
1106 div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
1107 /*
1108 * XXX
1109 * The -2 isn't documented, but seemed to make the wall
1110 * time match
1111 * what I expect. - mycroft
1112 */
1113 if (usemode == AUMODE_RECORD)
1114 div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1115 rec->sample_rate - 2);
1116 else
1117 div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
1118 play->sample_rate - 2);
1119 #if 0
1120 div |= EAP_CCB_INTRM;
1121 #else
1122 /*
1123 * It is not obvious how to acknowledge MCCB interrupts, so
1124 * we had better not enable them.
1125 */
1126 #endif
1127 EWRITE4(sc, EAP_ICSC, div);
1128 DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
1129 }
1130
1131 return (0);
1132 }
1133
1134 int
1135 eap_round_blocksize(void *addr, int blk)
1136 {
1137 return (blk & -32); /* keep good alignment */
1138 }
1139
1140 int
1141 eap_trigger_output(
1142 void *addr,
1143 void *start,
1144 void *end,
1145 int blksize,
1146 void (*intr)(void *),
1147 void *arg,
1148 struct audio_params *param)
1149 {
1150 struct eap_instance *ei = addr;
1151 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1152 struct eap_dma *p;
1153 u_int32_t icsc, sic;
1154 int sampshift;
1155
1156 #ifdef DIAGNOSTIC
1157 if (ei->ei_prun)
1158 panic("eap_trigger_output: already running");
1159 ei->ei_prun = 1;
1160 #endif
1161
1162 DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
1163 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1164 ei->ei_pintr = intr;
1165 ei->ei_parg = arg;
1166
1167 sic = EREAD4(sc, EAP_SIC);
1168 sic &= ~(EAP_S_EB(ei->index) | EAP_S_MB(ei->index) | EAP_INC_BITS);
1169
1170 if (ei->index == EAP_DAC2)
1171 sic |= EAP_SET_P2_ST_INC(0)
1172 | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
1173
1174 sampshift = 0;
1175 if (param->precision * param->factor == 16) {
1176 sic |= EAP_S_EB(ei->index);
1177 sampshift++;
1178 }
1179 if (param->channels == 2) {
1180 sic |= EAP_S_MB(ei->index);
1181 sampshift++;
1182 }
1183 EWRITE4(sc, EAP_SIC, sic & ~EAP_P_INTR_EN(ei->index));
1184 EWRITE4(sc, EAP_SIC, sic | EAP_P_INTR_EN(ei->index));
1185
1186 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1187 ;
1188 if (!p) {
1189 printf("eap_trigger_output: bad addr %p\n", start);
1190 return (EINVAL);
1191 }
1192
1193 if (ei->index == EAP_DAC2) {
1194 DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
1195 (int)DMAADDR(p),
1196 (int)EAP_SET_SIZE(0,
1197 (((char *)end - (char *)start) >> 2) - 1)));
1198 EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
1199 EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
1200 EWRITE4(sc, EAP_DAC2_SIZE,
1201 EAP_SET_SIZE(0,
1202 ((char *)end - (char *)start) >> 2) - 1);
1203 EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
1204 } else if (ei->index == EAP_DAC1) {
1205 DPRINTF(("eap_trigger_output: DAC1_ADDR=0x%x, DAC1_SIZE=0x%x\n",
1206 (int)DMAADDR(p),
1207 (int)EAP_SET_SIZE(0,
1208 (((char *)end - (char *)start) >> 2) - 1)));
1209 EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
1210 EWRITE4(sc, EAP_DAC1_ADDR, DMAADDR(p));
1211 EWRITE4(sc, EAP_DAC1_SIZE,
1212 EAP_SET_SIZE(0,
1213 ((char *)end - (char *)start) >> 2) - 1);
1214 EWRITE4(sc, EAP_DAC1_CSR, (blksize >> sampshift) - 1);
1215 }
1216 #ifdef DIAGNOSTIC
1217 else
1218 panic("eap_trigger_output: impossible instance %d", ei->index);
1219 #endif
1220
1221 if (sc->sc_1371)
1222 EWRITE4(sc, E1371_SRC, 0);
1223
1224 icsc = EREAD4(sc, EAP_ICSC);
1225 icsc |= EAP_DAC_EN(ei->index);
1226 EWRITE4(sc, EAP_ICSC, icsc);
1227
1228 DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
1229
1230 return (0);
1231 }
1232
1233 int
1234 eap_trigger_input(
1235 void *addr,
1236 void *start,
1237 void *end,
1238 int blksize,
1239 void (*intr)(void *),
1240 void *arg,
1241 struct audio_params *param)
1242 {
1243 struct eap_instance *ei = addr;
1244 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1245 struct eap_dma *p;
1246 u_int32_t icsc, sic;
1247 int sampshift;
1248
1249 #ifdef DIAGNOSTIC
1250 if (sc->sc_rrun)
1251 panic("eap_trigger_input: already running");
1252 sc->sc_rrun = 1;
1253 #endif
1254
1255 DPRINTFN(1, ("eap_trigger_input: ei=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1256 addr, start, end, blksize, intr, arg));
1257 sc->sc_rintr = intr;
1258 sc->sc_rarg = arg;
1259
1260 sic = EREAD4(sc, EAP_SIC);
1261 sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
1262 sampshift = 0;
1263 if (param->precision * param->factor == 16) {
1264 sic |= EAP_R1_S_EB;
1265 sampshift++;
1266 }
1267 if (param->channels == 2) {
1268 sic |= EAP_R1_S_MB;
1269 sampshift++;
1270 }
1271 EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
1272 EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
1273
1274 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1275 ;
1276 if (!p) {
1277 printf("eap_trigger_input: bad addr %p\n", start);
1278 return (EINVAL);
1279 }
1280
1281 DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
1282 (int)DMAADDR(p),
1283 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
1284 EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
1285 EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
1286 EWRITE4(sc, EAP_ADC_SIZE,
1287 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
1288
1289 EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
1290
1291 if (sc->sc_1371)
1292 EWRITE4(sc, E1371_SRC, 0);
1293
1294 icsc = EREAD4(sc, EAP_ICSC);
1295 icsc |= EAP_ADC_EN;
1296 EWRITE4(sc, EAP_ICSC, icsc);
1297
1298 DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
1299
1300 return (0);
1301 }
1302
1303 int
1304 eap_halt_output(void *addr)
1305 {
1306 struct eap_instance *ei = addr;
1307 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1308 u_int32_t icsc;
1309
1310 DPRINTF(("eap: eap_halt_output\n"));
1311 icsc = EREAD4(sc, EAP_ICSC);
1312 EWRITE4(sc, EAP_ICSC, icsc & ~(EAP_DAC_EN(ei->index)));
1313 #ifdef DIAGNOSTIC
1314 ei->ei_prun = 0;
1315 #endif
1316
1317 return (0);
1318 }
1319
1320 int
1321 eap_halt_input(void *addr)
1322 {
1323 struct eap_instance *ei = addr;
1324 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1325 u_int32_t icsc;
1326
1327 #define EAP_USE_FMDAC_ALSO
1328 DPRINTF(("eap: eap_halt_input\n"));
1329 icsc = EREAD4(sc, EAP_ICSC);
1330 EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
1331 #ifdef DIAGNOSTIC
1332 sc->sc_rrun = 0;
1333 #endif
1334 return (0);
1335 }
1336
1337 int
1338 eap_getdev(void *addr, struct audio_device *retp)
1339 {
1340 *retp = eap_device;
1341 return (0);
1342 }
1343
1344 int
1345 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1346 {
1347 struct eap_instance *ei = addr;
1348 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1349
1350 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
1351 }
1352
1353 int
1354 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1355 {
1356 struct eap_instance *ei = addr;
1357 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1358
1359 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
1360 }
1361
1362 int
1363 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
1364 {
1365 struct eap_instance *ei = addr;
1366 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1367
1368 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
1369 }
1370
1371 int
1372 eap1371_get_portnum_by_name(struct eap_softc *sc,
1373 char *class, char *device, char *qualifier)
1374 {
1375 return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
1376 device, qualifier));
1377 }
1378
1379 void
1380 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
1381 {
1382 eap1370_write_codec(sc, a, d);
1383
1384 sc->sc_port[a] = d;
1385 DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
1386 }
1387
1388 int
1389 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1390 {
1391 struct eap_instance *ei = addr;
1392 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1393 int lval, rval, l, r, la, ra;
1394 int l1, r1, l2, r2, m, o1, o2;
1395
1396 if (cp->dev == EAP_RECORD_SOURCE) {
1397 if (cp->type != AUDIO_MIXER_SET)
1398 return (EINVAL);
1399 m = sc->sc_record_source = cp->un.mask;
1400 l1 = l2 = r1 = r2 = 0;
1401 if (m & (1 << EAP_VOICE_VOL))
1402 l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
1403 if (m & (1 << EAP_FM_VOL))
1404 l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
1405 if (m & (1 << EAP_CD_VOL))
1406 l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
1407 if (m & (1 << EAP_LINE_VOL))
1408 l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
1409 if (m & (1 << EAP_AUX_VOL))
1410 l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
1411 if (m & (1 << EAP_MIC_VOL))
1412 l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
1413 eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
1414 eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
1415 eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
1416 eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
1417 return (0);
1418 }
1419 if (cp->dev == EAP_OUTPUT_SELECT) {
1420 if (cp->type != AUDIO_MIXER_SET)
1421 return (EINVAL);
1422 m = sc->sc_output_source = cp->un.mask;
1423 o1 = o2 = 0;
1424 if (m & (1 << EAP_VOICE_VOL))
1425 o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
1426 if (m & (1 << EAP_FM_VOL))
1427 o1 |= AK_M_FM_L | AK_M_FM_R;
1428 if (m & (1 << EAP_CD_VOL))
1429 o1 |= AK_M_CD_L | AK_M_CD_R;
1430 if (m & (1 << EAP_LINE_VOL))
1431 o1 |= AK_M_LINE_L | AK_M_LINE_R;
1432 if (m & (1 << EAP_AUX_VOL))
1433 o2 |= AK_M_AUX_L | AK_M_AUX_R;
1434 if (m & (1 << EAP_MIC_VOL))
1435 o1 |= AK_M_MIC;
1436 eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
1437 eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
1438 return (0);
1439 }
1440 if (cp->dev == EAP_MIC_PREAMP) {
1441 if (cp->type != AUDIO_MIXER_ENUM)
1442 return (EINVAL);
1443 if (cp->un.ord != 0 && cp->un.ord != 1)
1444 return (EINVAL);
1445 sc->sc_mic_preamp = cp->un.ord;
1446 eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
1447 return (0);
1448 }
1449 if (cp->type != AUDIO_MIXER_VALUE)
1450 return (EINVAL);
1451 if (cp->un.value.num_channels == 1)
1452 lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1453 else if (cp->un.value.num_channels == 2) {
1454 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1455 rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1456 } else
1457 return (EINVAL);
1458 ra = -1;
1459 switch (cp->dev) {
1460 case EAP_MASTER_VOL:
1461 l = VOL_TO_ATT5(lval);
1462 r = VOL_TO_ATT5(rval);
1463 la = AK_MASTER_L;
1464 ra = AK_MASTER_R;
1465 break;
1466 case EAP_MIC_VOL:
1467 if (cp->un.value.num_channels != 1)
1468 return (EINVAL);
1469 la = AK_MIC;
1470 goto lr;
1471 case EAP_VOICE_VOL:
1472 la = AK_VOICE_L;
1473 ra = AK_VOICE_R;
1474 goto lr;
1475 case EAP_FM_VOL:
1476 la = AK_FM_L;
1477 ra = AK_FM_R;
1478 goto lr;
1479 case EAP_CD_VOL:
1480 la = AK_CD_L;
1481 ra = AK_CD_R;
1482 goto lr;
1483 case EAP_LINE_VOL:
1484 la = AK_LINE_L;
1485 ra = AK_LINE_R;
1486 goto lr;
1487 case EAP_AUX_VOL:
1488 la = AK_AUX_L;
1489 ra = AK_AUX_R;
1490 lr:
1491 l = VOL_TO_GAIN5(lval);
1492 r = VOL_TO_GAIN5(rval);
1493 break;
1494 default:
1495 return (EINVAL);
1496 }
1497 eap1370_set_mixer(sc, la, l);
1498 if (ra >= 0) {
1499 eap1370_set_mixer(sc, ra, r);
1500 }
1501 return (0);
1502 }
1503
1504 int
1505 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1506 {
1507 struct eap_instance *ei = addr;
1508 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1509 int la, ra, l, r;
1510
1511 switch (cp->dev) {
1512 case EAP_RECORD_SOURCE:
1513 if (cp->type != AUDIO_MIXER_SET)
1514 return (EINVAL);
1515 cp->un.mask = sc->sc_record_source;
1516 return (0);
1517 case EAP_OUTPUT_SELECT:
1518 if (cp->type != AUDIO_MIXER_SET)
1519 return (EINVAL);
1520 cp->un.mask = sc->sc_output_source;
1521 return (0);
1522 case EAP_MIC_PREAMP:
1523 if (cp->type != AUDIO_MIXER_ENUM)
1524 return (EINVAL);
1525 cp->un.ord = sc->sc_mic_preamp;
1526 return (0);
1527 case EAP_MASTER_VOL:
1528 l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
1529 r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
1530 break;
1531 case EAP_MIC_VOL:
1532 if (cp->un.value.num_channels != 1)
1533 return (EINVAL);
1534 la = ra = AK_MIC;
1535 goto lr;
1536 case EAP_VOICE_VOL:
1537 la = AK_VOICE_L;
1538 ra = AK_VOICE_R;
1539 goto lr;
1540 case EAP_FM_VOL:
1541 la = AK_FM_L;
1542 ra = AK_FM_R;
1543 goto lr;
1544 case EAP_CD_VOL:
1545 la = AK_CD_L;
1546 ra = AK_CD_R;
1547 goto lr;
1548 case EAP_LINE_VOL:
1549 la = AK_LINE_L;
1550 ra = AK_LINE_R;
1551 goto lr;
1552 case EAP_AUX_VOL:
1553 la = AK_AUX_L;
1554 ra = AK_AUX_R;
1555 lr:
1556 l = GAIN5_TO_VOL(sc->sc_port[la]);
1557 r = GAIN5_TO_VOL(sc->sc_port[ra]);
1558 break;
1559 default:
1560 return (EINVAL);
1561 }
1562 if (cp->un.value.num_channels == 1)
1563 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
1564 else if (cp->un.value.num_channels == 2) {
1565 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
1566 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1567 } else
1568 return (EINVAL);
1569 return (0);
1570 }
1571
1572 int
1573 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
1574 {
1575
1576 switch (dip->index) {
1577 case EAP_MASTER_VOL:
1578 dip->type = AUDIO_MIXER_VALUE;
1579 dip->mixer_class = EAP_OUTPUT_CLASS;
1580 dip->prev = dip->next = AUDIO_MIXER_LAST;
1581 strcpy(dip->label.name, AudioNmaster);
1582 dip->un.v.num_channels = 2;
1583 strcpy(dip->un.v.units.name, AudioNvolume);
1584 return (0);
1585 case EAP_VOICE_VOL:
1586 dip->type = AUDIO_MIXER_VALUE;
1587 dip->mixer_class = EAP_INPUT_CLASS;
1588 dip->prev = AUDIO_MIXER_LAST;
1589 dip->next = AUDIO_MIXER_LAST;
1590 strcpy(dip->label.name, AudioNdac);
1591 dip->un.v.num_channels = 2;
1592 strcpy(dip->un.v.units.name, AudioNvolume);
1593 return (0);
1594 case EAP_FM_VOL:
1595 dip->type = AUDIO_MIXER_VALUE;
1596 dip->mixer_class = EAP_INPUT_CLASS;
1597 dip->prev = AUDIO_MIXER_LAST;
1598 dip->next = AUDIO_MIXER_LAST;
1599 strcpy(dip->label.name, AudioNfmsynth);
1600 dip->un.v.num_channels = 2;
1601 strcpy(dip->un.v.units.name, AudioNvolume);
1602 return (0);
1603 case EAP_CD_VOL:
1604 dip->type = AUDIO_MIXER_VALUE;
1605 dip->mixer_class = EAP_INPUT_CLASS;
1606 dip->prev = AUDIO_MIXER_LAST;
1607 dip->next = AUDIO_MIXER_LAST;
1608 strcpy(dip->label.name, AudioNcd);
1609 dip->un.v.num_channels = 2;
1610 strcpy(dip->un.v.units.name, AudioNvolume);
1611 return (0);
1612 case EAP_LINE_VOL:
1613 dip->type = AUDIO_MIXER_VALUE;
1614 dip->mixer_class = EAP_INPUT_CLASS;
1615 dip->prev = AUDIO_MIXER_LAST;
1616 dip->next = AUDIO_MIXER_LAST;
1617 strcpy(dip->label.name, AudioNline);
1618 dip->un.v.num_channels = 2;
1619 strcpy(dip->un.v.units.name, AudioNvolume);
1620 return (0);
1621 case EAP_AUX_VOL:
1622 dip->type = AUDIO_MIXER_VALUE;
1623 dip->mixer_class = EAP_INPUT_CLASS;
1624 dip->prev = AUDIO_MIXER_LAST;
1625 dip->next = AUDIO_MIXER_LAST;
1626 strcpy(dip->label.name, AudioNaux);
1627 dip->un.v.num_channels = 2;
1628 strcpy(dip->un.v.units.name, AudioNvolume);
1629 return (0);
1630 case EAP_MIC_VOL:
1631 dip->type = AUDIO_MIXER_VALUE;
1632 dip->mixer_class = EAP_INPUT_CLASS;
1633 dip->prev = AUDIO_MIXER_LAST;
1634 dip->next = EAP_MIC_PREAMP;
1635 strcpy(dip->label.name, AudioNmicrophone);
1636 dip->un.v.num_channels = 1;
1637 strcpy(dip->un.v.units.name, AudioNvolume);
1638 return (0);
1639 case EAP_RECORD_SOURCE:
1640 dip->mixer_class = EAP_RECORD_CLASS;
1641 dip->prev = dip->next = AUDIO_MIXER_LAST;
1642 strcpy(dip->label.name, AudioNsource);
1643 dip->type = AUDIO_MIXER_SET;
1644 dip->un.s.num_mem = 6;
1645 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1646 dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1647 strcpy(dip->un.s.member[1].label.name, AudioNcd);
1648 dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1649 strcpy(dip->un.s.member[2].label.name, AudioNline);
1650 dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1651 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1652 dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1653 strcpy(dip->un.s.member[4].label.name, AudioNaux);
1654 dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1655 strcpy(dip->un.s.member[5].label.name, AudioNdac);
1656 dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1657 return (0);
1658 case EAP_OUTPUT_SELECT:
1659 dip->mixer_class = EAP_OUTPUT_CLASS;
1660 dip->prev = dip->next = AUDIO_MIXER_LAST;
1661 strcpy(dip->label.name, AudioNselect);
1662 dip->type = AUDIO_MIXER_SET;
1663 dip->un.s.num_mem = 6;
1664 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1665 dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
1666 strcpy(dip->un.s.member[1].label.name, AudioNcd);
1667 dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
1668 strcpy(dip->un.s.member[2].label.name, AudioNline);
1669 dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
1670 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1671 dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
1672 strcpy(dip->un.s.member[4].label.name, AudioNaux);
1673 dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
1674 strcpy(dip->un.s.member[5].label.name, AudioNdac);
1675 dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
1676 return (0);
1677 case EAP_MIC_PREAMP:
1678 dip->type = AUDIO_MIXER_ENUM;
1679 dip->mixer_class = EAP_INPUT_CLASS;
1680 dip->prev = EAP_MIC_VOL;
1681 dip->next = AUDIO_MIXER_LAST;
1682 strcpy(dip->label.name, AudioNpreamp);
1683 dip->un.e.num_mem = 2;
1684 strcpy(dip->un.e.member[0].label.name, AudioNoff);
1685 dip->un.e.member[0].ord = 0;
1686 strcpy(dip->un.e.member[1].label.name, AudioNon);
1687 dip->un.e.member[1].ord = 1;
1688 return (0);
1689 case EAP_OUTPUT_CLASS:
1690 dip->type = AUDIO_MIXER_CLASS;
1691 dip->mixer_class = EAP_OUTPUT_CLASS;
1692 dip->next = dip->prev = AUDIO_MIXER_LAST;
1693 strcpy(dip->label.name, AudioCoutputs);
1694 return (0);
1695 case EAP_RECORD_CLASS:
1696 dip->type = AUDIO_MIXER_CLASS;
1697 dip->mixer_class = EAP_RECORD_CLASS;
1698 dip->next = dip->prev = AUDIO_MIXER_LAST;
1699 strcpy(dip->label.name, AudioCrecord);
1700 return (0);
1701 case EAP_INPUT_CLASS:
1702 dip->type = AUDIO_MIXER_CLASS;
1703 dip->mixer_class = EAP_INPUT_CLASS;
1704 dip->next = dip->prev = AUDIO_MIXER_LAST;
1705 strcpy(dip->label.name, AudioCinputs);
1706 return (0);
1707 }
1708 return (ENXIO);
1709 }
1710
1711 void *
1712 eap_malloc(void *addr, int direction, size_t size, int pool, int flags)
1713 {
1714 struct eap_instance *ei = addr;
1715 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1716 struct eap_dma *p;
1717 int error;
1718
1719 p = malloc(sizeof(*p), pool, flags);
1720 if (!p)
1721 return (0);
1722 error = eap_allocmem(sc, size, 16, p);
1723 if (error) {
1724 free(p, pool);
1725 return (0);
1726 }
1727 p->next = sc->sc_dmas;
1728 sc->sc_dmas = p;
1729 return (KERNADDR(p));
1730 }
1731
1732 void
1733 eap_free(void *addr, void *ptr, int pool)
1734 {
1735 struct eap_instance *ei = addr;
1736 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1737 struct eap_dma **pp, *p;
1738
1739 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1740 if (KERNADDR(p) == ptr) {
1741 eap_freemem(sc, p);
1742 *pp = p->next;
1743 free(p, pool);
1744 return;
1745 }
1746 }
1747 }
1748
1749 size_t
1750 eap_round_buffersize(void *addr, int direction, size_t size)
1751 {
1752
1753 return (size);
1754 }
1755
1756 paddr_t
1757 eap_mappage(void *addr, void *mem, off_t off, int prot)
1758 {
1759 struct eap_instance *ei = addr;
1760 struct eap_softc *sc = (struct eap_softc *)ei->parent;
1761 struct eap_dma *p;
1762
1763 if (off < 0)
1764 return (-1);
1765 for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1766 ;
1767 if (!p)
1768 return (-1);
1769 return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1770 off, prot, BUS_DMA_WAITOK));
1771 }
1772
1773 int
1774 eap_get_props(void *addr)
1775 {
1776
1777 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1778 AUDIO_PROP_FULLDUPLEX);
1779 }
1780
1781 #if NMIDI > 0
1782 int
1783 eap_midi_open(void *addr, int flags,
1784 void (*iintr)(void *, int),
1785 void (*ointr)(void *),
1786 void *arg)
1787 {
1788 struct eap_softc *sc = addr;
1789 u_int32_t uctrl;
1790
1791 sc->sc_iintr = iintr;
1792 sc->sc_ointr = ointr;
1793 sc->sc_arg = arg;
1794
1795 EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
1796 uctrl = 0;
1797 if (flags & FREAD)
1798 uctrl |= EAP_UC_RXINTEN;
1799 #if 0
1800 /* I don't understand ../midi.c well enough to use output interrupts */
1801 if (flags & FWRITE)
1802 uctrl |= EAP_UC_TXINTEN; */
1803 #endif
1804 EWRITE1(sc, EAP_UART_CONTROL, uctrl);
1805
1806 return (0);
1807 }
1808
1809 void
1810 eap_midi_close(void *addr)
1811 {
1812 struct eap_softc *sc = addr;
1813
1814 tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
1815 EWRITE1(sc, EAP_UART_CONTROL, 0);
1816 EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
1817
1818 sc->sc_iintr = 0;
1819 sc->sc_ointr = 0;
1820 }
1821
1822 int
1823 eap_midi_output(void *addr, int d)
1824 {
1825 struct eap_softc *sc = addr;
1826 int x;
1827
1828 for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1829 if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
1830 EWRITE1(sc, EAP_UART_DATA, d);
1831 return (0);
1832 }
1833 delay(MIDI_BUSY_DELAY);
1834 }
1835 return (EIO);
1836 }
1837
1838 void
1839 eap_midi_getinfo(void *addr, struct midi_info *mi)
1840 {
1841 mi->name = "AudioPCI MIDI UART";
1842 mi->props = MIDI_PROP_CAN_INPUT;
1843 }
1844
1845 #endif
1846