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