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