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