1 /* $OpenBSD: neo.c,v 1.40 2022/10/26 20:19:08 kn 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, WHETHER IN 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 THE POSSIBILITY 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 */ 32 33 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/device.h> 40 41 #include <dev/pci/pcidevs.h> 42 #include <dev/pci/pcivar.h> 43 44 #include <sys/audioio.h> 45 #include <dev/audio_if.h> 46 #include <dev/ic/ac97.h> 47 48 #include <dev/pci/neoreg.h> 49 50 /* -------------------------------------------------------------------- */ 51 /* 52 * As of 04/13/00, public documentation on the Neomagic 256 is not available. 53 * These comments were gleaned by looking at the driver carefully. 54 * 55 * The Neomagic 256 AV/ZX chips provide both video and audio capabilities 56 * on one chip. About 2-6 megabytes of memory are associated with 57 * the chip. Most of this goes to video frame buffers, but some is used for 58 * audio buffering. 59 * 60 * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA. 61 * Instead, the chip allows you to carve two ring buffers out of its 62 * memory. How you carve this and how much you can carve seems to be 63 * voodoo. The algorithm is in nm_init. 64 * 65 * Most Neomagic audio chips use the AC-97 codec interface. However, there 66 * seem to be a select few chips 256AV chips that do not support AC-97. 67 * This driver does not support them but there are rumors that it 68 * might work with wss isa drivers. This might require some playing around 69 * with your BIOS. 70 * 71 * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of 72 * them describe a memory region. The frame buffer is the first region 73 * and the register set is the second region. 74 * 75 * The register manipulation logic is taken from the Linux driver, 76 * which is in the public domain. 77 * 78 * The Neomagic is even nice enough to map the AC-97 codec registers into 79 * the register space to allow direct manipulation. Watch out, accessing 80 * AC-97 registers on the Neomagic requires great delicateness, otherwise 81 * the thing will hang the PCI bus, rendering your system frozen. 82 * 83 * For one, it seems the Neomagic status register that reports AC-97 84 * readiness should NOT be polled more often than once each 1ms. 85 * 86 * Also, writes to the AC-97 register space may take over 40us to 87 * complete. 88 * 89 * Unlike many sound engines, the Neomagic does not support (as fas as 90 * we know :) ) the notion of interrupting every n bytes transferred, 91 * unlike many DMA engines. Instead, it allows you to specify one 92 * location in each ring buffer (called the watermark). When the chip 93 * passes that location while playing, it signals an interrupt. 94 * 95 * The ring buffer size is currently 16k. That is about 100ms of audio 96 * at 44.1khz/stero/16 bit. However, to keep the buffer full, interrupts 97 * are generated more often than that, so 20-40 interrupts per second 98 * should not be unexpected. Increasing BUFFSIZE should help minimize 99 * the glitches due to drivers that spend too much time looping at high 100 * privilege levels as well as the impact of badly written audio 101 * interface clients. 102 * 103 * TO-DO list: 104 * neo_malloc/neo_free are still seriously broken. 105 * 106 * Figure out interaction with video stuff (look at Xfree86 driver?) 107 * 108 * Power management (neoactivate) 109 * 110 * Fix detection of Neo devices that don't work this driver (see neo_attach) 111 * 112 * Figure out how to shrink that huge table neo-coeff.h 113 */ 114 115 #define NM_BUFFSIZE 16384 116 117 #define NM256AV_PCI_ID 0x800510c8 118 #define NM256ZX_PCI_ID 0x800610c8 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 u_int32_t ac97_base, ac97_status, ac97_busy; 140 u_int32_t buftop, pbuf, rbuf, cbuf, acbuf; 141 u_int32_t playint, recint, misc1int, misc2int; 142 u_int32_t irsz, badintr; 143 144 u_int32_t pbufsize; 145 u_int32_t rbufsize; 146 147 u_int32_t pblksize; 148 u_int32_t rblksize; 149 150 u_int32_t pwmark; 151 u_int32_t rwmark; 152 153 struct ac97_codec_if *codec_if; 154 struct ac97_host_if host_if; 155 }; 156 157 static struct neo_firmware *nf; 158 159 /* -------------------------------------------------------------------- */ 160 161 /* 162 * prototypes 163 */ 164 165 static int nm_waitcd(struct neo_softc *sc); 166 static int nm_loadcoeff(struct neo_softc *sc, int dir, int num); 167 static int nm_init(struct neo_softc *); 168 169 int nmchan_getptr(struct neo_softc *, int); 170 /* talk to the card */ 171 static u_int32_t nm_rd(struct neo_softc *, int, int); 172 static void nm_wr(struct neo_softc *, int, u_int32_t, int); 173 static u_int32_t nm_rdbuf(struct neo_softc *, int, int); 174 static void nm_wrbuf(struct neo_softc *, int, u_int32_t, int); 175 176 int neo_match(struct device *, void *, void *); 177 void neo_attach(struct device *, struct device *, void *); 178 int neo_activate(struct device *, int); 179 int neo_intr(void *); 180 181 int neo_open(void *, int); 182 void neo_close(void *); 183 int neo_set_params(void *, int, int, struct audio_params *, struct audio_params *); 184 int neo_round_blocksize(void *, int); 185 int neo_trigger_output(void *, void *, void *, int, void (*)(void *), 186 void *, struct audio_params *); 187 int neo_trigger_input(void *, void *, void *, int, void (*)(void *), 188 void *, struct audio_params *); 189 int neo_halt_output(void *); 190 int neo_halt_input(void *); 191 int neo_mixer_set_port(void *, mixer_ctrl_t *); 192 int neo_mixer_get_port(void *, mixer_ctrl_t *); 193 int neo_attach_codec(void *sc, struct ac97_codec_if *); 194 int neo_read_codec(void *sc, u_int8_t a, u_int16_t *d); 195 int neo_write_codec(void *sc, u_int8_t a, u_int16_t d); 196 void neo_reset_codec(void *sc); 197 enum ac97_host_flags neo_flags_codec(void *sc); 198 int neo_query_devinfo(void *, mixer_devinfo_t *); 199 void *neo_malloc(void *, int, size_t, int, int); 200 void neo_free(void *, void *, int); 201 size_t neo_round_buffersize(void *, int, size_t); 202 void neo_set_mixer(struct neo_softc *sc, int a, int d); 203 204 struct cfdriver neo_cd = { 205 NULL, "neo", DV_DULL 206 }; 207 208 209 const struct cfattach neo_ca = { 210 sizeof(struct neo_softc), neo_match, neo_attach, NULL, 211 neo_activate 212 }; 213 214 215 #if 0 216 static u_int32_t badcards[] = { 217 0x0007103c, 218 0x008f1028, 219 }; 220 #endif 221 222 #define NUM_BADCARDS (sizeof(badcards) / sizeof(u_int32_t)) 223 224 /* The actual rates supported by the card. */ 225 static int samplerates[9] = { 226 8000, 227 11025, 228 16000, 229 22050, 230 24000, 231 32000, 232 44100, 233 48000, 234 99999999 235 }; 236 237 /* -------------------------------------------------------------------- */ 238 239 const struct audio_hw_if neo_hw_if = { 240 .open = neo_open, 241 .close = neo_close, 242 .set_params = neo_set_params, 243 .round_blocksize = neo_round_blocksize, 244 .halt_output = neo_halt_output, 245 .halt_input = neo_halt_input, 246 .set_port = neo_mixer_set_port, 247 .get_port = neo_mixer_get_port, 248 .query_devinfo = neo_query_devinfo, 249 .allocm = neo_malloc, 250 .freem = neo_free, 251 .round_buffersize = neo_round_buffersize, 252 .trigger_output = neo_trigger_output, 253 .trigger_input = neo_trigger_input, 254 255 }; 256 257 /* -------------------------------------------------------------------- */ 258 259 /* Hardware */ 260 static u_int32_t 261 nm_rd(struct neo_softc *sc, int regno, int size) 262 { 263 bus_space_tag_t st = sc->regiot; 264 bus_space_handle_t sh = sc->regioh; 265 266 switch (size) { 267 case 1: 268 return bus_space_read_1(st, sh, regno); 269 case 2: 270 return bus_space_read_2(st, sh, regno); 271 case 4: 272 return bus_space_read_4(st, sh, regno); 273 default: 274 return (0xffffffff); 275 } 276 } 277 278 static void 279 nm_wr(struct neo_softc *sc, int regno, u_int32_t data, int size) 280 { 281 bus_space_tag_t st = sc->regiot; 282 bus_space_handle_t sh = sc->regioh; 283 284 switch (size) { 285 case 1: 286 bus_space_write_1(st, sh, regno, data); 287 break; 288 case 2: 289 bus_space_write_2(st, sh, regno, data); 290 break; 291 case 4: 292 bus_space_write_4(st, sh, regno, data); 293 break; 294 } 295 } 296 297 static u_int32_t 298 nm_rdbuf(struct neo_softc *sc, int regno, int size) 299 { 300 bus_space_tag_t st = sc->bufiot; 301 bus_space_handle_t sh = sc->bufioh; 302 303 switch (size) { 304 case 1: 305 return bus_space_read_1(st, sh, regno); 306 case 2: 307 return bus_space_read_2(st, sh, regno); 308 case 4: 309 return bus_space_read_4(st, sh, regno); 310 default: 311 return (0xffffffff); 312 } 313 } 314 315 static void 316 nm_wrbuf(struct neo_softc *sc, int regno, u_int32_t data, int size) 317 { 318 bus_space_tag_t st = sc->bufiot; 319 bus_space_handle_t sh = sc->bufioh; 320 321 switch (size) { 322 case 1: 323 bus_space_write_1(st, sh, regno, data); 324 break; 325 case 2: 326 bus_space_write_2(st, sh, regno, data); 327 break; 328 case 4: 329 bus_space_write_4(st, sh, regno, data); 330 break; 331 } 332 } 333 334 /* ac97 codec */ 335 static int 336 nm_waitcd(struct neo_softc *sc) 337 { 338 int cnt = 10; 339 int fail = 1; 340 341 while (cnt-- > 0) { 342 if (nm_rd(sc, sc->ac97_status, 2) & sc->ac97_busy) 343 DELAY(100); 344 else { 345 fail = 0; 346 break; 347 } 348 } 349 return (fail); 350 } 351 352 353 static void 354 nm_ackint(struct neo_softc *sc, u_int32_t num) 355 { 356 if (sc->type == NM256AV_PCI_ID) 357 nm_wr(sc, NM_INT_REG, num << 1, 2); 358 else if (sc->type == NM256ZX_PCI_ID) 359 nm_wr(sc, NM_INT_REG, num, 4); 360 } 361 362 static int 363 nm_loadcoeff(struct neo_softc *sc, int dir, int num) 364 { 365 int ofs, sz, i; 366 u_int32_t addr; 367 368 if (nf == NULL) { 369 size_t buflen; 370 u_char *buf; 371 int error; 372 373 error = loadfirmware("neo-coefficients", &buf, &buflen); 374 if (error) 375 return (error); 376 nf = (struct neo_firmware *)buf; 377 } 378 379 addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c; 380 if (dir == AUMODE_RECORD) 381 num += 8; 382 sz = nf->coefficientSizes[num]; 383 ofs = 0; 384 while (num-- > 0) 385 ofs+= nf->coefficientSizes[num]; 386 for (i = 0; i < sz; i++) 387 nm_wrbuf(sc, sc->cbuf + i, nf->coefficients[ofs + i], 1); 388 nm_wr(sc, addr, sc->cbuf, 4); 389 if (dir == AUMODE_PLAY) 390 sz--; 391 nm_wr(sc, addr + 4, sc->cbuf + sz, 4); 392 return (0); 393 } 394 395 int 396 nmchan_getptr(struct neo_softc *sc, int mode) 397 { 398 if (mode == AUMODE_PLAY) 399 return (nm_rd(sc, NM_PBUFFER_CURRP, 4) - sc->pbuf); 400 else 401 return (nm_rd(sc, NM_RBUFFER_CURRP, 4) - sc->rbuf); 402 } 403 404 405 /* The interrupt handler */ 406 int 407 neo_intr(void *p) 408 { 409 struct neo_softc *sc = (struct neo_softc *)p; 410 int status, x; 411 int rv = 0; 412 413 mtx_enter(&audio_lock); 414 status = nm_rd(sc, NM_INT_REG, sc->irsz); 415 416 if (status & sc->playint) { 417 status &= ~sc->playint; 418 419 sc->pwmark += sc->pblksize; 420 sc->pwmark %= sc->pbufsize; 421 422 nm_wr(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark, 4); 423 424 nm_ackint(sc, sc->playint); 425 426 if (sc->pintr) 427 (*sc->pintr)(sc->parg); 428 429 rv = 1; 430 } 431 if (status & sc->recint) { 432 status &= ~sc->recint; 433 434 sc->rwmark += sc->rblksize; 435 sc->rwmark %= sc->rbufsize; 436 437 nm_ackint(sc, sc->recint); 438 if (sc->rintr) 439 (*sc->rintr)(sc->rarg); 440 441 rv = 1; 442 } 443 if (status & sc->misc1int) { 444 status &= ~sc->misc1int; 445 nm_ackint(sc, sc->misc1int); 446 x = nm_rd(sc, 0x400, 1); 447 nm_wr(sc, 0x400, x | 2, 1); 448 printf("%s: misc int 1\n", sc->dev.dv_xname); 449 rv = 1; 450 } 451 if (status & sc->misc2int) { 452 status &= ~sc->misc2int; 453 nm_ackint(sc, sc->misc2int); 454 x = nm_rd(sc, 0x400, 1); 455 nm_wr(sc, 0x400, x & ~2, 1); 456 printf("%s: misc int 2\n", sc->dev.dv_xname); 457 rv = 1; 458 } 459 if (status) { 460 status &= ~sc->misc2int; 461 nm_ackint(sc, sc->misc2int); 462 printf("%s: unknown int\n", sc->dev.dv_xname); 463 rv = 1; 464 } 465 mtx_leave(&audio_lock); 466 return (rv); 467 } 468 469 /* -------------------------------------------------------------------- */ 470 471 /* 472 * Probe and attach the card 473 */ 474 475 static int 476 nm_init(struct neo_softc *sc) 477 { 478 u_int32_t ofs, i; 479 480 if (sc->type == NM256AV_PCI_ID) { 481 sc->ac97_base = NM_MIXER_OFFSET; 482 sc->ac97_status = NM_MIXER_STATUS_OFFSET; 483 sc->ac97_busy = NM_MIXER_READY_MASK; 484 485 sc->buftop = 2560 * 1024; 486 487 sc->irsz = 2; 488 sc->playint = NM_PLAYBACK_INT; 489 sc->recint = NM_RECORD_INT; 490 sc->misc1int = NM_MISC_INT_1; 491 sc->misc2int = NM_MISC_INT_2; 492 } else if (sc->type == NM256ZX_PCI_ID) { 493 sc->ac97_base = NM_MIXER_OFFSET; 494 sc->ac97_status = NM2_MIXER_STATUS_OFFSET; 495 sc->ac97_busy = NM2_MIXER_READY_MASK; 496 497 sc->buftop = (nm_rd(sc, 0xa0b, 2)? 6144 : 4096) * 1024; 498 499 sc->irsz = 4; 500 sc->playint = NM2_PLAYBACK_INT; 501 sc->recint = NM2_RECORD_INT; 502 sc->misc1int = NM2_MISC_INT_1; 503 sc->misc2int = NM2_MISC_INT_2; 504 } else return -1; 505 sc->badintr = 0; 506 ofs = sc->buftop - 0x0400; 507 sc->buftop -= 0x1400; 508 509 if ((nm_rdbuf(sc, ofs, 4) & NM_SIG_MASK) == NM_SIGNATURE) { 510 i = nm_rdbuf(sc, ofs + 4, 4); 511 if (i != 0 && i != 0xffffffff) 512 sc->buftop = i; 513 } 514 515 sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT; 516 sc->rbuf = sc->cbuf - NM_BUFFSIZE; 517 sc->pbuf = sc->rbuf - NM_BUFFSIZE; 518 sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4); 519 520 nm_wr(sc, 0, 0x11, 1); 521 nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1); 522 nm_wr(sc, 0x214, 0, 2); 523 524 return 0; 525 } 526 527 528 void 529 neo_attach(struct device *parent, struct device *self, void *aux) 530 { 531 struct neo_softc *sc = (struct neo_softc *)self; 532 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 533 pci_chipset_tag_t pc = pa->pa_pc; 534 char const *intrstr; 535 pci_intr_handle_t ih; 536 int error; 537 538 sc->type = pa->pa_id; 539 540 /* Map I/O register */ 541 if (pci_mapreg_map(pa, PCI_MAPS, PCI_MAPREG_TYPE_MEM, 0, 542 &sc->bufiot, &sc->bufioh, NULL, NULL, 0)) { 543 printf("\n%s: can't map i/o space\n", sc->dev.dv_xname); 544 return; 545 } 546 547 548 if (pci_mapreg_map(pa, PCI_MAPS + 4, PCI_MAPREG_TYPE_MEM, 0, 549 &sc->regiot, &sc->regioh, NULL, NULL, 0)) { 550 printf("\n%s: can't map i/o space\n", sc->dev.dv_xname); 551 return; 552 } 553 554 /* Map and establish the interrupt. */ 555 if (pci_intr_map(pa, &ih)) { 556 printf("\n%s: couldn't map interrupt\n", sc->dev.dv_xname); 557 return; 558 } 559 intrstr = pci_intr_string(pc, ih); 560 sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO | IPL_MPSAFE, 561 neo_intr, sc, sc->dev.dv_xname); 562 563 if (sc->ih == NULL) { 564 printf("\n%s: couldn't establish interrupt", 565 sc->dev.dv_xname); 566 if (intrstr != NULL) 567 printf(" at %s", intrstr); 568 printf("\n"); 569 return; 570 } 571 printf(": %s\n", intrstr); 572 573 if ((error = nm_init(sc)) != 0) 574 return; 575 576 sc->host_if.arg = sc; 577 578 sc->host_if.attach = neo_attach_codec; 579 sc->host_if.read = neo_read_codec; 580 sc->host_if.write = neo_write_codec; 581 sc->host_if.reset = neo_reset_codec; 582 sc->host_if.flags = neo_flags_codec; 583 584 if ((error = ac97_attach(&sc->host_if)) != 0) 585 return; 586 587 audio_attach_mi(&neo_hw_if, sc, NULL, &sc->dev); 588 589 return; 590 } 591 592 int 593 neo_activate(struct device *self, int act) 594 { 595 struct neo_softc *sc = (struct neo_softc *)self; 596 597 switch (act) { 598 case DVACT_SUSPEND: 599 break; 600 case DVACT_RESUME: 601 nm_init(sc); 602 break; 603 } 604 return 0; 605 } 606 607 int 608 neo_match(struct device *parent, void *match, void *aux) 609 { 610 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 611 #if 0 612 u_int32_t subdev, badcard; 613 #endif 614 615 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC) 616 return (0); 617 618 #if 0 619 subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev); 620 #endif 621 switch (PCI_PRODUCT(pa->pa_id)) { 622 case PCI_PRODUCT_NEOMAGIC_NM256AV: 623 #if 0 624 i = 0; 625 while ((i < NUM_BADCARDS) && (badcards[i] != subdev)) 626 i++; 627 if (i == NUM_BADCARDS) 628 s = "NeoMagic 256AV"; 629 DEB(else) 630 DEB(device_printf(dev, "this is a non-ac97 NM256AV, not attaching\n")); 631 return (1); 632 #endif 633 case PCI_PRODUCT_NEOMAGIC_NM256ZX: 634 return (1); 635 } 636 637 return (0); 638 } 639 640 int 641 neo_read_codec(void *sc_, u_int8_t a, u_int16_t *d) 642 { 643 struct neo_softc *sc = sc_; 644 645 if (!nm_waitcd(sc)) { 646 *d = nm_rd(sc, sc->ac97_base + a, 2); 647 DELAY(1000); 648 return 0; 649 } 650 651 return (ENXIO); 652 } 653 654 655 int 656 neo_write_codec(void *sc_, u_int8_t a, u_int16_t d) 657 { 658 struct neo_softc *sc = sc_; 659 int cnt = 3; 660 661 if (!nm_waitcd(sc)) { 662 while (cnt-- > 0) { 663 nm_wr(sc, sc->ac97_base + a, d, 2); 664 if (!nm_waitcd(sc)) { 665 DELAY(1000); 666 return (0); 667 } 668 } 669 } 670 671 return (ENXIO); 672 } 673 674 675 int 676 neo_attach_codec(void *sc_, struct ac97_codec_if *codec_if) 677 { 678 struct neo_softc *sc = sc_; 679 680 sc->codec_if = codec_if; 681 return (0); 682 } 683 684 void 685 neo_reset_codec(void *sc) 686 { 687 nm_wr(sc, 0x6c0, 0x01, 1); 688 nm_wr(sc, 0x6cc, 0x87, 1); 689 nm_wr(sc, 0x6cc, 0x80, 1); 690 nm_wr(sc, 0x6cc, 0x00, 1); 691 692 return; 693 } 694 695 696 enum ac97_host_flags 697 neo_flags_codec(void *sc) 698 { 699 return (AC97_HOST_DONT_READANY); 700 } 701 702 int 703 neo_open(void *addr, int flags) 704 { 705 return (0); 706 } 707 708 /* 709 * Close function is called at splaudio(). 710 */ 711 void 712 neo_close(void *addr) 713 { 714 struct neo_softc *sc = addr; 715 716 neo_halt_output(sc); 717 neo_halt_input(sc); 718 719 sc->pintr = 0; 720 sc->rintr = 0; 721 } 722 723 /* Todo: don't commit settings to card until we've verified all parameters */ 724 int 725 neo_set_params(void *addr, int setmode, int usemode, 726 struct audio_params *play, struct audio_params *rec) 727 { 728 struct neo_softc *sc = addr; 729 u_int32_t base; 730 u_int8_t x; 731 int mode; 732 struct audio_params *p; 733 734 for (mode = AUMODE_RECORD; mode != -1; 735 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 736 if ((setmode & mode) == 0) 737 continue; 738 739 p = mode == AUMODE_PLAY ? play : rec; 740 741 if (p == NULL) continue; 742 743 for (x = 0; x < 8; x++) 744 if (p->sample_rate < (samplerates[x] + samplerates[x + 1]) / 2) 745 break; 746 747 p->sample_rate = samplerates[x]; 748 nm_loadcoeff(sc, mode, x); 749 750 x <<= 4; 751 x &= NM_RATE_MASK; 752 if (p->precision == 16) x |= NM_RATE_BITS_16; 753 if (p->channels == 2) x |= NM_RATE_STEREO; 754 755 base = (mode == AUMODE_PLAY) ? 756 NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET; 757 nm_wr(sc, base + NM_RATE_REG_OFFSET, x, 1); 758 759 switch (p->encoding) { 760 case AUDIO_ENCODING_SLINEAR_LE: 761 if (p->precision != 16) 762 return EINVAL; 763 break; 764 case AUDIO_ENCODING_ULINEAR_LE: 765 case AUDIO_ENCODING_ULINEAR_BE: 766 if (p->precision != 8) 767 return EINVAL; 768 break; 769 default: 770 return (EINVAL); 771 } 772 p->bps = AUDIO_BPS(p->precision); 773 p->msb = 1; 774 } 775 776 return (0); 777 } 778 779 int 780 neo_round_blocksize(void *addr, int blk) 781 { 782 return (NM_BUFFSIZE / 2); 783 } 784 785 int 786 neo_trigger_output(void *addr, void *start, void *end, int blksize, 787 void (*intr)(void *), void *arg, struct audio_params *param) 788 { 789 struct neo_softc *sc = addr; 790 int ssz; 791 792 mtx_enter(&audio_lock); 793 sc->pintr = intr; 794 sc->parg = arg; 795 796 ssz = (param->precision == 16) ? 2 : 1; 797 if (param->channels == 2) 798 ssz <<= 1; 799 800 sc->pbufsize = ((char *)end - (char *)start); 801 sc->pblksize = blksize; 802 sc->pwmark = blksize; 803 nm_wr(sc, NM_PBUFFER_START, sc->pbuf, 4); 804 nm_wr(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz, 4); 805 nm_wr(sc, NM_PBUFFER_CURRP, sc->pbuf, 4); 806 nm_wr(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark, 4); 807 nm_wr(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN | 808 NM_PLAYBACK_ENABLE_FLAG, 1); 809 nm_wr(sc, NM_AUDIO_MUTE_REG, 0, 2); 810 mtx_leave(&audio_lock); 811 return (0); 812 } 813 814 815 816 int 817 neo_trigger_input(void *addr, void *start, void *end, int blksize, 818 void (*intr)(void *), void *arg, struct audio_params *param) 819 { 820 struct neo_softc *sc = addr; 821 int ssz; 822 823 mtx_enter(&audio_lock); 824 sc->rintr = intr; 825 sc->rarg = arg; 826 827 ssz = (param->precision == 16) ? 2 : 1; 828 if (param->channels == 2) 829 ssz <<= 1; 830 831 sc->rbufsize = ((char *)end - (char *)start); 832 sc->rblksize = blksize; 833 sc->rwmark = blksize; 834 nm_wr(sc, NM_RBUFFER_START, sc->rbuf, 4); 835 nm_wr(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize, 4); 836 nm_wr(sc, NM_RBUFFER_CURRP, sc->rbuf, 4); 837 nm_wr(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark, 4); 838 nm_wr(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN | 839 NM_RECORD_ENABLE_FLAG, 1); 840 mtx_leave(&audio_lock); 841 return (0); 842 } 843 844 int 845 neo_halt_output(void *addr) 846 { 847 struct neo_softc *sc = (struct neo_softc *)addr; 848 849 mtx_enter(&audio_lock); 850 nm_wr(sc, NM_PLAYBACK_ENABLE_REG, 0, 1); 851 nm_wr(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH, 2); 852 853 sc->pintr = 0; 854 mtx_leave(&audio_lock); 855 return (0); 856 } 857 858 int 859 neo_halt_input(void *addr) 860 { 861 struct neo_softc *sc = (struct neo_softc *)addr; 862 863 mtx_enter(&audio_lock); 864 nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1); 865 866 sc->rintr = 0; 867 mtx_leave(&audio_lock); 868 return (0); 869 } 870 871 int 872 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp) 873 { 874 struct neo_softc *sc = addr; 875 876 return ((sc->codec_if->vtbl->mixer_set_port)(sc->codec_if, cp)); 877 } 878 879 int 880 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp) 881 { 882 struct neo_softc *sc = addr; 883 884 return ((sc->codec_if->vtbl->mixer_get_port)(sc->codec_if, cp)); 885 } 886 887 int 888 neo_query_devinfo(void *addr, mixer_devinfo_t *dip) 889 { 890 struct neo_softc *sc = addr; 891 892 return ((sc->codec_if->vtbl->query_devinfo)(sc->codec_if, dip)); 893 } 894 895 void * 896 neo_malloc(void *addr, int direction, size_t size, int pool, int flags) 897 { 898 struct neo_softc *sc = addr; 899 void *rv = 0; 900 901 switch (direction) { 902 case AUMODE_PLAY: 903 rv = (char *)sc->bufioh + sc->pbuf; 904 break; 905 case AUMODE_RECORD: 906 rv = (char *)sc->bufioh + sc->rbuf; 907 break; 908 default: 909 break; 910 } 911 912 return (rv); 913 } 914 915 void 916 neo_free(void *addr, void *ptr, int pool) 917 { 918 return; 919 } 920 921 size_t 922 neo_round_buffersize(void *addr, int direction, size_t size) 923 { 924 return (NM_BUFFSIZE); 925 } 926