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