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