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