1 /* $NetBSD: sv.c,v 1.11 2000/06/26 04:56:25 simonb Exp $ */ 2 /* $OpenBSD: sv.c,v 1.2 1998/07/13 01:50:15 csapuntz Exp $ */ 3 4 /* 5 * Copyright (c) 1999 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1998 Constantine Paul Sapuntzakis 42 * All rights reserved 43 * 44 * Author: Constantine Paul Sapuntzakis (csapuntz@cvs.openbsd.org) 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. The author's name or those of the contributors may be used to 55 * endorse or promote products derived from this software without 56 * specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS 59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 68 * POSSIBILITY OF SUCH DAMAGE. 69 */ 70 71 /* 72 * S3 SonicVibes driver 73 * Heavily based on the eap driver by Lennart Augustsson 74 */ 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/kernel.h> 79 #include <sys/malloc.h> 80 #include <sys/device.h> 81 82 #include <dev/pci/pcireg.h> 83 #include <dev/pci/pcivar.h> 84 #include <dev/pci/pcidevs.h> 85 86 #include <sys/audioio.h> 87 #include <dev/audio_if.h> 88 #include <dev/mulaw.h> 89 #include <dev/auconv.h> 90 91 #include <dev/ic/i8237reg.h> 92 #include <dev/pci/svreg.h> 93 #include <dev/pci/svvar.h> 94 95 #include <machine/bus.h> 96 97 #ifdef AUDIO_DEBUG 98 #define DPRINTF(x) if (svdebug) printf x 99 #define DPRINTFN(n,x) if (svdebug>(n)) printf x 100 int svdebug = 0; 101 #else 102 #define DPRINTF(x) 103 #define DPRINTFN(n,x) 104 #endif 105 106 int sv_match __P((struct device *, struct cfdata *, void *)); 107 void sv_attach __P((struct device *, struct device *, void *)); 108 int sv_intr __P((void *)); 109 110 struct sv_dma { 111 bus_dmamap_t map; 112 caddr_t addr; 113 bus_dma_segment_t segs[1]; 114 int nsegs; 115 size_t size; 116 struct sv_dma *next; 117 }; 118 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr) 119 #define KERNADDR(p) ((void *)((p)->addr)) 120 121 struct cfattach sv_ca = { 122 sizeof(struct sv_softc), sv_match, sv_attach 123 }; 124 125 struct audio_device sv_device = { 126 "S3 SonicVibes", 127 "", 128 "sv" 129 }; 130 131 #define ARRAY_SIZE(foo) ((sizeof(foo)) / sizeof(foo[0])) 132 133 int sv_allocmem __P((struct sv_softc *, size_t, size_t, struct sv_dma *)); 134 int sv_freemem __P((struct sv_softc *, struct sv_dma *)); 135 136 int sv_open __P((void *, int)); 137 void sv_close __P((void *)); 138 int sv_query_encoding __P((void *, struct audio_encoding *)); 139 int sv_set_params __P((void *, int, int, struct audio_params *, struct audio_params *)); 140 int sv_round_blocksize __P((void *, int)); 141 int sv_trigger_output __P((void *, void *, void *, int, void (*)(void *), 142 void *, struct audio_params *)); 143 int sv_trigger_input __P((void *, void *, void *, int, void (*)(void *), 144 void *, struct audio_params *)); 145 int sv_halt_output __P((void *)); 146 int sv_halt_input __P((void *)); 147 int sv_getdev __P((void *, struct audio_device *)); 148 int sv_mixer_set_port __P((void *, mixer_ctrl_t *)); 149 int sv_mixer_get_port __P((void *, mixer_ctrl_t *)); 150 int sv_query_devinfo __P((void *, mixer_devinfo_t *)); 151 void *sv_malloc __P((void *, int, size_t, int, int)); 152 void sv_free __P((void *, void *, int)); 153 size_t sv_round_buffersize __P((void *, int, size_t)); 154 paddr_t sv_mappage __P((void *, void *, off_t, int)); 155 int sv_get_props __P((void *)); 156 157 #ifdef AUDIO_DEBUG 158 void sv_dumpregs __P((struct sv_softc *sc)); 159 #endif 160 161 struct audio_hw_if sv_hw_if = { 162 sv_open, 163 sv_close, 164 NULL, 165 sv_query_encoding, 166 sv_set_params, 167 sv_round_blocksize, 168 NULL, 169 NULL, 170 NULL, 171 NULL, 172 NULL, 173 sv_halt_output, 174 sv_halt_input, 175 NULL, 176 sv_getdev, 177 NULL, 178 sv_mixer_set_port, 179 sv_mixer_get_port, 180 sv_query_devinfo, 181 sv_malloc, 182 sv_free, 183 sv_round_buffersize, 184 sv_mappage, 185 sv_get_props, 186 sv_trigger_output, 187 sv_trigger_input, 188 }; 189 190 191 static u_int8_t sv_read __P((struct sv_softc *, u_int8_t)); 192 static u_int8_t sv_read_indirect __P((struct sv_softc *, u_int8_t)); 193 static void sv_write __P((struct sv_softc *, u_int8_t, u_int8_t )); 194 static void sv_write_indirect __P((struct sv_softc *, u_int8_t, u_int8_t )); 195 static void sv_init_mixer __P((struct sv_softc *)); 196 197 static void sv_defer __P((struct device *self)); 198 199 static void 200 sv_write (sc, reg, val) 201 struct sv_softc *sc; 202 u_int8_t reg, val; 203 204 { 205 DPRINTFN(8,("sv_write(0x%x, 0x%x)\n", reg, val)); 206 bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val); 207 } 208 209 static u_int8_t 210 sv_read(sc, reg) 211 struct sv_softc *sc; 212 u_int8_t reg; 213 214 { 215 u_int8_t val; 216 217 val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, reg); 218 DPRINTFN(8,("sv_read(0x%x) = 0x%x\n", reg, val)); 219 return val; 220 } 221 222 static u_int8_t 223 sv_read_indirect(sc, reg) 224 struct sv_softc *sc; 225 u_int8_t reg; 226 { 227 u_int8_t val; 228 int s = splaudio(); 229 230 sv_write(sc, SV_CODEC_IADDR, reg & SV_IADDR_MASK); 231 val = sv_read(sc, SV_CODEC_IDATA); 232 splx(s); 233 return (val); 234 } 235 236 static void 237 sv_write_indirect(sc, reg, val) 238 struct sv_softc *sc; 239 u_int8_t reg, val; 240 { 241 u_int8_t iaddr = reg & SV_IADDR_MASK; 242 int s = splaudio(); 243 244 if (reg == SV_DMA_DATA_FORMAT) 245 iaddr |= SV_IADDR_MCE; 246 247 sv_write(sc, SV_CODEC_IADDR, iaddr); 248 sv_write(sc, SV_CODEC_IDATA, val); 249 splx(s); 250 } 251 252 int 253 sv_match(parent, match, aux) 254 struct device *parent; 255 struct cfdata *match; 256 void *aux; 257 { 258 struct pci_attach_args *pa = aux; 259 260 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_S3 && 261 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_S3_SONICVIBES) 262 return (1); 263 264 return (0); 265 } 266 267 int pci_alloc_io __P((pci_chipset_tag_t pc, pcitag_t pt, 268 int pcioffs, 269 bus_space_tag_t iot, bus_size_t size, 270 bus_size_t align, bus_size_t bound, int flags, 271 bus_space_handle_t *ioh)); 272 273 #define PCI_IO_ALLOC_LOW 0xa000 274 #define PCI_IO_ALLOC_HIGH 0xb000 275 int 276 pci_alloc_io(pc, pt, pcioffs, iot, size, align, bound, flags, ioh) 277 pci_chipset_tag_t pc; 278 pcitag_t pt; 279 int pcioffs; 280 bus_space_tag_t iot; 281 bus_size_t size; 282 bus_size_t align; 283 bus_size_t bound; 284 int flags; 285 bus_space_handle_t *ioh; 286 { 287 bus_addr_t addr; 288 int error; 289 290 error = bus_space_alloc(iot, PCI_IO_ALLOC_LOW, PCI_IO_ALLOC_HIGH, 291 size, align, bound, flags, &addr, ioh); 292 if (error) 293 return(error); 294 295 pci_conf_write(pc, pt, pcioffs, addr); 296 return (0); 297 } 298 299 /* 300 * Allocate IO addresses when all other configuration is done. 301 */ 302 void 303 sv_defer(self) 304 struct device *self; 305 { 306 struct sv_softc *sc = (struct sv_softc *)self; 307 pci_chipset_tag_t pc = sc->sc_pa.pa_pc; 308 pcitag_t pt = sc->sc_pa.pa_tag; 309 pcireg_t dmaio; 310 311 DPRINTF(("sv_defer: %p\n", sc)); 312 if (pci_alloc_io(pc, pt, SV_DMAA_CONFIG_OFF, 313 sc->sc_iot, SV_DMAA_SIZE, SV_DMAA_ALIGN, 0, 314 0, &sc->sc_dmaa_ioh)) { 315 printf("sv_attach: cannot allocate DMA A range\n"); 316 return; 317 } 318 dmaio = pci_conf_read(pc, pt, SV_DMAA_CONFIG_OFF); 319 DPRINTF(("sv_attach: addr a dmaio=0x%lx\n", (u_long)dmaio)); 320 pci_conf_write(pc, pt, SV_DMAA_CONFIG_OFF, 321 dmaio | SV_DMA_CHANNEL_ENABLE | SV_DMAA_EXTENDED_ADDR); 322 323 if (pci_alloc_io(pc, pt, SV_DMAC_CONFIG_OFF, 324 sc->sc_iot, SV_DMAC_SIZE, SV_DMAC_ALIGN, 0, 325 0, &sc->sc_dmac_ioh)) { 326 printf("sv_attach: cannot allocate DMA C range\n"); 327 return; 328 } 329 dmaio = pci_conf_read(pc, pt, SV_DMAC_CONFIG_OFF); 330 DPRINTF(("sv_attach: addr c dmaio=0x%lx\n", (u_long)dmaio)); 331 pci_conf_write(pc, pt, SV_DMAC_CONFIG_OFF, 332 dmaio | SV_DMA_CHANNEL_ENABLE); 333 334 sc->sc_dmaset = 1; 335 } 336 337 void 338 sv_attach(parent, self, aux) 339 struct device *parent, *self; 340 void *aux; 341 { 342 struct sv_softc *sc = (struct sv_softc *)self; 343 struct pci_attach_args *pa = aux; 344 pci_chipset_tag_t pc = pa->pa_pc; 345 pcitag_t pt = pa->pa_tag; 346 pci_intr_handle_t ih; 347 pcireg_t csr; 348 char const *intrstr; 349 u_int8_t reg; 350 struct audio_attach_args arg; 351 352 printf ("\n"); 353 354 /* Map I/O registers */ 355 if (pci_mapreg_map(pa, SV_ENHANCED_PORTBASE_SLOT, 356 PCI_MAPREG_TYPE_IO, 0, 357 &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) { 358 printf("%s: can't map enhanced i/o space\n", 359 sc->sc_dev.dv_xname); 360 return; 361 } 362 if (pci_mapreg_map(pa, SV_FM_PORTBASE_SLOT, 363 PCI_MAPREG_TYPE_IO, 0, 364 &sc->sc_opliot, &sc->sc_oplioh, NULL, NULL)) { 365 printf("%s: can't map FM i/o space\n", sc->sc_dev.dv_xname); 366 return; 367 } 368 if (pci_mapreg_map(pa, SV_MIDI_PORTBASE_SLOT, 369 PCI_MAPREG_TYPE_IO, 0, 370 &sc->sc_midiiot, &sc->sc_midiioh, NULL, NULL)) { 371 printf("%s: can't map MIDI i/o space\n", sc->sc_dev.dv_xname); 372 return; 373 } 374 DPRINTF(("sv: IO ports: enhanced=0x%x, OPL=0x%x, MIDI=0x%x\n", 375 (int)sc->sc_ioh, (int)sc->sc_oplioh, (int)sc->sc_midiioh)); 376 377 #ifdef alpha 378 /* XXX Force allocation through the SGMAP. */ 379 sc->sc_dmatag = alphabus_dma_get_tag(pa->pa_dmat, ALPHA_BUS_ISA); 380 #else 381 sc->sc_dmatag = pa->pa_dmat; 382 #endif 383 384 pci_conf_write(pc, pt, SV_DMAA_CONFIG_OFF, SV_DMAA_EXTENDED_ADDR); 385 pci_conf_write(pc, pt, SV_DMAC_CONFIG_OFF, 0); 386 387 /* Enable the device. */ 388 csr = pci_conf_read(pc, pt, PCI_COMMAND_STATUS_REG); 389 pci_conf_write(pc, pt, PCI_COMMAND_STATUS_REG, 390 csr | PCI_COMMAND_MASTER_ENABLE); 391 392 sv_write_indirect(sc, SV_ANALOG_POWER_DOWN_CONTROL, 0); 393 sv_write_indirect(sc, SV_DIGITAL_POWER_DOWN_CONTROL, 0); 394 395 /* initialize codec registers */ 396 reg = sv_read(sc, SV_CODEC_CONTROL); 397 reg |= SV_CTL_RESET; 398 sv_write(sc, SV_CODEC_CONTROL, reg); 399 delay(50); 400 401 reg = sv_read(sc, SV_CODEC_CONTROL); 402 reg &= ~SV_CTL_RESET; 403 reg |= SV_CTL_INTA | SV_CTL_ENHANCED; 404 405 /* This write clears the reset */ 406 sv_write(sc, SV_CODEC_CONTROL, reg); 407 delay(50); 408 409 /* This write actually shoves the new values in */ 410 sv_write(sc, SV_CODEC_CONTROL, reg); 411 412 DPRINTF(("sv_attach: control=0x%x\n", sv_read(sc, SV_CODEC_CONTROL))); 413 414 /* Enable DMA interrupts */ 415 reg = sv_read(sc, SV_CODEC_INTMASK); 416 reg &= ~(SV_INTMASK_DMAA | SV_INTMASK_DMAC); 417 reg |= SV_INTMASK_UD | SV_INTMASK_SINT | SV_INTMASK_MIDI; 418 sv_write(sc, SV_CODEC_INTMASK, reg); 419 420 sv_read(sc, SV_CODEC_STATUS); 421 422 /* Map and establish the interrupt. */ 423 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, 424 pa->pa_intrline, &ih)) { 425 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname); 426 return; 427 } 428 intrstr = pci_intr_string(pc, ih); 429 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, sv_intr, sc); 430 if (sc->sc_ih == NULL) { 431 printf("%s: couldn't establish interrupt", 432 sc->sc_dev.dv_xname); 433 if (intrstr != NULL) 434 printf(" at %s", intrstr); 435 printf("\n"); 436 return; 437 } 438 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 439 printf("%s: rev %d", sc->sc_dev.dv_xname, 440 sv_read_indirect(sc, SV_REVISION_LEVEL)); 441 if (sv_read(sc, SV_CODEC_CONTROL) & SV_CTL_MD1) 442 printf(", reverb SRAM present"); 443 if (!(sv_read_indirect(sc, SV_WAVETABLE_SOURCE_SELECT) & SV_WSS_WT0)) 444 printf(", wavetable ROM present"); 445 printf("\n"); 446 447 sv_init_mixer(sc); 448 449 audio_attach_mi(&sv_hw_if, sc, &sc->sc_dev); 450 451 arg.type = AUDIODEV_TYPE_OPL; 452 arg.hwif = 0; 453 arg.hdl = 0; 454 (void)config_found(&sc->sc_dev, &arg, audioprint); 455 456 sc->sc_pa = *pa; /* for deferred setup */ 457 config_defer(self, sv_defer); 458 } 459 460 #ifdef AUDIO_DEBUG 461 void 462 sv_dumpregs(sc) 463 struct sv_softc *sc; 464 { 465 int idx; 466 467 #if 0 468 for (idx = 0; idx < 0x50; idx += 4) 469 printf ("%02x = %x\n", idx, 470 pci_conf_read(pa->pa_pc, pa->pa_tag, idx)); 471 #endif 472 473 for (idx = 0; idx < 6; idx++) 474 printf ("REG %02x = %02x\n", idx, sv_read(sc, idx)); 475 476 for (idx = 0; idx < 0x32; idx++) 477 printf ("IREG %02x = %02x\n", idx, sv_read_indirect(sc, idx)); 478 479 for (idx = 0; idx < 0x10; idx++) 480 printf ("DMA %02x = %02x\n", idx, 481 bus_space_read_1(sc->sc_iot, sc->sc_dmaa_ioh, idx)); 482 } 483 #endif 484 485 int 486 sv_intr(p) 487 void *p; 488 { 489 struct sv_softc *sc = p; 490 u_int8_t intr; 491 492 intr = sv_read(sc, SV_CODEC_STATUS); 493 DPRINTFN(5,("sv_intr: intr=0x%x\n", intr)); 494 495 if (!(intr & (SV_INTSTATUS_DMAA | SV_INTSTATUS_DMAC))) 496 return (0); 497 498 if (intr & SV_INTSTATUS_DMAA) { 499 if (sc->sc_pintr) 500 sc->sc_pintr(sc->sc_parg); 501 } 502 503 if (intr & SV_INTSTATUS_DMAC) { 504 if (sc->sc_rintr) 505 sc->sc_rintr(sc->sc_rarg); 506 } 507 508 return (1); 509 } 510 511 int 512 sv_allocmem(sc, size, align, p) 513 struct sv_softc *sc; 514 size_t size; 515 size_t align; 516 struct sv_dma *p; 517 { 518 int error; 519 520 p->size = size; 521 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0, 522 p->segs, ARRAY_SIZE(p->segs), 523 &p->nsegs, BUS_DMA_NOWAIT); 524 if (error) 525 return (error); 526 527 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size, 528 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 529 if (error) 530 goto free; 531 532 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size, 533 0, BUS_DMA_NOWAIT, &p->map); 534 if (error) 535 goto unmap; 536 537 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL, 538 BUS_DMA_NOWAIT); 539 if (error) 540 goto destroy; 541 DPRINTF(("sv_allocmem: pa=%lx va=%lx pba=%lx\n", 542 (long)p->segs[0].ds_addr, (long)KERNADDR(p), (long)DMAADDR(p))); 543 return (0); 544 545 destroy: 546 bus_dmamap_destroy(sc->sc_dmatag, p->map); 547 unmap: 548 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size); 549 free: 550 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs); 551 return (error); 552 } 553 554 int 555 sv_freemem(sc, p) 556 struct sv_softc *sc; 557 struct sv_dma *p; 558 { 559 bus_dmamap_unload(sc->sc_dmatag, p->map); 560 bus_dmamap_destroy(sc->sc_dmatag, p->map); 561 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size); 562 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs); 563 return (0); 564 } 565 566 int 567 sv_open(addr, flags) 568 void *addr; 569 int flags; 570 { 571 struct sv_softc *sc = addr; 572 573 DPRINTF(("sv_open\n")); 574 if (!sc->sc_dmaset) 575 return (ENXIO); 576 sc->sc_pintr = 0; 577 sc->sc_rintr = 0; 578 579 return (0); 580 } 581 582 /* 583 * Close function is called at splaudio(). 584 */ 585 void 586 sv_close(addr) 587 void *addr; 588 { 589 struct sv_softc *sc = addr; 590 591 DPRINTF(("sv_close\n")); 592 sv_halt_output(sc); 593 sv_halt_input(sc); 594 595 sc->sc_pintr = 0; 596 sc->sc_rintr = 0; 597 } 598 599 int 600 sv_query_encoding(addr, fp) 601 void *addr; 602 struct audio_encoding *fp; 603 { 604 switch (fp->index) { 605 case 0: 606 strcpy(fp->name, AudioEulinear); 607 fp->encoding = AUDIO_ENCODING_ULINEAR; 608 fp->precision = 8; 609 fp->flags = 0; 610 return (0); 611 case 1: 612 strcpy(fp->name, AudioEmulaw); 613 fp->encoding = AUDIO_ENCODING_ULAW; 614 fp->precision = 8; 615 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 616 return (0); 617 case 2: 618 strcpy(fp->name, AudioEalaw); 619 fp->encoding = AUDIO_ENCODING_ALAW; 620 fp->precision = 8; 621 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 622 return (0); 623 case 3: 624 strcpy(fp->name, AudioEslinear); 625 fp->encoding = AUDIO_ENCODING_SLINEAR; 626 fp->precision = 8; 627 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 628 return (0); 629 case 4: 630 strcpy(fp->name, AudioEslinear_le); 631 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 632 fp->precision = 16; 633 fp->flags = 0; 634 return (0); 635 case 5: 636 strcpy(fp->name, AudioEulinear_le); 637 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 638 fp->precision = 16; 639 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 640 return (0); 641 case 6: 642 strcpy(fp->name, AudioEslinear_be); 643 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 644 fp->precision = 16; 645 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 646 return (0); 647 case 7: 648 strcpy(fp->name, AudioEulinear_be); 649 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 650 fp->precision = 16; 651 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 652 return (0); 653 default: 654 return (EINVAL); 655 } 656 } 657 658 int 659 sv_set_params(addr, setmode, usemode, play, rec) 660 void *addr; 661 int setmode, usemode; 662 struct audio_params *play, *rec; 663 { 664 struct sv_softc *sc = addr; 665 struct audio_params *p = NULL; 666 int mode; 667 u_int32_t val; 668 669 /* 670 * This device only has one clock, so make the sample rates match. 671 */ 672 if (play->sample_rate != rec->sample_rate && 673 usemode == (AUMODE_PLAY | AUMODE_RECORD)) { 674 if (setmode == AUMODE_PLAY) { 675 rec->sample_rate = play->sample_rate; 676 setmode |= AUMODE_RECORD; 677 } else if (setmode == AUMODE_RECORD) { 678 play->sample_rate = rec->sample_rate; 679 setmode |= AUMODE_PLAY; 680 } else 681 return (EINVAL); 682 } 683 684 for (mode = AUMODE_RECORD; mode != -1; 685 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 686 if ((setmode & mode) == 0) 687 continue; 688 689 p = mode == AUMODE_PLAY ? play : rec; 690 691 if (p->sample_rate < 2000 || p->sample_rate > 48000 || 692 (p->precision != 8 && p->precision != 16) || 693 (p->channels != 1 && p->channels != 2)) 694 return (EINVAL); 695 696 p->factor = 1; 697 p->sw_code = 0; 698 switch (p->encoding) { 699 case AUDIO_ENCODING_SLINEAR_BE: 700 if (p->precision == 16) 701 p->sw_code = swap_bytes; 702 else 703 p->sw_code = change_sign8; 704 break; 705 case AUDIO_ENCODING_SLINEAR_LE: 706 if (p->precision != 16) 707 p->sw_code = change_sign8; 708 break; 709 case AUDIO_ENCODING_ULINEAR_BE: 710 if (p->precision == 16) { 711 if (mode == AUMODE_PLAY) 712 p->sw_code = swap_bytes_change_sign16_le; 713 else 714 p->sw_code = change_sign16_swap_bytes_le; 715 } 716 break; 717 case AUDIO_ENCODING_ULINEAR_LE: 718 if (p->precision == 16) 719 p->sw_code = change_sign16_le; 720 break; 721 case AUDIO_ENCODING_ULAW: 722 if (mode == AUMODE_PLAY) { 723 p->factor = 2; 724 p->sw_code = mulaw_to_slinear16_le; 725 } else 726 p->sw_code = ulinear8_to_mulaw; 727 break; 728 case AUDIO_ENCODING_ALAW: 729 if (mode == AUMODE_PLAY) { 730 p->factor = 2; 731 p->sw_code = alaw_to_slinear16_le; 732 } else 733 p->sw_code = ulinear8_to_alaw; 734 break; 735 default: 736 return (EINVAL); 737 } 738 } 739 740 val = p->sample_rate * 65536 / 48000; 741 /* 742 * If the sample rate is exactly 48KHz, the fraction would overflow the 743 * register, so we have to bias it. This causes a little clock drift. 744 * The drift is below normal crystal tolerance (.0001%), so although 745 * this seems a little silly, we can pretty much ignore it. 746 * (I tested the output speed with values of 1-20, just to be sure this 747 * register isn't *supposed* to have a bias. It isn't.) 748 * - mycroft 749 */ 750 if (val > 65535) 751 val = 65535; 752 753 sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_0, val & 0xff); 754 sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_1, val >> 8); 755 756 #define F_REF 24576000 757 758 #define ABS(x) (((x) < 0) ? (-x) : (x)) 759 760 if (setmode & AUMODE_RECORD) { 761 /* The ADC reference frequency (f_out) is 512 * sample rate */ 762 763 /* f_out is dervied from the 24.576MHZ crystal by three values: 764 M & N & R. The equation is as follows: 765 766 f_out = (m + 2) * f_ref / ((n + 2) * (2 ^ a)) 767 768 with the constraint that: 769 770 80 MhZ < (m + 2) / (n + 2) * f_ref <= 150Mhz 771 and n, m >= 1 772 */ 773 774 int goal_f_out = 512 * rec->sample_rate; 775 int a, n, m, best_n = 0, best_m = 0, best_error = 10000000; 776 int pll_sample; 777 int error; 778 779 for (a = 0; a < 8; a++) { 780 if ((goal_f_out * (1 << a)) >= 80000000) 781 break; 782 } 783 784 /* a != 8 because sample_rate >= 2000 */ 785 786 for (n = 33; n > 2; n--) { 787 m = (goal_f_out * n * (1 << a)) / F_REF; 788 if ((m > 257) || (m < 3)) 789 continue; 790 791 pll_sample = (m * F_REF) / (n * (1 << a)); 792 pll_sample /= 512; 793 794 /* Threshold might be good here */ 795 error = pll_sample - rec->sample_rate; 796 error = ABS(error); 797 798 if (error < best_error) { 799 best_error = error; 800 best_n = n; 801 best_m = m; 802 if (error == 0) break; 803 } 804 } 805 806 best_n -= 2; 807 best_m -= 2; 808 809 sv_write_indirect(sc, SV_ADC_PLL_M, best_m); 810 sv_write_indirect(sc, SV_ADC_PLL_N, 811 best_n | (a << SV_PLL_R_SHIFT)); 812 } 813 814 return (0); 815 } 816 817 int 818 sv_round_blocksize(addr, blk) 819 void *addr; 820 int blk; 821 { 822 return (blk & -32); /* keep good alignment */ 823 } 824 825 int 826 sv_trigger_output(addr, start, end, blksize, intr, arg, param) 827 void *addr; 828 void *start, *end; 829 int blksize; 830 void (*intr) __P((void *)); 831 void *arg; 832 struct audio_params *param; 833 { 834 struct sv_softc *sc = addr; 835 struct sv_dma *p; 836 u_int8_t mode; 837 int dma_count; 838 839 DPRINTFN(1, ("sv_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n", 840 addr, start, end, blksize, intr, arg)); 841 sc->sc_pintr = intr; 842 sc->sc_parg = arg; 843 844 mode = sv_read_indirect(sc, SV_DMA_DATA_FORMAT); 845 mode &= ~(SV_DMAA_FORMAT16 | SV_DMAA_STEREO); 846 if (param->precision * param->factor == 16) 847 mode |= SV_DMAA_FORMAT16; 848 if (param->channels == 2) 849 mode |= SV_DMAA_STEREO; 850 sv_write_indirect(sc, SV_DMA_DATA_FORMAT, mode); 851 852 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next) 853 ; 854 if (!p) { 855 printf("sv_trigger_output: bad addr %p\n", start); 856 return (EINVAL); 857 } 858 859 dma_count = ((char *)end - (char *)start) - 1; 860 DPRINTF(("sv_trigger_output: dma start loop input addr=%x cc=%d\n", 861 (int)DMAADDR(p), dma_count)); 862 863 bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0, 864 DMAADDR(p)); 865 bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_COUNT0, 866 dma_count); 867 bus_space_write_1(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_MODE, 868 DMA37MD_READ | DMA37MD_LOOP); 869 870 DPRINTF(("sv_trigger_output: current addr=%x\n", 871 bus_space_read_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0))); 872 873 dma_count = blksize - 1; 874 875 sv_write_indirect(sc, SV_DMAA_COUNT1, dma_count >> 8); 876 sv_write_indirect(sc, SV_DMAA_COUNT0, dma_count & 0xFF); 877 878 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE); 879 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode | SV_PLAY_ENABLE); 880 881 return (0); 882 } 883 884 int 885 sv_trigger_input(addr, start, end, blksize, intr, arg, param) 886 void *addr; 887 void *start, *end; 888 int blksize; 889 void (*intr) __P((void *)); 890 void *arg; 891 struct audio_params *param; 892 { 893 struct sv_softc *sc = addr; 894 struct sv_dma *p; 895 u_int8_t mode; 896 int dma_count; 897 898 DPRINTFN(1, ("sv_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n", 899 addr, start, end, blksize, intr, arg)); 900 sc->sc_rintr = intr; 901 sc->sc_rarg = arg; 902 903 mode = sv_read_indirect(sc, SV_DMA_DATA_FORMAT); 904 mode &= ~(SV_DMAC_FORMAT16 | SV_DMAC_STEREO); 905 if (param->precision * param->factor == 16) 906 mode |= SV_DMAC_FORMAT16; 907 if (param->channels == 2) 908 mode |= SV_DMAC_STEREO; 909 sv_write_indirect(sc, SV_DMA_DATA_FORMAT, mode); 910 911 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next) 912 ; 913 if (!p) { 914 printf("sv_trigger_input: bad addr %p\n", start); 915 return (EINVAL); 916 } 917 918 dma_count = (((char *)end - (char *)start) >> 1) - 1; 919 DPRINTF(("sv_trigger_input: dma start loop input addr=%x cc=%d\n", 920 (int)DMAADDR(p), dma_count)); 921 922 bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0, 923 DMAADDR(p)); 924 bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_COUNT0, 925 dma_count); 926 bus_space_write_1(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_MODE, 927 DMA37MD_WRITE | DMA37MD_LOOP); 928 929 DPRINTF(("sv_trigger_input: current addr=%x\n", 930 bus_space_read_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0))); 931 932 dma_count = (blksize >> 1) - 1; 933 934 sv_write_indirect(sc, SV_DMAC_COUNT1, dma_count >> 8); 935 sv_write_indirect(sc, SV_DMAC_COUNT0, dma_count & 0xFF); 936 937 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE); 938 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode | SV_RECORD_ENABLE); 939 940 return (0); 941 } 942 943 int 944 sv_halt_output(addr) 945 void *addr; 946 { 947 struct sv_softc *sc = addr; 948 u_int8_t mode; 949 950 DPRINTF(("sv: sv_halt_output\n")); 951 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE); 952 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode & ~SV_PLAY_ENABLE); 953 954 return (0); 955 } 956 957 int 958 sv_halt_input(addr) 959 void *addr; 960 { 961 struct sv_softc *sc = addr; 962 u_int8_t mode; 963 964 DPRINTF(("sv: sv_halt_input\n")); 965 mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE); 966 sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode & ~SV_RECORD_ENABLE); 967 968 return (0); 969 } 970 971 int 972 sv_getdev(addr, retp) 973 void *addr; 974 struct audio_device *retp; 975 { 976 *retp = sv_device; 977 return (0); 978 } 979 980 981 /* 982 * Mixer related code is here 983 * 984 */ 985 986 #define SV_INPUT_CLASS 0 987 #define SV_OUTPUT_CLASS 1 988 #define SV_RECORD_CLASS 2 989 990 #define SV_LAST_CLASS 2 991 992 static const char *mixer_classes[] = 993 { AudioCinputs, AudioCoutputs, AudioCrecord }; 994 995 static const struct { 996 u_int8_t l_port; 997 u_int8_t r_port; 998 u_int8_t mask; 999 u_int8_t class; 1000 const char *audio; 1001 } ports[] = { 1002 { SV_LEFT_AUX1_INPUT_CONTROL, SV_RIGHT_AUX1_INPUT_CONTROL, SV_AUX1_MASK, 1003 SV_INPUT_CLASS, "aux1" }, 1004 { SV_LEFT_CD_INPUT_CONTROL, SV_RIGHT_CD_INPUT_CONTROL, SV_CD_MASK, 1005 SV_INPUT_CLASS, AudioNcd }, 1006 { SV_LEFT_LINE_IN_INPUT_CONTROL, SV_RIGHT_LINE_IN_INPUT_CONTROL, SV_LINE_IN_MASK, 1007 SV_INPUT_CLASS, AudioNline }, 1008 { SV_MIC_INPUT_CONTROL, 0, SV_MIC_MASK, SV_INPUT_CLASS, AudioNmicrophone }, 1009 { SV_LEFT_SYNTH_INPUT_CONTROL, SV_RIGHT_SYNTH_INPUT_CONTROL, 1010 SV_SYNTH_MASK, SV_INPUT_CLASS, AudioNfmsynth }, 1011 { SV_LEFT_AUX2_INPUT_CONTROL, SV_RIGHT_AUX2_INPUT_CONTROL, SV_AUX2_MASK, 1012 SV_INPUT_CLASS, "aux2" }, 1013 { SV_LEFT_PCM_INPUT_CONTROL, SV_RIGHT_PCM_INPUT_CONTROL, SV_PCM_MASK, 1014 SV_INPUT_CLASS, AudioNdac }, 1015 { SV_LEFT_MIXER_OUTPUT_CONTROL, SV_RIGHT_MIXER_OUTPUT_CONTROL, 1016 SV_MIXER_OUT_MASK, SV_OUTPUT_CLASS, AudioNmaster } 1017 }; 1018 1019 1020 static const struct { 1021 int idx; 1022 const char *name; 1023 } record_sources[] = { 1024 { SV_REC_CD, AudioNcd }, 1025 { SV_REC_DAC, AudioNdac }, 1026 { SV_REC_AUX2, "aux2" }, 1027 { SV_REC_LINE, AudioNline }, 1028 { SV_REC_AUX1, "aux1" }, 1029 { SV_REC_MIC, AudioNmicrophone }, 1030 { SV_REC_MIXER, AudioNmixerout } 1031 }; 1032 1033 1034 #define SV_DEVICES_PER_PORT 2 1035 #define SV_FIRST_MIXER (SV_LAST_CLASS + 1) 1036 #define SV_LAST_MIXER (SV_DEVICES_PER_PORT * (ARRAY_SIZE(ports)) + SV_LAST_CLASS) 1037 #define SV_RECORD_SOURCE (SV_LAST_MIXER + 1) 1038 #define SV_MIC_BOOST (SV_LAST_MIXER + 2) 1039 #define SV_RECORD_GAIN (SV_LAST_MIXER + 3) 1040 #define SV_SRS_MODE (SV_LAST_MIXER + 4) 1041 1042 int 1043 sv_query_devinfo(addr, dip) 1044 void *addr; 1045 mixer_devinfo_t *dip; 1046 { 1047 int i; 1048 1049 /* It's a class */ 1050 if (dip->index <= SV_LAST_CLASS) { 1051 dip->type = AUDIO_MIXER_CLASS; 1052 dip->mixer_class = dip->index; 1053 dip->next = dip->prev = AUDIO_MIXER_LAST; 1054 strcpy(dip->label.name, 1055 mixer_classes[dip->index]); 1056 return (0); 1057 } 1058 1059 if (dip->index >= SV_FIRST_MIXER && 1060 dip->index <= SV_LAST_MIXER) { 1061 int off = dip->index - SV_FIRST_MIXER; 1062 int mute = (off % SV_DEVICES_PER_PORT); 1063 int idx = off / SV_DEVICES_PER_PORT; 1064 1065 dip->mixer_class = ports[idx].class; 1066 strcpy(dip->label.name, ports[idx].audio); 1067 1068 if (!mute) { 1069 dip->type = AUDIO_MIXER_VALUE; 1070 dip->prev = AUDIO_MIXER_LAST; 1071 dip->next = dip->index + 1; 1072 1073 if (ports[idx].r_port != 0) 1074 dip->un.v.num_channels = 2; 1075 else 1076 dip->un.v.num_channels = 1; 1077 1078 strcpy(dip->un.v.units.name, AudioNvolume); 1079 } else { 1080 dip->type = AUDIO_MIXER_ENUM; 1081 dip->prev = dip->index - 1; 1082 dip->next = AUDIO_MIXER_LAST; 1083 1084 strcpy(dip->label.name, AudioNmute); 1085 dip->un.e.num_mem = 2; 1086 strcpy(dip->un.e.member[0].label.name, AudioNoff); 1087 dip->un.e.member[0].ord = 0; 1088 strcpy(dip->un.e.member[1].label.name, AudioNon); 1089 dip->un.e.member[1].ord = 1; 1090 } 1091 1092 return (0); 1093 } 1094 1095 switch (dip->index) { 1096 case SV_RECORD_SOURCE: 1097 dip->mixer_class = SV_RECORD_CLASS; 1098 dip->prev = AUDIO_MIXER_LAST; 1099 dip->next = SV_RECORD_GAIN; 1100 strcpy(dip->label.name, AudioNsource); 1101 dip->type = AUDIO_MIXER_ENUM; 1102 1103 dip->un.e.num_mem = ARRAY_SIZE(record_sources); 1104 for (i = 0; i < ARRAY_SIZE(record_sources); i++) { 1105 strcpy(dip->un.e.member[i].label.name, 1106 record_sources[i].name); 1107 dip->un.e.member[i].ord = record_sources[i].idx; 1108 } 1109 return (0); 1110 1111 case SV_RECORD_GAIN: 1112 dip->mixer_class = SV_RECORD_CLASS; 1113 dip->prev = SV_RECORD_SOURCE; 1114 dip->next = AUDIO_MIXER_LAST; 1115 strcpy(dip->label.name, "gain"); 1116 dip->type = AUDIO_MIXER_VALUE; 1117 dip->un.v.num_channels = 1; 1118 strcpy(dip->un.v.units.name, AudioNvolume); 1119 return (0); 1120 1121 case SV_MIC_BOOST: 1122 dip->mixer_class = SV_RECORD_CLASS; 1123 dip->prev = AUDIO_MIXER_LAST; 1124 dip->next = AUDIO_MIXER_LAST; 1125 strcpy(dip->label.name, "micboost"); 1126 goto on_off; 1127 1128 case SV_SRS_MODE: 1129 dip->mixer_class = SV_OUTPUT_CLASS; 1130 dip->prev = dip->next = AUDIO_MIXER_LAST; 1131 strcpy(dip->label.name, AudioNspatial); 1132 1133 on_off: 1134 dip->type = AUDIO_MIXER_ENUM; 1135 dip->un.e.num_mem = 2; 1136 strcpy(dip->un.e.member[0].label.name, AudioNoff); 1137 dip->un.e.member[0].ord = 0; 1138 strcpy(dip->un.e.member[1].label.name, AudioNon); 1139 dip->un.e.member[1].ord = 1; 1140 return (0); 1141 } 1142 1143 return (ENXIO); 1144 } 1145 1146 int 1147 sv_mixer_set_port(addr, cp) 1148 void *addr; 1149 mixer_ctrl_t *cp; 1150 { 1151 struct sv_softc *sc = addr; 1152 u_int8_t reg; 1153 int idx; 1154 1155 if (cp->dev >= SV_FIRST_MIXER && 1156 cp->dev <= SV_LAST_MIXER) { 1157 int off = cp->dev - SV_FIRST_MIXER; 1158 int mute = (off % SV_DEVICES_PER_PORT); 1159 idx = off / SV_DEVICES_PER_PORT; 1160 1161 if (mute) { 1162 if (cp->type != AUDIO_MIXER_ENUM) 1163 return (EINVAL); 1164 1165 reg = sv_read_indirect(sc, ports[idx].l_port); 1166 if (cp->un.ord) 1167 reg |= SV_MUTE_BIT; 1168 else 1169 reg &= ~SV_MUTE_BIT; 1170 sv_write_indirect(sc, ports[idx].l_port, reg); 1171 1172 if (ports[idx].r_port) { 1173 reg = sv_read_indirect(sc, ports[idx].r_port); 1174 if (cp->un.ord) 1175 reg |= SV_MUTE_BIT; 1176 else 1177 reg &= ~SV_MUTE_BIT; 1178 sv_write_indirect(sc, ports[idx].r_port, reg); 1179 } 1180 } else { 1181 int lval, rval; 1182 1183 if (cp->type != AUDIO_MIXER_VALUE) 1184 return (EINVAL); 1185 1186 if (cp->un.value.num_channels != 1 && 1187 cp->un.value.num_channels != 2) 1188 return (EINVAL); 1189 1190 if (ports[idx].r_port == 0) { 1191 if (cp->un.value.num_channels != 1) 1192 return (EINVAL); 1193 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]; 1194 rval = 0; /* shut up GCC */ 1195 } else { 1196 if (cp->un.value.num_channels != 2) 1197 return (EINVAL); 1198 1199 lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]; 1200 rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]; 1201 } 1202 1203 1204 reg = sv_read_indirect(sc, ports[idx].l_port); 1205 reg &= ~(ports[idx].mask); 1206 lval = (AUDIO_MAX_GAIN - lval) * ports[idx].mask / 1207 AUDIO_MAX_GAIN; 1208 reg |= lval; 1209 sv_write_indirect(sc, ports[idx].l_port, reg); 1210 1211 if (ports[idx].r_port != 0) { 1212 reg = sv_read_indirect(sc, ports[idx].r_port); 1213 reg &= ~(ports[idx].mask); 1214 1215 rval = (AUDIO_MAX_GAIN - rval) * ports[idx].mask / 1216 AUDIO_MAX_GAIN; 1217 reg |= rval; 1218 1219 sv_write_indirect(sc, ports[idx].r_port, reg); 1220 } 1221 1222 sv_read_indirect(sc, ports[idx].l_port); 1223 } 1224 1225 return (0); 1226 } 1227 1228 1229 switch (cp->dev) { 1230 case SV_RECORD_SOURCE: 1231 if (cp->type != AUDIO_MIXER_ENUM) 1232 return (EINVAL); 1233 1234 for (idx = 0; idx < ARRAY_SIZE(record_sources); idx++) { 1235 if (record_sources[idx].idx == cp->un.ord) 1236 goto found; 1237 } 1238 1239 return (EINVAL); 1240 1241 found: 1242 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL); 1243 reg &= ~SV_REC_SOURCE_MASK; 1244 reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK); 1245 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg); 1246 1247 reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL); 1248 reg &= ~SV_REC_SOURCE_MASK; 1249 reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK); 1250 sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg); 1251 return (0); 1252 1253 case SV_RECORD_GAIN: 1254 { 1255 int val; 1256 1257 if (cp->type != AUDIO_MIXER_VALUE) 1258 return (EINVAL); 1259 1260 if (cp->un.value.num_channels != 1) 1261 return (EINVAL); 1262 1263 val = (cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] * SV_REC_GAIN_MASK) 1264 / AUDIO_MAX_GAIN; 1265 1266 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL); 1267 reg &= ~SV_REC_GAIN_MASK; 1268 reg |= val; 1269 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg); 1270 1271 reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL); 1272 reg &= ~SV_REC_GAIN_MASK; 1273 reg |= val; 1274 sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg); 1275 } 1276 return (0); 1277 1278 case SV_MIC_BOOST: 1279 if (cp->type != AUDIO_MIXER_ENUM) 1280 return (EINVAL); 1281 1282 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL); 1283 if (cp->un.ord) { 1284 reg |= SV_MIC_BOOST_BIT; 1285 } else { 1286 reg &= ~SV_MIC_BOOST_BIT; 1287 } 1288 1289 sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg); 1290 return (0); 1291 1292 case SV_SRS_MODE: 1293 if (cp->type != AUDIO_MIXER_ENUM) 1294 return (EINVAL); 1295 1296 reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL); 1297 if (cp->un.ord) { 1298 reg &= ~SV_SRS_SPACE_ONOFF; 1299 } else { 1300 reg |= SV_SRS_SPACE_ONOFF; 1301 } 1302 1303 sv_write_indirect(sc, SV_SRS_SPACE_CONTROL, reg); 1304 return (0); 1305 } 1306 1307 return (EINVAL); 1308 } 1309 1310 int 1311 sv_mixer_get_port(addr, cp) 1312 void *addr; 1313 mixer_ctrl_t *cp; 1314 { 1315 struct sv_softc *sc = addr; 1316 int val; 1317 u_int8_t reg; 1318 1319 if (cp->dev >= SV_FIRST_MIXER && 1320 cp->dev <= SV_LAST_MIXER) { 1321 int off = cp->dev - SV_FIRST_MIXER; 1322 int mute = (off % 2); 1323 int idx = off / 2; 1324 1325 if (mute) { 1326 if (cp->type != AUDIO_MIXER_ENUM) 1327 return (EINVAL); 1328 1329 reg = sv_read_indirect(sc, ports[idx].l_port); 1330 cp->un.ord = ((reg & SV_MUTE_BIT) ? 1 : 0); 1331 } else { 1332 if (cp->type != AUDIO_MIXER_VALUE) 1333 return (EINVAL); 1334 1335 if (cp->un.value.num_channels != 1 && 1336 cp->un.value.num_channels != 2) 1337 return (EINVAL); 1338 1339 if ((ports[idx].r_port == 0 && 1340 cp->un.value.num_channels != 1) || 1341 (ports[idx].r_port != 0 && 1342 cp->un.value.num_channels != 2)) 1343 return (EINVAL); 1344 1345 reg = sv_read_indirect(sc, ports[idx].l_port); 1346 reg &= ports[idx].mask; 1347 1348 val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN) / ports[idx].mask); 1349 1350 if (ports[idx].r_port != 0) { 1351 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = val; 1352 1353 reg = sv_read_indirect(sc, ports[idx].r_port); 1354 reg &= ports[idx].mask; 1355 1356 val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN) / ports[idx].mask); 1357 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = val; 1358 } else 1359 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = val; 1360 } 1361 1362 return (0); 1363 } 1364 1365 switch (cp->dev) { 1366 case SV_RECORD_SOURCE: 1367 if (cp->type != AUDIO_MIXER_ENUM) 1368 return (EINVAL); 1369 1370 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL); 1371 cp->un.ord = ((reg & SV_REC_SOURCE_MASK) >> SV_REC_SOURCE_SHIFT); 1372 1373 return (0); 1374 1375 case SV_RECORD_GAIN: 1376 if (cp->type != AUDIO_MIXER_VALUE) 1377 return (EINVAL); 1378 if (cp->un.value.num_channels != 1) 1379 return (EINVAL); 1380 1381 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL) & SV_REC_GAIN_MASK; 1382 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = 1383 (((unsigned int)reg) * AUDIO_MAX_GAIN) / SV_REC_GAIN_MASK; 1384 1385 return (0); 1386 1387 case SV_MIC_BOOST: 1388 if (cp->type != AUDIO_MIXER_ENUM) 1389 return (EINVAL); 1390 reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL); 1391 cp->un.ord = ((reg & SV_MIC_BOOST_BIT) ? 1 : 0); 1392 return (0); 1393 1394 1395 case SV_SRS_MODE: 1396 if (cp->type != AUDIO_MIXER_ENUM) 1397 return (EINVAL); 1398 reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL); 1399 cp->un.ord = ((reg & SV_SRS_SPACE_ONOFF) ? 0 : 1); 1400 return (0); 1401 } 1402 1403 return (EINVAL); 1404 } 1405 1406 1407 static void 1408 sv_init_mixer(sc) 1409 struct sv_softc *sc; 1410 { 1411 mixer_ctrl_t cp; 1412 int i; 1413 1414 cp.type = AUDIO_MIXER_ENUM; 1415 cp.dev = SV_SRS_MODE; 1416 cp.un.ord = 0; 1417 1418 sv_mixer_set_port(sc, &cp); 1419 1420 for (i = 0; i < ARRAY_SIZE(ports); i++) { 1421 if (ports[i].audio == AudioNdac) { 1422 cp.type = AUDIO_MIXER_ENUM; 1423 cp.dev = SV_FIRST_MIXER + i * SV_DEVICES_PER_PORT + 1; 1424 cp.un.ord = 0; 1425 sv_mixer_set_port(sc, &cp); 1426 break; 1427 } 1428 } 1429 } 1430 1431 void * 1432 sv_malloc(addr, direction, size, pool, flags) 1433 void *addr; 1434 int direction; 1435 size_t size; 1436 int pool, flags; 1437 { 1438 struct sv_softc *sc = addr; 1439 struct sv_dma *p; 1440 int error; 1441 1442 p = malloc(sizeof(*p), pool, flags); 1443 if (!p) 1444 return (0); 1445 error = sv_allocmem(sc, size, 16, p); 1446 if (error) { 1447 free(p, pool); 1448 return (0); 1449 } 1450 p->next = sc->sc_dmas; 1451 sc->sc_dmas = p; 1452 return (KERNADDR(p)); 1453 } 1454 1455 void 1456 sv_free(addr, ptr, pool) 1457 void *addr; 1458 void *ptr; 1459 int pool; 1460 { 1461 struct sv_softc *sc = addr; 1462 struct sv_dma **pp, *p; 1463 1464 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) { 1465 if (KERNADDR(p) == ptr) { 1466 sv_freemem(sc, p); 1467 *pp = p->next; 1468 free(p, pool); 1469 return; 1470 } 1471 } 1472 } 1473 1474 size_t 1475 sv_round_buffersize(addr, direction, size) 1476 void *addr; 1477 int direction; 1478 size_t size; 1479 { 1480 return (size); 1481 } 1482 1483 paddr_t 1484 sv_mappage(addr, mem, off, prot) 1485 void *addr; 1486 void *mem; 1487 off_t off; 1488 int prot; 1489 { 1490 struct sv_softc *sc = addr; 1491 struct sv_dma *p; 1492 1493 if (off < 0) 1494 return (-1); 1495 for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next) 1496 ; 1497 if (!p) 1498 return (-1); 1499 return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs, 1500 off, prot, BUS_DMA_WAITOK)); 1501 } 1502 1503 int 1504 sv_get_props(addr) 1505 void *addr; 1506 { 1507 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX); 1508 } 1509