1 /* $OpenBSD: fms.c,v 1.20 2008/06/26 05:42:17 ray 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 return 0; 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 return 0; 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 return 0; 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 return 0; 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 return 0; 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 return 0; 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 return 0; 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 return 0; 509 default: 510 return EINVAL; 511 } 512 } 513 514 void 515 fms_get_default_params(void *addr, int mode, struct audio_params *params) 516 { 517 ac97_get_default_params(params); 518 } 519 520 /* 521 * Range below -limit- is set to -rate- 522 * What a pity FM801 does not have 24000 523 * 24000 -> 22050 sounds rather poor 524 */ 525 struct { 526 int limit; 527 int rate; 528 } fms_rates[11] = { 529 { 6600, 5500 }, 530 { 8750, 8000 }, 531 { 10250, 9600 }, 532 { 13200, 11025 }, 533 { 17500, 16000 }, 534 { 20500, 19200 }, 535 { 26500, 22050 }, 536 { 35000, 32000 }, 537 { 41000, 38400 }, 538 { 46000, 44100 }, 539 { 48000, 48000 }, 540 /* anything above -> 48000 */ 541 }; 542 543 int 544 fms_set_params(addr, setmode, usemode, play, rec) 545 void *addr; 546 int setmode, usemode; 547 struct audio_params *play, *rec; 548 { 549 struct fms_softc *sc = addr; 550 int i; 551 552 if (setmode & AUMODE_PLAY) { 553 play->factor = 1; 554 play->sw_code = 0; 555 switch(play->encoding) { 556 case AUDIO_ENCODING_ULAW: 557 play->factor = 2; 558 play->sw_code = mulaw_to_slinear16_le; 559 break; 560 case AUDIO_ENCODING_SLINEAR_LE: 561 if (play->precision == 8) 562 play->sw_code = change_sign8; 563 break; 564 case AUDIO_ENCODING_ULINEAR_LE: 565 if (play->precision == 16) 566 play->sw_code = change_sign16_le; 567 break; 568 case AUDIO_ENCODING_ALAW: 569 play->factor = 2; 570 play->sw_code = alaw_to_slinear16_le; 571 break; 572 case AUDIO_ENCODING_SLINEAR_BE: 573 if (play->precision == 16) 574 play->sw_code = swap_bytes; 575 else 576 play->sw_code = change_sign8; 577 break; 578 case AUDIO_ENCODING_ULINEAR_BE: 579 if (play->precision == 16) 580 play->sw_code = change_sign16_swap_bytes_le; 581 break; 582 default: 583 return EINVAL; 584 } 585 for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit; 586 i++) 587 ; 588 play->sample_rate = fms_rates[i].rate; 589 sc->sc_play_reg = (play->channels == 2 ? FM_PLAY_STEREO : 0) | 590 (play->precision * play->factor == 16 ? FM_PLAY_16BIT : 0) | 591 (i << 8); 592 } 593 594 if (setmode & AUMODE_RECORD) { 595 596 rec->factor = 1; 597 rec->sw_code = 0; 598 switch(rec->encoding) { 599 case AUDIO_ENCODING_ULAW: 600 rec->sw_code = ulinear8_to_mulaw; 601 break; 602 case AUDIO_ENCODING_SLINEAR_LE: 603 if (rec->precision == 8) 604 rec->sw_code = change_sign8; 605 break; 606 case AUDIO_ENCODING_ULINEAR_LE: 607 if (rec->precision == 16) 608 rec->sw_code = change_sign16_le; 609 break; 610 case AUDIO_ENCODING_ALAW: 611 rec->sw_code = ulinear8_to_alaw; 612 break; 613 case AUDIO_ENCODING_SLINEAR_BE: 614 if (play->precision == 16) 615 play->sw_code = swap_bytes; 616 else 617 play->sw_code = change_sign8; 618 break; 619 case AUDIO_ENCODING_ULINEAR_BE: 620 if (play->precision == 16) 621 play->sw_code = swap_bytes_change_sign16_le; 622 break; 623 default: 624 return EINVAL; 625 } 626 for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit; 627 i++) 628 ; 629 rec->sample_rate = fms_rates[i].rate; 630 sc->sc_rec_reg = 631 (rec->channels == 2 ? FM_REC_STEREO : 0) | 632 (rec->precision * rec->factor == 16 ? FM_REC_16BIT : 0) | 633 (i << 8); 634 } 635 636 return 0; 637 } 638 639 int 640 fms_round_blocksize(addr, blk) 641 void *addr; 642 int blk; 643 { 644 return (blk + 0xf) & ~0xf; 645 } 646 647 int 648 fms_halt_output(addr) 649 void *addr; 650 { 651 struct fms_softc *sc = addr; 652 u_int16_t k1; 653 654 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL); 655 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL, 656 (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) | 657 FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST); 658 659 return 0; 660 } 661 662 int 663 fms_halt_input(addr) 664 void *addr; 665 { 666 struct fms_softc *sc = addr; 667 u_int16_t k1; 668 669 k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL); 670 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL, 671 (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) | 672 FM_REC_BUF1_LAST | FM_REC_BUF2_LAST); 673 674 return 0; 675 } 676 677 int 678 fms_getdev(addr, retp) 679 void *addr; 680 struct audio_device *retp; 681 { 682 *retp = fms_device; 683 return 0; 684 } 685 686 int 687 fms_set_port(addr, cp) 688 void *addr; 689 mixer_ctrl_t *cp; 690 { 691 struct fms_softc *sc = addr; 692 693 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp)); 694 } 695 696 int 697 fms_get_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_get_port(sc->codec_if, cp)); 704 } 705 706 void * 707 fms_malloc(addr, direction, size, pool, flags) 708 void *addr; 709 int direction; 710 size_t size; 711 int pool, flags; 712 { 713 struct fms_softc *sc = addr; 714 struct fms_dma *p; 715 int error; 716 int rseg; 717 718 p = malloc(sizeof(*p), pool, flags); 719 if (!p) 720 return 0; 721 722 p->size = size; 723 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &p->seg, 1, 724 &rseg, BUS_DMA_NOWAIT)) != 0) { 725 printf("%s: unable to allocate dma, error = %d\n", 726 sc->sc_dev.dv_xname, error); 727 goto fail_alloc; 728 } 729 730 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr, 731 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { 732 printf("%s: unable to map dma, error = %d\n", 733 sc->sc_dev.dv_xname, error); 734 goto fail_map; 735 } 736 737 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, 738 BUS_DMA_NOWAIT, &p->map)) != 0) { 739 printf("%s: unable to create dma map, error = %d\n", 740 sc->sc_dev.dv_xname, error); 741 goto fail_create; 742 } 743 744 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL, 745 BUS_DMA_NOWAIT)) != 0) { 746 printf("%s: unable to load dma map, error = %d\n", 747 sc->sc_dev.dv_xname, error); 748 goto fail_load; 749 } 750 751 p->next = sc->sc_dmas; 752 sc->sc_dmas = p; 753 754 return p->addr; 755 756 757 fail_load: 758 bus_dmamap_destroy(sc->sc_dmat, p->map); 759 fail_create: 760 bus_dmamem_unmap(sc->sc_dmat, p->addr, size); 761 fail_map: 762 bus_dmamem_free(sc->sc_dmat, &p->seg, 1); 763 fail_alloc: 764 free(p, pool); 765 return 0; 766 } 767 768 void 769 fms_free(addr, ptr, pool) 770 void *addr; 771 void *ptr; 772 int pool; 773 { 774 struct fms_softc *sc = addr; 775 struct fms_dma **pp, *p; 776 777 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next) 778 if (p->addr == ptr) { 779 bus_dmamap_unload(sc->sc_dmat, p->map); 780 bus_dmamap_destroy(sc->sc_dmat, p->map); 781 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 782 bus_dmamem_free(sc->sc_dmat, &p->seg, 1); 783 784 *pp = p->next; 785 free(p, pool); 786 return; 787 } 788 789 panic("fms_free: trying to free unallocated memory"); 790 } 791 792 paddr_t 793 fms_mappage(addr, mem, off, prot) 794 void *addr; 795 void *mem; 796 off_t off; 797 int prot; 798 { 799 struct fms_softc *sc = addr; 800 struct fms_dma *p; 801 802 if (off < 0) 803 return -1; 804 805 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next) 806 ; 807 if (!p) 808 return -1; 809 810 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot, 811 BUS_DMA_WAITOK); 812 } 813 814 int 815 fms_get_props(addr) 816 void *addr; 817 { 818 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | 819 AUDIO_PROP_FULLDUPLEX; 820 } 821 822 int 823 fms_query_devinfo(addr, dip) 824 void *addr; 825 mixer_devinfo_t *dip; 826 { 827 struct fms_softc *sc = addr; 828 829 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip)); 830 } 831 832 int 833 fms_trigger_output(addr, start, end, blksize, intr, arg, param) 834 void *addr; 835 void *start, *end; 836 int blksize; 837 void (*intr)(void *); 838 void *arg; 839 struct audio_params *param; 840 { 841 struct fms_softc *sc = addr; 842 struct fms_dma *p; 843 844 sc->sc_pintr = intr; 845 sc->sc_parg = arg; 846 847 for (p = sc->sc_dmas; p && p->addr != start; p = p->next) 848 ; 849 850 if (!p) 851 panic("fms_trigger_output: request with bad start " 852 "address (%p)", start); 853 854 sc->sc_play_start = p->map->dm_segs[0].ds_addr; 855 sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start); 856 sc->sc_play_blksize = blksize; 857 sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize; 858 sc->sc_play_flip = 0; 859 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1); 860 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1, 861 sc->sc_play_start); 862 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2, 863 sc->sc_play_nextblk); 864 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL, 865 FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg); 866 return 0; 867 } 868 869 870 int 871 fms_trigger_input(addr, start, end, blksize, intr, arg, param) 872 void *addr; 873 void *start, *end; 874 int blksize; 875 void (*intr)(void *); 876 void *arg; 877 struct audio_params *param; 878 { 879 struct fms_softc *sc = addr; 880 struct fms_dma *p; 881 882 sc->sc_rintr = intr; 883 sc->sc_rarg = arg; 884 885 for (p = sc->sc_dmas; p && p->addr != start; p = p->next) 886 ; 887 888 if (!p) 889 panic("fms_trigger_input: request with bad start " 890 "address (%p)", start); 891 892 sc->sc_rec_start = p->map->dm_segs[0].ds_addr; 893 sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start); 894 sc->sc_rec_blksize = blksize; 895 sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize; 896 sc->sc_rec_flip = 0; 897 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1); 898 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1, 899 sc->sc_rec_start); 900 bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2, 901 sc->sc_rec_nextblk); 902 bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL, 903 FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg); 904 return 0; 905 } 906 907 908