1 /* $NetBSD: emuxki.c,v 1.59 2009/11/26 15:17:09 njoly 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.59 2009/11/26 15:17:09 njoly 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(device_t, cfdata_t, void *); 76 static void emuxki_attach(device_t, device_t, void *); 77 static int emuxki_detach(device_t, 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_NEW(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(device_t parent, cfdata_t match, void *aux) 384 { 385 struct pci_attach_args *pa; 386 387 pa = aux; 388 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CREATIVELABS) 389 return 0; 390 391 switch (PCI_PRODUCT(pa->pa_id)) { 392 case PCI_PRODUCT_CREATIVELABS_SBLIVE: 393 case PCI_PRODUCT_CREATIVELABS_SBLIVE2: 394 case PCI_PRODUCT_CREATIVELABS_AUDIGY: 395 return 1; 396 default: 397 return 0; 398 } 399 } 400 401 static void 402 emuxki_attach(device_t parent, device_t self, void *aux) 403 { 404 struct emuxki_softc *sc; 405 struct pci_attach_args *pa; 406 char devinfo[256]; 407 pci_intr_handle_t ih; 408 const char *intrstr; 409 410 sc = device_private(self); 411 sc->sc_dev = 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(self, "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(self, "couldn't establish interrupt"); 441 if (intrstr != NULL) 442 aprint_error(" at %s", intrstr); 443 aprint_error("\n"); 444 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 445 return; 446 } 447 aprint_normal_dev(self, "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(device_t self, int flags) 485 { 486 struct emuxki_softc *sc; 487 488 sc = device_private(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 int rv; 1728 1729 /* XXX different semantics in these cases */ 1730 if (voice->use & EMU_VOICE_USE_PLAY) { 1731 /* returns number of samples (an l/r pair counts 1) */ 1732 rv = emuxki_read(voice->sc, 1733 voice->dataloc.chan[0]->num, EMU_CHAN_CCCA_CURRADDR) - 1734 voice->dataloc.chan[0]->loop.start; 1735 } else { 1736 idxreg = 0; 1737 /* returns number of bytes */ 1738 switch (voice->dataloc.source) { 1739 case EMU_RECSRC_MIC: 1740 idxreg = (voice->sc->sc_type & EMUXKI_AUDIGY) ? 1741 EMU_A_MICIDX : EMU_MICIDX; 1742 break; 1743 case EMU_RECSRC_ADC: 1744 idxreg = (voice->sc->sc_type & EMUXKI_AUDIGY) ? 1745 EMU_A_ADCIDX : EMU_ADCIDX; 1746 break; 1747 case EMU_RECSRC_FX: 1748 idxreg = EMU_FXIDX; 1749 break; 1750 default: 1751 #ifdef EMUXKI_DEBUG 1752 printf("emu: bad recording source!\n"); 1753 #endif 1754 break; 1755 } 1756 rv = emuxki_read(voice->sc, 0, EMU_RECIDX(idxreg) 1757 & EMU_RECIDX_MASK); 1758 } 1759 return rv; 1760 } 1761 1762 static void 1763 emuxki_resched_timer(struct emuxki_softc *sc) 1764 { 1765 struct emuxki_voice *voice; 1766 uint16_t timerate; 1767 uint8_t active; 1768 int s; 1769 1770 timerate = 1024; 1771 active = 0; 1772 s = splaudio(); 1773 LIST_FOREACH(voice, &sc->voices, next) { 1774 if ((voice->state & EMU_VOICE_STATE_STARTED) == 0) 1775 continue; 1776 active = 1; 1777 if (voice->timerate < timerate) 1778 timerate = voice->timerate; 1779 } 1780 1781 if (timerate & ~EMU_TIMER_RATE_MASK) 1782 timerate = 0; 1783 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_TIMER, timerate); 1784 if (!active && (sc->timerstate & EMU_TIMER_STATE_ENABLED)) { 1785 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 1786 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) & 1787 ~EMU_INTE_INTERTIMERENB); 1788 sc->timerstate &= ~EMU_TIMER_STATE_ENABLED; 1789 } else if (active && !(sc->timerstate & EMU_TIMER_STATE_ENABLED)) { 1790 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 1791 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) | 1792 EMU_INTE_INTERTIMERENB); 1793 sc->timerstate |= EMU_TIMER_STATE_ENABLED; 1794 } 1795 splx(s); 1796 } 1797 1798 static int 1799 emuxki_voice_adc_rate(struct emuxki_voice *voice) 1800 { 1801 1802 switch(voice->sample_rate) { 1803 case 48000: 1804 return EMU_ADCCR_SAMPLERATE_48; 1805 break; 1806 case 44100: 1807 return EMU_ADCCR_SAMPLERATE_44; 1808 break; 1809 case 32000: 1810 return EMU_ADCCR_SAMPLERATE_32; 1811 break; 1812 case 24000: 1813 return EMU_ADCCR_SAMPLERATE_24; 1814 break; 1815 case 22050: 1816 return EMU_ADCCR_SAMPLERATE_22; 1817 break; 1818 case 16000: 1819 return EMU_ADCCR_SAMPLERATE_16; 1820 break; 1821 case 12000: 1822 if(voice->sc->sc_type & EMUXKI_AUDIGY) 1823 return EMU_A_ADCCR_SAMPLERATE_12; 1824 else { 1825 #ifdef EMUXKI_DEBUG 1826 printf("recording sample_rate not supported : %u\n", voice->sample_rate); 1827 #endif 1828 return -1; 1829 } 1830 break; 1831 case 11000: 1832 if(voice->sc->sc_type & EMUXKI_AUDIGY) 1833 return EMU_A_ADCCR_SAMPLERATE_11; 1834 else 1835 return EMU_ADCCR_SAMPLERATE_11; 1836 break; 1837 case 8000: 1838 if(voice->sc->sc_type & EMUXKI_AUDIGY) 1839 return EMU_A_ADCCR_SAMPLERATE_8; 1840 else 1841 return EMU_ADCCR_SAMPLERATE_8; 1842 break; 1843 default: 1844 #ifdef EMUXKI_DEBUG 1845 printf("recording sample_rate not supported : %u\n", voice->sample_rate); 1846 #endif 1847 return -1; 1848 } 1849 return -1; /* shouldn't get here */ 1850 } 1851 1852 1853 static void 1854 emuxki_voice_start(struct emuxki_voice *voice, 1855 void (*inth) (void *), void *inthparam) 1856 { 1857 uint32_t val; 1858 1859 voice->inth = inth; 1860 voice->inthparam = inthparam; 1861 if (voice->use & EMU_VOICE_USE_PLAY) { 1862 voice->trigblk = 1; 1863 emuxki_channel_start(voice->dataloc.chan[0]); 1864 if (voice->stereo) 1865 emuxki_channel_start(voice->dataloc.chan[1]); 1866 } else { 1867 voice->trigblk = 1; 1868 switch (voice->dataloc.source) { 1869 case EMU_RECSRC_ADC: 1870 /* XXX need to program DSP to output L+R 1871 * XXX in monaural case? */ 1872 if (voice->sc->sc_type & EMUXKI_AUDIGY) { 1873 val = EMU_A_ADCCR_LCHANENABLE; 1874 if (voice->stereo) 1875 val |= EMU_A_ADCCR_RCHANENABLE; 1876 } else { 1877 val = EMU_ADCCR_LCHANENABLE; 1878 if (voice->stereo) 1879 val |= EMU_ADCCR_RCHANENABLE; 1880 } 1881 val |= emuxki_voice_adc_rate(voice); 1882 emuxki_write(voice->sc, 0, EMU_ADCCR, 0); 1883 emuxki_write(voice->sc, 0, EMU_ADCCR, val); 1884 break; 1885 case EMU_RECSRC_MIC: 1886 case EMU_RECSRC_FX: 1887 printf("unimplemented\n"); 1888 break; 1889 case EMU_RECSRC_NOTSET: 1890 default: 1891 printf("Bad dataloc.source %d\n", 1892 voice->dataloc.source); 1893 break; 1894 } 1895 #if 0 1896 switch (voice->dataloc.source) { 1897 case EMU_RECSRC_ADC: 1898 case EMU_RECSRC_FX: 1899 case EMU_RECSRC_MIC: 1900 /* DMA completion interrupt is useless; use timer */ 1901 int s; 1902 s = splaudio(); 1903 val = emu_rd(sc, INTE, 4); 1904 val |= emuxki_recsrc_intrmasks[voice->dataloc.source]; 1905 emu_wr(sc, INTE, val, 4); 1906 splx(s); 1907 break; 1908 default: 1909 break; 1910 } 1911 #endif 1912 } 1913 voice->state |= EMU_VOICE_STATE_STARTED; 1914 emuxki_resched_timer(voice->sc); 1915 } 1916 1917 static void 1918 emuxki_voice_halt(struct emuxki_voice *voice) 1919 { 1920 1921 if (voice->use & EMU_VOICE_USE_PLAY) { 1922 emuxki_channel_stop(voice->dataloc.chan[0]); 1923 if (voice->stereo) 1924 emuxki_channel_stop(voice->dataloc.chan[1]); 1925 } else { 1926 switch (voice->dataloc.source) { 1927 case EMU_RECSRC_ADC: 1928 emuxki_write(voice->sc, 0, EMU_ADCCR, 0); 1929 break; 1930 case EMU_RECSRC_FX: 1931 case EMU_RECSRC_MIC: 1932 printf("unimplemented\n"); 1933 break; 1934 default: 1935 case EMU_RECSRC_NOTSET: 1936 printf("Bad dataloc.source %d\n", 1937 voice->dataloc.source); 1938 break; 1939 } 1940 1941 switch (voice->dataloc.source) { 1942 case EMU_RECSRC_ADC: 1943 case EMU_RECSRC_FX: 1944 case EMU_RECSRC_MIC: 1945 /* This should reset buffer pointer */ 1946 emuxki_write(voice->sc, 0, 1947 emuxki_recsrc_szreg[voice->dataloc.source], 1948 EMU_RECBS_BUFSIZE_NONE); 1949 #if 0 1950 int s; 1951 s = splaudio(); 1952 val = emu_rd(sc, INTE, 4); 1953 val &= ~emuxki_recsrc_intrmasks[voice->dataloc.source]; 1954 emu_wr(sc, INTE, val, 4); 1955 splx(s); 1956 #endif 1957 break; 1958 default: 1959 break; 1960 } 1961 } 1962 voice->state &= ~EMU_VOICE_STATE_STARTED; 1963 emuxki_resched_timer(voice->sc); 1964 } 1965 1966 /* 1967 * The interrupt handler 1968 */ 1969 static int 1970 emuxki_intr(void *arg) 1971 { 1972 struct emuxki_softc *sc; 1973 struct emuxki_voice *voice; 1974 uint32_t ipr, curblk; 1975 int claim; 1976 1977 sc = arg; 1978 claim = 0; 1979 while ((ipr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_IPR))) { 1980 if (ipr & EMU_IPR_INTERVALTIMER) { 1981 LIST_FOREACH(voice, &sc->voices, next) { 1982 if ((voice->state & 1983 EMU_VOICE_STATE_STARTED) == 0) 1984 continue; 1985 1986 curblk = emuxki_voice_curaddr(voice) / 1987 voice->blksize; 1988 #if 0 1989 if (curblk == voice->trigblk) { 1990 voice->inth(voice->inthparam); 1991 voice->trigblk++; 1992 voice->trigblk %= voice->blkmod; 1993 } 1994 #else 1995 while ((curblk >= voice->trigblk && 1996 curblk < (voice->trigblk + voice->blkmod / 2)) || 1997 ((int)voice->trigblk - (int)curblk) > 1998 (voice->blkmod / 2 + 1)) { 1999 voice->inth(voice->inthparam); 2000 voice->trigblk++; 2001 voice->trigblk %= voice->blkmod; 2002 } 2003 #endif 2004 } 2005 } 2006 2007 /* Got interrupt */ 2008 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_IPR, ipr); 2009 2010 claim = 1; 2011 } 2012 2013 return claim; 2014 } 2015 2016 2017 /* 2018 * Audio Architecture callbacks 2019 */ 2020 2021 static int 2022 emuxki_open(void *addr, int flags) 2023 { 2024 struct emuxki_softc *sc; 2025 2026 sc = addr; 2027 #ifdef EMUXKI_DEBUG 2028 printf("%s: emuxki_open called\n", device_xname(sc->sc_dev)); 2029 #endif 2030 2031 /* 2032 * Multiple voice support would be added as soon as I find a way to 2033 * trick the audio arch into supporting multiple voices. 2034 * Or I might integrate a modified audio arch supporting 2035 * multiple voices. 2036 */ 2037 2038 /* 2039 * I did this because i have problems identifying the selected 2040 * recording source(s) which is necessary when setting recording 2041 * params This will be addressed very soon 2042 */ 2043 if (flags & AUOPEN_READ) { 2044 sc->rvoice = emuxki_voice_new(sc, 0 /* EMU_VOICE_USE_RECORD */); 2045 if (sc->rvoice == NULL) 2046 return EBUSY; 2047 2048 /* XXX Hardcode RECSRC_ADC for now */ 2049 sc->rvoice->dataloc.source = EMU_RECSRC_ADC; 2050 } 2051 2052 if (flags & AUOPEN_WRITE) { 2053 sc->pvoice = emuxki_voice_new(sc, EMU_VOICE_USE_PLAY); 2054 if (sc->pvoice == NULL) { 2055 if (sc->rvoice) { 2056 emuxki_voice_delete(sc->rvoice); 2057 sc->rvoice = NULL; 2058 } 2059 return EBUSY; 2060 } 2061 } 2062 2063 return 0; 2064 } 2065 2066 static void 2067 emuxki_close(void *addr) 2068 { 2069 struct emuxki_softc *sc; 2070 2071 sc = addr; 2072 #ifdef EMUXKI_DEBUG 2073 printf("%s: emu10K1_close called\n", device_xname(sc->sc_dev)); 2074 #endif 2075 2076 /* No multiple voice support for now */ 2077 if (sc->rvoice != NULL) { 2078 emuxki_voice_delete(sc->rvoice); 2079 sc->rvoice = NULL; 2080 } 2081 if (sc->pvoice != NULL) { 2082 emuxki_voice_delete(sc->pvoice); 2083 sc->pvoice = NULL; 2084 } 2085 } 2086 2087 static int 2088 emuxki_query_encoding(void *addr, struct audio_encoding *fp) 2089 { 2090 #ifdef EMUXKI_DEBUG 2091 struct emuxki_softc *sc; 2092 2093 sc = addr; 2094 printf("%s: emuxki_query_encoding called\n", device_xname(sc->sc_dev)); 2095 #endif 2096 2097 switch (fp->index) { 2098 case 0: 2099 strcpy(fp->name, AudioEulinear); 2100 fp->encoding = AUDIO_ENCODING_ULINEAR; 2101 fp->precision = 8; 2102 fp->flags = 0; 2103 break; 2104 case 1: 2105 strcpy(fp->name, AudioEmulaw); 2106 fp->encoding = AUDIO_ENCODING_ULAW; 2107 fp->precision = 8; 2108 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2109 break; 2110 case 2: 2111 strcpy(fp->name, AudioEalaw); 2112 fp->encoding = AUDIO_ENCODING_ALAW; 2113 fp->precision = 8; 2114 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2115 break; 2116 case 3: 2117 strcpy(fp->name, AudioEslinear); 2118 fp->encoding = AUDIO_ENCODING_SLINEAR; 2119 fp->precision = 8; 2120 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2121 break; 2122 case 4: 2123 strcpy(fp->name, AudioEslinear_le); 2124 fp->encoding = AUDIO_ENCODING_SLINEAR_LE; 2125 fp->precision = 16; 2126 fp->flags = 0; 2127 break; 2128 case 5: 2129 strcpy(fp->name, AudioEulinear_le); 2130 fp->encoding = AUDIO_ENCODING_ULINEAR_LE; 2131 fp->precision = 16; 2132 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2133 break; 2134 case 6: 2135 strcpy(fp->name, AudioEslinear_be); 2136 fp->encoding = AUDIO_ENCODING_SLINEAR_BE; 2137 fp->precision = 16; 2138 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2139 break; 2140 case 7: 2141 strcpy(fp->name, AudioEulinear_be); 2142 fp->encoding = AUDIO_ENCODING_ULINEAR_BE; 2143 fp->precision = 16; 2144 fp->flags = AUDIO_ENCODINGFLAG_EMULATED; 2145 break; 2146 default: 2147 return EINVAL; 2148 } 2149 return 0; 2150 } 2151 2152 static int 2153 emuxki_set_vparms(struct emuxki_voice *voice, const audio_params_t *p, 2154 stream_filter_list_t *fil) 2155 { 2156 int mode, i; 2157 2158 mode = (voice->use & EMU_VOICE_USE_PLAY) ? 2159 AUMODE_PLAY : AUMODE_RECORD; 2160 i = auconv_set_converter(emuxki_formats, EMUXKI_NFORMATS, 2161 mode, p, FALSE, fil); 2162 if (i < 0) 2163 return EINVAL; 2164 if (fil->req_size > 0) 2165 p = &fil->filters[0].param; 2166 return emuxki_voice_set_audioparms 2167 (voice, p->channels == 2, p->precision == 16, p->sample_rate); 2168 } 2169 2170 static int 2171 emuxki_set_params(void *addr, int setmode, int usemode, audio_params_t *play, 2172 audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil) 2173 { 2174 struct emuxki_softc *sc; 2175 struct audio_params *p; 2176 struct emuxki_voice *v; 2177 stream_filter_list_t *fil; 2178 int mode, error; 2179 2180 sc = addr; 2181 for (mode = AUMODE_RECORD; mode != -1; 2182 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 2183 if ((usemode & setmode & mode) == 0) 2184 continue; 2185 2186 if (mode == AUMODE_PLAY) { 2187 p = play; 2188 fil = pfil; 2189 v = sc->pvoice; 2190 } else { 2191 p = rec; 2192 fil = rfil; 2193 v = sc->rvoice; 2194 } 2195 2196 if (v == NULL) { 2197 continue; 2198 } 2199 2200 /* No multiple voice support for now */ 2201 if ((error = emuxki_set_vparms(v, p, fil))) 2202 return error; 2203 } 2204 2205 return 0; 2206 } 2207 2208 static int 2209 emuxki_halt_output(void *addr) 2210 { 2211 struct emuxki_softc *sc; 2212 2213 sc = addr; 2214 /* No multiple voice support for now */ 2215 if (sc->pvoice == NULL) 2216 return ENXIO; 2217 2218 emuxki_voice_halt(sc->pvoice); 2219 return 0; 2220 } 2221 2222 static int 2223 emuxki_halt_input(void *addr) 2224 { 2225 struct emuxki_softc *sc; 2226 2227 sc = addr; 2228 #ifdef EMUXKI_DEBUG 2229 printf("%s: emuxki_halt_input called\n", device_xname(sc->sc_dev)); 2230 #endif 2231 2232 /* No multiple voice support for now */ 2233 if (sc->rvoice == NULL) 2234 return ENXIO; 2235 emuxki_voice_halt(sc->rvoice); 2236 return 0; 2237 } 2238 2239 static int 2240 emuxki_getdev(void *addr, struct audio_device *dev) 2241 { 2242 struct emuxki_softc *sc; 2243 2244 sc = addr; 2245 *dev = sc->sc_audv; 2246 return 0; 2247 } 2248 2249 static int 2250 emuxki_set_port(void *addr, mixer_ctrl_t *mctl) 2251 { 2252 struct emuxki_softc *sc; 2253 2254 sc = addr; 2255 return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl); 2256 } 2257 2258 static int 2259 emuxki_get_port(void *addr, mixer_ctrl_t *mctl) 2260 { 2261 struct emuxki_softc *sc; 2262 2263 sc = addr; 2264 return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl); 2265 } 2266 2267 static int 2268 emuxki_query_devinfo(void *addr, mixer_devinfo_t *minfo) 2269 { 2270 struct emuxki_softc *sc; 2271 2272 sc = addr; 2273 return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo); 2274 } 2275 2276 static void * 2277 emuxki_allocm(void *addr, int direction, size_t size, 2278 struct malloc_type *type, int flags) 2279 { 2280 if (direction == AUMODE_PLAY) 2281 return emuxki_pmem_alloc(addr, size, type, flags); 2282 else 2283 return emuxki_rmem_alloc(addr, size, type, flags); 2284 } 2285 2286 static void 2287 emuxki_freem(void *addr, void *ptr, struct malloc_type *type) 2288 { 2289 struct emuxki_softc *sc; 2290 struct emuxki_mem *mem; 2291 uint32_t *ptb, silentpage; 2292 size_t numblocks; 2293 int i, s; 2294 2295 sc = addr; 2296 ptb = KERNADDR(sc->ptb); 2297 silentpage = DMAADDR(sc->silentpage) << 1; 2298 LIST_FOREACH(mem, &sc->mem, next) { 2299 if (KERNADDR(mem->dmamem) != ptr) 2300 continue; 2301 2302 s = splaudio(); 2303 if (mem->ptbidx != EMU_RMEM) { 2304 numblocks = DMASIZE(mem->dmamem) / EMU_PTESIZE; 2305 if (DMASIZE(mem->dmamem) % EMU_PTESIZE) 2306 numblocks++; 2307 for (i = 0; i < numblocks; i++) 2308 ptb[mem->ptbidx + i] = 2309 htole32(silentpage | (mem->ptbidx + i)); 2310 } 2311 LIST_REMOVE(mem, next); 2312 splx(s); 2313 2314 emuxki_mem_delete(mem, type); 2315 break; 2316 } 2317 } 2318 2319 /* blocksize should be a divisor of allowable buffersize */ 2320 /* XXX probably this could be done better */ 2321 static int 2322 emuxki_round_blocksize(void *addr, int blksize, 2323 int mode, const audio_params_t* param) 2324 { 2325 #if 0 2326 struct emuxki_softc *sc; 2327 struct audio_softc *au; 2328 #endif 2329 int bufsize; 2330 #if 0 2331 sc = addr; 2332 if (sc == NULL) 2333 return blksize; 2334 2335 au = device_private(sc->sc_audev); 2336 if (au == NULL) 2337 return blksize; 2338 2339 bufsize = emuxki_round_buffersize(sc, AUMODE_RECORD, 2340 au->sc_rr.bufsize); 2341 #else 2342 bufsize = 65536; 2343 #endif 2344 2345 while (bufsize > blksize) 2346 bufsize /= 2; 2347 2348 return bufsize; 2349 } 2350 2351 static size_t 2352 emuxki_round_buffersize(void *addr, int direction, size_t bsize) 2353 { 2354 2355 if (direction == AUMODE_PLAY) { 2356 if (bsize < EMU_PTESIZE) 2357 bsize = EMU_PTESIZE; 2358 else if (bsize > (EMU_PTESIZE * EMU_MAXPTE)) 2359 bsize = EMU_PTESIZE * EMU_MAXPTE; 2360 /* Would be better if set to max available */ 2361 else if (bsize % EMU_PTESIZE) 2362 bsize = bsize - 2363 (bsize % EMU_PTESIZE) + 2364 EMU_PTESIZE; 2365 } else { 2366 int idx; 2367 2368 /* find nearest lower recbuf size */ 2369 for(idx = sizeof(emuxki_recbuf_sz) / 2370 sizeof(emuxki_recbuf_sz[0]); --idx >= 0; ) { 2371 if (bsize >= emuxki_recbuf_sz[idx]) { 2372 bsize = emuxki_recbuf_sz[idx]; 2373 break; 2374 } 2375 } 2376 2377 if (bsize == 0) 2378 bsize = 384; 2379 } 2380 2381 return bsize; 2382 } 2383 2384 static paddr_t 2385 emuxki_mappage(void *addr, void *ptr, off_t off, int prot) 2386 { 2387 struct emuxki_softc *sc; 2388 struct emuxki_mem *mem; 2389 2390 sc = addr; 2391 LIST_FOREACH(mem, &sc->mem, next) { 2392 if (KERNADDR(mem->dmamem) == ptr) { 2393 struct dmamem *dm = mem->dmamem; 2394 2395 return bus_dmamem_mmap(dm->dmat, dm->segs, dm->nsegs, 2396 off, prot, BUS_DMA_WAITOK); 2397 } 2398 } 2399 2400 return -1; 2401 } 2402 2403 static int 2404 emuxki_get_props(void *addr) 2405 { 2406 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | 2407 AUDIO_PROP_FULLDUPLEX; 2408 } 2409 2410 static int 2411 emuxki_trigger_output(void *addr, void *start, void *end, int blksize, 2412 void (*inth) (void *), void *inthparam, const audio_params_t *params) 2413 { 2414 struct emuxki_softc *sc; 2415 /* No multiple voice support for now */ 2416 struct emuxki_voice *voice; 2417 int error; 2418 2419 sc = addr; 2420 voice = sc->pvoice; 2421 if (voice == NULL) 2422 return ENXIO; 2423 if ((error = emuxki_voice_set_audioparms(voice, params->channels == 2, 2424 params->precision == 16, params->sample_rate))) 2425 return error; 2426 if ((error = emuxki_voice_set_bufparms(voice, start, 2427 (char *)end - (char *)start, blksize))) 2428 return error; 2429 emuxki_voice_commit_parms(voice); 2430 emuxki_voice_start(voice, inth, inthparam); 2431 2432 return 0; 2433 } 2434 2435 static int 2436 emuxki_trigger_input(void *addr, void *start, void *end, int blksize, 2437 void (*inth) (void *), void *inthparam, const audio_params_t *params) 2438 { 2439 struct emuxki_softc *sc; 2440 /* No multiple voice support for now */ 2441 struct emuxki_voice *voice; 2442 int error; 2443 2444 sc = addr; 2445 voice = sc->rvoice; 2446 if (voice == NULL) 2447 return ENXIO; 2448 if ((error = emuxki_voice_set_audioparms(voice, params->channels == 2, 2449 params->precision == 16, params->sample_rate))) 2450 return error; 2451 if ((error = emuxki_voice_set_bufparms(voice, start, 2452 (char *)end - (char *)start, blksize))) 2453 return error; 2454 emuxki_voice_start(voice, inth, inthparam); 2455 2456 return 0; 2457 } 2458 2459 /* 2460 * AC97 callbacks 2461 */ 2462 2463 static int 2464 emuxki_ac97_attach(void *arg, struct ac97_codec_if *codecif) 2465 { 2466 struct emuxki_softc *sc; 2467 2468 sc = arg; 2469 sc->codecif = codecif; 2470 return 0; 2471 } 2472 2473 static int 2474 emuxki_ac97_read(void *arg, uint8_t reg, uint16_t *val) 2475 { 2476 struct emuxki_softc *sc; 2477 int s; 2478 2479 sc = arg; 2480 s = splaudio(); 2481 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg); 2482 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA); 2483 splx(s); 2484 2485 return 0; 2486 } 2487 2488 static int 2489 emuxki_ac97_write(void *arg, uint8_t reg, uint16_t val) 2490 { 2491 struct emuxki_softc *sc; 2492 int s; 2493 2494 sc = arg; 2495 s = splaudio(); 2496 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg); 2497 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA, val); 2498 splx(s); 2499 2500 return 0; 2501 } 2502 2503 static int 2504 emuxki_ac97_reset(void *arg) 2505 { 2506 2507 return 0; 2508 } 2509 2510 enum ac97_host_flags 2511 emuxki_ac97_flags(void *arg) 2512 { 2513 2514 return AC97_HOST_SWAPPED_CHANNELS; 2515 } 2516