1 /* $OpenBSD: neo.c,v 1.32 2016/09/19 06:46:44 ratchov 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 * privelege 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 int neo_get_props(void *); 203 void neo_set_mixer(struct neo_softc *sc, int a, int d); 204 205 struct cfdriver neo_cd = { 206 NULL, "neo", DV_DULL 207 }; 208 209 210 struct cfattach neo_ca = { 211 sizeof(struct neo_softc), neo_match, neo_attach, NULL, 212 neo_activate 213 }; 214 215 216 #if 0 217 static u_int32_t badcards[] = { 218 0x0007103c, 219 0x008f1028, 220 }; 221 #endif 222 223 #define NUM_BADCARDS (sizeof(badcards) / sizeof(u_int32_t)) 224 225 /* The actual rates supported by the card. */ 226 static int samplerates[9] = { 227 8000, 228 11025, 229 16000, 230 22050, 231 24000, 232 32000, 233 44100, 234 48000, 235 99999999 236 }; 237 238 /* -------------------------------------------------------------------- */ 239 240 struct audio_hw_if neo_hw_if = { 241 neo_open, 242 neo_close, 243 neo_set_params, 244 #if 1 245 neo_round_blocksize, 246 #else 247 NULL, 248 #endif 249 NULL, 250 NULL, 251 NULL, 252 NULL, 253 NULL, 254 neo_halt_output, 255 neo_halt_input, 256 NULL, 257 NULL, 258 neo_mixer_set_port, 259 neo_mixer_get_port, 260 neo_query_devinfo, 261 neo_malloc, 262 neo_free, 263 neo_round_buffersize, 264 neo_get_props, 265 neo_trigger_output, 266 neo_trigger_input 267 268 }; 269 270 /* -------------------------------------------------------------------- */ 271 272 /* Hardware */ 273 static u_int32_t 274 nm_rd(struct neo_softc *sc, int regno, int size) 275 { 276 bus_space_tag_t st = sc->regiot; 277 bus_space_handle_t sh = sc->regioh; 278 279 switch (size) { 280 case 1: 281 return bus_space_read_1(st, sh, regno); 282 case 2: 283 return bus_space_read_2(st, sh, regno); 284 case 4: 285 return bus_space_read_4(st, sh, regno); 286 default: 287 return (0xffffffff); 288 } 289 } 290 291 static void 292 nm_wr(struct neo_softc *sc, int regno, u_int32_t data, int size) 293 { 294 bus_space_tag_t st = sc->regiot; 295 bus_space_handle_t sh = sc->regioh; 296 297 switch (size) { 298 case 1: 299 bus_space_write_1(st, sh, regno, data); 300 break; 301 case 2: 302 bus_space_write_2(st, sh, regno, data); 303 break; 304 case 4: 305 bus_space_write_4(st, sh, regno, data); 306 break; 307 } 308 } 309 310 static u_int32_t 311 nm_rdbuf(struct neo_softc *sc, int regno, int size) 312 { 313 bus_space_tag_t st = sc->bufiot; 314 bus_space_handle_t sh = sc->bufioh; 315 316 switch (size) { 317 case 1: 318 return bus_space_read_1(st, sh, regno); 319 case 2: 320 return bus_space_read_2(st, sh, regno); 321 case 4: 322 return bus_space_read_4(st, sh, regno); 323 default: 324 return (0xffffffff); 325 } 326 } 327 328 static void 329 nm_wrbuf(struct neo_softc *sc, int regno, u_int32_t data, int size) 330 { 331 bus_space_tag_t st = sc->bufiot; 332 bus_space_handle_t sh = sc->bufioh; 333 334 switch (size) { 335 case 1: 336 bus_space_write_1(st, sh, regno, data); 337 break; 338 case 2: 339 bus_space_write_2(st, sh, regno, data); 340 break; 341 case 4: 342 bus_space_write_4(st, sh, regno, data); 343 break; 344 } 345 } 346 347 /* ac97 codec */ 348 static int 349 nm_waitcd(struct neo_softc *sc) 350 { 351 int cnt = 10; 352 int fail = 1; 353 354 while (cnt-- > 0) { 355 if (nm_rd(sc, sc->ac97_status, 2) & sc->ac97_busy) 356 DELAY(100); 357 else { 358 fail = 0; 359 break; 360 } 361 } 362 return (fail); 363 } 364 365 366 static void 367 nm_ackint(struct neo_softc *sc, u_int32_t num) 368 { 369 if (sc->type == NM256AV_PCI_ID) 370 nm_wr(sc, NM_INT_REG, num << 1, 2); 371 else if (sc->type == NM256ZX_PCI_ID) 372 nm_wr(sc, NM_INT_REG, num, 4); 373 } 374 375 static int 376 nm_loadcoeff(struct neo_softc *sc, int dir, int num) 377 { 378 int ofs, sz, i; 379 u_int32_t addr; 380 381 if (nf == NULL) { 382 size_t buflen; 383 u_char *buf; 384 int error; 385 386 error = loadfirmware("neo-coefficients", &buf, &buflen); 387 if (error) 388 return (error); 389 nf = (struct neo_firmware *)buf; 390 } 391 392 addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c; 393 if (dir == AUMODE_RECORD) 394 num += 8; 395 sz = nf->coefficientSizes[num]; 396 ofs = 0; 397 while (num-- > 0) 398 ofs+= nf->coefficientSizes[num]; 399 for (i = 0; i < sz; i++) 400 nm_wrbuf(sc, sc->cbuf + i, nf->coefficients[ofs + i], 1); 401 nm_wr(sc, addr, sc->cbuf, 4); 402 if (dir == AUMODE_PLAY) 403 sz--; 404 nm_wr(sc, addr + 4, sc->cbuf + sz, 4); 405 return (0); 406 } 407 408 int 409 nmchan_getptr(struct neo_softc *sc, int mode) 410 { 411 if (mode == AUMODE_PLAY) 412 return (nm_rd(sc, NM_PBUFFER_CURRP, 4) - sc->pbuf); 413 else 414 return (nm_rd(sc, NM_RBUFFER_CURRP, 4) - sc->rbuf); 415 } 416 417 418 /* The interrupt handler */ 419 int 420 neo_intr(void *p) 421 { 422 struct neo_softc *sc = (struct neo_softc *)p; 423 int status, x; 424 int rv = 0; 425 426 mtx_enter(&audio_lock); 427 status = nm_rd(sc, NM_INT_REG, sc->irsz); 428 429 if (status & sc->playint) { 430 status &= ~sc->playint; 431 432 sc->pwmark += sc->pblksize; 433 sc->pwmark %= sc->pbufsize; 434 435 nm_wr(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark, 4); 436 437 nm_ackint(sc, sc->playint); 438 439 if (sc->pintr) 440 (*sc->pintr)(sc->parg); 441 442 rv = 1; 443 } 444 if (status & sc->recint) { 445 status &= ~sc->recint; 446 447 sc->rwmark += sc->rblksize; 448 sc->rwmark %= sc->rbufsize; 449 450 nm_ackint(sc, sc->recint); 451 if (sc->rintr) 452 (*sc->rintr)(sc->rarg); 453 454 rv = 1; 455 } 456 if (status & sc->misc1int) { 457 status &= ~sc->misc1int; 458 nm_ackint(sc, sc->misc1int); 459 x = nm_rd(sc, 0x400, 1); 460 nm_wr(sc, 0x400, x | 2, 1); 461 printf("%s: misc int 1\n", sc->dev.dv_xname); 462 rv = 1; 463 } 464 if (status & sc->misc2int) { 465 status &= ~sc->misc2int; 466 nm_ackint(sc, sc->misc2int); 467 x = nm_rd(sc, 0x400, 1); 468 nm_wr(sc, 0x400, x & ~2, 1); 469 printf("%s: misc int 2\n", sc->dev.dv_xname); 470 rv = 1; 471 } 472 if (status) { 473 status &= ~sc->misc2int; 474 nm_ackint(sc, sc->misc2int); 475 printf("%s: unknown int\n", sc->dev.dv_xname); 476 rv = 1; 477 } 478 mtx_leave(&audio_lock); 479 return (rv); 480 } 481 482 /* -------------------------------------------------------------------- */ 483 484 /* 485 * Probe and attach the card 486 */ 487 488 static int 489 nm_init(struct neo_softc *sc) 490 { 491 u_int32_t ofs, i; 492 493 if (sc->type == NM256AV_PCI_ID) { 494 sc->ac97_base = NM_MIXER_OFFSET; 495 sc->ac97_status = NM_MIXER_STATUS_OFFSET; 496 sc->ac97_busy = NM_MIXER_READY_MASK; 497 498 sc->buftop = 2560 * 1024; 499 500 sc->irsz = 2; 501 sc->playint = NM_PLAYBACK_INT; 502 sc->recint = NM_RECORD_INT; 503 sc->misc1int = NM_MISC_INT_1; 504 sc->misc2int = NM_MISC_INT_2; 505 } else if (sc->type == NM256ZX_PCI_ID) { 506 sc->ac97_base = NM_MIXER_OFFSET; 507 sc->ac97_status = NM2_MIXER_STATUS_OFFSET; 508 sc->ac97_busy = NM2_MIXER_READY_MASK; 509 510 sc->buftop = (nm_rd(sc, 0xa0b, 2)? 6144 : 4096) * 1024; 511 512 sc->irsz = 4; 513 sc->playint = NM2_PLAYBACK_INT; 514 sc->recint = NM2_RECORD_INT; 515 sc->misc1int = NM2_MISC_INT_1; 516 sc->misc2int = NM2_MISC_INT_2; 517 } else return -1; 518 sc->badintr = 0; 519 ofs = sc->buftop - 0x0400; 520 sc->buftop -= 0x1400; 521 522 if ((nm_rdbuf(sc, ofs, 4) & NM_SIG_MASK) == NM_SIGNATURE) { 523 i = nm_rdbuf(sc, ofs + 4, 4); 524 if (i != 0 && i != 0xffffffff) 525 sc->buftop = i; 526 } 527 528 sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT; 529 sc->rbuf = sc->cbuf - NM_BUFFSIZE; 530 sc->pbuf = sc->rbuf - NM_BUFFSIZE; 531 sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4); 532 533 nm_wr(sc, 0, 0x11, 1); 534 nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1); 535 nm_wr(sc, 0x214, 0, 2); 536 537 return 0; 538 } 539 540 541 void 542 neo_attach(struct device *parent, struct device *self, void *aux) 543 { 544 struct neo_softc *sc = (struct neo_softc *)self; 545 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 546 pci_chipset_tag_t pc = pa->pa_pc; 547 char const *intrstr; 548 pci_intr_handle_t ih; 549 int error; 550 551 sc->type = pa->pa_id; 552 553 /* Map I/O register */ 554 if (pci_mapreg_map(pa, PCI_MAPS, PCI_MAPREG_TYPE_MEM, 0, 555 &sc->bufiot, &sc->bufioh, NULL, NULL, 0)) { 556 printf("\n%s: can't map i/o space\n", sc->dev.dv_xname); 557 return; 558 } 559 560 561 if (pci_mapreg_map(pa, PCI_MAPS + 4, PCI_MAPREG_TYPE_MEM, 0, 562 &sc->regiot, &sc->regioh, NULL, NULL, 0)) { 563 printf("\n%s: can't map i/o space\n", sc->dev.dv_xname); 564 return; 565 } 566 567 /* Map and establish the interrupt. */ 568 if (pci_intr_map(pa, &ih)) { 569 printf("\n%s: couldn't map interrupt\n", sc->dev.dv_xname); 570 return; 571 } 572 intrstr = pci_intr_string(pc, ih); 573 sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO | IPL_MPSAFE, 574 neo_intr, sc, sc->dev.dv_xname); 575 576 if (sc->ih == NULL) { 577 printf("\n%s: couldn't establish interrupt", 578 sc->dev.dv_xname); 579 if (intrstr != NULL) 580 printf(" at %s", intrstr); 581 printf("\n"); 582 return; 583 } 584 printf(": %s\n", intrstr); 585 586 if ((error = nm_init(sc)) != 0) 587 return; 588 589 sc->host_if.arg = sc; 590 591 sc->host_if.attach = neo_attach_codec; 592 sc->host_if.read = neo_read_codec; 593 sc->host_if.write = neo_write_codec; 594 sc->host_if.reset = neo_reset_codec; 595 sc->host_if.flags = neo_flags_codec; 596 597 if ((error = ac97_attach(&sc->host_if)) != 0) 598 return; 599 600 audio_attach_mi(&neo_hw_if, sc, &sc->dev); 601 602 return; 603 } 604 605 int 606 neo_activate(struct device *self, int act) 607 { 608 struct neo_softc *sc = (struct neo_softc *)self; 609 610 switch (act) { 611 case DVACT_SUSPEND: 612 break; 613 case DVACT_RESUME: 614 nm_init(sc); 615 (sc->codec_if->vtbl->restore_ports)(sc->codec_if); 616 break; 617 } 618 return 0; 619 } 620 621 int 622 neo_match(struct device *parent, void *match, void *aux) 623 { 624 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 625 #if 0 626 u_int32_t subdev, badcard; 627 #endif 628 629 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC) 630 return (0); 631 632 #if 0 633 subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev); 634 #endif 635 switch (PCI_PRODUCT(pa->pa_id)) { 636 case PCI_PRODUCT_NEOMAGIC_NM256AV: 637 #if 0 638 i = 0; 639 while ((i < NUM_BADCARDS) && (badcards[i] != subdev)) 640 i++; 641 if (i == NUM_BADCARDS) 642 s = "NeoMagic 256AV"; 643 DEB(else) 644 DEB(device_printf(dev, "this is a non-ac97 NM256AV, not attaching\n")); 645 return (1); 646 #endif 647 case PCI_PRODUCT_NEOMAGIC_NM256ZX: 648 return (1); 649 } 650 651 return (0); 652 } 653 654 int 655 neo_read_codec(void *sc_, u_int8_t a, u_int16_t *d) 656 { 657 struct neo_softc *sc = sc_; 658 659 if (!nm_waitcd(sc)) { 660 *d = nm_rd(sc, sc->ac97_base + a, 2); 661 DELAY(1000); 662 return 0; 663 } 664 665 return (ENXIO); 666 } 667 668 669 int 670 neo_write_codec(void *sc_, u_int8_t a, u_int16_t d) 671 { 672 struct neo_softc *sc = sc_; 673 int cnt = 3; 674 675 if (!nm_waitcd(sc)) { 676 while (cnt-- > 0) { 677 nm_wr(sc, sc->ac97_base + a, d, 2); 678 if (!nm_waitcd(sc)) { 679 DELAY(1000); 680 return (0); 681 } 682 } 683 } 684 685 return (ENXIO); 686 } 687 688 689 int 690 neo_attach_codec(void *sc_, struct ac97_codec_if *codec_if) 691 { 692 struct neo_softc *sc = sc_; 693 694 sc->codec_if = codec_if; 695 return (0); 696 } 697 698 void 699 neo_reset_codec(void *sc) 700 { 701 nm_wr(sc, 0x6c0, 0x01, 1); 702 nm_wr(sc, 0x6cc, 0x87, 1); 703 nm_wr(sc, 0x6cc, 0x80, 1); 704 nm_wr(sc, 0x6cc, 0x00, 1); 705 706 return; 707 } 708 709 710 enum ac97_host_flags 711 neo_flags_codec(void *sc) 712 { 713 return (AC97_HOST_DONT_READANY); 714 } 715 716 int 717 neo_open(void *addr, int flags) 718 { 719 return (0); 720 } 721 722 /* 723 * Close function is called at splaudio(). 724 */ 725 void 726 neo_close(void *addr) 727 { 728 struct neo_softc *sc = addr; 729 730 neo_halt_output(sc); 731 neo_halt_input(sc); 732 733 sc->pintr = 0; 734 sc->rintr = 0; 735 } 736 737 /* Todo: don't commit settings to card until we've verified all parameters */ 738 int 739 neo_set_params(void *addr, int setmode, int usemode, 740 struct audio_params *play, struct audio_params *rec) 741 { 742 struct neo_softc *sc = addr; 743 u_int32_t base; 744 u_int8_t x; 745 int mode; 746 struct audio_params *p; 747 748 for (mode = AUMODE_RECORD; mode != -1; 749 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 750 if ((setmode & mode) == 0) 751 continue; 752 753 p = mode == AUMODE_PLAY ? play : rec; 754 755 if (p == NULL) continue; 756 757 for (x = 0; x < 8; x++) 758 if (p->sample_rate < (samplerates[x] + samplerates[x + 1]) / 2) 759 break; 760 761 p->sample_rate = samplerates[x]; 762 nm_loadcoeff(sc, mode, x); 763 764 x <<= 4; 765 x &= NM_RATE_MASK; 766 if (p->precision == 16) x |= NM_RATE_BITS_16; 767 if (p->channels == 2) x |= NM_RATE_STEREO; 768 769 base = (mode == AUMODE_PLAY) ? 770 NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET; 771 nm_wr(sc, base + NM_RATE_REG_OFFSET, x, 1); 772 773 switch (p->encoding) { 774 case AUDIO_ENCODING_SLINEAR_LE: 775 if (p->precision != 16) 776 return EINVAL; 777 break; 778 case AUDIO_ENCODING_ULINEAR_LE: 779 case AUDIO_ENCODING_ULINEAR_BE: 780 if (p->precision != 8) 781 return EINVAL; 782 break; 783 default: 784 return (EINVAL); 785 } 786 p->bps = AUDIO_BPS(p->precision); 787 p->msb = 1; 788 } 789 790 return (0); 791 } 792 793 int 794 neo_round_blocksize(void *addr, int blk) 795 { 796 return (NM_BUFFSIZE / 2); 797 } 798 799 int 800 neo_trigger_output(void *addr, void *start, void *end, int blksize, 801 void (*intr)(void *), void *arg, struct audio_params *param) 802 { 803 struct neo_softc *sc = addr; 804 int ssz; 805 806 mtx_enter(&audio_lock); 807 sc->pintr = intr; 808 sc->parg = arg; 809 810 ssz = (param->precision == 16) ? 2 : 1; 811 if (param->channels == 2) 812 ssz <<= 1; 813 814 sc->pbufsize = ((char *)end - (char *)start); 815 sc->pblksize = blksize; 816 sc->pwmark = blksize; 817 nm_wr(sc, NM_PBUFFER_START, sc->pbuf, 4); 818 nm_wr(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz, 4); 819 nm_wr(sc, NM_PBUFFER_CURRP, sc->pbuf, 4); 820 nm_wr(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark, 4); 821 nm_wr(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN | 822 NM_PLAYBACK_ENABLE_FLAG, 1); 823 nm_wr(sc, NM_AUDIO_MUTE_REG, 0, 2); 824 mtx_leave(&audio_lock); 825 return (0); 826 } 827 828 829 830 int 831 neo_trigger_input(void *addr, void *start, void *end, int blksize, 832 void (*intr)(void *), void *arg, struct audio_params *param) 833 { 834 struct neo_softc *sc = addr; 835 int ssz; 836 837 mtx_enter(&audio_lock); 838 sc->rintr = intr; 839 sc->rarg = arg; 840 841 ssz = (param->precision == 16) ? 2 : 1; 842 if (param->channels == 2) 843 ssz <<= 1; 844 845 sc->rbufsize = ((char *)end - (char *)start); 846 sc->rblksize = blksize; 847 sc->rwmark = blksize; 848 nm_wr(sc, NM_RBUFFER_START, sc->rbuf, 4); 849 nm_wr(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize, 4); 850 nm_wr(sc, NM_RBUFFER_CURRP, sc->rbuf, 4); 851 nm_wr(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark, 4); 852 nm_wr(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN | 853 NM_RECORD_ENABLE_FLAG, 1); 854 mtx_leave(&audio_lock); 855 return (0); 856 } 857 858 int 859 neo_halt_output(void *addr) 860 { 861 struct neo_softc *sc = (struct neo_softc *)addr; 862 863 mtx_enter(&audio_lock); 864 nm_wr(sc, NM_PLAYBACK_ENABLE_REG, 0, 1); 865 nm_wr(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH, 2); 866 867 sc->pintr = 0; 868 mtx_leave(&audio_lock); 869 return (0); 870 } 871 872 int 873 neo_halt_input(void *addr) 874 { 875 struct neo_softc *sc = (struct neo_softc *)addr; 876 877 mtx_enter(&audio_lock); 878 nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1); 879 880 sc->rintr = 0; 881 mtx_leave(&audio_lock); 882 return (0); 883 } 884 885 int 886 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp) 887 { 888 struct neo_softc *sc = addr; 889 890 return ((sc->codec_if->vtbl->mixer_set_port)(sc->codec_if, cp)); 891 } 892 893 int 894 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp) 895 { 896 struct neo_softc *sc = addr; 897 898 return ((sc->codec_if->vtbl->mixer_get_port)(sc->codec_if, cp)); 899 } 900 901 int 902 neo_query_devinfo(void *addr, mixer_devinfo_t *dip) 903 { 904 struct neo_softc *sc = addr; 905 906 return ((sc->codec_if->vtbl->query_devinfo)(sc->codec_if, dip)); 907 } 908 909 void * 910 neo_malloc(void *addr, int direction, size_t size, int pool, int flags) 911 { 912 struct neo_softc *sc = addr; 913 void *rv = 0; 914 915 switch (direction) { 916 case AUMODE_PLAY: 917 rv = (char *)sc->bufioh + sc->pbuf; 918 break; 919 case AUMODE_RECORD: 920 rv = (char *)sc->bufioh + sc->rbuf; 921 break; 922 default: 923 break; 924 } 925 926 return (rv); 927 } 928 929 void 930 neo_free(void *addr, void *ptr, int pool) 931 { 932 return; 933 } 934 935 size_t 936 neo_round_buffersize(void *addr, int direction, size_t size) 937 { 938 return (NM_BUFFSIZE); 939 } 940 941 942 int 943 neo_get_props(void *addr) 944 { 945 return (AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX); 946 } 947