1 /* $OpenBSD: emuxki.c,v 1.33 2009/03/29 21:53:52 sthen Exp $ */ 2 /* $NetBSD: emuxki.c,v 1.1 2001/10/17 18:39:41 jdolecek Exp $ */ 3 4 /*- 5 * Copyright (c) 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Yannick Montulet. 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 * Driver for Creative Labs SBLive! series and probably PCI512. 35 * 36 * Known bugs: 37 * - inversed stereo at ac97 codec level 38 * (XXX jdolecek - don't see the problem? maybe because auvia(4) has 39 * it swapped too?) 40 * - bass disappear when you plug rear jack-in on Cambridge FPS2000 speakers 41 * (and presumably all speakers that support front and rear jack-in) 42 * 43 * TODO: 44 * - Digital Outputs 45 * - (midi/mpu),joystick support 46 * - Multiple voices play (problem with /dev/audio architecture) 47 * - Multiple sources recording (Pb with audio(4)) 48 * - Independent modification of each channel's parameters (via mixer ?) 49 * - DSP FX patches (to make fx like chipmunk) 50 */ 51 52 #include <sys/types.h> 53 #include <sys/device.h> 54 #include <sys/errno.h> 55 #include <sys/malloc.h> 56 #include <sys/systm.h> 57 #include <sys/param.h> 58 #include <sys/audioio.h> 59 #include <sys/selinfo.h> 60 61 #include <dev/pci/pcireg.h> 62 #include <dev/pci/pcivar.h> 63 #include <dev/pci/pcidevs.h> 64 65 #include <dev/audio_if.h> 66 #include <dev/audiovar.h> 67 #include <dev/auconv.h> 68 #include <dev/mulaw.h> 69 #include <dev/ic/ac97.h> 70 71 #include <dev/pci/emuxkireg.h> 72 #include <dev/pci/emuxkivar.h> 73 74 #define slinear16_to_ulinear8_le linear16_to_ulinear8_le; 75 76 /* autconf goo */ 77 int emuxki_match(struct device *, void *, void *); 78 void emuxki_attach(struct device *, struct device *, void *); 79 int emuxki_detach(struct device *, int); 80 int emuxki_scinit(struct emuxki_softc *sc); 81 void emuxki_pci_shutdown(struct emuxki_softc *sc); 82 83 /* dma mem mgmt */ 84 struct dmamem *emuxki_dmamem_alloc(bus_dma_tag_t, size_t, bus_size_t, 85 int, int, int); 86 void emuxki_dmamem_free(struct dmamem *, int); 87 void emuxki_dmamem_delete(struct dmamem *mem, int type); 88 89 struct emuxki_mem *emuxki_mem_new(struct emuxki_softc *sc, int ptbidx, 90 size_t size, int type, int flags); 91 void emuxki_mem_delete(struct emuxki_mem *mem, int type); 92 93 /* Emu10k1 init & shutdown */ 94 int emuxki_init(struct emuxki_softc *); 95 void emuxki_shutdown(struct emuxki_softc *); 96 97 /* Emu10k1 mem mgmt */ 98 void *emuxki_pmem_alloc(struct emuxki_softc *, size_t,int,int); 99 void *emuxki_rmem_alloc(struct emuxki_softc *, size_t,int,int); 100 101 /* 102 * Emu10k1 channels funcs : There is no direct access to channels, everything 103 * is done through voices I will at least provide channel based fx params 104 * modification, later... 105 */ 106 107 /* Emu10k1 voice mgmt */ 108 struct emuxki_voice *emuxki_voice_new(struct emuxki_softc *, u_int8_t); 109 void emuxki_voice_delete(struct emuxki_voice *); 110 int emuxki_voice_set_audioparms(struct emuxki_voice *, u_int8_t, u_int8_t, u_int32_t); 111 /* emuxki_voice_set_fxparms will come later, it'll need channel distinction */ 112 int emuxki_voice_set_bufparms(struct emuxki_voice *, void *, u_int32_t, u_int16_t); 113 int emuxki_voice_set_stereo(struct emuxki_voice *voice, u_int8_t stereo); 114 int emuxki_voice_dataloc_create(struct emuxki_voice *voice); 115 void emuxki_voice_dataloc_destroy(struct emuxki_voice *voice); 116 void emuxki_voice_commit_parms(struct emuxki_voice *); 117 void emuxki_voice_recsrc_release(struct emuxki_softc *sc, emuxki_recsrc_t source); 118 int emuxki_recsrc_reserve(struct emuxki_voice *voice, emuxki_recsrc_t source); 119 int emuxki_voice_adc_rate(struct emuxki_voice *); 120 u_int32_t emuxki_voice_curaddr(struct emuxki_voice *); 121 int emuxki_set_vparms(struct emuxki_voice *voice, struct audio_params *p); 122 int emuxki_voice_set_srate(struct emuxki_voice *voice, u_int32_t srate); 123 void emuxki_voice_start(struct emuxki_voice *, void (*) (void *), void *); 124 void emuxki_voice_halt(struct emuxki_voice *); 125 int emuxki_voice_channel_create(struct emuxki_voice *voice); 126 void emuxki_voice_channel_destroy(struct emuxki_voice *voice); 127 128 struct emuxki_channel *emuxki_channel_new(struct emuxki_voice *voice, u_int8_t num); 129 void emuxki_channel_delete(struct emuxki_channel *chan); 130 void emuxki_channel_start(struct emuxki_channel *chan); 131 void emuxki_channel_stop(struct emuxki_channel *chan); 132 void emuxki_channel_commit_fx(struct emuxki_channel *chan); 133 void emuxki_channel_commit_parms(struct emuxki_channel *chan); 134 void emuxki_channel_set_bufparms(struct emuxki_channel *chan, u_int32_t start, u_int32_t end); 135 void emuxki_channel_set_srate(struct emuxki_channel *chan, u_int32_t srate); 136 void emuxki_channel_set_fxsend(struct emuxki_channel *chan, 137 struct emuxki_chanparms_fxsend *fxsend); 138 void emuxki_chanparms_set_defaults(struct emuxki_channel *chan); 139 140 void emuxki_resched_timer(struct emuxki_softc *sc); 141 142 /* 143 * Emu10k1 stream mgmt : not done yet 144 */ 145 #if 0 146 struct emuxki_stream *emuxki_stream_new(struct emu10k1 *); 147 void emuxki_stream_delete(struct emuxki_stream *); 148 int emuxki_stream_set_audio_params(struct emuxki_stream *, u_int8_t, 149 u_int8_t, u_int8_t, u_int16_t); 150 void emuxki_stream_start(struct emuxki_stream *); 151 void emuxki_stream_halt(struct emuxki_stream *); 152 #endif 153 154 /* fx interface */ 155 void emuxki_initfx(struct emuxki_softc *sc); 156 void emuxki_dsp_addop(struct emuxki_softc *sc, u_int16_t *pc, u_int8_t op, 157 u_int16_t r, u_int16_t a, u_int16_t x, u_int16_t y); 158 void emuxki_write_micro(struct emuxki_softc *sc, u_int32_t pc, u_int32_t data); 159 160 /* audio interface callbacks */ 161 162 int emuxki_open(void *, int); 163 void emuxki_close(void *); 164 165 int emuxki_query_encoding(void *, struct audio_encoding *); 166 int emuxki_set_params(void *, int, int, 167 struct audio_params *, 168 struct audio_params *); 169 void emuxki_get_default_params(void *, int, struct audio_params *); 170 171 int emuxki_round_blocksize(void *, int); 172 size_t emuxki_round_buffersize(void *, int, size_t); 173 174 int emuxki_trigger_output(void *, void *, void *, int, void (*)(void *), 175 void *, struct audio_params *); 176 int emuxki_trigger_input(void *, void *, void *, int, void (*) (void *), 177 void *, struct audio_params *); 178 int emuxki_halt_output(void *); 179 int emuxki_halt_input(void *); 180 181 int emuxki_getdev(void *, struct audio_device *); 182 int emuxki_set_port(void *, mixer_ctrl_t *); 183 int emuxki_get_port(void *, mixer_ctrl_t *); 184 int emuxki_query_devinfo(void *, mixer_devinfo_t *); 185 186 void *emuxki_allocm(void *, int, size_t, int, int); 187 void emuxki_freem(void *, void *, int); 188 189 paddr_t emuxki_mappage(void *, void *, off_t, int); 190 int emuxki_get_props(void *); 191 192 /* Interrupt handler */ 193 int emuxki_intr(void *); 194 195 /* Emu10k1 AC97 interface callbacks */ 196 int emuxki_ac97_init(struct emuxki_softc *sc); 197 int emuxki_ac97_attach(void *, struct ac97_codec_if *); 198 int emuxki_ac97_read(void *, u_int8_t, u_int16_t *); 199 int emuxki_ac97_write(void *, u_int8_t, u_int16_t); 200 void emuxki_ac97_reset(void *); 201 202 const struct pci_matchid emuxki_devices[] = { 203 { PCI_VENDOR_CREATIVELABS, PCI_PRODUCT_CREATIVELABS_SBLIVE }, 204 { PCI_VENDOR_CREATIVELABS, PCI_PRODUCT_CREATIVELABS_AUDIGY }, 205 { PCI_VENDOR_CREATIVELABS, PCI_PRODUCT_CREATIVELABS_AUDIGY2 }, 206 }; 207 208 /* 209 * Autoconfig goo. 210 */ 211 struct cfdriver emu_cd = { 212 NULL, "emu", DV_DULL 213 }; 214 215 struct cfattach emu_ca = { 216 sizeof(struct emuxki_softc), 217 emuxki_match, 218 emuxki_attach, 219 emuxki_detach, 220 NULL /* config activate */ 221 }; 222 223 struct audio_hw_if emuxki_hw_if = { 224 emuxki_open, 225 emuxki_close, 226 NULL, /* drain */ 227 emuxki_query_encoding, 228 emuxki_set_params, 229 emuxki_round_blocksize, 230 NULL, /* commit settings */ 231 NULL, /* init_output */ 232 NULL, /* init_input */ 233 NULL, /* start_output */ 234 NULL, /* start_input */ 235 emuxki_halt_output, 236 emuxki_halt_input, 237 NULL, /* speaker_ctl */ 238 emuxki_getdev, 239 NULL, /* setfd */ 240 emuxki_set_port, 241 emuxki_get_port, 242 emuxki_query_devinfo, 243 emuxki_allocm, 244 emuxki_freem, 245 emuxki_round_buffersize, 246 emuxki_mappage, 247 emuxki_get_props, 248 emuxki_trigger_output, 249 emuxki_trigger_input, 250 emuxki_get_default_params 251 }; 252 253 #if 0 254 static const int emuxki_recsrc_intrmasks[EMU_NUMRECSRCS] = 255 { EMU_INTE_MICBUFENABLE, EMU_INTE_ADCBUFENABLE, EMU_INTE_EFXBUFENABLE }; 256 #endif 257 static const u_int32_t emuxki_recsrc_bufaddrreg[EMU_NUMRECSRCS] = 258 { EMU_MICBA, EMU_ADCBA, EMU_FXBA }; 259 static const u_int32_t emuxki_recsrc_szreg[EMU_NUMRECSRCS] = 260 { EMU_MICBS, EMU_ADCBS, EMU_FXBS }; 261 static const int emuxki_recbuf_sz[] = { 262 0, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792, 263 2048, 2560, 3072, 3584, 4096, 5120, 6144, 7168, 8192, 10240, 264 12288, 14366, 16384, 20480, 24576, 28672, 32768, 40960, 49152, 265 57344, 65536 266 }; 267 268 /* 269 * DMA memory mgmt 270 */ 271 272 void 273 emuxki_dmamem_delete(struct dmamem *mem, int type) 274 { 275 free(mem->segs, type); 276 free(mem, type); 277 } 278 279 struct dmamem * 280 emuxki_dmamem_alloc(bus_dma_tag_t dmat, size_t size, bus_size_t align, 281 int nsegs, int type, int flags) 282 { 283 struct dmamem *mem; 284 int bus_dma_flags; 285 286 /* Allocate memory for structure */ 287 if ((mem = malloc(sizeof(*mem), type, flags)) == NULL) 288 return (NULL); 289 mem->dmat = dmat; 290 mem->size = size; 291 mem->align = align; 292 mem->nsegs = nsegs; 293 mem->bound = 0; 294 295 mem->segs = malloc(mem->nsegs * sizeof(*(mem->segs)), type, flags); 296 if (mem->segs == NULL) { 297 free(mem, type); 298 return (NULL); 299 } 300 301 bus_dma_flags = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK; 302 if (bus_dmamem_alloc(dmat, mem->size, mem->align, mem->bound, 303 mem->segs, mem->nsegs, &(mem->rsegs), 304 bus_dma_flags)) { 305 emuxki_dmamem_delete(mem, type); 306 return (NULL); 307 } 308 309 if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, mem->size, 310 &(mem->kaddr), bus_dma_flags | BUS_DMA_COHERENT)) { 311 bus_dmamem_free(dmat, mem->segs, mem->nsegs); 312 emuxki_dmamem_delete(mem, type); 313 return (NULL); 314 } 315 316 if (bus_dmamap_create(dmat, mem->size, mem->nsegs, mem->size, 317 mem->bound, bus_dma_flags, &(mem->map))) { 318 bus_dmamem_unmap(dmat, mem->kaddr, mem->size); 319 bus_dmamem_free(dmat, mem->segs, mem->nsegs); 320 emuxki_dmamem_delete(mem, type); 321 return (NULL); 322 } 323 324 if (bus_dmamap_load(dmat, mem->map, mem->kaddr, 325 mem->size, NULL, bus_dma_flags)) { 326 bus_dmamap_destroy(dmat, mem->map); 327 bus_dmamem_unmap(dmat, mem->kaddr, mem->size); 328 bus_dmamem_free(dmat, mem->segs, mem->nsegs); 329 emuxki_dmamem_delete(mem, type); 330 return (NULL); 331 } 332 333 return (mem); 334 } 335 336 void 337 emuxki_dmamem_free(struct dmamem *mem, int type) 338 { 339 bus_dmamap_unload(mem->dmat, mem->map); 340 bus_dmamap_destroy(mem->dmat, mem->map); 341 bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size); 342 bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs); 343 emuxki_dmamem_delete(mem, type); 344 } 345 346 347 /* 348 * Autoconf device callbacks : attach and detach 349 */ 350 351 void 352 emuxki_pci_shutdown(struct emuxki_softc *sc) 353 { 354 if (sc->sc_ih != NULL) 355 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 356 if (sc->sc_ios) 357 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 358 } 359 360 int 361 emuxki_scinit(struct emuxki_softc *sc) 362 { 363 int err; 364 365 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 366 /* enable spdif(?) output on non-APS */ 367 (sc->sc_flags & EMUXKI_APS? 0 : EMU_HCFG_GPOUTPUT0) | 368 EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK | 369 EMU_HCFG_MUTEBUTTONENABLE); 370 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 371 EMU_INTE_SAMPLERATER | EMU_INTE_PCIERRENABLE); 372 373 if ((err = emuxki_init(sc))) 374 return (err); 375 376 if (sc->sc_flags & EMUXKI_AUDIGY2) { 377 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 378 EMU_HCFG_AUDIOENABLE | EMU_HCFG_AC3ENABLE_CDSPDIF | 379 EMU_HCFG_AC3ENABLE_GPSPDIF | EMU_HCFG_AUTOMUTE); 380 } else if (sc->sc_flags & EMUXKI_AUDIGY) { 381 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 382 EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE); 383 } else { 384 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 385 EMU_HCFG_AUDIOENABLE | EMU_HCFG_JOYENABLE | 386 EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_AUTOMUTE); 387 } 388 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 389 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) | 390 EMU_INTE_VOLINCRENABLE | EMU_INTE_VOLDECRENABLE | 391 EMU_INTE_MUTEENABLE); 392 393 if (sc->sc_flags & EMUXKI_AUDIGY2) { 394 if (sc->sc_flags & EMUXKI_CA0108) { 395 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG, 396 0x0060 | bus_space_read_4(sc->sc_iot, sc->sc_ioh, 397 EMU_A_IOCFG)); 398 } else { 399 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG, 400 EMU_A_IOCFG_GPOUT0 | bus_space_read_4(sc->sc_iot, 401 sc->sc_ioh, EMU_A_IOCFG)); 402 } 403 } 404 405 /* No multiple voice support for now */ 406 sc->pvoice = sc->rvoice = NULL; 407 408 return (0); 409 } 410 411 int 412 emuxki_ac97_init(struct emuxki_softc *sc) 413 { 414 sc->hostif.arg = sc; 415 sc->hostif.attach = emuxki_ac97_attach; 416 sc->hostif.read = emuxki_ac97_read; 417 sc->hostif.write = emuxki_ac97_write; 418 sc->hostif.reset = emuxki_ac97_reset; 419 sc->hostif.flags = NULL; 420 return (ac97_attach(&(sc->hostif))); 421 } 422 423 int 424 emuxki_match(struct device *parent, void *match, void *aux) 425 { 426 return (pci_matchbyid((struct pci_attach_args *)aux, emuxki_devices, 427 sizeof(emuxki_devices)/sizeof(emuxki_devices[0]))); 428 } 429 430 void 431 emuxki_attach(struct device *parent, struct device *self, void *aux) 432 { 433 struct emuxki_softc *sc = (struct emuxki_softc *) self; 434 struct pci_attach_args *pa = aux; 435 pci_intr_handle_t ih; 436 const char *intrstr; 437 438 if (pci_mapreg_map(pa, EMU_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 439 &(sc->sc_iot), &(sc->sc_ioh), &(sc->sc_iob), &(sc->sc_ios), 0)) { 440 printf(": can't map i/o space\n"); 441 return; 442 } 443 444 sc->sc_pc = pa->pa_pc; 445 sc->sc_dmat = pa->pa_dmat; 446 447 if (pci_intr_map(pa, &ih)) { 448 printf(": can't map interrupt\n"); 449 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 450 return; 451 } 452 453 intrstr = pci_intr_string(pa->pa_pc, ih); 454 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO, emuxki_intr, 455 sc, sc->sc_dev.dv_xname); 456 if (sc->sc_ih == NULL) { 457 printf(": can't establish interrupt"); 458 if (intrstr != NULL) 459 printf(" at %s", intrstr); 460 printf("\n"); 461 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 462 return; 463 } 464 printf(": %s\n", intrstr); 465 466 /* XXX it's unknown whether APS is made from Audigy as well */ 467 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_AUDIGY) { 468 sc->sc_flags |= EMUXKI_AUDIGY; 469 if (PCI_REVISION(pa->pa_class) == 0x04 || 470 PCI_REVISION(pa->pa_class) == 0x08) { 471 sc->sc_flags |= EMUXKI_AUDIGY2; 472 strlcpy(sc->sc_audv.name, "Audigy2", sizeof sc->sc_audv.name); 473 } else { 474 strlcpy(sc->sc_audv.name, "Audigy", sizeof sc->sc_audv.name); 475 } 476 } else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_AUDIGY2) { 477 sc->sc_flags |= EMUXKI_AUDIGY | EMUXKI_AUDIGY2; 478 if (pci_conf_read(pa->pa_pc, pa->pa_tag, 479 PCI_SUBSYS_ID_REG) == 0x10011102) { 480 sc->sc_flags |= EMUXKI_CA0108; 481 strlcpy(sc->sc_audv.name, "Audigy2Value", sizeof sc->sc_audv.name); 482 } else 483 strlcpy(sc->sc_audv.name, "Audigy2", sizeof sc->sc_audv.name); 484 } else if (pci_conf_read(pa->pa_pc, pa->pa_tag, 485 PCI_SUBSYS_ID_REG) == EMU_SUBSYS_APS) { 486 sc->sc_flags |= EMUXKI_APS; 487 strlcpy(sc->sc_audv.name, "E-mu APS", sizeof sc->sc_audv.name); 488 } else { 489 sc->sc_flags |= EMUXKI_SBLIVE; 490 strlcpy(sc->sc_audv.name, "SB Live!", sizeof sc->sc_audv.name); 491 } 492 snprintf(sc->sc_audv.version, sizeof sc->sc_audv.version, "0x%02x", 493 PCI_REVISION(pa->pa_class)); 494 strlcpy(sc->sc_audv.config, "emuxki", sizeof sc->sc_audv.config); 495 496 if (emuxki_scinit(sc) || 497 /* APS has no ac97 XXX */ 498 (sc->sc_flags & EMUXKI_APS || emuxki_ac97_init(sc)) || 499 (sc->sc_audev = audio_attach_mi(&emuxki_hw_if, sc, self)) == NULL) { 500 emuxki_pci_shutdown(sc); 501 return; 502 } 503 } 504 505 int 506 emuxki_detach(struct device *self, int flags) 507 { 508 struct emuxki_softc *sc = (struct emuxki_softc *) self; 509 510 if (sc->sc_audev != NULL) /* Test in case audio didn't attach */ 511 config_detach(sc->sc_audev, 0); 512 513 /* All voices should be stopped now but add some code here if not */ 514 515 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 516 EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK | 517 EMU_HCFG_MUTEBUTTONENABLE); 518 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 0); 519 520 emuxki_shutdown(sc); 521 522 emuxki_pci_shutdown(sc); 523 524 return (0); 525 } 526 527 528 /* Misc stuff relative to emu10k1 */ 529 530 static __inline u_int32_t 531 emuxki_rate_to_pitch(u_int32_t rate) 532 { 533 static const u_int32_t logMagTable[128] = { 534 0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 535 0x13aa2, 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 536 0x2655d, 0x28ed5, 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 537 0x381b6, 0x3a93d, 0x3d081, 0x3f782, 0x41e42, 0x444c1, 0x46b01, 538 0x49101, 0x4b6c4, 0x4dc49, 0x50191, 0x5269e, 0x54b6f, 0x57006, 539 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7, 0x646ee, 0x66a00, 540 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829, 0x759d4, 541 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e, 542 0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 543 0x93d26, 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 544 0xa11d8, 0xa2f9d, 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 545 0xadf26, 0xafbe7, 0xb1885, 0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 546 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899, 0xc1404, 0xc2f50, 0xc4a7b, 547 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c, 0xceaec, 0xd053f, 548 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3, 0xdba4a, 549 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3, 550 0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 551 0xf2c83, 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 552 0xfd1a7, 0xfe8df 553 }; 554 static const u_int8_t logSlopeTable[128] = { 555 0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58, 556 0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53, 557 0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f, 558 0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b, 559 0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47, 560 0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44, 561 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41, 562 0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e, 563 0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c, 564 0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39, 565 0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37, 566 0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35, 567 0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34, 568 0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32, 569 0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30, 570 0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f 571 }; 572 int8_t i; 573 574 if (rate == 0) 575 return 0; /* Bail out if no leading "1" */ 576 rate *= 11185; /* Scale 48000 to 0x20002380 */ 577 for (i = 31; i > 0; i--) { 578 if (rate & 0x80000000) { /* Detect leading "1" */ 579 return (((u_int32_t) (i - 15) << 20) + 580 logMagTable[0x7f & (rate >> 24)] + 581 (0x7f & (rate >> 17)) * 582 logSlopeTable[0x7f & (rate >> 24)]); 583 } 584 rate <<= 1; 585 } 586 587 return 0; /* Should never reach this point */ 588 } 589 590 /* Emu10k1 Low level */ 591 592 static __inline u_int32_t 593 emuxki_read(struct emuxki_softc *sc, u_int16_t chano, u_int32_t reg) 594 { 595 u_int32_t ptr, mask = 0xffffffff; 596 u_int8_t size, offset = 0; 597 int s; 598 599 ptr = ((((u_int32_t) reg) << 16) & 600 (sc->sc_flags & EMUXKI_AUDIGY ? 601 EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK)) | 602 (chano & EMU_PTR_CHNO_MASK); 603 if (reg & 0xff000000) { 604 size = (reg >> 24) & 0x3f; 605 offset = (reg >> 16) & 0x1f; 606 mask = ((1 << size) - 1) << offset; 607 } 608 609 s = splaudio(); 610 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_PTR, ptr); 611 ptr = (bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_DATA) & mask) 612 >> offset; 613 splx(s); 614 615 return (ptr); 616 } 617 618 static __inline void 619 emuxki_write(struct emuxki_softc *sc, u_int16_t chano, 620 u_int32_t reg, u_int32_t data) 621 { 622 u_int32_t ptr, mask; 623 u_int8_t size, offset; 624 int s; 625 626 ptr = ((((u_int32_t) reg) << 16) & 627 (sc->sc_flags & EMUXKI_AUDIGY ? 628 EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK)) | 629 (chano & EMU_PTR_CHNO_MASK); 630 631 /* BE CAREFUL WITH THAT AXE, EUGENE */ 632 if (ptr == 0x52 || ptr == 0x53) 633 return; 634 635 if (reg & 0xff000000) { 636 size = (reg >> 24) & 0x3f; 637 offset = (reg >> 16) & 0x1f; 638 mask = ((1 << size) - 1) << offset; 639 data = ((data << offset) & mask) | 640 (emuxki_read(sc, chano, reg & 0xffff) & ~mask); 641 } 642 643 s = splaudio(); 644 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_PTR, ptr); 645 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_DATA, data); 646 splx(s); 647 } 648 649 /* Microcode should this go in /sys/dev/microcode ? */ 650 651 void 652 emuxki_write_micro(struct emuxki_softc *sc, u_int32_t pc, u_int32_t data) 653 { 654 emuxki_write(sc, 0, 655 (sc->sc_flags & EMUXKI_AUDIGY ? 656 EMU_A_MICROCODEBASE : EMU_MICROCODEBASE) + pc, 657 data); 658 } 659 660 void 661 emuxki_dsp_addop(struct emuxki_softc *sc, u_int16_t *pc, u_int8_t op, 662 u_int16_t r, u_int16_t a, u_int16_t x, u_int16_t y) 663 { 664 if (sc->sc_flags & EMUXKI_AUDIGY) { 665 emuxki_write_micro(sc, *pc << 1, 666 ((x << 12) & EMU_A_DSP_LOWORD_OPX_MASK) | 667 (y & EMU_A_DSP_LOWORD_OPY_MASK)); 668 emuxki_write_micro(sc, (*pc << 1) + 1, 669 ((op << 24) & EMU_A_DSP_HIWORD_OPCODE_MASK) | 670 ((r << 12) & EMU_A_DSP_HIWORD_RESULT_MASK) | 671 (a & EMU_A_DSP_HIWORD_OPA_MASK)); 672 } else { 673 emuxki_write_micro(sc, *pc << 1, 674 ((x << 10) & EMU_DSP_LOWORD_OPX_MASK) | 675 (y & EMU_DSP_LOWORD_OPY_MASK)); 676 emuxki_write_micro(sc, (*pc << 1) + 1, 677 ((op << 20) & EMU_DSP_HIWORD_OPCODE_MASK) | 678 ((r << 10) & EMU_DSP_HIWORD_RESULT_MASK) | 679 (a & EMU_DSP_HIWORD_OPA_MASK)); 680 } 681 (*pc)++; 682 } 683 684 /* init and shutdown */ 685 686 void 687 emuxki_initfx(struct emuxki_softc *sc) 688 { 689 u_int16_t pc; 690 691 /* Set all GPRs to 0 */ 692 for (pc = 0; pc < 256; pc++) 693 emuxki_write(sc, 0, EMU_DSP_GPR(pc), 0); 694 for (pc = 0; pc < 160; pc++) { 695 emuxki_write(sc, 0, EMU_TANKMEMDATAREGBASE + pc, 0); 696 emuxki_write(sc, 0, EMU_TANKMEMADDRREGBASE + pc, 0); 697 } 698 pc = 0; 699 700 if (sc->sc_flags & EMUXKI_AUDIGY) { 701 /* AC97 Out (l/r) = AC97 In (l/r) + FX[0/1] * 4 */ 702 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 703 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT), 704 EMU_A_DSP_CST(0), 705 EMU_DSP_FX(0), EMU_A_DSP_CST(4)); 706 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 707 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_FRONT), 708 EMU_A_DSP_CST(0), 709 EMU_DSP_FX(1), EMU_A_DSP_CST(4)); 710 711 /* Rear channel OUT (l/r) = FX[2/3] * 4 */ 712 #if 0 713 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 714 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_REAR), 715 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT), 716 EMU_DSP_FX(0), EMU_A_DSP_CST(4)); 717 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 718 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_REAR), 719 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_FRONT), 720 EMU_DSP_FX(1), EMU_A_DSP_CST(4)); 721 #endif 722 /* ADC recording (l/r) = AC97 In (l/r) */ 723 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 724 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_ADC), 725 EMU_A_DSP_INL(EMU_DSP_IN_AC97), 726 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0)); 727 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 728 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_ADC), 729 EMU_A_DSP_INR(EMU_DSP_IN_AC97), 730 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0)); 731 732 /* zero out the rest of the microcode */ 733 while (pc < 512) 734 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 735 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0), 736 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0)); 737 738 emuxki_write(sc, 0, EMU_A_DBG, 0); /* Is it really necessary ? */ 739 } else { 740 /* AC97 Out (l/r) = AC97 In (l/r) + FX[0/1] * 4 */ 741 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 742 EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT), 743 EMU_DSP_CST(0), 744 EMU_DSP_FX(0), EMU_DSP_CST(4)); 745 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 746 EMU_DSP_OUTR(EMU_DSP_OUT_A_FRONT), 747 EMU_DSP_CST(0), 748 EMU_DSP_FX(1), EMU_DSP_CST(4)); 749 750 /* Rear channel OUT (l/r) = FX[2/3] * 4 */ 751 #if 0 752 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 753 EMU_DSP_OUTL(EMU_DSP_OUT_AD_REAR), 754 EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT), 755 EMU_DSP_FX(0), EMU_DSP_CST(4)); 756 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 757 EMU_DSP_OUTR(EMU_DSP_OUT_AD_REAR), 758 EMU_DSP_OUTR(EMU_DSP_OUT_A_FRONT), 759 EMU_DSP_FX(1), EMU_DSP_CST(4)); 760 #endif 761 /* ADC recording (l/r) = AC97 In (l/r) */ 762 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 763 EMU_DSP_OUTL(EMU_DSP_OUT_ADC), 764 EMU_DSP_INL(EMU_DSP_IN_AC97), 765 EMU_DSP_CST(0), EMU_DSP_CST(0)); 766 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 767 EMU_DSP_OUTR(EMU_DSP_OUT_ADC), 768 EMU_DSP_INR(EMU_DSP_IN_AC97), 769 EMU_DSP_CST(0), EMU_DSP_CST(0)); 770 771 /* zero out the rest of the microcode */ 772 while (pc < 512) 773 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 774 EMU_DSP_CST(0), EMU_DSP_CST(0), 775 EMU_DSP_CST(0), EMU_DSP_CST(0)); 776 777 emuxki_write(sc, 0, EMU_DBG, 0); /* Is it really necessary ? */ 778 } 779 } 780 781 int 782 emuxki_init(struct emuxki_softc *sc) 783 { 784 u_int16_t i; 785 u_int32_t spcs, *ptb; 786 bus_addr_t silentpage; 787 788 /* disable any channel interrupt */ 789 emuxki_write(sc, 0, EMU_CLIEL, 0); 790 emuxki_write(sc, 0, EMU_CLIEH, 0); 791 emuxki_write(sc, 0, EMU_SOLEL, 0); 792 emuxki_write(sc, 0, EMU_SOLEH, 0); 793 794 /* Set recording buffers sizes to zero */ 795 emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE); 796 emuxki_write(sc, 0, EMU_MICBA, 0); 797 emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE); 798 emuxki_write(sc, 0, EMU_FXBA, 0); 799 emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE); 800 emuxki_write(sc, 0, EMU_ADCBA, 0); 801 802 if (sc->sc_flags & EMUXKI_AUDIGY) { 803 emuxki_write(sc, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS); 804 emuxki_write(sc, 0, EMU_AC97SLOT, EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE); 805 } 806 807 /* Initialize all channels to stopped and no effects */ 808 for (i = 0; i < EMU_NUMCHAN; i++) { 809 emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0); 810 emuxki_write(sc, i, EMU_CHAN_IP, 0); 811 emuxki_write(sc, i, EMU_CHAN_VTFT, 0xffff); 812 emuxki_write(sc, i, EMU_CHAN_CVCF, 0xffff); 813 emuxki_write(sc, i, EMU_CHAN_PTRX, 0); 814 emuxki_write(sc, i, EMU_CHAN_CPF, 0); 815 emuxki_write(sc, i, EMU_CHAN_CCR, 0); 816 emuxki_write(sc, i, EMU_CHAN_PSST, 0); 817 emuxki_write(sc, i, EMU_CHAN_DSL, 0x10); /* Why 16 ? */ 818 emuxki_write(sc, i, EMU_CHAN_CCCA, 0); 819 emuxki_write(sc, i, EMU_CHAN_Z1, 0); 820 emuxki_write(sc, i, EMU_CHAN_Z2, 0); 821 emuxki_write(sc, i, EMU_CHAN_FXRT, 0x32100000); 822 emuxki_write(sc, i, EMU_CHAN_ATKHLDM, 0); 823 emuxki_write(sc, i, EMU_CHAN_DCYSUSM, 0); 824 emuxki_write(sc, i, EMU_CHAN_IFATN, 0xffff); 825 emuxki_write(sc, i, EMU_CHAN_PEFE, 0); 826 emuxki_write(sc, i, EMU_CHAN_FMMOD, 0); 827 emuxki_write(sc, i, EMU_CHAN_TREMFRQ, 24); 828 emuxki_write(sc, i, EMU_CHAN_FM2FRQ2, 24); 829 emuxki_write(sc, i, EMU_CHAN_TEMPENV, 0); 830 831 /* these are last so OFF prevents writing */ 832 emuxki_write(sc, i, EMU_CHAN_LFOVAL2, 0); 833 emuxki_write(sc, i, EMU_CHAN_LFOVAL1, 0); 834 emuxki_write(sc, i, EMU_CHAN_ATKHLDV, 0); 835 emuxki_write(sc, i, EMU_CHAN_ENVVOL, 0); 836 emuxki_write(sc, i, EMU_CHAN_ENVVAL, 0); 837 } 838 839 /* set digital outputs format */ 840 spcs = (EMU_SPCS_CLKACCY_1000PPM | EMU_SPCS_SAMPLERATE_48 | 841 EMU_SPCS_CHANNELNUM_LEFT | EMU_SPCS_SOURCENUM_UNSPEC | 842 EMU_SPCS_GENERATIONSTATUS | 0x00001200 /* Cat code. */ | 843 0x00000000 /* IEC-958 Mode */ | EMU_SPCS_EMPHASIS_NONE | 844 EMU_SPCS_COPYRIGHT); 845 emuxki_write(sc, 0, EMU_SPCS0, spcs); 846 emuxki_write(sc, 0, EMU_SPCS1, spcs); 847 emuxki_write(sc, 0, EMU_SPCS2, spcs); 848 849 if (sc->sc_flags & EMUXKI_CA0108) { 850 u_int32_t tmp; 851 852 /* Setup SRCMulti_I2S SamplingRate */ 853 tmp = emuxki_read(sc, 0, EMU_A_SPDIF_SAMPLERATE) & 0xfffff1ff; 854 emuxki_write(sc, 0, EMU_A_SPDIF_SAMPLERATE, tmp | 0x400); 855 856 /* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */ 857 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCSEL); 858 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, 859 EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI); 860 861 /* Setup SRCMulti Input Audio Enable */ 862 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, 0x7b0000); 863 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, 0xff000000); 864 865 /* Setup SPDIF Out Audio Enable 866 * The Audigy 2 Value has a separate SPDIF out, 867 * so no need for a mixer switch */ 868 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, 0x7a0000); 869 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, 0xff000000); 870 tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG) & ~0x8; /* Clear bit 3 */ 871 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG, tmp); 872 } else if(sc->sc_flags & EMUXKI_AUDIGY2) { 873 emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE, EMU_A2_SPDIF_UNKNOWN); 874 875 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCSEL); 876 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, 877 EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI); 878 879 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCMULTI); 880 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, EMU_A2_SRCMULTI_ENABLE_INPUT); 881 } 882 883 884 /* Let's play with sound processor */ 885 emuxki_initfx(sc); 886 887 /* Here is our Page Table */ 888 if ((sc->ptb = emuxki_dmamem_alloc(sc->sc_dmat, 889 EMU_MAXPTE * sizeof(u_int32_t), 890 EMU_DMA_ALIGN, EMU_DMAMEM_NSEG, 891 M_DEVBUF, M_WAITOK)) == NULL) 892 return (ENOMEM); 893 894 /* This is necessary unless you like Metallic noise... */ 895 if ((sc->silentpage = emuxki_dmamem_alloc(sc->sc_dmat, EMU_PTESIZE, 896 EMU_DMA_ALIGN, EMU_DMAMEM_NSEG, M_DEVBUF, M_WAITOK))==NULL){ 897 emuxki_dmamem_free(sc->ptb, M_DEVBUF); 898 return (ENOMEM); 899 } 900 901 /* Zero out the silent page */ 902 /* This might not be always true, it might be 128 for 8bit channels */ 903 memset(KERNADDR(sc->silentpage), 0, DMASIZE(sc->silentpage)); 904 905 /* 906 * Set all the PTB Entries to the silent page We shift the physical 907 * address by one and OR it with the page number. I don't know what 908 * the ORed index is for, might be a very useful unused feature... 909 */ 910 silentpage = DMAADDR(sc->silentpage) << 1; 911 ptb = KERNADDR(sc->ptb); 912 for (i = 0; i < EMU_MAXPTE; i++) 913 ptb[i] = htole32(silentpage | i); 914 915 /* Write PTB address and set TCB to none */ 916 emuxki_write(sc, 0, EMU_PTB, DMAADDR(sc->ptb)); 917 emuxki_write(sc, 0, EMU_TCBS, 0); /* This means 16K TCB */ 918 emuxki_write(sc, 0, EMU_TCB, 0); /* No TCB use for now */ 919 920 /* 921 * Set channels MAPs to the silent page. 922 * I don't know what MAPs are for. 923 */ 924 silentpage |= EMU_CHAN_MAP_PTI_MASK; 925 for (i = 0; i < EMU_NUMCHAN; i++) { 926 emuxki_write(sc, i, EMU_CHAN_MAPA, silentpage); 927 emuxki_write(sc, i, EMU_CHAN_MAPB, silentpage); 928 sc->channel[i] = NULL; 929 } 930 931 /* Init voices list */ 932 LIST_INIT(&(sc->voices)); 933 934 /* Timer is stopped */ 935 sc->timerstate &= ~EMU_TIMER_STATE_ENABLED; 936 return (0); 937 } 938 939 void 940 emuxki_shutdown(struct emuxki_softc *sc) 941 { 942 u_int32_t i; 943 944 /* Disable any Channels interrupts */ 945 emuxki_write(sc, 0, EMU_CLIEL, 0); 946 emuxki_write(sc, 0, EMU_CLIEH, 0); 947 emuxki_write(sc, 0, EMU_SOLEL, 0); 948 emuxki_write(sc, 0, EMU_SOLEH, 0); 949 950 /* 951 * Should do some voice(stream) stopping stuff here, that's what will 952 * stop and deallocate all channels. 953 */ 954 955 /* Stop all channels */ 956 /* XXX This shouldn't be necessary, I'll remove once everything works */ 957 for (i = 0; i < EMU_NUMCHAN; i++) 958 emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0); 959 for (i = 0; i < EMU_NUMCHAN; i++) { 960 emuxki_write(sc, i, EMU_CHAN_VTFT, 0); 961 emuxki_write(sc, i, EMU_CHAN_CVCF, 0); 962 emuxki_write(sc, i, EMU_CHAN_PTRX, 0); 963 emuxki_write(sc, i, EMU_CHAN_CPF, 0); 964 } 965 966 /* 967 * Deallocate Emu10k1 caches and recording buffers. Again it will be 968 * removed because it will be done in voice shutdown. 969 */ 970 emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE); 971 emuxki_write(sc, 0, EMU_MICBA, 0); 972 emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE); 973 emuxki_write(sc, 0, EMU_FXBA, 0); 974 if (sc->sc_flags & EMUXKI_AUDIGY) { 975 emuxki_write(sc, 0, EMU_A_FXWC1, 0); 976 emuxki_write(sc, 0, EMU_A_FXWC2, 0); 977 } else 978 emuxki_write(sc, 0, EMU_FXWC, 0); 979 emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE); 980 emuxki_write(sc, 0, EMU_ADCBA, 0); 981 982 /* 983 * XXX I don't know yet how I will handle tank cache buffer, 984 * I don't even clearly know what it is for. 985 */ 986 emuxki_write(sc, 0, EMU_TCB, 0); /* 16K again */ 987 emuxki_write(sc, 0, EMU_TCBS, 0); 988 989 emuxki_write(sc, 0, EMU_DBG, 0x8000); /* necessary ? */ 990 991 emuxki_dmamem_free(sc->silentpage, M_DEVBUF); 992 emuxki_dmamem_free(sc->ptb, M_DEVBUF); 993 } 994 995 /* Emu10k1 Memory management */ 996 997 struct emuxki_mem * 998 emuxki_mem_new(struct emuxki_softc *sc, int ptbidx, 999 size_t size, int type, int flags) 1000 { 1001 struct emuxki_mem *mem; 1002 1003 if ((mem = malloc(sizeof(*mem), type, flags)) == NULL) 1004 return (NULL); 1005 1006 mem->ptbidx = ptbidx; 1007 if ((mem->dmamem = emuxki_dmamem_alloc(sc->sc_dmat, size, 1008 EMU_DMA_ALIGN, EMU_DMAMEM_NSEG, type, flags)) == NULL) { 1009 free(mem, type); 1010 return (NULL); 1011 } 1012 return (mem); 1013 } 1014 1015 void 1016 emuxki_mem_delete(struct emuxki_mem *mem, int type) 1017 { 1018 emuxki_dmamem_free(mem->dmamem, type); 1019 free(mem, type); 1020 } 1021 1022 void * 1023 emuxki_pmem_alloc(struct emuxki_softc *sc, size_t size, int type, int flags) 1024 { 1025 int i, j, s; 1026 size_t numblocks; 1027 struct emuxki_mem *mem; 1028 u_int32_t *ptb, silentpage; 1029 1030 ptb = KERNADDR(sc->ptb); 1031 silentpage = DMAADDR(sc->silentpage) << 1; 1032 numblocks = size / EMU_PTESIZE; 1033 if (size % EMU_PTESIZE) 1034 numblocks++; 1035 1036 for (i = 0; i < EMU_MAXPTE; i++) 1037 if ((letoh32(ptb[i]) & EMU_CHAN_MAP_PTE_MASK) == silentpage) { 1038 /* We look for a free PTE */ 1039 s = splaudio(); 1040 for (j = 0; j < numblocks; j++) 1041 if ((letoh32(ptb[i + j]) 1042 & EMU_CHAN_MAP_PTE_MASK) 1043 != silentpage) 1044 break; 1045 if (j == numblocks) { 1046 if ((mem = emuxki_mem_new(sc, i, 1047 size, type, flags)) == NULL) { 1048 splx(s); 1049 return (NULL); 1050 } 1051 for (j = 0; j < numblocks; j++) 1052 ptb[i + j] = 1053 htole32((((DMAADDR(mem->dmamem) + 1054 j * EMU_PTESIZE)) << 1) | (i + j)); 1055 LIST_INSERT_HEAD(&(sc->mem), mem, next); 1056 splx(s); 1057 return (KERNADDR(mem->dmamem)); 1058 } else 1059 i += j; 1060 splx(s); 1061 } 1062 return (NULL); 1063 } 1064 1065 void * 1066 emuxki_rmem_alloc(struct emuxki_softc *sc, size_t size, int type, int flags) 1067 { 1068 struct emuxki_mem *mem; 1069 int s; 1070 1071 mem = emuxki_mem_new(sc, EMU_RMEM, size, type, flags); 1072 if (mem == NULL) 1073 return (NULL); 1074 1075 s = splaudio(); 1076 LIST_INSERT_HEAD(&(sc->mem), mem, next); 1077 splx(s); 1078 1079 return (KERNADDR(mem->dmamem)); 1080 } 1081 1082 /* 1083 * emuxki_channel_* : Channel management functions 1084 * emuxki_chanparms_* : Channel parameters modification functions 1085 */ 1086 1087 /* 1088 * is splaudio necessary here, can the same voice be manipulated by two 1089 * different threads at a time ? 1090 */ 1091 void 1092 emuxki_chanparms_set_defaults(struct emuxki_channel *chan) 1093 { 1094 chan->fxsend.a.level = chan->fxsend.b.level = 1095 chan->fxsend.c.level = chan->fxsend.d.level = 1096 /* for audigy */ 1097 chan->fxsend.e.level = chan->fxsend.f.level = 1098 chan->fxsend.g.level = chan->fxsend.h.level = 1099 chan->voice->sc->sc_flags & EMUXKI_AUDIGY ? 1100 0xc0 : 0xff; /* not max */ 1101 1102 chan->fxsend.a.dest = 0x0; 1103 chan->fxsend.b.dest = 0x1; 1104 chan->fxsend.c.dest = 0x2; 1105 chan->fxsend.d.dest = 0x3; 1106 /* for audigy */ 1107 chan->fxsend.e.dest = 0x4; 1108 chan->fxsend.f.dest = 0x5; 1109 chan->fxsend.g.dest = 0x6; 1110 chan->fxsend.h.dest = 0x7; 1111 1112 chan->pitch.initial = 0x0000; /* shouldn't it be 0xE000 ? */ 1113 chan->pitch.current = 0x0000; /* should it be 0x0400 */ 1114 chan->pitch.target = 0x0000; /* the unity pitch shift ? */ 1115 chan->pitch.envelope_amount = 0x00; /* none */ 1116 1117 chan->initial_attenuation = 0x00; /* no attenuation */ 1118 chan->volume.current = 0x0000; /* no volume */ 1119 chan->volume.target = 0xffff; 1120 chan->volume.envelope.current_state = 0x8000; /* 0 msec delay */ 1121 chan->volume.envelope.hold_time = 0x7f; /* 0 msec */ 1122 chan->volume.envelope.attack_time = 0x7F; /* 5.5msec */ 1123 chan->volume.envelope.sustain_level = 0x7F; /* full */ 1124 chan->volume.envelope.decay_time = 0x7F; /* 22msec */ 1125 1126 chan->filter.initial_cutoff_frequency = 0xff; /* no filter */ 1127 chan->filter.current_cutoff_frequency = 0xffff; /* no filtering */ 1128 chan->filter.target_cutoff_frequency = 0xffff; /* no filtering */ 1129 chan->filter.lowpass_resonance_height = 0x0; 1130 chan->filter.interpolation_ROM = 0x1; /* full band */ 1131 chan->filter.envelope_amount = 0x7f; /* none */ 1132 chan->filter.LFO_modulation_depth = 0x00; /* none */ 1133 1134 chan->loop.start = 0x000000; 1135 chan->loop.end = 0x000010; /* Why ? */ 1136 1137 chan->modulation.envelope.current_state = 0x8000; 1138 chan->modulation.envelope.hold_time = 0x00; /* 127 better ? */ 1139 chan->modulation.envelope.attack_time = 0x00; /* infinite */ 1140 chan->modulation.envelope.sustain_level = 0x00; /* off */ 1141 chan->modulation.envelope.decay_time = 0x7f; /* 22 msec */ 1142 chan->modulation.LFO_state = 0x8000; 1143 1144 chan->vibrato_LFO.state = 0x8000; 1145 chan->vibrato_LFO.modulation_depth = 0x00; /* none */ 1146 chan->vibrato_LFO.vibrato_depth = 0x00; 1147 chan->vibrato_LFO.frequency = 0x00; /* Why set to 24 when 1148 * initialized ? */ 1149 1150 chan->tremolo_depth = 0x00; 1151 } 1152 1153 /* only call it at splaudio */ 1154 struct emuxki_channel * 1155 emuxki_channel_new(struct emuxki_voice *voice, u_int8_t num) 1156 { 1157 struct emuxki_channel *chan; 1158 1159 chan = malloc(sizeof(struct emuxki_channel), M_DEVBUF, M_WAITOK); 1160 if (chan == NULL) 1161 return (NULL); 1162 1163 chan->voice = voice; 1164 chan->num = num; 1165 emuxki_chanparms_set_defaults(chan); 1166 chan->voice->sc->channel[num] = chan; 1167 return (chan); 1168 } 1169 1170 /* only call it at splaudio */ 1171 void 1172 emuxki_channel_delete(struct emuxki_channel *chan) 1173 { 1174 chan->voice->sc->channel[chan->num] = NULL; 1175 free(chan, M_DEVBUF); 1176 } 1177 1178 void 1179 emuxki_channel_set_fxsend(struct emuxki_channel *chan, 1180 struct emuxki_chanparms_fxsend *fxsend) 1181 { 1182 /* Could do a memcpy ...*/ 1183 chan->fxsend.a.level = fxsend->a.level; 1184 chan->fxsend.b.level = fxsend->b.level; 1185 chan->fxsend.c.level = fxsend->c.level; 1186 chan->fxsend.d.level = fxsend->d.level; 1187 chan->fxsend.a.dest = fxsend->a.dest; 1188 chan->fxsend.b.dest = fxsend->b.dest; 1189 chan->fxsend.c.dest = fxsend->c.dest; 1190 chan->fxsend.d.dest = fxsend->d.dest; 1191 1192 /* for audigy */ 1193 chan->fxsend.e.level = fxsend->e.level; 1194 chan->fxsend.f.level = fxsend->f.level; 1195 chan->fxsend.g.level = fxsend->g.level; 1196 chan->fxsend.h.level = fxsend->h.level; 1197 chan->fxsend.e.dest = fxsend->e.dest; 1198 chan->fxsend.f.dest = fxsend->f.dest; 1199 chan->fxsend.g.dest = fxsend->g.dest; 1200 chan->fxsend.h.dest = fxsend->h.dest; 1201 } 1202 1203 void 1204 emuxki_channel_set_srate(struct emuxki_channel *chan, u_int32_t srate) 1205 { 1206 chan->pitch.target = (srate << 8) / 375; 1207 chan->pitch.target = (chan->pitch.target >> 1) + 1208 (chan->pitch.target & 1); 1209 chan->pitch.target &= 0xffff; 1210 chan->pitch.current = chan->pitch.target; 1211 chan->pitch.initial = 1212 (emuxki_rate_to_pitch(srate) >> 8) & EMU_CHAN_IP_MASK; 1213 } 1214 1215 /* voice params must be set before calling this */ 1216 void 1217 emuxki_channel_set_bufparms(struct emuxki_channel *chan, 1218 u_int32_t start, u_int32_t end) 1219 { 1220 chan->loop.start = start & EMU_CHAN_PSST_LOOPSTARTADDR_MASK; 1221 chan->loop.end = end & EMU_CHAN_DSL_LOOPENDADDR_MASK; 1222 } 1223 1224 void 1225 emuxki_channel_commit_fx(struct emuxki_channel *chan) 1226 { 1227 struct emuxki_softc *sc = chan->voice->sc; 1228 u_int8_t chano = chan->num; 1229 1230 if (sc->sc_flags & EMUXKI_AUDIGY) { 1231 emuxki_write(sc, chano, EMU_A_CHAN_FXRT1, 1232 (chan->fxsend.d.dest << 24) | 1233 (chan->fxsend.c.dest << 16) | 1234 (chan->fxsend.b.dest << 8) | 1235 (chan->fxsend.a.dest)); 1236 emuxki_write(sc, chano, EMU_A_CHAN_FXRT2, 1237 (chan->fxsend.h.dest << 24) | 1238 (chan->fxsend.g.dest << 16) | 1239 (chan->fxsend.f.dest << 8) | 1240 (chan->fxsend.e.dest)); 1241 emuxki_write(sc, chano, EMU_A_CHAN_SENDAMOUNTS, 1242 (chan->fxsend.e.level << 24) | 1243 (chan->fxsend.f.level << 16) | 1244 (chan->fxsend.g.level << 8) | 1245 (chan->fxsend.h.level)); 1246 } else { 1247 emuxki_write(sc, chano, EMU_CHAN_FXRT, 1248 (chan->fxsend.d.dest << 28) | 1249 (chan->fxsend.c.dest << 24) | 1250 (chan->fxsend.b.dest << 20) | 1251 (chan->fxsend.a.dest << 16)); 1252 } 1253 1254 emuxki_write(sc, chano, 0x10000000 | EMU_CHAN_PTRX, 1255 (chan->fxsend.a.level << 8) | chan->fxsend.b.level); 1256 emuxki_write(sc, chano, EMU_CHAN_DSL, 1257 (chan->fxsend.d.level << 24) | chan->loop.end); 1258 emuxki_write(sc, chano, EMU_CHAN_PSST, 1259 (chan->fxsend.c.level << 24) | chan->loop.start); 1260 } 1261 1262 void 1263 emuxki_channel_commit_parms(struct emuxki_channel *chan) 1264 { 1265 struct emuxki_voice *voice = chan->voice; 1266 struct emuxki_softc *sc = voice->sc; 1267 u_int32_t start, mapval; 1268 u_int8_t chano = chan->num; 1269 int s; 1270 1271 start = chan->loop.start + 1272 (voice->stereo ? 28 : 30) * (voice->b16 + 1); 1273 mapval = DMAADDR(sc->silentpage) << 1 | EMU_CHAN_MAP_PTI_MASK; 1274 1275 s = splaudio(); 1276 emuxki_write(sc, chano, EMU_CHAN_CPF_STEREO, voice->stereo); 1277 1278 emuxki_channel_commit_fx(chan); 1279 1280 emuxki_write(sc, chano, EMU_CHAN_CCCA, 1281 (chan->filter.lowpass_resonance_height << 28) | 1282 (chan->filter.interpolation_ROM << 25) | 1283 (voice->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT) | start); 1284 emuxki_write(sc, chano, EMU_CHAN_Z1, 0); 1285 emuxki_write(sc, chano, EMU_CHAN_Z2, 0); 1286 emuxki_write(sc, chano, EMU_CHAN_MAPA, mapval); 1287 emuxki_write(sc, chano, EMU_CHAN_MAPB, mapval); 1288 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRFILTER, 1289 chan->filter.current_cutoff_frequency); 1290 emuxki_write(sc, chano, EMU_CHAN_VTFT_FILTERTARGET, 1291 chan->filter.target_cutoff_frequency); 1292 emuxki_write(sc, chano, EMU_CHAN_ATKHLDM, 1293 (chan->modulation.envelope.hold_time << 8) | 1294 chan->modulation.envelope.attack_time); 1295 emuxki_write(sc, chano, EMU_CHAN_DCYSUSM, 1296 (chan->modulation.envelope.sustain_level << 8) | 1297 chan->modulation.envelope.decay_time); 1298 emuxki_write(sc, chano, EMU_CHAN_LFOVAL1, 1299 chan->modulation.LFO_state); 1300 emuxki_write(sc, chano, EMU_CHAN_LFOVAL2, 1301 chan->vibrato_LFO.state); 1302 emuxki_write(sc, chano, EMU_CHAN_FMMOD, 1303 (chan->vibrato_LFO.modulation_depth << 8) | 1304 chan->filter.LFO_modulation_depth); 1305 emuxki_write(sc, chano, EMU_CHAN_TREMFRQ, 1306 (chan->tremolo_depth << 8)); 1307 emuxki_write(sc, chano, EMU_CHAN_FM2FRQ2, 1308 (chan->vibrato_LFO.vibrato_depth << 8) | 1309 chan->vibrato_LFO.frequency); 1310 emuxki_write(sc, chano, EMU_CHAN_ENVVAL, 1311 chan->modulation.envelope.current_state); 1312 emuxki_write(sc, chano, EMU_CHAN_ATKHLDV, 1313 (chan->volume.envelope.hold_time << 8) | 1314 chan->volume.envelope.attack_time); 1315 emuxki_write(sc, chano, EMU_CHAN_ENVVOL, 1316 chan->volume.envelope.current_state); 1317 emuxki_write(sc, chano, EMU_CHAN_PEFE, 1318 (chan->pitch.envelope_amount << 8) | 1319 chan->filter.envelope_amount); 1320 splx(s); 1321 } 1322 1323 void 1324 emuxki_channel_start(struct emuxki_channel *chan) 1325 { 1326 struct emuxki_voice *voice = chan->voice; 1327 struct emuxki_softc *sc = voice->sc; 1328 u_int8_t cache_sample, cache_invalid_size, chano = chan->num; 1329 u_int32_t sample; 1330 int s; 1331 1332 cache_sample = voice->stereo ? 4 : 2; 1333 sample = voice->b16 ? 0x00000000 : 0x80808080; 1334 cache_invalid_size = (voice->stereo ? 28 : 30) * (voice->b16 + 1); 1335 1336 s = splaudio(); 1337 while (cache_sample--) { 1338 emuxki_write(sc, chano, EMU_CHAN_CD0 + cache_sample, 1339 sample); 1340 } 1341 emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0); 1342 emuxki_write(sc, chano, EMU_CHAN_CCR_READADDRESS, 64); 1343 emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 1344 cache_invalid_size); 1345 emuxki_write(sc, chano, EMU_CHAN_IFATN, 1346 (chan->filter.target_cutoff_frequency << 8) | 1347 chan->initial_attenuation); 1348 emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET, 1349 chan->volume.target); 1350 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL, 1351 chan->volume.current); 1352 emuxki_write(sc, 0, 1353 EMU_MKSUBREG(1, chano, EMU_SOLEL + (chano >> 5)), 1354 0); /* Clear stop on loop */ 1355 emuxki_write(sc, 0, 1356 EMU_MKSUBREG(1, chano, EMU_CLIEL + (chano >> 5)), 1357 0); /* Clear loop interrupt */ 1358 emuxki_write(sc, chano, EMU_CHAN_DCYSUSV, 1359 (chan->volume.envelope.sustain_level << 8) | 1360 chan->volume.envelope.decay_time); 1361 emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET, 1362 chan->pitch.target); 1363 emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH, 1364 chan->pitch.current); 1365 emuxki_write(sc, chano, EMU_CHAN_IP, chan->pitch.initial); 1366 1367 splx(s); 1368 } 1369 1370 void 1371 emuxki_channel_stop(struct emuxki_channel *chan) 1372 { 1373 int s; 1374 u_int8_t chano = chan->num; 1375 struct emuxki_softc *sc = chan->voice->sc; 1376 1377 s = splaudio(); 1378 emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET, 0); 1379 emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH, 0); 1380 emuxki_write(sc, chano, EMU_CHAN_IFATN_ATTENUATION, 0xff); 1381 emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET, 0); 1382 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL, 0); 1383 emuxki_write(sc, chano, EMU_CHAN_IP, 0); 1384 splx(s); 1385 } 1386 1387 /* 1388 * Voices management 1389 * emuxki_voice_dataloc : use(play or rec) independent dataloc union helpers 1390 * emuxki_voice_channel_* : play part of dataloc union helpers 1391 * emuxki_voice_recsrc_* : rec part of dataloc union helpers 1392 */ 1393 1394 /* Allocate channels for voice in case of play voice */ 1395 int 1396 emuxki_voice_channel_create(struct emuxki_voice *voice) 1397 { 1398 struct emuxki_channel **channel = voice->sc->channel; 1399 u_int8_t i, stereo = voice->stereo; 1400 int s; 1401 1402 for (i = 0; i < EMU_NUMCHAN; i += stereo + 1) { 1403 if ((stereo && (channel[i + 1] != NULL)) || 1404 (channel[i] != NULL)) /* Looking for free channels */ 1405 continue; 1406 s = splaudio(); 1407 if (stereo) { 1408 voice->dataloc.chan[1] = 1409 emuxki_channel_new(voice, i + 1); 1410 if (voice->dataloc.chan[1] == NULL) { 1411 splx(s); 1412 return (ENOMEM); 1413 } 1414 } 1415 voice->dataloc.chan[0] = emuxki_channel_new(voice, i); 1416 if (voice->dataloc.chan[0] == NULL) { 1417 if (stereo) { 1418 emuxki_channel_delete(voice->dataloc.chan[1]); 1419 voice->dataloc.chan[1] = NULL; 1420 } 1421 splx(s); 1422 return (ENOMEM); 1423 } 1424 splx(s); 1425 return (0); 1426 } 1427 return (EAGAIN); 1428 } 1429 1430 /* When calling this function we assume no one can access the voice */ 1431 void 1432 emuxki_voice_channel_destroy(struct emuxki_voice *voice) 1433 { 1434 emuxki_channel_delete(voice->dataloc.chan[0]); 1435 voice->dataloc.chan[0] = NULL; 1436 if (voice->stereo) 1437 emuxki_channel_delete(voice->dataloc.chan[1]); 1438 voice->dataloc.chan[1] = NULL; 1439 } 1440 1441 /* 1442 * Will come back when used in voice_dataloc_create 1443 */ 1444 int 1445 emuxki_recsrc_reserve(struct emuxki_voice *voice, emuxki_recsrc_t source) 1446 { 1447 if (source < 0 || source >= EMU_NUMRECSRCS) { 1448 #ifdef EMUXKI_DEBUG 1449 printf("Tried to reserve invalid source: %d\n", source); 1450 #endif 1451 return (EINVAL); 1452 } 1453 if (voice->sc->recsrc[source] == voice) 1454 return (0); /* XXX */ 1455 if (voice->sc->recsrc[source] != NULL) 1456 return (EBUSY); 1457 voice->sc->recsrc[source] = voice; 1458 return (0); 1459 } 1460 1461 /* When calling this function we assume the voice is stopped */ 1462 void 1463 emuxki_voice_recsrc_release(struct emuxki_softc *sc, emuxki_recsrc_t source) 1464 { 1465 sc->recsrc[source] = NULL; 1466 } 1467 1468 int 1469 emuxki_voice_dataloc_create(struct emuxki_voice *voice) 1470 { 1471 int error; 1472 1473 if (voice->use & EMU_VOICE_USE_PLAY) { 1474 if ((error = emuxki_voice_channel_create(voice))) 1475 return (error); 1476 } else { 1477 if ((error = 1478 emuxki_recsrc_reserve(voice, voice->dataloc.source))) 1479 return (error); 1480 } 1481 return (0); 1482 } 1483 1484 void 1485 emuxki_voice_dataloc_destroy(struct emuxki_voice *voice) 1486 { 1487 if (voice->use & EMU_VOICE_USE_PLAY) { 1488 if (voice->dataloc.chan[0] != NULL) 1489 emuxki_voice_channel_destroy(voice); 1490 } else { 1491 if (voice->dataloc.source != EMU_RECSRC_NOTSET) { 1492 emuxki_voice_recsrc_release(voice->sc, 1493 voice->dataloc.source); 1494 voice->dataloc.source = EMU_RECSRC_NOTSET; 1495 } 1496 } 1497 } 1498 1499 struct emuxki_voice * 1500 emuxki_voice_new(struct emuxki_softc *sc, u_int8_t use) 1501 { 1502 struct emuxki_voice *voice; 1503 int s; 1504 1505 s = splaudio(); 1506 voice = sc->lvoice; 1507 sc->lvoice = NULL; 1508 splx(s); 1509 1510 if (!voice) { 1511 if (!(voice = malloc(sizeof(*voice), M_DEVBUF, M_WAITOK))) 1512 return (NULL); 1513 } else if (voice->use != use) 1514 emuxki_voice_dataloc_destroy(voice); 1515 else 1516 goto skip_initialize; 1517 1518 voice->sc = sc; 1519 voice->state = !EMU_VOICE_STATE_STARTED; 1520 voice->stereo = EMU_VOICE_STEREO_NOTSET; 1521 voice->b16 = 0; 1522 voice->sample_rate = 0; 1523 if (use & EMU_VOICE_USE_PLAY) 1524 voice->dataloc.chan[0] = voice->dataloc.chan[1] = NULL; 1525 else 1526 voice->dataloc.source = EMU_RECSRC_NOTSET; 1527 voice->buffer = NULL; 1528 voice->blksize = 0; 1529 voice->trigblk = 0; 1530 voice->blkmod = 0; 1531 voice->inth = NULL; 1532 voice->inthparam = NULL; 1533 voice->use = use; 1534 1535 skip_initialize: 1536 s = splaudio(); 1537 LIST_INSERT_HEAD((&sc->voices), voice, next); 1538 splx(s); 1539 1540 return (voice); 1541 } 1542 1543 void 1544 emuxki_voice_delete(struct emuxki_voice *voice) 1545 { 1546 struct emuxki_softc *sc = voice->sc; 1547 struct emuxki_voice *lvoice; 1548 int s; 1549 1550 if (voice->state & EMU_VOICE_STATE_STARTED) 1551 emuxki_voice_halt(voice); 1552 1553 s = splaudio(); 1554 LIST_REMOVE(voice, next); 1555 lvoice = sc->lvoice; 1556 sc->lvoice = voice; 1557 splx(s); 1558 1559 if (lvoice) { 1560 emuxki_voice_dataloc_destroy(lvoice); 1561 free(lvoice, M_DEVBUF); 1562 } 1563 } 1564 1565 int 1566 emuxki_voice_set_stereo(struct emuxki_voice *voice, u_int8_t stereo) 1567 { 1568 int error; 1569 emuxki_recsrc_t source = 0; /* XXX: gcc */ 1570 struct emuxki_chanparms_fxsend fxsend; 1571 1572 if (! (voice->use & EMU_VOICE_USE_PLAY)) 1573 source = voice->dataloc.source; 1574 emuxki_voice_dataloc_destroy(voice); 1575 if (! (voice->use & EMU_VOICE_USE_PLAY)) 1576 voice->dataloc.source = source; 1577 voice->stereo = stereo; 1578 if ((error = emuxki_voice_dataloc_create(voice))) 1579 return (error); 1580 if (voice->use & EMU_VOICE_USE_PLAY) { 1581 fxsend.a.dest = 0x0; 1582 fxsend.b.dest = 0x1; 1583 fxsend.c.dest = 0x2; 1584 fxsend.d.dest = 0x3; 1585 /* for audigy */ 1586 fxsend.e.dest = 0x4; 1587 fxsend.f.dest = 0x5; 1588 fxsend.g.dest = 0x6; 1589 fxsend.h.dest = 0x7; 1590 if (voice->stereo) { 1591 fxsend.a.level = fxsend.c.level = 0xc0; 1592 fxsend.b.level = fxsend.d.level = 0x00; 1593 fxsend.e.level = fxsend.g.level = 0xc0; 1594 fxsend.f.level = fxsend.h.level = 0x00; 1595 emuxki_channel_set_fxsend(voice->dataloc.chan[0], 1596 &fxsend); 1597 fxsend.a.level = fxsend.c.level = 0x00; 1598 fxsend.b.level = fxsend.d.level = 0xc0; 1599 fxsend.e.level = fxsend.g.level = 0x00; 1600 fxsend.f.level = fxsend.h.level = 0xc0; 1601 emuxki_channel_set_fxsend(voice->dataloc.chan[1], 1602 &fxsend); 1603 } /* No else : default is good for mono */ 1604 } 1605 return (0); 1606 } 1607 1608 int 1609 emuxki_voice_set_srate(struct emuxki_voice *voice, u_int32_t srate) 1610 { 1611 if (voice->use & EMU_VOICE_USE_PLAY) { 1612 if (srate < 4000) 1613 srate = 4000; 1614 if (srate > 48000) 1615 srate = 48000; 1616 voice->sample_rate = srate; 1617 emuxki_channel_set_srate(voice->dataloc.chan[0], srate); 1618 if (voice->stereo) 1619 emuxki_channel_set_srate(voice->dataloc.chan[1], 1620 srate); 1621 } else { 1622 if (srate < 8000) 1623 srate = 8000; 1624 if (srate > 48000) 1625 srate = 48000; 1626 voice->sample_rate = srate; 1627 if (emuxki_voice_adc_rate(voice) < 0) { 1628 voice->sample_rate = 0; 1629 return (EINVAL); 1630 } 1631 } 1632 return (0); 1633 } 1634 1635 int 1636 emuxki_voice_set_audioparms(struct emuxki_voice *voice, u_int8_t stereo, 1637 u_int8_t b16, u_int32_t srate) 1638 { 1639 int error = 0; 1640 1641 /* 1642 * Audio driver tried to set recording AND playing params even if 1643 * device opened in play or record only mode ==> 1644 * modified emuxki_set_params. 1645 * Stays here for now just in case ... 1646 */ 1647 if (voice == NULL) { 1648 #ifdef EMUXKI_DEBUG 1649 printf("warning: tried to set unallocated voice params !!\n"); 1650 #endif 1651 return (0); 1652 } 1653 1654 if (voice->stereo == stereo && voice->b16 == b16 && 1655 voice->sample_rate == srate) 1656 return (0); 1657 1658 #ifdef EMUXKI_DEBUG 1659 printf("Setting %s voice params : %s, %u bits, %u hz\n", 1660 (voice->use & EMU_VOICE_USE_PLAY) ? "play" : "record", 1661 stereo ? "stereo" : "mono", (b16 + 1) * 8, srate); 1662 #endif 1663 1664 voice->b16 = b16; 1665 1666 /* sample rate must be set after any channel number changes */ 1667 if ((voice->stereo != stereo) || (voice->sample_rate != srate)) { 1668 if (voice->stereo != stereo) { 1669 if ((error = emuxki_voice_set_stereo(voice, stereo))) 1670 return (error); 1671 } 1672 error = emuxki_voice_set_srate(voice, srate); 1673 } 1674 return error; 1675 } 1676 1677 /* voice audio parms (see just before) must be set prior to this */ 1678 int 1679 emuxki_voice_set_bufparms(struct emuxki_voice *voice, void *ptr, 1680 u_int32_t bufsize, u_int16_t blksize) 1681 { 1682 struct emuxki_mem *mem; 1683 struct emuxki_channel **chan; 1684 u_int32_t start, end; 1685 u_int8_t sample_size; 1686 int idx; 1687 int error = EFAULT; 1688 1689 LIST_FOREACH(mem, &voice->sc->mem, next) { 1690 if (KERNADDR(mem->dmamem) != ptr) 1691 continue; 1692 1693 voice->buffer = mem; 1694 sample_size = (voice->b16 + 1) * (voice->stereo + 1); 1695 voice->trigblk = 0; /* This shouldn't be needed */ 1696 voice->blkmod = bufsize / blksize; 1697 if (bufsize % blksize) /* This should not happen */ 1698 voice->blkmod++; 1699 error = 0; 1700 1701 if (voice->use & EMU_VOICE_USE_PLAY) { 1702 voice->blksize = blksize / sample_size; 1703 chan = voice->dataloc.chan; 1704 start = (mem->ptbidx << 12) / sample_size; 1705 end = start + bufsize / sample_size; 1706 emuxki_channel_set_bufparms(chan[0], 1707 start, end); 1708 if (voice->stereo) 1709 emuxki_channel_set_bufparms(chan[1], 1710 start, end); 1711 voice->timerate = (u_int32_t) 48000 * 1712 voice->blksize / voice->sample_rate; 1713 if (voice->timerate < 5) 1714 error = EINVAL; 1715 } else { 1716 voice->blksize = blksize; 1717 for(idx = sizeof(emuxki_recbuf_sz) / 1718 sizeof(emuxki_recbuf_sz[0]); --idx >= 0;) 1719 if (emuxki_recbuf_sz[idx] == bufsize) 1720 break; 1721 if (idx < 0) { 1722 #ifdef EMUXKI_DEBUG 1723 printf("Invalid bufsize: %d\n", bufsize); 1724 #endif 1725 return (EINVAL); 1726 } 1727 emuxki_write(voice->sc, 0, 1728 emuxki_recsrc_szreg[voice->dataloc.source], idx); 1729 emuxki_write(voice->sc, 0, 1730 emuxki_recsrc_bufaddrreg[voice->dataloc.source], 1731 DMAADDR(mem->dmamem)); 1732 1733 /* Use timer to emulate DMA completion interrupt */ 1734 voice->timerate = (u_int32_t) 48000 * blksize / 1735 (voice->sample_rate * sample_size); 1736 if (voice->timerate < 5) { 1737 #ifdef EMUXKI_DEBUG 1738 printf("Invalid timerate: %d, blksize %d\n", 1739 voice->timerate, blksize); 1740 #endif 1741 error = EINVAL; 1742 } 1743 } 1744 1745 break; 1746 } 1747 1748 return (error); 1749 } 1750 1751 void 1752 emuxki_voice_commit_parms(struct emuxki_voice *voice) 1753 { 1754 if (voice->use & EMU_VOICE_USE_PLAY) { 1755 emuxki_channel_commit_parms(voice->dataloc.chan[0]); 1756 if (voice->stereo) 1757 emuxki_channel_commit_parms(voice->dataloc.chan[1]); 1758 } 1759 } 1760 1761 u_int32_t 1762 emuxki_voice_curaddr(struct emuxki_voice *voice) 1763 { 1764 int idxreg = 0; 1765 1766 /* XXX different semantics in these cases */ 1767 if (voice->use & EMU_VOICE_USE_PLAY) { 1768 /* returns number of samples (an l/r pair counts 1) */ 1769 return (emuxki_read(voice->sc, 1770 voice->dataloc.chan[0]->num, 1771 EMU_CHAN_CCCA_CURRADDR) - 1772 voice->dataloc.chan[0]->loop.start); 1773 } else { 1774 /* returns number of bytes */ 1775 switch (voice->dataloc.source) { 1776 case EMU_RECSRC_MIC: 1777 idxreg = (voice->sc->sc_flags & EMUXKI_AUDIGY) ? 1778 EMU_A_MICIDX : EMU_MICIDX; 1779 break; 1780 case EMU_RECSRC_ADC: 1781 idxreg = (voice->sc->sc_flags & EMUXKI_AUDIGY) ? 1782 EMU_A_ADCIDX : EMU_ADCIDX; 1783 break; 1784 case EMU_RECSRC_FX: 1785 idxreg = EMU_FXIDX; 1786 break; 1787 default: 1788 #ifdef EMUXKI_DEBUG 1789 printf("emu: bad recording source!\n"); 1790 #endif 1791 break; 1792 } 1793 return (emuxki_read(voice->sc, 0, EMU_RECIDX(idxreg)) 1794 & EMU_RECIDX_MASK); 1795 } 1796 return (0); 1797 } 1798 1799 void 1800 emuxki_resched_timer(struct emuxki_softc *sc) 1801 { 1802 struct emuxki_voice *voice; 1803 u_int16_t timerate = 1024; 1804 u_int8_t active = 0; 1805 int s; 1806 1807 s = splaudio(); 1808 LIST_FOREACH(voice, &sc->voices, next) { 1809 if ((voice->state & EMU_VOICE_STATE_STARTED) == 0) 1810 continue; 1811 active = 1; 1812 if (voice->timerate < timerate) 1813 timerate = voice->timerate; 1814 } 1815 1816 if (timerate & ~EMU_TIMER_RATE_MASK) 1817 timerate = 0; 1818 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_TIMER, timerate); 1819 if (!active && (sc->timerstate & EMU_TIMER_STATE_ENABLED)) { 1820 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 1821 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) & 1822 ~EMU_INTE_INTERTIMERENB); 1823 sc->timerstate &= ~EMU_TIMER_STATE_ENABLED; 1824 } else if (active && !(sc->timerstate & EMU_TIMER_STATE_ENABLED)) { 1825 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 1826 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) | 1827 EMU_INTE_INTERTIMERENB); 1828 sc->timerstate |= EMU_TIMER_STATE_ENABLED; 1829 } 1830 splx(s); 1831 } 1832 1833 int 1834 emuxki_voice_adc_rate(struct emuxki_voice *voice) 1835 { 1836 switch(voice->sample_rate) { 1837 case 48000: 1838 return EMU_ADCCR_SAMPLERATE_48; 1839 break; 1840 case 44100: 1841 return EMU_ADCCR_SAMPLERATE_44; 1842 break; 1843 case 32000: 1844 return EMU_ADCCR_SAMPLERATE_32; 1845 break; 1846 case 24000: 1847 return EMU_ADCCR_SAMPLERATE_24; 1848 break; 1849 case 22050: 1850 return EMU_ADCCR_SAMPLERATE_22; 1851 break; 1852 case 16000: 1853 return EMU_ADCCR_SAMPLERATE_16; 1854 break; 1855 case 12000: 1856 if (voice->sc->sc_flags & EMUXKI_AUDIGY) 1857 return EMU_A_ADCCR_SAMPLERATE_12; 1858 else { 1859 #ifdef EMUXKI_DEBUG 1860 printf("recording sample_rate not supported : %u\n", voice->sample_rate); 1861 #endif 1862 return (-1); 1863 } 1864 break; 1865 case 11000: 1866 if (voice->sc->sc_flags & EMUXKI_AUDIGY) 1867 return EMU_A_ADCCR_SAMPLERATE_11; 1868 else 1869 return EMU_ADCCR_SAMPLERATE_11; 1870 break; 1871 case 8000: 1872 if (voice->sc->sc_flags & EMUXKI_AUDIGY) 1873 return EMU_A_ADCCR_SAMPLERATE_8; 1874 else 1875 return EMU_ADCCR_SAMPLERATE_8; 1876 break; 1877 default: 1878 #ifdef EMUXKI_DEBUG 1879 printf("recording sample_rate not supported : %u\n", voice->sample_rate); 1880 #endif 1881 return (-1); 1882 } 1883 return (-1); /* shouldn't get here */ 1884 } 1885 1886 void 1887 emuxki_voice_start(struct emuxki_voice *voice, 1888 void (*inth) (void *), void *inthparam) 1889 { 1890 u_int32_t val; 1891 1892 voice->inth = inth; 1893 voice->inthparam = inthparam; 1894 if (voice->use & EMU_VOICE_USE_PLAY) { 1895 voice->trigblk = 1; 1896 emuxki_channel_start(voice->dataloc.chan[0]); 1897 if (voice->stereo) 1898 emuxki_channel_start(voice->dataloc.chan[1]); 1899 } else { 1900 voice->trigblk = 1; 1901 switch (voice->dataloc.source) { 1902 case EMU_RECSRC_ADC: 1903 /* XXX need to program DSP to output L+R 1904 * XXX in monaural case? */ 1905 if (voice->sc->sc_flags & EMUXKI_AUDIGY) { 1906 val = EMU_A_ADCCR_LCHANENABLE; 1907 if (voice->stereo) 1908 val |= EMU_A_ADCCR_RCHANENABLE; 1909 } else { 1910 val = EMU_ADCCR_LCHANENABLE; 1911 if (voice->stereo) 1912 val |= EMU_ADCCR_RCHANENABLE; 1913 } 1914 val |= emuxki_voice_adc_rate(voice); 1915 emuxki_write(voice->sc, 0, EMU_ADCCR, 0); 1916 emuxki_write(voice->sc, 0, EMU_ADCCR, val); 1917 break; 1918 case EMU_RECSRC_MIC: 1919 case EMU_RECSRC_FX: 1920 printf("unimplemented\n"); 1921 break; 1922 case EMU_RECSRC_NOTSET: 1923 default: 1924 break; 1925 } 1926 #if 0 1927 /* DMA completion interrupt is useless; use timer */ 1928 int s; 1929 s = splaudio(); 1930 val = emu_rd(sc, INTE, 4); 1931 val |= emuxki_recsrc_intrmasks[voice->dataloc.source]; 1932 emu_wr(sc, INTE, val, 4); 1933 splx(s); 1934 #endif 1935 } 1936 voice->state |= EMU_VOICE_STATE_STARTED; 1937 emuxki_resched_timer(voice->sc); 1938 } 1939 1940 void 1941 emuxki_voice_halt(struct emuxki_voice *voice) 1942 { 1943 if (voice->use & EMU_VOICE_USE_PLAY) { 1944 emuxki_channel_stop(voice->dataloc.chan[0]); 1945 if (voice->stereo) 1946 emuxki_channel_stop(voice->dataloc.chan[1]); 1947 } else { 1948 switch (voice->dataloc.source) { 1949 case EMU_RECSRC_ADC: 1950 emuxki_write(voice->sc, 0, EMU_ADCCR, 0); 1951 break; 1952 case EMU_RECSRC_FX: 1953 case EMU_RECSRC_MIC: 1954 printf("unimplemented\n"); 1955 break; 1956 case EMU_RECSRC_NOTSET: 1957 printf("Bad dataloc.source\n"); 1958 } 1959 /* This should reset buffer pointer */ 1960 emuxki_write(voice->sc, 0, 1961 emuxki_recsrc_szreg[voice->dataloc.source], 1962 EMU_RECBS_BUFSIZE_NONE); 1963 #if 0 1964 int s; 1965 s = splaudio(); 1966 val = emu_rd(sc, INTE, 4); 1967 val &= ~emuxki_recsrc_intrmasks[voice->dataloc.source]; 1968 emu_wr(sc, INTE, val, 4); 1969 splx(s); 1970 #endif 1971 } 1972 voice->state &= ~EMU_VOICE_STATE_STARTED; 1973 emuxki_resched_timer(voice->sc); 1974 } 1975 1976 /* 1977 * The interrupt handler 1978 */ 1979 int 1980 emuxki_intr(void *arg) 1981 { 1982 struct emuxki_softc *sc = arg; 1983 u_int32_t ipr, curblk, us = 0; 1984 struct emuxki_voice *voice; 1985 1986 while ((ipr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_IPR))) { 1987 if (ipr & EMU_IPR_INTERVALTIMER) { 1988 LIST_FOREACH(voice, &sc->voices, next) { 1989 if ((voice->state & 1990 EMU_VOICE_STATE_STARTED) == 0) 1991 continue; 1992 1993 curblk = emuxki_voice_curaddr(voice) / 1994 voice->blksize; 1995 #if 0 1996 if (curblk == voice->trigblk) { 1997 voice->inth(voice->inthparam); 1998 voice->trigblk++; 1999 voice->trigblk %= voice->blkmod; 2000 } 2001 #else 2002 while ((curblk >= voice->trigblk && 2003 curblk < (voice->trigblk + voice->blkmod / 2)) || 2004 ((int)voice->trigblk - (int)curblk) > 2005 (voice->blkmod / 2 + 1)) { 2006 voice->inth(voice->inthparam); 2007 voice->trigblk++; 2008 voice->trigblk %= voice->blkmod; 2009 } 2010 #endif 2011 } 2012 us = 1; 2013 } 2014 2015 /* Got interrupt */ 2016 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_IPR, ipr); 2017 } 2018 2019 return (us); 2020 } 2021 2022 2023 /* 2024 * Audio Architecture callbacks 2025 */ 2026 2027 int 2028 emuxki_open(void *addr, int flags) 2029 { 2030 struct emuxki_softc *sc = addr; 2031 2032 #ifdef EMUXKI_DEBUG 2033 printf("%s: emuxki_open called\n", sc->sc_dev.dv_xname); 2034 #endif 2035 2036 /* 2037 * Multiple voice support would be added as soon as I find a way to 2038 * trick the audio arch into supporting multiple voices. 2039 * Or I might integrate a modified audio arch supporting 2040 * multiple voices. 2041 */ 2042 2043 /* 2044 * I did this because i have problems identifying the selected 2045 * recording source(s) which is necessary when setting recording 2046 * params. This will be addressed very soon. 2047 */ 2048 if (flags & AUOPEN_READ) { 2049 sc->rvoice = emuxki_voice_new(sc, 0 /* EMU_VOICE_USE_RECORD */); 2050 if (sc->rvoice == NULL) 2051 return (EBUSY); 2052 2053 /* XXX Hardcode RECSRC_ADC for now */ 2054 sc->rvoice->dataloc.source = EMU_RECSRC_ADC; 2055 } 2056 2057 if (flags & AUOPEN_WRITE) { 2058 sc->pvoice = emuxki_voice_new(sc, EMU_VOICE_USE_PLAY); 2059 if (sc->pvoice == NULL) { 2060 if (flags & AUOPEN_READ) 2061 emuxki_voice_delete(sc->rvoice); 2062 return (EBUSY); 2063 } 2064 } 2065 2066 return (0); 2067 } 2068 2069 void 2070 emuxki_close(void *addr) 2071 { 2072 struct emuxki_softc *sc = addr; 2073 2074 #ifdef EMUXKI_DEBUG 2075 printf("%s: emu10K1_close called\n", sc->sc_dev.dv_xname); 2076 #endif 2077 2078 /* No multiple voice support for now */ 2079 if (sc->rvoice != NULL) 2080 emuxki_voice_delete(sc->rvoice); 2081 sc->rvoice = NULL; 2082 if (sc->pvoice != NULL) 2083 emuxki_voice_delete(sc->pvoice); 2084 sc->pvoice = NULL; 2085 } 2086 2087 int 2088 emuxki_query_encoding(void *addr, struct audio_encoding *fp) 2089 { 2090 #ifdef EMUXKI_DEBUG 2091 struct emuxki_softc *sc = addr; 2092 2093 printf("%s: emuxki_query_encoding called\n", sc->sc_dev.dv_xname); 2094 #endif 2095 2096 switch (fp->index) { 2097 case 0: 2098 strlcpy(fp->name, AudioEulinear, sizeof fp->name); 2099 fp->encoding = AUDIO_ENCODING_ULINEAR; 2100 fp->precision = 8; 2101 fp->flags = 0; 2102 break; 2103 case 1: 2104 strlcpy(fp->name, AudioEmulaw, sizeof fp->name); 2105 fp->encoding = AUDIO_ENCODING_ULAW; 2106 fp->precision = 8; 2107 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2108 break; 2109 case 2: 2110 strlcpy(fp->name, AudioEalaw, sizeof fp->name); 2111 fp->encoding = AUDIO_ENCODING_ALAW; 2112 fp->precision = 8; 2113 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2114 break; 2115 case 3: 2116 strlcpy(fp->name, AudioEslinear, sizeof fp->name); 2117 fp->encoding = AUDIO_ENCODING_SLINEAR; 2118 fp->precision = 8; 2119 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2120 break; 2121 case 4: 2122 strlcpy(fp->name, AudioEslinear_le, sizeof fp->name); 2123 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 2124 fp->precision = 16; 2125 fp->flags = 0; 2126 break; 2127 case 5: 2128 strlcpy(fp->name, AudioEulinear_le, sizeof fp->name); 2129 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 2130 fp->precision = 16; 2131 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2132 break; 2133 case 6: 2134 strlcpy(fp->name, AudioEslinear_be, sizeof fp->name); 2135 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 2136 fp->precision = 16; 2137 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2138 break; 2139 case 7: 2140 strlcpy(fp->name, AudioEulinear_be, sizeof fp->name); 2141 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 2142 fp->precision = 16; 2143 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2144 break; 2145 default: 2146 return (EINVAL); 2147 } 2148 return (0); 2149 } 2150 2151 int 2152 emuxki_set_vparms(struct emuxki_voice *voice, struct audio_params *p) 2153 { 2154 u_int8_t b16, mode; 2155 2156 mode = (voice->use & EMU_VOICE_USE_PLAY) ? 2157 AUMODE_PLAY : AUMODE_RECORD; 2158 p->factor = 1; 2159 p->sw_code = NULL; 2160 if (p->channels > 2) 2161 p->channels = 2; 2162 if (p->precision > 16) 2163 p->precision = 16; 2164 /* Will change when streams come in use */ 2165 2166 /* 2167 * Always use slinear_le for recording, as how to set otherwise 2168 * isn't known. 2169 */ 2170 if (mode == AUMODE_PLAY) 2171 b16 = (p->precision == 16); 2172 else { 2173 b16 = 1; 2174 if (p->precision == 8) 2175 p->factor *= 2; 2176 } 2177 2178 switch (p->encoding) { 2179 case AUDIO_ENCODING_ULAW: 2180 if (mode == AUMODE_PLAY) { 2181 p->factor = 2; 2182 p->sw_code = mulaw_to_slinear16_le; 2183 b16 = 1; 2184 } else 2185 p->sw_code = slinear16_to_mulaw_le; 2186 break; 2187 2188 case AUDIO_ENCODING_ALAW: 2189 if (mode == AUMODE_PLAY) { 2190 p->factor = 2; 2191 p->sw_code = alaw_to_slinear16_le; 2192 b16 = 1; 2193 } else 2194 p->sw_code = slinear16_to_alaw_le; 2195 break; 2196 2197 case AUDIO_ENCODING_SLINEAR_LE: 2198 if (p->precision == 8) { 2199 if (mode == AUMODE_PLAY) 2200 p->sw_code = change_sign8; 2201 else 2202 p->sw_code = linear16_to_linear8_le; 2203 } 2204 break; 2205 2206 case AUDIO_ENCODING_ULINEAR_LE: 2207 if (p->precision == 16) 2208 p->sw_code = change_sign16_le; 2209 else if (mode == AUMODE_RECORD) 2210 p->sw_code = slinear16_to_ulinear8_le; 2211 break; 2212 2213 case AUDIO_ENCODING_SLINEAR_BE: 2214 if (p->precision == 16) 2215 p->sw_code = swap_bytes; 2216 else { 2217 if (mode == AUMODE_PLAY) 2218 p->sw_code = change_sign8; 2219 else 2220 p->sw_code = linear16_to_linear8_le; 2221 } 2222 break; 2223 2224 case AUDIO_ENCODING_ULINEAR_BE: 2225 if (p->precision == 16) { 2226 if (mode == AUMODE_PLAY) 2227 p->sw_code = swap_bytes_change_sign16_le; 2228 else 2229 p->sw_code = change_sign16_swap_bytes_le; 2230 } else if (mode == AUMODE_RECORD) 2231 p->sw_code = slinear16_to_ulinear8_le; 2232 break; 2233 2234 default: 2235 return (EINVAL); 2236 } 2237 2238 return (emuxki_voice_set_audioparms(voice, p->channels == 2, 2239 b16, p->sample_rate)); 2240 } 2241 2242 int 2243 emuxki_set_params(void *addr, int setmode, int usemode, 2244 struct audio_params *play, struct audio_params *rec) 2245 { 2246 struct emuxki_softc *sc = addr; 2247 int mode, error; 2248 struct audio_params *p; 2249 2250 for (mode = AUMODE_RECORD; mode != -1; 2251 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 2252 if ((usemode & setmode & mode) == 0) 2253 continue; 2254 2255 p = (mode == AUMODE_PLAY) ? play : rec; 2256 2257 /* No multiple voice support for now */ 2258 if ((error = emuxki_set_vparms((mode == AUMODE_PLAY) ? 2259 sc->pvoice : sc->rvoice, p))) 2260 return (error); 2261 } 2262 2263 return (0); 2264 } 2265 2266 void 2267 emuxki_get_default_params(void *addr, int mode, struct audio_params *params) 2268 { 2269 ac97_get_default_params(params); 2270 } 2271 2272 int 2273 emuxki_halt_output(void *addr) 2274 { 2275 struct emuxki_softc *sc = addr; 2276 2277 /* No multiple voice support for now */ 2278 if (sc->pvoice == NULL) 2279 return (ENXIO); 2280 2281 emuxki_voice_halt(sc->pvoice); 2282 return (0); 2283 } 2284 2285 int 2286 emuxki_halt_input(void *addr) 2287 { 2288 struct emuxki_softc *sc = addr; 2289 2290 #ifdef EMUXKI_DEBUG 2291 printf("%s: emuxki_halt_input called\n", sc->sc_dev.dv_xname); 2292 #endif 2293 2294 /* No multiple voice support for now */ 2295 if (sc->rvoice == NULL) 2296 return (ENXIO); 2297 emuxki_voice_halt(sc->rvoice); 2298 return (0); 2299 } 2300 2301 int 2302 emuxki_getdev(void *v, struct audio_device *adp) 2303 { 2304 struct emuxki_softc *sc = v; 2305 *adp = sc->sc_audv; 2306 return 0; 2307 } 2308 2309 int 2310 emuxki_set_port(void *addr, mixer_ctrl_t *mctl) 2311 { 2312 struct emuxki_softc *sc = addr; 2313 2314 return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl); 2315 } 2316 2317 int 2318 emuxki_get_port(void *addr, mixer_ctrl_t *mctl) 2319 { 2320 struct emuxki_softc *sc = addr; 2321 2322 return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl); 2323 } 2324 2325 int 2326 emuxki_query_devinfo(void *addr, mixer_devinfo_t *minfo) 2327 { 2328 struct emuxki_softc *sc = addr; 2329 2330 return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo); 2331 } 2332 2333 void * 2334 emuxki_allocm(void *addr, int direction, size_t size, int type, int flags) 2335 { 2336 struct emuxki_softc *sc = addr; 2337 2338 if (direction == AUMODE_PLAY) 2339 return emuxki_pmem_alloc(sc, size, type, flags); 2340 else 2341 return emuxki_rmem_alloc(sc, size, type, flags); 2342 } 2343 2344 void 2345 emuxki_freem(void *addr, void *ptr, int type) 2346 { 2347 struct emuxki_softc *sc = addr; 2348 int i, s; 2349 struct emuxki_mem *mem; 2350 size_t numblocks; 2351 u_int32_t *ptb, silentpage; 2352 2353 ptb = KERNADDR(sc->ptb); 2354 silentpage = DMAADDR(sc->silentpage) << 1; 2355 LIST_FOREACH(mem, &sc->mem, next) { 2356 if (KERNADDR(mem->dmamem) != ptr) 2357 continue; 2358 2359 s = splaudio(); 2360 if (mem->ptbidx != EMU_RMEM) { 2361 numblocks = DMASIZE(mem->dmamem) / EMU_PTESIZE; 2362 if (DMASIZE(mem->dmamem) % EMU_PTESIZE) 2363 numblocks++; 2364 for (i = 0; i < numblocks; i++) 2365 ptb[mem->ptbidx + i] = 2366 htole32(silentpage | (mem->ptbidx + i)); 2367 } 2368 LIST_REMOVE(mem, next); 2369 splx(s); 2370 2371 emuxki_mem_delete(mem, type); 2372 break; 2373 } 2374 } 2375 2376 /* blocksize should be a divisor of allowable buffersize */ 2377 /* XXX probably this could be done better */ 2378 int 2379 emuxki_round_blocksize(void *addr, int blksize) 2380 { 2381 int bufsize = 65536; 2382 2383 while (bufsize > blksize) 2384 bufsize /= 2; 2385 2386 return bufsize; 2387 } 2388 2389 size_t 2390 emuxki_round_buffersize(void *addr, int direction, size_t bsize) 2391 { 2392 2393 if (direction == AUMODE_PLAY) { 2394 if (bsize < EMU_PTESIZE) 2395 bsize = EMU_PTESIZE; 2396 else if (bsize > (EMU_PTESIZE * EMU_MAXPTE)) 2397 bsize = EMU_PTESIZE * EMU_MAXPTE; 2398 /* Would be better if set to max available */ 2399 else if (bsize % EMU_PTESIZE) 2400 bsize = bsize - 2401 (bsize % EMU_PTESIZE) + 2402 EMU_PTESIZE; 2403 } else { 2404 int idx; 2405 2406 /* find nearest lower recbuf size */ 2407 for(idx = sizeof(emuxki_recbuf_sz) / 2408 sizeof(emuxki_recbuf_sz[0]); --idx >= 0; ) { 2409 if (bsize >= emuxki_recbuf_sz[idx]) { 2410 bsize = emuxki_recbuf_sz[idx]; 2411 break; 2412 } 2413 } 2414 2415 if (bsize == 0) 2416 bsize = 384; 2417 } 2418 2419 return (bsize); 2420 } 2421 2422 paddr_t 2423 emuxki_mappage(void *addr, void *ptr, off_t off, int prot) 2424 { 2425 struct emuxki_softc *sc = addr; 2426 struct emuxki_mem *mem; 2427 2428 LIST_FOREACH(mem, &sc->mem, next) { 2429 if (KERNADDR(mem->dmamem) == ptr) { 2430 struct dmamem *dm = mem->dmamem; 2431 2432 return bus_dmamem_mmap(dm->dmat, dm->segs, dm->nsegs, 2433 off, prot, BUS_DMA_WAITOK); 2434 } 2435 } 2436 2437 return (-1); 2438 } 2439 2440 int 2441 emuxki_get_props(void *addr) 2442 { 2443 return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | 2444 AUDIO_PROP_FULLDUPLEX); 2445 } 2446 2447 int 2448 emuxki_trigger_output(void *addr, void *start, void *end, int blksize, 2449 void (*inth) (void *), void *inthparam, 2450 struct audio_params *params) 2451 { 2452 struct emuxki_softc *sc = addr; 2453 /* No multiple voice support for now */ 2454 struct emuxki_voice *voice = sc->pvoice; 2455 int error; 2456 2457 if (voice == NULL) 2458 return (ENXIO); 2459 if ((error = emuxki_set_vparms(voice, params))) 2460 return (error); 2461 if ((error = emuxki_voice_set_bufparms(voice, start, 2462 (caddr_t)end - (caddr_t)start, blksize))) 2463 return (error); 2464 emuxki_voice_commit_parms(voice); 2465 emuxki_voice_start(voice, inth, inthparam); 2466 2467 return (0); 2468 } 2469 2470 int 2471 emuxki_trigger_input(void *addr, void *start, void *end, int blksize, 2472 void (*inth) (void *), void *inthparam, 2473 struct audio_params *params) 2474 { 2475 struct emuxki_softc *sc = addr; 2476 /* No multiple voice support for now */ 2477 struct emuxki_voice *voice = sc->rvoice; 2478 int error; 2479 2480 if (voice == NULL) 2481 return (ENXIO); 2482 if ((error = emuxki_set_vparms(voice, params))) 2483 return (error); 2484 if ((error = emuxki_voice_set_bufparms(voice, start, 2485 (caddr_t)end - (caddr_t)start, 2486 blksize))) 2487 return (error); 2488 emuxki_voice_start(voice, inth, inthparam); 2489 2490 return (0); 2491 } 2492 2493 2494 /* 2495 * AC97 callbacks 2496 */ 2497 2498 int 2499 emuxki_ac97_attach(void *arg, struct ac97_codec_if *codecif) 2500 { 2501 struct emuxki_softc *sc = arg; 2502 2503 sc->codecif = codecif; 2504 return (0); 2505 } 2506 2507 int 2508 emuxki_ac97_read(void *arg, u_int8_t reg, u_int16_t *val) 2509 { 2510 struct emuxki_softc *sc = arg; 2511 int s; 2512 2513 s = splaudio(); 2514 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg); 2515 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA); 2516 splx(s); 2517 2518 return (0); 2519 } 2520 2521 int 2522 emuxki_ac97_write(void *arg, u_int8_t reg, u_int16_t val) 2523 { 2524 struct emuxki_softc *sc = arg; 2525 int s; 2526 2527 s = splaudio(); 2528 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg); 2529 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA, val); 2530 splx(s); 2531 2532 return (0); 2533 } 2534 2535 void 2536 emuxki_ac97_reset(void *arg) 2537 { 2538 } 2539