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