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