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