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