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