1 /* $NetBSD: auvia.c,v 1.17 2002/04/02 16:02:38 fvdl Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tyler C. Sarna 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * VIA Technologies VT82C686A Southbridge Audio Driver 41 * 42 * Documentation links: 43 * 44 * ftp://ftp.alsa-project.org/pub/manuals/via/686a.pdf 45 * ftp://ftp.alsa-project.org/pub/manuals/general/ac97r21.pdf 46 * ftp://ftp.alsa-project.org/pub/manuals/ad/AD1881_0.pdf (example AC'97 codec) 47 */ 48 49 #include <sys/cdefs.h> 50 __KERNEL_RCSID(0, "$NetBSD: auvia.c,v 1.17 2002/04/02 16:02:38 fvdl Exp $"); 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/malloc.h> 55 #include <sys/device.h> 56 #include <sys/audioio.h> 57 58 #include <uvm/uvm_extern.h> 59 60 #include <dev/pci/pcidevs.h> 61 #include <dev/pci/pcivar.h> 62 63 #include <dev/audio_if.h> 64 #include <dev/mulaw.h> 65 #include <dev/auconv.h> 66 67 #include <dev/ic/ac97reg.h> 68 #include <dev/ic/ac97var.h> 69 70 #include <dev/pci/auviavar.h> 71 72 struct auvia_dma { 73 struct auvia_dma *next; 74 caddr_t addr; 75 size_t size; 76 bus_dmamap_t map; 77 bus_dma_segment_t seg; 78 }; 79 80 struct auvia_dma_op { 81 u_int32_t ptr; 82 u_int32_t flags; 83 #define AUVIA_DMAOP_EOL 0x80000000 84 #define AUVIA_DMAOP_FLAG 0x40000000 85 #define AUVIA_DMAOP_STOP 0x20000000 86 #define AUVIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF) 87 }; 88 89 /* rev. H and later seem to support only fixed rate 48 kHz */ 90 #define AUVIA_FIXED_RATE 48000 91 92 int auvia_match(struct device *, struct cfdata *, void *); 93 void auvia_attach(struct device *, struct device *, void *); 94 int auvia_open(void *, int); 95 void auvia_close(void *); 96 int auvia_query_encoding(void *addr, struct audio_encoding *fp); 97 int auvia_set_params(void *, int, int, struct audio_params *, 98 struct audio_params *); 99 int auvia_round_blocksize(void *, int); 100 int auvia_halt_output(void *); 101 int auvia_halt_input(void *); 102 int auvia_getdev(void *, struct audio_device *); 103 int auvia_set_port(void *, mixer_ctrl_t *); 104 int auvia_get_port(void *, mixer_ctrl_t *); 105 int auvia_query_devinfo(void *, mixer_devinfo_t *); 106 void * auvia_malloc(void *, int, size_t, int, int); 107 void auvia_free(void *, void *, int); 108 size_t auvia_round_buffersize(void *, int, size_t); 109 paddr_t auvia_mappage(void *, void *, off_t, int); 110 int auvia_get_props(void *); 111 int auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *, 112 struct auvia_dma *, void *, void *, int); 113 int auvia_trigger_output(void *, void *, void *, int, void (*)(void *), 114 void *, struct audio_params *); 115 int auvia_trigger_input(void *, void *, void *, int, void (*)(void *), 116 void *, struct audio_params *); 117 118 int auvia_intr __P((void *)); 119 120 struct cfattach auvia_ca = { 121 sizeof (struct auvia_softc), auvia_match, auvia_attach 122 }; 123 124 #define AUVIA_PCICONF_JUNK 0x40 125 #define AUVIA_PCICONF_ENABLES 0x00FF0000 /* reg 42 mask */ 126 #define AUVIA_PCICONF_ACLINKENAB 0x00008000 /* ac link enab */ 127 #define AUVIA_PCICONF_ACNOTRST 0x00004000 /* ~(ac reset) */ 128 #define AUVIA_PCICONF_ACSYNC 0x00002000 /* ac sync */ 129 #define AUVIA_PCICONF_ACVSR 0x00000800 /* var. samp. rate */ 130 #define AUVIA_PCICONF_ACSGD 0x00000400 /* SGD enab */ 131 #define AUVIA_PCICONF_ACFM 0x00000200 /* FM enab */ 132 #define AUVIA_PCICONF_ACSB 0x00000100 /* SB enab */ 133 134 #define AUVIA_PLAY_STAT 0x00 135 #define AUVIA_RECORD_STAT 0x10 136 #define AUVIA_RPSTAT_INTR 0x03 137 #define AUVIA_PLAY_CONTROL 0x01 138 #define AUVIA_RECORD_CONTROL 0x11 139 #define AUVIA_RPCTRL_START 0x80 140 #define AUVIA_RPCTRL_TERMINATE 0x40 141 #define AUVIA_PLAY_MODE 0x02 142 #define AUVIA_RECORD_MODE 0x12 143 #define AUVIA_RPMODE_INTR_FLAG 0x01 144 #define AUVIA_RPMODE_INTR_EOL 0x02 145 #define AUVIA_RPMODE_STEREO 0x10 146 #define AUVIA_RPMODE_16BIT 0x20 147 #define AUVIA_RPMODE_AUTOSTART 0x80 148 #define AUVIA_PLAY_DMAOPS_BASE 0x04 149 #define AUVIA_RECORD_DMAOPS_BASE 0x14 150 151 #define AUVIA_CODEC_CTL 0x80 152 #define AUVIA_CODEC_READ 0x00800000 153 #define AUVIA_CODEC_BUSY 0x01000000 154 #define AUVIA_CODEC_PRIVALID 0x02000000 155 #define AUVIA_CODEC_INDEX(x) ((x)<<16) 156 157 #define TIMEOUT 50 158 159 struct audio_hw_if auvia_hw_if = { 160 auvia_open, 161 auvia_close, 162 NULL, /* drain */ 163 auvia_query_encoding, 164 auvia_set_params, 165 auvia_round_blocksize, 166 NULL, /* commit_settings */ 167 NULL, /* init_output */ 168 NULL, /* init_input */ 169 NULL, /* start_output */ 170 NULL, /* start_input */ 171 auvia_halt_output, 172 auvia_halt_input, 173 NULL, /* speaker_ctl */ 174 auvia_getdev, 175 NULL, /* setfd */ 176 auvia_set_port, 177 auvia_get_port, 178 auvia_query_devinfo, 179 auvia_malloc, 180 auvia_free, 181 auvia_round_buffersize, 182 auvia_mappage, 183 auvia_get_props, 184 auvia_trigger_output, 185 auvia_trigger_input, 186 NULL, /* dev_ioctl */ 187 }; 188 189 int auvia_attach_codec(void *, struct ac97_codec_if *); 190 int auvia_write_codec(void *, u_int8_t, u_int16_t); 191 int auvia_read_codec(void *, u_int8_t, u_int16_t *); 192 void auvia_reset_codec(void *); 193 int auvia_waitready_codec(struct auvia_softc *sc); 194 int auvia_waitvalid_codec(struct auvia_softc *sc); 195 196 197 int 198 auvia_match(struct device *parent, struct cfdata *match, void *aux) 199 { 200 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 201 202 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH) 203 return 0; 204 switch (PCI_PRODUCT(pa->pa_id)) { 205 case PCI_PRODUCT_VIATECH_VT82C686A_AC97: 206 break; 207 default: 208 return 0; 209 } 210 211 return 1; 212 } 213 214 215 void 216 auvia_attach(struct device *parent, struct device *self, void *aux) 217 { 218 struct pci_attach_args *pa = aux; 219 struct auvia_softc *sc = (struct auvia_softc *) self; 220 const char *intrstr = NULL; 221 struct mixer_ctrl ctl; 222 pci_chipset_tag_t pc = pa->pa_pc; 223 pcitag_t pt = pa->pa_tag; 224 pci_intr_handle_t ih; 225 pcireg_t pr; 226 u_int16_t v; 227 int r, i; 228 229 r = PCI_REVISION(pa->pa_class); 230 sc->sc_revision[1] = '\0'; 231 if (r == 0x20) { 232 sc->sc_revision[0] = 'H'; 233 } else if ((r >= 0x10) && (r <= 0x14)) { 234 sc->sc_revision[0] = 'A' + (r - 0x10); 235 } else { 236 sprintf(sc->sc_revision, "0x%02X", r); 237 } 238 239 printf(": VIA VT82C686A AC'97 Audio (rev %s)\n", 240 sc->sc_revision); 241 242 if (pci_intr_map(pa, &ih)) { 243 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname); 244 return; 245 } 246 intrstr = pci_intr_string(pc, ih); 247 248 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc); 249 if (sc->sc_ih == NULL) { 250 printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname); 251 if (intrstr != NULL) 252 printf(" at %s", intrstr); 253 printf("\n"); 254 return; 255 } 256 257 sc->sc_dmat = pa->pa_dmat; 258 sc->sc_pc = pc; 259 sc->sc_pt = pt; 260 261 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 262 263 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot, 264 &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) { 265 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname); 266 return; 267 } 268 269 /* disable SBPro compat & others */ 270 pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK); 271 272 pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */ 273 /* XXX what to do about MIDI, FM, joystick? */ 274 275 pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST 276 | AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD); 277 278 pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB); 279 280 pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr); 281 282 sc->host_if.arg = sc; 283 sc->host_if.attach = auvia_attach_codec; 284 sc->host_if.read = auvia_read_codec; 285 sc->host_if.write = auvia_write_codec; 286 sc->host_if.reset = auvia_reset_codec; 287 288 if ((r = ac97_attach(&sc->host_if)) != 0) { 289 printf("%s: can't attach codec (error 0x%X)\n", 290 sc->sc_dev.dv_xname, r); 291 return; 292 } 293 294 /* 295 * Print a warning if the codec doesn't support hardware variable 296 * rate audio. 297 */ 298 if (auvia_read_codec(sc, AC97_REG_EXTENDED_ID, &v) 299 || !(v & AC97_CODEC_DOES_VRA)) { 300 printf("%s: warning: codec doesn't support hardware AC'97 2.0 Variable Rate Audio\n", 301 sc->sc_dev.dv_xname); 302 sc->sc_fixed_rate = AUVIA_FIXED_RATE; /* XXX wrong value */ 303 } else { 304 /* enable VRA */ 305 auvia_write_codec(sc, AC97_REG_EXTENDED_STATUS, 306 AC97_ENAB_VRA | AC97_ENAB_MICVRA); 307 sc->sc_fixed_rate = 0; 308 } 309 310 /* disable mutes */ 311 for (i = 0; i < 4; i++) { 312 static struct { 313 char *class, *device; 314 } d[] = { 315 { AudioCoutputs, AudioNmaster}, 316 { AudioCinputs, AudioNdac}, 317 { AudioCinputs, AudioNcd}, 318 { AudioCinputs, AudioNline}, 319 { AudioCrecord, AudioNvolume}, 320 }; 321 322 ctl.type = AUDIO_MIXER_ENUM; 323 ctl.un.ord = 0; 324 325 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, 326 d[i].class, d[i].device, AudioNmute); 327 auvia_set_port(sc, &ctl); 328 } 329 330 /* set a reasonable default volume */ 331 332 ctl.type = AUDIO_MIXER_VALUE; 333 ctl.un.value.num_channels = 2; 334 ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \ 335 ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 199; 336 337 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, 338 AudioCoutputs, AudioNmaster, NULL); 339 auvia_set_port(sc, &ctl); 340 341 audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev); 342 } 343 344 345 int 346 auvia_attach_codec(void *addr, struct ac97_codec_if *cif) 347 { 348 struct auvia_softc *sc = addr; 349 350 sc->codec_if = cif; 351 352 return 0; 353 } 354 355 356 void 357 auvia_reset_codec(void *addr) 358 { 359 #ifdef notyet /* XXX seems to make codec become unready... ??? */ 360 struct auvia_softc *sc = addr; 361 pcireg_t r; 362 363 /* perform a codec cold reset */ 364 365 r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK); 366 367 r &= ~AUVIA_PCICONF_ACNOTRST; /* enable RESET (active low) */ 368 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r); 369 delay(2); 370 371 r |= AUVIA_PCICONF_ACNOTRST; /* disable RESET (inactive high) */ 372 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r); 373 delay(200); 374 375 auvia_waitready_codec(sc); 376 #endif 377 } 378 379 380 int 381 auvia_waitready_codec(struct auvia_softc *sc) 382 { 383 int i; 384 385 /* poll until codec not busy */ 386 for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh, 387 AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++) 388 delay(1); 389 if (i >= TIMEOUT) { 390 printf("%s: codec busy\n", sc->sc_dev.dv_xname); 391 return 1; 392 } 393 394 return 0; 395 } 396 397 398 int 399 auvia_waitvalid_codec(struct auvia_softc *sc) 400 { 401 int i; 402 403 /* poll until codec valid */ 404 for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh, 405 AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++) 406 delay(1); 407 if (i >= TIMEOUT) { 408 printf("%s: codec invalid\n", sc->sc_dev.dv_xname); 409 return 1; 410 } 411 412 return 0; 413 } 414 415 416 int 417 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val) 418 { 419 struct auvia_softc *sc = addr; 420 421 if (auvia_waitready_codec(sc)) 422 return 1; 423 424 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL, 425 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val); 426 427 return 0; 428 } 429 430 431 int 432 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val) 433 { 434 struct auvia_softc *sc = addr; 435 436 if (auvia_waitready_codec(sc)) 437 return 1; 438 439 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL, 440 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg)); 441 442 if (auvia_waitready_codec(sc)) 443 return 1; 444 445 if (auvia_waitvalid_codec(sc)) 446 return 1; 447 448 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL); 449 450 return 0; 451 } 452 453 454 int 455 auvia_open(void *addr, int flags) 456 { 457 return 0; 458 } 459 460 461 void 462 auvia_close(void *addr) 463 { 464 struct auvia_softc *sc = addr; 465 466 auvia_halt_output(sc); 467 auvia_halt_input(sc); 468 469 sc->sc_play.sc_intr = NULL; 470 sc->sc_record.sc_intr = NULL; 471 } 472 473 474 int 475 auvia_query_encoding(void *addr, struct audio_encoding *fp) 476 { 477 switch (fp->index) { 478 case 0: 479 strcpy(fp->name, AudioEulinear); 480 fp->encoding = AUDIO_ENCODING_ULINEAR; 481 fp->precision = 8; 482 fp->flags = 0; 483 return (0); 484 case 1: 485 strcpy(fp->name, AudioEmulaw); 486 fp->encoding = AUDIO_ENCODING_ULAW; 487 fp->precision = 8; 488 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 489 return (0); 490 case 2: 491 strcpy(fp->name, AudioEalaw); 492 fp->encoding = AUDIO_ENCODING_ALAW; 493 fp->precision = 8; 494 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 495 return (0); 496 case 3: 497 strcpy(fp->name, AudioEslinear); 498 fp->encoding = AUDIO_ENCODING_SLINEAR; 499 fp->precision = 8; 500 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 501 return (0); 502 case 4: 503 strcpy(fp->name, AudioEslinear_le); 504 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 505 fp->precision = 16; 506 fp->flags = 0; 507 return (0); 508 case 5: 509 strcpy(fp->name, AudioEulinear_le); 510 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 511 fp->precision = 16; 512 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 513 return (0); 514 case 6: 515 strcpy(fp->name, AudioEslinear_be); 516 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 517 fp->precision = 16; 518 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 519 return (0); 520 case 7: 521 strcpy(fp->name, AudioEulinear_be); 522 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 523 fp->precision = 16; 524 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 525 return (0); 526 default: 527 return (EINVAL); 528 } 529 } 530 531 532 int 533 auvia_set_params(void *addr, int setmode, int usemode, 534 struct audio_params *play, struct audio_params *rec) 535 { 536 struct auvia_softc *sc = addr; 537 struct audio_params *p; 538 u_int16_t regval; 539 int reg, mode; 540 541 /* for mode in (RECORD, PLAY) */ 542 for (mode = AUMODE_RECORD; mode != -1; 543 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 544 if ((setmode & mode) == 0) 545 continue; 546 547 p = mode == AUMODE_PLAY ? play : rec; 548 549 if (p->sample_rate < 4000 || p->sample_rate > 48000 || 550 (p->precision != 8 && p->precision != 16) || 551 (p->channels != 1 && p->channels != 2)) 552 return (EINVAL); 553 554 reg = mode == AUMODE_PLAY ? 555 AC97_REG_PCM_FRONT_DAC_RATE : AC97_REG_PCM_LR_ADC_RATE; 556 557 if (!sc->sc_fixed_rate) { 558 auvia_write_codec(sc, reg, (u_int16_t) p->sample_rate); 559 auvia_read_codec(sc, reg, ®val); 560 p->sample_rate = regval; 561 } else 562 p->sample_rate = sc->sc_fixed_rate; 563 564 p->factor = 1; 565 p->sw_code = 0; 566 switch (p->encoding) { 567 case AUDIO_ENCODING_SLINEAR_BE: 568 if (p->precision == 16) 569 p->sw_code = swap_bytes; 570 else 571 p->sw_code = change_sign8; 572 break; 573 case AUDIO_ENCODING_SLINEAR_LE: 574 if (p->precision != 16) 575 p->sw_code = change_sign8; 576 break; 577 case AUDIO_ENCODING_ULINEAR_BE: 578 if (p->precision == 16) { 579 if (mode == AUMODE_PLAY) 580 p->sw_code = swap_bytes_change_sign16_le; 581 else 582 p->sw_code = change_sign16_swap_bytes_le; 583 } 584 break; 585 case AUDIO_ENCODING_ULINEAR_LE: 586 if (p->precision == 16) 587 p->sw_code = change_sign16_le; 588 break; 589 case AUDIO_ENCODING_ULAW: 590 if (mode == AUMODE_PLAY) { 591 p->factor = 2; 592 p->sw_code = mulaw_to_slinear16_le; 593 } else 594 p->sw_code = ulinear8_to_mulaw; 595 break; 596 case AUDIO_ENCODING_ALAW: 597 if (mode == AUMODE_PLAY) { 598 p->factor = 2; 599 p->sw_code = alaw_to_slinear16_le; 600 } else 601 p->sw_code = ulinear8_to_alaw; 602 break; 603 default: 604 return (EINVAL); 605 } 606 607 regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0) 608 | (p->precision * p->factor == 16 ? 609 AUVIA_RPMODE_16BIT : 0) 610 | AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL 611 | AUVIA_RPMODE_AUTOSTART; 612 613 if (mode == AUMODE_PLAY) { 614 sc->sc_play.sc_reg = regval; 615 } else { 616 sc->sc_record.sc_reg = regval; 617 } 618 } 619 620 return 0; 621 } 622 623 624 int 625 auvia_round_blocksize(void *addr, int blk) 626 { 627 return (blk & -32); 628 } 629 630 631 int 632 auvia_halt_output(void *addr) 633 { 634 struct auvia_softc *sc = addr; 635 636 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL, 637 AUVIA_RPCTRL_TERMINATE); 638 639 return 0; 640 } 641 642 643 int 644 auvia_halt_input(void *addr) 645 { 646 struct auvia_softc *sc = addr; 647 648 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL, 649 AUVIA_RPCTRL_TERMINATE); 650 651 return 0; 652 } 653 654 655 int 656 auvia_getdev(void *addr, struct audio_device *retp) 657 { 658 struct auvia_softc *sc = addr; 659 660 if (retp) { 661 strncpy(retp->name, "VIA VT82C686A", sizeof(retp->name)); 662 strncpy(retp->version, sc->sc_revision, sizeof(retp->version)); 663 strncpy(retp->config, "auvia", sizeof(retp->config)); 664 } 665 666 return 0; 667 } 668 669 670 int 671 auvia_set_port(void *addr, mixer_ctrl_t *cp) 672 { 673 struct auvia_softc *sc = addr; 674 675 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp)); 676 } 677 678 679 int 680 auvia_get_port(void *addr, mixer_ctrl_t *cp) 681 { 682 struct auvia_softc *sc = addr; 683 684 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp)); 685 } 686 687 688 int 689 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip) 690 { 691 struct auvia_softc *sc = addr; 692 693 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip)); 694 } 695 696 697 void * 698 auvia_malloc(void *addr, int direction, size_t size, int pool, int flags) 699 { 700 struct auvia_softc *sc = addr; 701 struct auvia_dma *p; 702 int error; 703 int rseg; 704 705 p = malloc(sizeof(*p), pool, flags); 706 if (!p) 707 return 0; 708 709 p->size = size; 710 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg, 711 1, &rseg, BUS_DMA_NOWAIT)) != 0) { 712 printf("%s: unable to allocate dma, error = %d\n", 713 sc->sc_dev.dv_xname, error); 714 goto fail_alloc; 715 } 716 717 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr, 718 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { 719 printf("%s: unable to map dma, error = %d\n", 720 sc->sc_dev.dv_xname, error); 721 goto fail_map; 722 } 723 724 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, 725 BUS_DMA_NOWAIT, &p->map)) != 0) { 726 printf("%s: unable to create dma map, error = %d\n", 727 sc->sc_dev.dv_xname, error); 728 goto fail_create; 729 } 730 731 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL, 732 BUS_DMA_NOWAIT)) != 0) { 733 printf("%s: unable to load dma map, error = %d\n", 734 sc->sc_dev.dv_xname, error); 735 goto fail_load; 736 } 737 738 p->next = sc->sc_dmas; 739 sc->sc_dmas = p; 740 741 return p->addr; 742 743 744 fail_load: 745 bus_dmamap_destroy(sc->sc_dmat, p->map); 746 fail_create: 747 bus_dmamem_unmap(sc->sc_dmat, p->addr, size); 748 fail_map: 749 bus_dmamem_free(sc->sc_dmat, &p->seg, 1); 750 fail_alloc: 751 free(p, pool); 752 return 0; 753 } 754 755 756 void 757 auvia_free(void *addr, void *ptr, int pool) 758 { 759 struct auvia_softc *sc = addr; 760 struct auvia_dma **pp, *p; 761 762 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next) 763 if (p->addr == ptr) { 764 bus_dmamap_unload(sc->sc_dmat, p->map); 765 bus_dmamap_destroy(sc->sc_dmat, p->map); 766 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 767 bus_dmamem_free(sc->sc_dmat, &p->seg, 1); 768 769 *pp = p->next; 770 free(p, pool); 771 return; 772 } 773 774 panic("auvia_free: trying to free unallocated memory"); 775 } 776 777 778 size_t 779 auvia_round_buffersize(void *addr, int direction, size_t size) 780 { 781 return size; 782 } 783 784 785 paddr_t 786 auvia_mappage(void *addr, void *mem, off_t off, int prot) 787 { 788 struct auvia_softc *sc = addr; 789 struct auvia_dma *p; 790 791 if (off < 0) 792 return -1; 793 794 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next) 795 ; 796 797 if (!p) 798 return -1; 799 800 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot, 801 BUS_DMA_WAITOK); 802 } 803 804 805 int 806 auvia_get_props(void *addr) 807 { 808 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT 809 | AUDIO_PROP_FULLDUPLEX; 810 } 811 812 813 int 814 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch, 815 struct auvia_dma *p, void *start, void *end, int blksize) 816 { 817 struct auvia_dma_op *op; 818 struct auvia_dma *dp; 819 bus_addr_t s, e; 820 size_t l; 821 int segs; 822 823 s = p->map->dm_segs[0].ds_addr; 824 l = ((char *)end - (char *)start); 825 e = s + l; 826 segs = (l + blksize - 1) / blksize; 827 828 if (segs > (ch->sc_dma_op_count)) { 829 /* if old list was too small, free it */ 830 if (ch->sc_dma_ops) { 831 auvia_free(sc, ch->sc_dma_ops, M_DEVBUF); 832 } 833 834 ch->sc_dma_ops = auvia_malloc(sc, 0, 835 sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK); 836 837 if (ch->sc_dma_ops == NULL) { 838 printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname); 839 return 1; 840 } 841 842 for (dp = sc->sc_dmas; 843 dp && dp->addr != (void *)(ch->sc_dma_ops); 844 dp = dp->next) 845 ; 846 847 if (!dp) 848 panic("%s: build_dma_ops: where'd my memory go??? " 849 "address (%p)\n", sc->sc_dev.dv_xname, 850 ch->sc_dma_ops); 851 852 ch->sc_dma_op_count = segs; 853 ch->sc_dma_ops_dma = dp; 854 } 855 856 dp = ch->sc_dma_ops_dma; 857 op = ch->sc_dma_ops; 858 859 while (l) { 860 op->ptr = s; 861 l = l - blksize; 862 if (!l) { 863 /* if last block */ 864 op->flags = AUVIA_DMAOP_EOL | blksize; 865 } else { 866 op->flags = AUVIA_DMAOP_FLAG | blksize; 867 } 868 s += blksize; 869 op++; 870 } 871 872 return 0; 873 } 874 875 876 int 877 auvia_trigger_output(void *addr, void *start, void *end, 878 int blksize, void (*intr)(void *), void *arg, 879 struct audio_params *param) 880 { 881 struct auvia_softc *sc = addr; 882 struct auvia_softc_chan *ch = &(sc->sc_play); 883 struct auvia_dma *p; 884 885 for (p = sc->sc_dmas; p && p->addr != start; p = p->next) 886 ; 887 888 if (!p) 889 panic("auvia_trigger_output: request with bad start " 890 "address (%p)\n", start); 891 892 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) { 893 return 1; 894 } 895 896 ch->sc_intr = intr; 897 ch->sc_arg = arg; 898 899 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_DMAOPS_BASE, 900 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr); 901 902 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_MODE, 903 ch->sc_reg); 904 905 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL, 906 AUVIA_RPCTRL_START); 907 908 return 0; 909 } 910 911 912 int 913 auvia_trigger_input(void *addr, void *start, void *end, 914 int blksize, void (*intr)(void *), void *arg, 915 struct audio_params *param) 916 { 917 struct auvia_softc *sc = addr; 918 struct auvia_softc_chan *ch = &(sc->sc_record); 919 struct auvia_dma *p; 920 921 for (p = sc->sc_dmas; p && p->addr != start; p = p->next) 922 ; 923 924 if (!p) 925 panic("auvia_trigger_input: request with bad start " 926 "address (%p)\n", start); 927 928 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) { 929 return 1; 930 } 931 932 ch->sc_intr = intr; 933 ch->sc_arg = arg; 934 935 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_DMAOPS_BASE, 936 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr); 937 938 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_MODE, 939 ch->sc_reg); 940 941 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL, 942 AUVIA_RPCTRL_START); 943 944 return 0; 945 } 946 947 948 int 949 auvia_intr(void *arg) 950 { 951 struct auvia_softc *sc = arg; 952 u_int8_t r; 953 int rval; 954 955 rval = 0; 956 957 r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT); 958 if (r & AUVIA_RPSTAT_INTR) { 959 if (sc->sc_record.sc_intr) 960 sc->sc_record.sc_intr(sc->sc_record.sc_arg); 961 962 /* clear interrupts */ 963 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT, 964 AUVIA_RPSTAT_INTR); 965 rval = 1; 966 } 967 r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT); 968 if (r & AUVIA_RPSTAT_INTR) { 969 if (sc->sc_play.sc_intr) 970 sc->sc_play.sc_intr(sc->sc_play.sc_arg); 971 972 /* clear interrupts */ 973 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT, 974 AUVIA_RPSTAT_INTR); 975 rval = 1; 976 } 977 978 return rval; 979 } 980