1 /* $OpenBSD: fms.c,v 1.22 2010/07/15 03:43:11 jakemsr Exp $ */ 2 /* $NetBSD: fms.c,v 1.5.4.1 2000/06/30 16:27:50 simonb 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 Witold J. Wnuk. 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 * Forte Media FM801 Audio Device Driver 35 */ 36 37 #include "radio.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/device.h> 44 #include <sys/audioio.h> 45 46 #include <machine/bus.h> 47 #include <machine/cpu.h> 48 49 #include <dev/pci/pcidevs.h> 50 #include <dev/pci/pcivar.h> 51 52 #include <dev/audio_if.h> 53 #include <dev/mulaw.h> 54 #include <dev/auconv.h> 55 56 #include <dev/ic/ac97.h> 57 #if 0 58 #include <dev/ic/mpuvar.h> 59 #endif 60 61 #include <dev/pci/fmsreg.h> 62 #include <dev/pci/fmsvar.h> 63 64 65 struct fms_dma { 66 struct fms_dma *next; 67 caddr_t addr; 68 size_t size; 69 bus_dmamap_t map; 70 bus_dma_segment_t seg; 71 }; 72 73 74 75 int fms_match(struct device *, void *, void *); 76 void fms_attach(struct device *, struct device *, void *); 77 int fms_intr(void *); 78 79 int fms_open(void *, int); 80 void fms_close(void *); 81 int fms_query_encoding(void *, struct audio_encoding *); 82 int fms_set_params(void *, int, int, struct audio_params *, 83 struct audio_params *); 84 void fms_get_default_params(void *, int, struct audio_params *); 85 int fms_round_blocksize(void *, int); 86 int fms_halt_output(void *); 87 int fms_halt_input(void *); 88 int fms_getdev(void *, struct audio_device *); 89 int fms_set_port(void *, mixer_ctrl_t *); 90 int fms_get_port(void *, mixer_ctrl_t *); 91 int fms_query_devinfo(void *, mixer_devinfo_t *); 92 void *fms_malloc(void *, int, size_t, int, int); 93 void fms_free(void *, void *, int); 94 paddr_t fms_mappage(void *, void *, off_t, int); 95 int fms_get_props(void *); 96 int fms_trigger_output(void *, void *, void *, int, void (*)(void *), 97 void *, struct audio_params *); 98 int fms_trigger_input(void *, void *, void *, int, void (*)(void *), 99 void *, struct audio_params *); 100 101 struct cfdriver fms_cd = { 102 NULL, "fms", DV_DULL 103 }; 104 105 struct cfattach fms_ca = { 106 sizeof (struct fms_softc), fms_match, fms_attach 107 }; 108 109 struct audio_device fms_device = { 110 "Forte Media 801", 111 "1.0", 112 "fms" 113 }; 114 115 116 struct audio_hw_if fms_hw_if = { 117 fms_open, 118 fms_close, 119 NULL, 120 fms_query_encoding, 121 fms_set_params, 122 fms_round_blocksize, 123 NULL, 124 NULL, 125 NULL, 126 NULL, 127 NULL, 128 fms_halt_output, 129 fms_halt_input, 130 NULL, 131 fms_getdev, 132 NULL, 133 fms_set_port, 134 fms_get_port, 135 fms_query_devinfo, 136 fms_malloc, 137 fms_free, 138 NULL, 139 fms_mappage, 140 fms_get_props, 141 fms_trigger_output, 142 fms_trigger_input, 143 fms_get_default_params 144 }; 145 146 int fms_attach_codec(void *, struct ac97_codec_if *); 147 int fms_read_codec(void *, u_int8_t, u_int16_t *); 148 int fms_write_codec(void *, u_int8_t, u_int16_t); 149 void fms_reset_codec(void *); 150 151 int fms_allocmem(struct fms_softc *, size_t, size_t, 152 struct fms_dma *); 153 int fms_freemem(struct fms_softc *, struct fms_dma *); 154 155 int 156 fms_match(parent, match, aux) 157 struct device *parent; 158 void *match; 159 void *aux; 160 { 161 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 162 163 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_FORTEMEDIA && 164 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_FORTEMEDIA_FM801) 165 return (1); 166 return (0); 167 } 168 169 void 170 fms_attach(parent, self, aux) 171 struct device *parent; 172 struct device *self; 173 void *aux; 174 { 175 struct pci_attach_args *pa = aux; 176 struct fms_softc *sc = (struct fms_softc *) self; 177 struct audio_attach_args aa; 178 pci_chipset_tag_t pc = pa->pa_pc; 179 pcitag_t pt = pa->pa_tag; 180 pci_intr_handle_t ih; 181 bus_size_t iosize; 182 const char *intrstr; 183 u_int16_t k1; 184 int i; 185 186 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot, 187 &sc->sc_ioh, NULL, &iosize, 0)) { 188 printf(": can't map i/o space\n"); 189 return; 190 } 191 192 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2, 193 &sc->sc_mpu_ioh)) { 194 printf(": can't get mpu subregion handle\n"); 195 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize); 196 return; 197 } 198 199 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4, 200 &sc->sc_opl_ioh)) { 201 printf(": can't get opl subregion handle\n"); 202 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize); 203 return; 204 } 205 206 if (pci_intr_map(pa, &ih)) { 207 printf(": couldn't map interrupt\n"); 208 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize); 209 return; 210 } 211 intrstr = pci_intr_string(pc, ih); 212 213 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, fms_intr, sc, 214 sc->sc_dev.dv_xname); 215 if (sc->sc_ih == NULL) { 216 printf(": couldn't establish interrupt"); 217 if (intrstr != NULL) 218 printf(" at %s", intrstr); 219 printf("\n"); 220 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize); 221 return; 222 } 223 224 printf(": %s\n", intrstr); 225 226 sc->sc_dmat = pa->pa_dmat; 227 228 /* Disable legacy audio (SBPro compatibility) */ 229 pci_conf_write(pc, pt, 0x40, 0); 230 231 /* Reset codec and AC'97 */ 232 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020); 233 delay(2); /* > 1us according to AC'97 documentation */ 234 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000); 235 delay(1); /* > 168.2ns according to AC'97 documentation */ 236 237 /* Set up volume */ 238 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808); 239 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808); 240 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808); 241 242 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000); 243 244 /* Unmask playback, record and mpu interrupts, mask the rest */ 245 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK); 246 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK, 247 (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) | 248 FM_INTMASK_VOL); 249 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS, 250 FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU | 251 FM_INTSTATUS_VOL); 252 253 #if NRADIO > 0 254 fmsradio_attach(sc); 255 #endif /* NRADIO > 0 */ 256 257 sc->host_if.arg = sc; 258 sc->host_if.attach = fms_attach_codec; 259 sc->host_if.read = fms_read_codec; 260 sc->host_if.write = fms_write_codec; 261 sc->host_if.reset = fms_reset_codec; 262 263 if (ac97_attach(&sc->host_if) != 0) 264 return; 265 266 /* Turn mute off */ 267 for (i = 0; i < 3; i++) { 268 static struct { 269 char *class, *device; 270 } d[] = { 271 { AudioCoutputs, AudioNmaster }, 272 { AudioCinputs, AudioNdac }, 273 { AudioCrecord, AudioNvolume } 274 }; 275 struct mixer_ctrl ctl; 276 277 ctl.type = AUDIO_MIXER_ENUM; 278 ctl.un.ord = 0; 279 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, 280 d[i].class, d[i].device, AudioNmute); 281 fms_set_port(sc, &ctl); 282 } 283 284 audio_attach_mi(&fms_hw_if, sc, &sc->sc_dev); 285 286 aa.type = AUDIODEV_TYPE_OPL; 287 aa.hwif = NULL; 288 aa.hdl = NULL; 289 config_found(&sc->sc_dev, &aa, audioprint); 290 291 aa.type = AUDIODEV_TYPE_MPU; 292 aa.hwif = NULL; 293 aa.hdl = NULL; 294 sc->sc_mpu_dev = config_found(&sc->sc_dev, &aa, audioprint); 295 } 296 297 /* 298 * Each AC-link frame takes 20.8us, data should be ready in next frame, 299 * we allow more than two. 300 */ 301 #define TIMO 50 302 int 303 fms_read_codec(addr, reg, val) 304 void *addr; 305 u_int8_t reg; 306 u_int16_t *val; 307 { 308 struct fms_softc *sc = addr; 309 int i; 310 311 /* Poll until codec is ready */ 312 for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh, 313 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++) 314 delay(1); 315 if (i >= TIMO) { 316 printf("fms: codec busy\n"); 317 return 1; 318 } 319 320 /* Write register index, read access */ 321 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, 322 reg | FM_CODEC_CMD_READ); 323 324 /* Poll until we have valid data */ 325 for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh, 326 FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++) 327 delay(1); 328 if (i >= TIMO) { 329 printf("fms: no data from codec\n"); 330 return 1; 331 } 332 333 /* Read data */ 334 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA); 335 return 0; 336 } 337 338 int 339 fms_write_codec(addr, reg, val) 340 void *addr; 341 u_int8_t reg; 342 u_int16_t val; 343 { 344 struct fms_softc *sc = addr; 345 int i; 346 347 /* Poll until codec is ready */ 348 for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh, 349 FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++) 350 delay(1); 351 if (i >= TIMO) { 352 printf("fms: codec busy\n"); 353 return 1; 354 } 355 356 /* Write data */ 357 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val); 358 /* Write index register, write access */ 359 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg); 360 return 0; 361 } 362 #undef TIMO 363 364 int 365 fms_attach_codec(addr, cif) 366 void *addr; 367 struct ac97_codec_if *cif; 368 { 369 struct fms_softc *sc = addr; 370 371 sc->codec_if = cif; 372 return 0; 373 } 374 375 /* Cold Reset */ 376 void 377 fms_reset_codec(addr) 378 void *addr; 379 { 380 struct fms_softc *sc = addr; 381 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020); 382 delay(2); 383 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000); 384 delay(1); 385 } 386 387 int 388 fms_intr(arg) 389 void *arg; 390 { 391 struct fms_softc *sc = arg; 392 u_int16_t istat; 393 394 istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS); 395 396 if (istat & FM_INTSTATUS_PLAY) { 397 if ((sc->sc_play_nextblk += sc->sc_play_blksize) >= 398 sc->sc_play_end) 399 sc->sc_play_nextblk = sc->sc_play_start; 400 401 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 402 sc->sc_play_flip++ & 1 ? 403 FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk); 404 405 if (sc->sc_pintr) 406 sc->sc_pintr(sc->sc_parg); 407 else 408 printf("unexpected play intr\n"); 409 } 410 411 if (istat & FM_INTSTATUS_REC) { 412 if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >= 413 sc->sc_rec_end) 414 sc->sc_rec_nextblk = sc->sc_rec_start; 415 416 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 417 sc->sc_rec_flip++ & 1 ? 418 FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk); 419 420 if (sc->sc_rintr) 421 sc->sc_rintr(sc->sc_rarg); 422 else 423 printf("unexpected rec intr\n"); 424 } 425 426 #if 0 427 if (istat & FM_INTSTATUS_MPU) 428 mpu_intr(sc->sc_mpu_dev); 429 #endif 430 431 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS, 432 istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC)); 433 434 return 1; 435 } 436 437 int 438 fms_open(addr, flags) 439 void *addr; 440 int flags; 441 { 442 /* UNUSED struct fms_softc *sc = addr;*/ 443 444 return 0; 445 } 446 447 void 448 fms_close(addr) 449 void *addr; 450 { 451 /* UNUSED struct fms_softc *sc = addr;*/ 452 } 453 454 int 455 fms_query_encoding(addr, fp) 456 void *addr; 457 struct audio_encoding *fp; 458 { 459 460 switch (fp->index) { 461 case 0: 462 strlcpy(fp->name, AudioEmulaw, sizeof fp->name); 463 fp->encoding = AUDIO_ENCODING_ULAW; 464 fp->precision = 8; 465 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 466 break; 467 case 1: 468 strlcpy(fp->name, AudioEslinear_le, sizeof fp->name); 469 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 470 fp->precision = 16; 471 fp->flags = 0; 472 break; 473 case 2: 474 strlcpy(fp->name, AudioEulinear, sizeof fp->name); 475 fp->encoding = AUDIO_ENCODING_ULINEAR; 476 fp->precision = 8; 477 fp->flags = 0; 478 break; 479 case 3: 480 strlcpy(fp->name, AudioEalaw, sizeof fp->name); 481 fp->encoding = AUDIO_ENCODING_ALAW; 482 fp->precision = 8; 483 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 484 break; 485 case 4: 486 strlcpy(fp->name, AudioEulinear_le, sizeof fp->name); 487 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 488 fp->precision = 16; 489 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 490 break; 491 case 5: 492 strlcpy(fp->name, AudioEslinear, sizeof fp->name); 493 fp->encoding = AUDIO_ENCODING_SLINEAR; 494 fp->precision = 8; 495 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 496 break; 497 case 6: 498 strlcpy(fp->name, AudioEulinear_be, sizeof fp->name); 499 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 500 fp->precision = 16; 501 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 502 break; 503 case 7: 504 strlcpy(fp->name, AudioEslinear_be, sizeof fp->name); 505 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 506 fp->precision = 16; 507 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 508 break; 509 default: 510 return EINVAL; 511 } 512 fp->bps = AUDIO_BPS(fp->precision); 513 fp->msb = 1; 514 515 return 0; 516 } 517 518 void 519 fms_get_default_params(void *addr, int mode, struct audio_params *params) 520 { 521 ac97_get_default_params(params); 522 } 523 524 /* 525 * Range below -limit- is set to -rate- 526 * What a pity FM801 does not have 24000 527 * 24000 -> 22050 sounds rather poor 528 */ 529 struct { 530 int limit; 531 int rate; 532 } fms_rates[11] = { 533 { 6600, 5500 }, 534 { 8750, 8000 }, 535 { 10250, 9600 }, 536 { 13200, 11025 }, 537 { 17500, 16000 }, 538 { 20500, 19200 }, 539 { 26500, 22050 }, 540 { 35000, 32000 }, 541 { 41000, 38400 }, 542 { 46000, 44100 }, 543 { 48000, 48000 }, 544 /* anything above -> 48000 */ 545 }; 546 547 int 548 fms_set_params(addr, setmode, usemode, play, rec) 549 void *addr; 550 int setmode, usemode; 551 struct audio_params *play, *rec; 552 { 553 struct fms_softc *sc = addr; 554 int i; 555 556 if (setmode & AUMODE_PLAY) { 557 play->factor = 1; 558 play->sw_code = 0; 559 switch(play->encoding) { 560 case AUDIO_ENCODING_ULAW: 561 play->factor = 2; 562 play->sw_code = mulaw_to_slinear16_le; 563 break; 564 case AUDIO_ENCODING_SLINEAR_LE: 565 if (play->precision == 8) 566 play->sw_code = change_sign8; 567 break; 568 case AUDIO_ENCODING_ULINEAR_LE: 569 if (play->precision == 16) 570 play->sw_code = change_sign16_le; 571 break; 572 case AUDIO_ENCODING_ALAW: 573 play->factor = 2; 574 play->sw_code = alaw_to_slinear16_le; 575 break; 576 case AUDIO_ENCODING_SLINEAR_BE: 577 if (play->precision == 16) 578 play->sw_code = swap_bytes; 579 else 580 play->sw_code = change_sign8; 581 break; 582 case AUDIO_ENCODING_ULINEAR_BE: 583 if (play->precision == 16) 584 play->sw_code = change_sign16_swap_bytes_le; 585 break; 586 default: 587 return EINVAL; 588 } 589 play->bps = AUDIO_BPS(play->precision); 590 play->msb = 1; 591 592 for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit; 593 i++) 594 ; 595 play->sample_rate = fms_rates[i].rate; 596 sc->sc_play_reg = (play->channels == 2 ? FM_PLAY_STEREO : 0) | 597 (play->precision * play->factor == 16 ? FM_PLAY_16BIT : 0) | 598 (i << 8); 599 } 600 601 if (setmode & AUMODE_RECORD) { 602 603 rec->factor = 1; 604 rec->sw_code = 0; 605 switch(rec->encoding) { 606 case AUDIO_ENCODING_ULAW: 607 rec->sw_code = ulinear8_to_mulaw; 608 break; 609 case AUDIO_ENCODING_SLINEAR_LE: 610 if (rec->precision == 8) 611 rec->sw_code = change_sign8; 612 break; 613 case AUDIO_ENCODING_ULINEAR_LE: 614 if (rec->precision == 16) 615 rec->sw_code = change_sign16_le; 616 break; 617 case AUDIO_ENCODING_ALAW: 618 rec->sw_code = ulinear8_to_alaw; 619 break; 620 case AUDIO_ENCODING_SLINEAR_BE: 621 if (rec->precision == 16) 622 rec->sw_code = swap_bytes; 623 else 624 rec->sw_code = change_sign8; 625 break; 626 case AUDIO_ENCODING_ULINEAR_BE: 627 if (rec->precision == 16) 628 rec->sw_code = swap_bytes_change_sign16_le; 629 break; 630 default: 631 return EINVAL; 632 } 633 rec->bps = AUDIO_BPS(rec->precision); 634 rec->msb = 1; 635 636 for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit; 637 i++) 638 ; 639 rec->sample_rate = fms_rates[i].rate; 640 sc->sc_rec_reg = 641 (rec->channels == 2 ? FM_REC_STEREO : 0) | 642 (rec->precision * rec->factor == 16 ? FM_REC_16BIT : 0) | 643 (i << 8); 644 } 645 646 return 0; 647 } 648 649 int 650 fms_round_blocksize(addr, blk) 651 void *addr; 652 int blk; 653 { 654 return (blk + 0xf) & ~0xf; 655 } 656 657 int 658 fms_halt_output(addr) 659 void *addr; 660 { 661 struct fms_softc *sc = addr; 662 u_int16_t k1; 663 664 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL); 665 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL, 666 (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) | 667 FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST); 668 669 return 0; 670 } 671 672 int 673 fms_halt_input(addr) 674 void *addr; 675 { 676 struct fms_softc *sc = addr; 677 u_int16_t k1; 678 679 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL); 680 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL, 681 (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) | 682 FM_REC_BUF1_LAST | FM_REC_BUF2_LAST); 683 684 return 0; 685 } 686 687 int 688 fms_getdev(addr, retp) 689 void *addr; 690 struct audio_device *retp; 691 { 692 *retp = fms_device; 693 return 0; 694 } 695 696 int 697 fms_set_port(addr, cp) 698 void *addr; 699 mixer_ctrl_t *cp; 700 { 701 struct fms_softc *sc = addr; 702 703 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp)); 704 } 705 706 int 707 fms_get_port(addr, cp) 708 void *addr; 709 mixer_ctrl_t *cp; 710 { 711 struct fms_softc *sc = addr; 712 713 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp)); 714 } 715 716 void * 717 fms_malloc(addr, direction, size, pool, flags) 718 void *addr; 719 int direction; 720 size_t size; 721 int pool, flags; 722 { 723 struct fms_softc *sc = addr; 724 struct fms_dma *p; 725 int error; 726 int rseg; 727 728 p = malloc(sizeof(*p), pool, flags); 729 if (!p) 730 return 0; 731 732 p->size = size; 733 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &p->seg, 1, 734 &rseg, BUS_DMA_NOWAIT)) != 0) { 735 printf("%s: unable to allocate dma, error = %d\n", 736 sc->sc_dev.dv_xname, error); 737 goto fail_alloc; 738 } 739 740 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr, 741 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { 742 printf("%s: unable to map dma, error = %d\n", 743 sc->sc_dev.dv_xname, error); 744 goto fail_map; 745 } 746 747 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, 748 BUS_DMA_NOWAIT, &p->map)) != 0) { 749 printf("%s: unable to create dma map, error = %d\n", 750 sc->sc_dev.dv_xname, error); 751 goto fail_create; 752 } 753 754 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL, 755 BUS_DMA_NOWAIT)) != 0) { 756 printf("%s: unable to load dma map, error = %d\n", 757 sc->sc_dev.dv_xname, error); 758 goto fail_load; 759 } 760 761 p->next = sc->sc_dmas; 762 sc->sc_dmas = p; 763 764 return p->addr; 765 766 767 fail_load: 768 bus_dmamap_destroy(sc->sc_dmat, p->map); 769 fail_create: 770 bus_dmamem_unmap(sc->sc_dmat, p->addr, size); 771 fail_map: 772 bus_dmamem_free(sc->sc_dmat, &p->seg, 1); 773 fail_alloc: 774 free(p, pool); 775 return 0; 776 } 777 778 void 779 fms_free(addr, ptr, pool) 780 void *addr; 781 void *ptr; 782 int pool; 783 { 784 struct fms_softc *sc = addr; 785 struct fms_dma **pp, *p; 786 787 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next) 788 if (p->addr == ptr) { 789 bus_dmamap_unload(sc->sc_dmat, p->map); 790 bus_dmamap_destroy(sc->sc_dmat, p->map); 791 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 792 bus_dmamem_free(sc->sc_dmat, &p->seg, 1); 793 794 *pp = p->next; 795 free(p, pool); 796 return; 797 } 798 799 panic("fms_free: trying to free unallocated memory"); 800 } 801 802 paddr_t 803 fms_mappage(addr, mem, off, prot) 804 void *addr; 805 void *mem; 806 off_t off; 807 int prot; 808 { 809 struct fms_softc *sc = addr; 810 struct fms_dma *p; 811 812 if (off < 0) 813 return -1; 814 815 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next) 816 ; 817 if (!p) 818 return -1; 819 820 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot, 821 BUS_DMA_WAITOK); 822 } 823 824 int 825 fms_get_props(addr) 826 void *addr; 827 { 828 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | 829 AUDIO_PROP_FULLDUPLEX; 830 } 831 832 int 833 fms_query_devinfo(addr, dip) 834 void *addr; 835 mixer_devinfo_t *dip; 836 { 837 struct fms_softc *sc = addr; 838 839 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip)); 840 } 841 842 int 843 fms_trigger_output(addr, start, end, blksize, intr, arg, param) 844 void *addr; 845 void *start, *end; 846 int blksize; 847 void (*intr)(void *); 848 void *arg; 849 struct audio_params *param; 850 { 851 struct fms_softc *sc = addr; 852 struct fms_dma *p; 853 854 sc->sc_pintr = intr; 855 sc->sc_parg = arg; 856 857 for (p = sc->sc_dmas; p && p->addr != start; p = p->next) 858 ; 859 860 if (!p) 861 panic("fms_trigger_output: request with bad start " 862 "address (%p)", start); 863 864 sc->sc_play_start = p->map->dm_segs[0].ds_addr; 865 sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start); 866 sc->sc_play_blksize = blksize; 867 sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize; 868 sc->sc_play_flip = 0; 869 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1); 870 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1, 871 sc->sc_play_start); 872 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2, 873 sc->sc_play_nextblk); 874 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL, 875 FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg); 876 return 0; 877 } 878 879 880 int 881 fms_trigger_input(addr, start, end, blksize, intr, arg, param) 882 void *addr; 883 void *start, *end; 884 int blksize; 885 void (*intr)(void *); 886 void *arg; 887 struct audio_params *param; 888 { 889 struct fms_softc *sc = addr; 890 struct fms_dma *p; 891 892 sc->sc_rintr = intr; 893 sc->sc_rarg = arg; 894 895 for (p = sc->sc_dmas; p && p->addr != start; p = p->next) 896 ; 897 898 if (!p) 899 panic("fms_trigger_input: request with bad start " 900 "address (%p)", start); 901 902 sc->sc_rec_start = p->map->dm_segs[0].ds_addr; 903 sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start); 904 sc->sc_rec_blksize = blksize; 905 sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize; 906 sc->sc_rec_flip = 0; 907 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1); 908 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1, 909 sc->sc_rec_start); 910 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2, 911 sc->sc_rec_nextblk); 912 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL, 913 FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg); 914 return 0; 915 } 916 917 918