1 /* $NetBSD: neo.c,v 1.12 2002/04/15 04:13:25 simonb Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 5 * All rights reserved. 6 * 7 * Derived from the public domain Linux driver 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * FreeBSD: src/sys/dev/sound/pci/neomagic.c,v 1.8 2000/03/20 15:30:50 cg Exp 31 * OpenBSD: neo.c,v 1.4 2000/07/19 09:04:37 csapuntz Exp 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: neo.c,v 1.12 2002/04/15 04:13:25 simonb Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/device.h> 42 43 #include <machine/bus.h> 44 45 #include <dev/pci/pcidevs.h> 46 #include <dev/pci/pcivar.h> 47 48 #include <dev/pci/neoreg.h> 49 #include <dev/pci/neo-coeff.h> 50 51 #include <sys/audioio.h> 52 #include <dev/audio_if.h> 53 #include <dev/mulaw.h> 54 #include <dev/auconv.h> 55 56 #include <dev/ic/ac97var.h> 57 58 59 /* -------------------------------------------------------------------- */ 60 /* 61 * As of 04/13/00, public documentation on the Neomagic 256 is not available. 62 * These comments were gleaned by looking at the driver carefully. 63 * 64 * The Neomagic 256 AV/ZX chips provide both video and audio capabilities 65 * on one chip. About 2-6 megabytes of memory are associated with 66 * the chip. Most of this goes to video frame buffers, but some is used for 67 * audio buffering 68 * 69 * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA. 70 * Instead, the chip allows you to carve out two ring buffers out of its 71 * memory. However you carve this and how much you can carve seems to be 72 * voodoo. The algorithm is in nm_init. 73 * 74 * Most Neomagic audio chips use the AC-97 codec interface. However, there 75 * seem to be a select few chips 256AV chips that do not support AC-97. 76 * This driver does not support them but there are rumors that it 77 * might work with wss isa drivers. This might require some playing around 78 * with your BIOS. 79 * 80 * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of 81 * them describe a memory region. The frame buffer is the first region 82 * and the register set is the secodn region. 83 * 84 * The register manipulation logic is taken from the Linux driver, 85 * which is in the public domain. 86 * 87 * The Neomagic is even nice enough to map the AC-97 codec registers into 88 * the register space to allow direct manipulation. Watch out, accessing 89 * AC-97 registers on the Neomagic requires great delicateness, otherwise 90 * the thing will hang the PCI bus, rendering your system frozen. 91 * 92 * For one, it seems the Neomagic status register that reports AC-97 93 * readiness should NOT be polled more often than once each 1ms. 94 * 95 * Also, writes to the AC-97 register space may take order 40us to 96 * complete. 97 * 98 * Unlike many sound engines, the Neomagic does not support (as far as 99 * we know :) the notion of interrupting every n bytes transferred, 100 * unlike many DMA engines. Instead, it allows you to specify one 101 * location in each ring buffer (called the watermark). When the chip 102 * passes that location while playing, it signals an interrupt. 103 * 104 * The ring buffer size is currently 16k. That is about 100ms of audio 105 * at 44.1khz/stero/16 bit. However, to keep the buffer full, interrupts 106 * are generated more often than that, so 20-40 interrupts per second 107 * should not be unexpected. Increasing BUFFSIZE should help minimize 108 * of glitches due to drivers that spend to much time looping at high 109 * privelege levels as well as the impact of badly written audio 110 * interface clients. 111 * 112 * TO-DO list: 113 * Figure out interaction with video stuff (look at Xfree86 driver?) 114 * 115 * Figure out how to shrink that huge table neo-coeff.h 116 */ 117 118 #define NM_BUFFSIZE 16384 119 120 /* device private data */ 121 struct neo_softc { 122 struct device dev; 123 124 bus_space_tag_t bufiot; 125 bus_space_handle_t bufioh; 126 127 bus_space_tag_t regiot; 128 bus_space_handle_t regioh; 129 130 u_int32_t type; 131 void *ih; 132 133 void (*pintr)(void *); /* dma completion intr handler */ 134 void *parg; /* arg for intr() */ 135 136 void (*rintr)(void *); /* dma completion intr handler */ 137 void *rarg; /* arg for intr() */ 138 139 vaddr_t buf_vaddr; 140 vaddr_t rbuf_vaddr; 141 vaddr_t pbuf_vaddr; 142 int pbuf_allocated; 143 int rbuf_allocated; 144 145 bus_addr_t buf_pciaddr; 146 bus_addr_t rbuf_pciaddr; 147 bus_addr_t pbuf_pciaddr; 148 149 u_int32_t ac97_base, ac97_status, ac97_busy; 150 u_int32_t buftop, pbuf, rbuf, cbuf, acbuf; 151 u_int32_t playint, recint, misc1int, misc2int; 152 u_int32_t irsz, badintr; 153 154 u_int32_t pbufsize; 155 u_int32_t rbufsize; 156 157 u_int32_t pblksize; 158 u_int32_t rblksize; 159 160 u_int32_t pwmark; 161 u_int32_t rwmark; 162 163 struct ac97_codec_if *codec_if; 164 struct ac97_host_if host_if; 165 166 void *powerhook; 167 }; 168 169 /* -------------------------------------------------------------------- */ 170 171 /* 172 * prototypes 173 */ 174 175 static int nm_waitcd(struct neo_softc *sc); 176 static int nm_loadcoeff(struct neo_softc *sc, int dir, int num); 177 static int nm_init(struct neo_softc *); 178 179 int neo_match(struct device *, struct cfdata *, void *); 180 void neo_attach(struct device *, struct device *, void *); 181 int neo_intr(void *); 182 183 int neo_open(void *, int); 184 void neo_close(void *); 185 int neo_query_encoding(void *, struct audio_encoding *); 186 int neo_set_params(void *, int, int, struct audio_params *, 187 struct audio_params *); 188 int neo_round_blocksize(void *, int); 189 int neo_trigger_output(void *, void *, void *, int, void (*)(void *), 190 void *, struct audio_params *); 191 int neo_trigger_input(void *, void *, void *, int, void (*)(void *), 192 void *, struct audio_params *); 193 int neo_halt_output(void *); 194 int neo_halt_input(void *); 195 int neo_getdev(void *, struct audio_device *); 196 int neo_mixer_set_port(void *, mixer_ctrl_t *); 197 int neo_mixer_get_port(void *, mixer_ctrl_t *); 198 int neo_attach_codec(void *sc, struct ac97_codec_if *); 199 int neo_read_codec(void *sc, u_int8_t a, u_int16_t *d); 200 int neo_write_codec(void *sc, u_int8_t a, u_int16_t d); 201 void neo_reset_codec(void *sc); 202 enum ac97_host_flags neo_flags_codec(void *sc); 203 int neo_query_devinfo(void *, mixer_devinfo_t *); 204 void *neo_malloc(void *, int, size_t, int, int); 205 void neo_free(void *, void *, int); 206 size_t neo_round_buffersize(void *, int, size_t); 207 paddr_t neo_mappage(void *, void *, off_t, int); 208 int neo_get_props(void *); 209 void neo_set_mixer(struct neo_softc *sc, int a, int d); 210 void neo_power(int why, void *arg); 211 212 struct cfattach neo_ca = { 213 sizeof(struct neo_softc), neo_match, neo_attach 214 }; 215 216 struct audio_device neo_device = { 217 "NeoMagic 256", 218 "", 219 "neo" 220 }; 221 222 /* The actual rates supported by the card. */ 223 static const int samplerates[9] = { 224 8000, 225 11025, 226 16000, 227 22050, 228 24000, 229 32000, 230 44100, 231 48000, 232 99999999 233 }; 234 235 /* -------------------------------------------------------------------- */ 236 237 struct audio_hw_if neo_hw_if = { 238 neo_open, 239 neo_close, 240 NULL, /* drain */ 241 neo_query_encoding, 242 neo_set_params, 243 neo_round_blocksize, 244 NULL, /* commit_setting */ 245 NULL, /* init_output */ 246 NULL, /* init_input */ 247 NULL, /* start_output */ 248 NULL, /* start_input */ 249 neo_halt_output, 250 neo_halt_input, 251 NULL, /* speaker_ctl */ 252 neo_getdev, 253 NULL, /* getfd */ 254 neo_mixer_set_port, 255 neo_mixer_get_port, 256 neo_query_devinfo, 257 neo_malloc, 258 neo_free, 259 neo_round_buffersize, 260 neo_mappage, 261 neo_get_props, 262 neo_trigger_output, 263 neo_trigger_input, 264 NULL, 265 }; 266 267 /* -------------------------------------------------------------------- */ 268 269 #define nm_rd_1(sc, regno) \ 270 bus_space_read_1((sc)->regiot, (sc)->regioh, (regno)) 271 272 #define nm_rd_2(sc, regno) \ 273 bus_space_read_2((sc)->regiot, (sc)->regioh, (regno)) 274 275 #define nm_rd_4(sc, regno) \ 276 bus_space_read_4((sc)->regiot, (sc)->regioh, (regno)) 277 278 #define nm_wr_1(sc, regno, val) \ 279 bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val)) 280 281 #define nm_wr_2(sc, regno, val) \ 282 bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val)) 283 284 #define nm_wr_4(sc, regno, val) \ 285 bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val)) 286 287 #define nm_rdbuf_4(sc, regno) \ 288 bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno)) 289 290 #define nm_wrbuf_1(sc, regno, val) \ 291 bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val)) 292 293 /* ac97 codec */ 294 static int 295 nm_waitcd(struct neo_softc *sc) 296 { 297 int cnt = 10; 298 int fail = 1; 299 300 while (cnt-- > 0) { 301 if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy) 302 DELAY(100); 303 else { 304 fail = 0; 305 break; 306 } 307 } 308 return (fail); 309 } 310 311 312 static void 313 nm_ackint(struct neo_softc *sc, u_int32_t num) 314 { 315 316 switch (sc->type) { 317 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: 318 nm_wr_2(sc, NM_INT_REG, num << 1); 319 break; 320 321 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: 322 nm_wr_4(sc, NM_INT_REG, num); 323 break; 324 } 325 } 326 327 static int 328 nm_loadcoeff(struct neo_softc *sc, int dir, int num) 329 { 330 int ofs, sz, i; 331 u_int32_t addr; 332 333 addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c; 334 if (dir == AUMODE_RECORD) 335 num += 8; 336 sz = coefficientSizes[num]; 337 ofs = 0; 338 while (num-- > 0) 339 ofs+= coefficientSizes[num]; 340 for (i = 0; i < sz; i++) 341 nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]); 342 nm_wr_4(sc, addr, sc->cbuf); 343 if (dir == AUMODE_PLAY) 344 sz--; 345 nm_wr_4(sc, addr + 4, sc->cbuf + sz); 346 return 0; 347 } 348 349 /* The interrupt handler */ 350 int 351 neo_intr(void *p) 352 { 353 struct neo_softc *sc = (struct neo_softc *)p; 354 int status, x, active; 355 int rv = 0; 356 357 active = (sc->pintr || sc->rintr); 358 status = (sc->irsz == 2) ? 359 nm_rd_2(sc, NM_INT_REG) : 360 nm_rd_4(sc, NM_INT_REG); 361 362 if (status & sc->playint) { 363 status &= ~sc->playint; 364 365 sc->pwmark += sc->pblksize; 366 sc->pwmark %= sc->pbufsize; 367 368 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark); 369 370 nm_ackint(sc, sc->playint); 371 372 if (sc->pintr) 373 (*sc->pintr)(sc->parg); 374 375 rv = 1; 376 } 377 if (status & sc->recint) { 378 status &= ~sc->recint; 379 380 sc->rwmark += sc->rblksize; 381 sc->rwmark %= sc->rbufsize; 382 383 nm_ackint(sc, sc->recint); 384 if (sc->rintr) 385 (*sc->rintr)(sc->rarg); 386 387 rv = 1; 388 } 389 if (status & sc->misc1int) { 390 status &= ~sc->misc1int; 391 nm_ackint(sc, sc->misc1int); 392 x = nm_rd_1(sc, 0x400); 393 nm_wr_1(sc, 0x400, x | 2); 394 printf("%s: misc int 1\n", sc->dev.dv_xname); 395 rv = 1; 396 } 397 if (status & sc->misc2int) { 398 status &= ~sc->misc2int; 399 nm_ackint(sc, sc->misc2int); 400 x = nm_rd_1(sc, 0x400); 401 nm_wr_1(sc, 0x400, x & ~2); 402 printf("%s: misc int 2\n", sc->dev.dv_xname); 403 rv = 1; 404 } 405 if (status) { 406 status &= ~sc->misc2int; 407 nm_ackint(sc, sc->misc2int); 408 printf("%s: unknown int\n", sc->dev.dv_xname); 409 rv = 1; 410 } 411 412 return (rv); 413 } 414 415 /* -------------------------------------------------------------------- */ 416 417 /* 418 * Probe and attach the card 419 */ 420 421 static int 422 nm_init(struct neo_softc *sc) 423 { 424 u_int32_t ofs, i; 425 426 switch (sc->type) { 427 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: 428 sc->ac97_base = NM_MIXER_OFFSET; 429 sc->ac97_status = NM_MIXER_STATUS_OFFSET; 430 sc->ac97_busy = NM_MIXER_READY_MASK; 431 432 sc->buftop = 2560 * 1024; 433 434 sc->irsz = 2; 435 sc->playint = NM_PLAYBACK_INT; 436 sc->recint = NM_RECORD_INT; 437 sc->misc1int = NM_MISC_INT_1; 438 sc->misc2int = NM_MISC_INT_2; 439 break; 440 441 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: 442 sc->ac97_base = NM_MIXER_OFFSET; 443 sc->ac97_status = NM2_MIXER_STATUS_OFFSET; 444 sc->ac97_busy = NM2_MIXER_READY_MASK; 445 446 sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024; 447 448 sc->irsz = 4; 449 sc->playint = NM2_PLAYBACK_INT; 450 sc->recint = NM2_RECORD_INT; 451 sc->misc1int = NM2_MISC_INT_1; 452 sc->misc2int = NM2_MISC_INT_2; 453 break; 454 #ifdef DIAGNOSTIC 455 default: 456 panic("nm_init: impossible"); 457 #endif 458 } 459 460 sc->badintr = 0; 461 ofs = sc->buftop - 0x0400; 462 sc->buftop -= 0x1400; 463 464 if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) { 465 i = nm_rdbuf_4(sc, ofs + 4); 466 if (i != 0 && i != 0xffffffff) 467 sc->buftop = i; 468 } 469 470 sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT; 471 sc->rbuf = sc->cbuf - NM_BUFFSIZE; 472 sc->pbuf = sc->rbuf - NM_BUFFSIZE; 473 sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4); 474 475 sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh); 476 sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf; 477 sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf; 478 479 sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf; 480 sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf; 481 482 nm_wr_1(sc, 0, 0x11); 483 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0); 484 nm_wr_2(sc, 0x214, 0); 485 486 return 0; 487 } 488 489 int 490 neo_match(struct device *parent, struct cfdata *match, void *aux) 491 { 492 struct pci_attach_args *pa = aux; 493 pcireg_t subdev; 494 495 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC) 496 return (0); 497 498 subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 499 500 switch (PCI_PRODUCT(pa->pa_id)) { 501 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: 502 /* 503 * We have to weed-out the non-AC'97 versions of 504 * the chip (i.e. the ones that are known to work 505 * in WSS emulation mode), as they won't work with 506 * this driver. 507 */ 508 switch (PCI_VENDOR(subdev)) { 509 case PCI_VENDOR_DELL: 510 switch (PCI_PRODUCT(subdev)) { 511 case 0x008f: 512 return (0); 513 } 514 break; 515 516 case PCI_VENDOR_HP: 517 switch (PCI_PRODUCT(subdev)) { 518 case 0x0007: 519 return (0); 520 } 521 break; 522 523 case PCI_VENDOR_IBM: 524 switch (PCI_PRODUCT(subdev)) { 525 case 0x00dd: 526 return (0); 527 } 528 break; 529 } 530 return (1); 531 532 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: 533 return (1); 534 } 535 536 return (0); 537 } 538 539 void 540 neo_power(int why, void *addr) 541 { 542 struct neo_softc *sc = (struct neo_softc *)addr; 543 544 if (why == PWR_RESUME) { 545 nm_init(sc); 546 (sc->codec_if->vtbl->restore_ports)(sc->codec_if); 547 } 548 } 549 550 void 551 neo_attach(struct device *parent, struct device *self, void *aux) 552 { 553 struct neo_softc *sc = (struct neo_softc *)self; 554 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 555 pci_chipset_tag_t pc = pa->pa_pc; 556 char const *intrstr; 557 pci_intr_handle_t ih; 558 pcireg_t csr; 559 int error; 560 561 sc->type = PCI_PRODUCT(pa->pa_id); 562 563 printf(": NeoMagic 256%s audio\n", 564 sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX"); 565 566 /* Map I/O register */ 567 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0, 568 &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) { 569 printf("%s: can't map buffer\n", sc->dev.dv_xname); 570 return; 571 } 572 573 if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM, 0, 574 &sc->regiot, &sc->regioh, NULL, NULL)) { 575 printf("%s: can't map registers\n", sc->dev.dv_xname); 576 return; 577 } 578 579 /* Map and establish the interrupt. */ 580 if (pci_intr_map(pa, &ih)) { 581 printf("%s: couldn't map interrupt\n", sc->dev.dv_xname); 582 return; 583 } 584 585 intrstr = pci_intr_string(pc, ih); 586 sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc); 587 588 if (sc->ih == NULL) { 589 printf("%s: couldn't establish interrupt", 590 sc->dev.dv_xname); 591 if (intrstr != NULL) 592 printf(" at %s", intrstr); 593 printf("\n"); 594 return; 595 } 596 printf("%s: interruping at %s\n", sc->dev.dv_xname, intrstr); 597 598 if ((error = nm_init(sc)) != 0) 599 return; 600 601 /* Enable the device. */ 602 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 603 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 604 csr | PCI_COMMAND_MASTER_ENABLE); 605 606 sc->host_if.arg = sc; 607 608 sc->host_if.attach = neo_attach_codec; 609 sc->host_if.read = neo_read_codec; 610 sc->host_if.write = neo_write_codec; 611 sc->host_if.reset = neo_reset_codec; 612 sc->host_if.flags = neo_flags_codec; 613 614 if ((error = ac97_attach(&sc->host_if)) != 0) 615 return; 616 617 sc->powerhook = powerhook_establish(neo_power, sc); 618 619 audio_attach_mi(&neo_hw_if, sc, &sc->dev); 620 } 621 622 int 623 neo_read_codec(void *v, u_int8_t a, u_int16_t *d) 624 { 625 struct neo_softc *sc = v; 626 627 if (!nm_waitcd(sc)) { 628 *d = nm_rd_2(sc, sc->ac97_base + a); 629 DELAY(1000); 630 return 0; 631 } 632 633 return (ENXIO); 634 } 635 636 637 int 638 neo_write_codec(void *v, u_int8_t a, u_int16_t d) 639 { 640 struct neo_softc *sc = v; 641 int cnt = 3; 642 643 if (!nm_waitcd(sc)) { 644 while (cnt-- > 0) { 645 nm_wr_2(sc, sc->ac97_base + a, d); 646 if (!nm_waitcd(sc)) { 647 DELAY(1000); 648 return (0); 649 } 650 } 651 } 652 653 return (ENXIO); 654 } 655 656 int 657 neo_attach_codec(void *v, struct ac97_codec_if *codec_if) 658 { 659 struct neo_softc *sc = v; 660 661 sc->codec_if = codec_if; 662 return (0); 663 } 664 665 void 666 neo_reset_codec(void *v) 667 { 668 struct neo_softc *sc = v; 669 670 nm_wr_1(sc, 0x6c0, 0x01); 671 nm_wr_1(sc, 0x6cc, 0x87); 672 nm_wr_1(sc, 0x6cc, 0x80); 673 nm_wr_1(sc, 0x6cc, 0x00); 674 } 675 676 enum ac97_host_flags 677 neo_flags_codec(void *v) 678 { 679 680 return (AC97_HOST_DONT_READ); 681 } 682 683 int 684 neo_open(void *addr, int flags) 685 { 686 687 return (0); 688 } 689 690 /* 691 * Close function is called at splaudio(). 692 */ 693 void 694 neo_close(void *addr) 695 { 696 struct neo_softc *sc = addr; 697 698 neo_halt_output(sc); 699 neo_halt_input(sc); 700 701 sc->pintr = 0; 702 sc->rintr = 0; 703 } 704 705 int 706 neo_query_encoding(void *addr, struct audio_encoding *fp) 707 { 708 709 switch (fp->index) { 710 case 0: 711 strcpy(fp->name, AudioEulinear); 712 fp->encoding = AUDIO_ENCODING_ULINEAR; 713 fp->precision = 8; 714 fp->flags = 0; 715 return (0); 716 case 1: 717 strcpy(fp->name, AudioEmulaw); 718 fp->encoding = AUDIO_ENCODING_ULAW; 719 fp->precision = 8; 720 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 721 return (0); 722 case 2: 723 strcpy(fp->name, AudioEalaw); 724 fp->encoding = AUDIO_ENCODING_ALAW; 725 fp->precision = 8; 726 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 727 return (0); 728 case 3: 729 strcpy(fp->name, AudioEslinear); 730 fp->encoding = AUDIO_ENCODING_SLINEAR; 731 fp->precision = 8; 732 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 733 return (0); 734 case 4: 735 strcpy(fp->name, AudioEslinear_le); 736 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 737 fp->precision = 16; 738 fp->flags = 0; 739 return (0); 740 case 5: 741 strcpy(fp->name, AudioEulinear_le); 742 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 743 fp->precision = 16; 744 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 745 return (0); 746 case 6: 747 strcpy(fp->name, AudioEslinear_be); 748 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 749 fp->precision = 16; 750 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 751 return (0); 752 case 7: 753 strcpy(fp->name, AudioEulinear_be); 754 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 755 fp->precision = 16; 756 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 757 return (0); 758 default: 759 return (EINVAL); 760 } 761 } 762 763 /* Todo: don't commit settings to card until we've verified all parameters */ 764 int 765 neo_set_params(void *addr, int setmode, int usemode, struct audio_params *play, 766 struct audio_params *rec) 767 { 768 struct neo_softc *sc = addr; 769 u_int32_t base; 770 u_int8_t x; 771 int mode; 772 struct audio_params *p; 773 774 for (mode = AUMODE_RECORD; mode != -1; 775 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 776 if ((setmode & mode) == 0) 777 continue; 778 779 p = mode == AUMODE_PLAY ? play : rec; 780 781 if (p == NULL) continue; 782 783 for (x = 0; x < 8; x++) { 784 if (p->sample_rate < 785 (samplerates[x] + samplerates[x + 1]) / 2) 786 break; 787 } 788 if (x == 8) 789 return (EINVAL); 790 791 p->sample_rate = samplerates[x]; 792 nm_loadcoeff(sc, mode, x); 793 794 x <<= 4; 795 x &= NM_RATE_MASK; 796 if (p->precision == 16) 797 x |= NM_RATE_BITS_16; 798 if (p->channels == 2) 799 x |= NM_RATE_STEREO; 800 801 base = (mode == AUMODE_PLAY)? 802 NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET; 803 nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x); 804 805 p->factor = 1; 806 p->sw_code = 0; 807 switch (p->encoding) { 808 case AUDIO_ENCODING_SLINEAR_BE: 809 if (p->precision == 16) 810 p->sw_code = swap_bytes; 811 else 812 p->sw_code = change_sign8; 813 break; 814 case AUDIO_ENCODING_SLINEAR_LE: 815 if (p->precision != 16) 816 p->sw_code = change_sign8; 817 break; 818 case AUDIO_ENCODING_ULINEAR_BE: 819 if (p->precision == 16) { 820 if (mode == AUMODE_PLAY) 821 p->sw_code = 822 swap_bytes_change_sign16_le; 823 else 824 p->sw_code = 825 change_sign16_swap_bytes_le; 826 } 827 break; 828 case AUDIO_ENCODING_ULINEAR_LE: 829 if (p->precision == 16) 830 p->sw_code = change_sign16_le; 831 break; 832 case AUDIO_ENCODING_ULAW: 833 if (mode == AUMODE_PLAY) { 834 p->factor = 2; 835 p->sw_code = mulaw_to_slinear16_le; 836 } else 837 p->sw_code = ulinear8_to_mulaw; 838 break; 839 case AUDIO_ENCODING_ALAW: 840 if (mode == AUMODE_PLAY) { 841 p->factor = 2; 842 p->sw_code = alaw_to_slinear16_le; 843 } else 844 p->sw_code = ulinear8_to_alaw; 845 break; 846 default: 847 return (EINVAL); 848 } 849 } 850 851 852 return (0); 853 } 854 855 int 856 neo_round_blocksize(void *addr, int blk) 857 { 858 859 return (NM_BUFFSIZE / 2); 860 } 861 862 int 863 neo_trigger_output(void *addr, void *start, void *end, int blksize, 864 void (*intr)(void *), void *arg, struct audio_params *param) 865 { 866 struct neo_softc *sc = addr; 867 int ssz; 868 869 sc->pintr = intr; 870 sc->parg = arg; 871 872 ssz = (param->precision * param->factor == 16) ? 2 : 1; 873 if (param->channels == 2) 874 ssz <<= 1; 875 876 sc->pbufsize = ((char*)end - (char *)start); 877 sc->pblksize = blksize; 878 sc->pwmark = blksize; 879 880 nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf); 881 nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz); 882 nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf); 883 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark); 884 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN | 885 NM_PLAYBACK_ENABLE_FLAG); 886 nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0); 887 888 return (0); 889 } 890 891 int 892 neo_trigger_input(void *addr, void *start, void *end, int blksize, 893 void (*intr)(void *), void *arg, struct audio_params *param) 894 { 895 struct neo_softc *sc = addr; 896 int ssz; 897 898 sc->rintr = intr; 899 sc->rarg = arg; 900 901 ssz = (param->precision * param->factor == 16) ? 2 : 1; 902 if (param->channels == 2) 903 ssz <<= 1; 904 905 sc->rbufsize = ((char*)end - (char *)start); 906 sc->rblksize = blksize; 907 sc->rwmark = blksize; 908 909 nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf); 910 nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize); 911 nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf); 912 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark); 913 nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN | 914 NM_RECORD_ENABLE_FLAG); 915 916 return (0); 917 } 918 919 int 920 neo_halt_output(void *addr) 921 { 922 struct neo_softc *sc = (struct neo_softc *)addr; 923 924 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0); 925 nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH); 926 927 return (0); 928 } 929 930 int 931 neo_halt_input(void *addr) 932 { 933 struct neo_softc *sc = (struct neo_softc *)addr; 934 935 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0); 936 937 return (0); 938 } 939 940 int 941 neo_getdev(void *addr, struct audio_device *retp) 942 { 943 944 *retp = neo_device; 945 return (0); 946 } 947 948 int 949 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp) 950 { 951 struct neo_softc *sc = addr; 952 953 return ((sc->codec_if->vtbl->mixer_set_port)(sc->codec_if, cp)); 954 } 955 956 int 957 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp) 958 { 959 struct neo_softc *sc = addr; 960 961 return ((sc->codec_if->vtbl->mixer_get_port)(sc->codec_if, cp)); 962 } 963 964 int 965 neo_query_devinfo(void *addr, mixer_devinfo_t *dip) 966 { 967 struct neo_softc *sc = addr; 968 969 return ((sc->codec_if->vtbl->query_devinfo)(sc->codec_if, dip)); 970 } 971 972 void * 973 neo_malloc(void *addr, int direction, size_t size, int pool, int flags) 974 { 975 struct neo_softc *sc = addr; 976 void *rv = NULL; 977 978 switch (direction) { 979 case AUMODE_PLAY: 980 if (sc->pbuf_allocated == 0) { 981 rv = (void *) sc->pbuf_vaddr; 982 sc->pbuf_allocated = 1; 983 } 984 break; 985 986 case AUMODE_RECORD: 987 if (sc->rbuf_allocated == 0) { 988 rv = (void *) sc->rbuf_vaddr; 989 sc->rbuf_allocated = 1; 990 } 991 break; 992 } 993 994 return (rv); 995 } 996 997 void 998 neo_free(void *addr, void *ptr, int pool) 999 { 1000 struct neo_softc *sc = addr; 1001 vaddr_t v = (vaddr_t) ptr; 1002 1003 if (v == sc->pbuf_vaddr) 1004 sc->pbuf_allocated = 0; 1005 else if (v == sc->rbuf_vaddr) 1006 sc->rbuf_allocated = 0; 1007 else 1008 printf("neo_free: bad address %p\n", ptr); 1009 } 1010 1011 size_t 1012 neo_round_buffersize(void *addr, int direction, size_t size) 1013 { 1014 1015 return (NM_BUFFSIZE); 1016 } 1017 1018 paddr_t 1019 neo_mappage(void *addr, void *mem, off_t off, int prot) 1020 { 1021 struct neo_softc *sc = addr; 1022 vaddr_t v = (vaddr_t) mem; 1023 bus_addr_t pciaddr; 1024 1025 if (v == sc->pbuf_vaddr) 1026 pciaddr = sc->pbuf_pciaddr; 1027 else if (v == sc->rbuf_vaddr) 1028 pciaddr = sc->rbuf_pciaddr; 1029 else 1030 return (-1); 1031 1032 return (bus_space_mmap(sc->bufiot, pciaddr, off, prot, 1033 BUS_SPACE_MAP_LINEAR)); 1034 } 1035 1036 int 1037 neo_get_props(void *addr) 1038 { 1039 1040 return (AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP | 1041 AUDIO_PROP_FULLDUPLEX); 1042 } 1043