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