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