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