1 /* $NetBSD: emuxki.c,v 1.62 2012/01/30 19:41:19 drochner Exp $ */ 2 3 /*- 4 * Copyright (c) 2001, 2007 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, and by Andrew Doran. 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.62 2012/01/30 19:41:19 drochner Exp $"); 53 54 #include <sys/param.h> 55 #include <sys/device.h> 56 #include <sys/errno.h> 57 #include <sys/systm.h> 58 #include <sys/audioio.h> 59 #include <sys/select.h> 60 #include <sys/mutex.h> 61 #include <sys/kmem.h> 62 #include <sys/malloc.h> 63 64 #include <dev/audio_if.h> 65 #include <dev/audiovar.h> 66 #include <dev/auconv.h> 67 #include <dev/mulaw.h> 68 69 #include <dev/ic/ac97reg.h> 70 #include <dev/ic/ac97var.h> 71 72 #include <dev/pci/pcireg.h> 73 #include <dev/pci/pcivar.h> 74 #include <dev/pci/pcidevs.h> 75 #include <dev/pci/emuxkireg.h> 76 #include <dev/pci/emuxkivar.h> 77 78 /* autoconf goo */ 79 static int emuxki_match(device_t, cfdata_t, void *); 80 static void emuxki_attach(device_t, device_t, void *); 81 static int emuxki_detach(device_t, int); 82 83 /* DMA mem mgmt */ 84 static struct dmamem *dmamem_alloc(bus_dma_tag_t, size_t, bus_size_t, 85 int); 86 static void dmamem_free(struct dmamem *); 87 88 /* Emu10k1 init & shutdown */ 89 static int emuxki_init(struct emuxki_softc *); 90 static void emuxki_shutdown(struct emuxki_softc *); 91 92 /* Emu10k1 mem mgmt */ 93 static void *emuxki_pmem_alloc(struct emuxki_softc *, size_t); 94 static void *emuxki_rmem_alloc(struct emuxki_softc *, size_t); 95 96 /* 97 * Emu10k1 channels funcs : There is no direct access to channels, everything 98 * is done through voices I will at least provide channel based fx params 99 * modification, later... 100 */ 101 102 /* Emu10k1 voice mgmt */ 103 static struct emuxki_voice *emuxki_voice_new(struct emuxki_softc *, 104 uint8_t); 105 static void emuxki_voice_delete(struct emuxki_voice *); 106 static int emuxki_voice_set_audioparms(struct emuxki_softc *, 107 struct emuxki_voice *, uint8_t, 108 uint8_t, uint32_t); 109 /* emuxki_voice_set_fxparms will come later, it'll need channel distinction */ 110 static int emuxki_voice_set_bufparms(struct emuxki_voice *, 111 void *, uint32_t, uint16_t); 112 static void emuxki_voice_commit_parms(struct emuxki_voice *); 113 static int emuxki_voice_adc_rate(struct emuxki_voice *); 114 static uint32_t emuxki_voice_curaddr(struct emuxki_voice *); 115 static void emuxki_voice_start(struct emuxki_voice *, 116 void (*) (void *), void *); 117 static void emuxki_voice_halt(struct emuxki_voice *); 118 119 /* 120 * Emu10k1 stream mgmt : not done yet 121 */ 122 #if 0 123 static struct emuxki_stream *emuxki_stream_new(struct emu10k1 *); 124 static void emuxki_stream_delete(struct emuxki_stream *); 125 static int emuxki_stream_set_audio_params(struct emuxki_stream *, 126 uint8_t, uint8_t, uint8_t, uint16_t); 127 static void emuxki_stream_start(struct emuxki_stream *); 128 static void emuxki_stream_halt(struct emuxki_stream *); 129 #endif 130 131 /* audio interface callbacks */ 132 133 static int emuxki_open(void *, int); 134 static void emuxki_close(void *); 135 136 static int emuxki_query_encoding(void *, struct audio_encoding *); 137 static int emuxki_set_params(void *, int, int, audio_params_t *, 138 audio_params_t *, stream_filter_list_t *, 139 stream_filter_list_t *); 140 141 static int emuxki_round_blocksize(void *, int, int, const audio_params_t *); 142 static size_t emuxki_round_buffersize(void *, int, size_t); 143 144 static int emuxki_trigger_output(void *, void *, void *, int, 145 void (*)(void *), void *, const audio_params_t *); 146 static int emuxki_trigger_input(void *, void *, void *, int, 147 void (*) (void *), void *, const audio_params_t *); 148 static int emuxki_halt_output(void *); 149 static int emuxki_halt_input(void *); 150 151 static int emuxki_getdev(void *, struct audio_device *); 152 static int emuxki_set_port(void *, mixer_ctrl_t *); 153 static int emuxki_get_port(void *, mixer_ctrl_t *); 154 static int emuxki_query_devinfo(void *, mixer_devinfo_t *); 155 156 static void *emuxki_allocm(void *, int, size_t); 157 static void emuxki_freem(void *, void *, size_t); 158 159 static paddr_t emuxki_mappage(void *, void *, off_t, int); 160 static int emuxki_get_props(void *); 161 static void emuxki_get_locks(void *, kmutex_t **, kmutex_t **); 162 163 /* Interrupt handler */ 164 static int emuxki_intr(void *); 165 166 /* Emu10k1 AC97 interface callbacks */ 167 static int emuxki_ac97_attach(void *, struct ac97_codec_if *); 168 static int emuxki_ac97_read(void *, uint8_t, uint16_t *); 169 static int emuxki_ac97_write(void *, uint8_t, uint16_t); 170 static int emuxki_ac97_reset(void *); 171 static enum ac97_host_flags emuxki_ac97_flags(void *); 172 173 /* 174 * Autoconfig goo. 175 */ 176 CFATTACH_DECL_NEW(emuxki, sizeof(struct emuxki_softc), 177 emuxki_match, emuxki_attach, emuxki_detach, NULL); 178 179 static const struct audio_hw_if emuxki_hw_if = { 180 emuxki_open, 181 emuxki_close, 182 NULL, /* drain */ 183 emuxki_query_encoding, 184 emuxki_set_params, 185 emuxki_round_blocksize, 186 NULL, /* commit settings */ 187 NULL, /* init_output */ 188 NULL, /* init_input */ 189 NULL, /* start_output */ 190 NULL, /* start_input */ 191 emuxki_halt_output, 192 emuxki_halt_input, 193 NULL, /* speaker_ctl */ 194 emuxki_getdev, 195 NULL, /* setfd */ 196 emuxki_set_port, 197 emuxki_get_port, 198 emuxki_query_devinfo, 199 emuxki_allocm, 200 emuxki_freem, 201 emuxki_round_buffersize, 202 emuxki_mappage, 203 emuxki_get_props, 204 emuxki_trigger_output, 205 emuxki_trigger_input, 206 NULL, /* dev_ioctl */ 207 emuxki_get_locks, 208 }; 209 210 #if 0 211 static const int emuxki_recsrc_intrmasks[EMU_NUMRECSRCS] = 212 { EMU_INTE_MICBUFENABLE, EMU_INTE_ADCBUFENABLE, EMU_INTE_EFXBUFENABLE }; 213 #endif 214 static const uint32_t emuxki_recsrc_bufaddrreg[EMU_NUMRECSRCS] = 215 { EMU_MICBA, EMU_ADCBA, EMU_FXBA }; 216 static const uint32_t emuxki_recsrc_szreg[EMU_NUMRECSRCS] = 217 { EMU_MICBS, EMU_ADCBS, EMU_FXBS }; 218 static const int emuxki_recbuf_sz[] = { 219 0, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792, 220 2048, 2560, 3072, 3584, 4096, 5120, 6144, 7168, 8192, 10240, 221 12288, 14366, 16384, 20480, 24576, 28672, 32768, 40960, 49152, 222 57344, 65536 223 }; 224 225 #define EMUXKI_NFORMATS 4 226 static const struct audio_format emuxki_formats[EMUXKI_NFORMATS] = { 227 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, 228 2, AUFMT_STEREO, 0, {4000, 48000}}, 229 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, 230 1, AUFMT_MONAURAL, 0, {4000, 48000}}, 231 {NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 8, 8, 232 2, AUFMT_STEREO, 0, {4000, 48000}}, 233 {NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 8, 8, 234 1, AUFMT_MONAURAL, 0, {4000, 48000}}, 235 }; 236 237 /* 238 * DMA memory mgmt 239 */ 240 241 static void 242 dmamem_delete(struct dmamem *mem) 243 { 244 245 kmem_free(mem->segs, mem->nsegs * sizeof(*(mem->segs))); 246 kmem_free(mem, sizeof(*mem)); 247 } 248 249 static struct dmamem * 250 dmamem_alloc(bus_dma_tag_t dmat, size_t size, bus_size_t align, int nsegs) 251 { 252 struct dmamem *mem; 253 254 /* Allocate memory for structure */ 255 if ((mem = kmem_alloc(sizeof(*mem), KM_SLEEP)) == NULL) 256 return NULL; 257 mem->dmat = dmat; 258 mem->size = size; 259 mem->align = align; 260 mem->nsegs = nsegs; 261 mem->bound = 0; 262 263 mem->segs = kmem_alloc(mem->nsegs * sizeof(*(mem->segs)), KM_SLEEP); 264 if (mem->segs == NULL) { 265 kmem_free(mem, sizeof(*mem)); 266 return NULL; 267 } 268 269 if (bus_dmamem_alloc(dmat, mem->size, mem->align, mem->bound, 270 mem->segs, mem->nsegs, &(mem->rsegs), 271 BUS_DMA_WAITOK)) { 272 dmamem_delete(mem); 273 return NULL; 274 } 275 276 if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, mem->size, 277 &(mem->kaddr), BUS_DMA_WAITOK | BUS_DMA_COHERENT)) { 278 bus_dmamem_free(dmat, mem->segs, mem->nsegs); 279 dmamem_delete(mem); 280 return NULL; 281 } 282 283 if (bus_dmamap_create(dmat, mem->size, mem->nsegs, mem->size, 284 mem->bound, BUS_DMA_WAITOK, &(mem->map))) { 285 bus_dmamem_unmap(dmat, mem->kaddr, mem->size); 286 bus_dmamem_free(dmat, mem->segs, mem->nsegs); 287 dmamem_delete(mem); 288 return NULL; 289 } 290 291 if (bus_dmamap_load(dmat, mem->map, mem->kaddr, 292 mem->size, NULL, BUS_DMA_WAITOK)) { 293 bus_dmamap_destroy(dmat, mem->map); 294 bus_dmamem_unmap(dmat, mem->kaddr, mem->size); 295 bus_dmamem_free(dmat, mem->segs, mem->nsegs); 296 dmamem_delete(mem); 297 return NULL; 298 } 299 300 return mem; 301 } 302 303 static void 304 dmamem_free(struct dmamem *mem) 305 { 306 307 bus_dmamap_unload(mem->dmat, mem->map); 308 bus_dmamap_destroy(mem->dmat, mem->map); 309 bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size); 310 bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs); 311 dmamem_delete(mem); 312 } 313 314 315 /* 316 * Autoconf device callbacks : attach and detach 317 */ 318 319 static void 320 emuxki_pci_shutdown(struct emuxki_softc *sc) 321 { 322 323 if (sc->sc_ih != NULL) 324 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 325 if (sc->sc_ios) 326 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 327 } 328 329 static int 330 emuxki_scinit(struct emuxki_softc *sc) 331 { 332 int err; 333 334 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 335 EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK | 336 EMU_HCFG_MUTEBUTTONENABLE); 337 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 338 EMU_INTE_SAMPLERATER | EMU_INTE_PCIERRENABLE); 339 340 if ((err = emuxki_init(sc))) 341 return err; 342 343 if (sc->sc_type & EMUXKI_AUDIGY2) { 344 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 345 EMU_HCFG_AUDIOENABLE | EMU_HCFG_AC3ENABLE_CDSPDIF | 346 EMU_HCFG_AC3ENABLE_GPSPDIF | EMU_HCFG_AUTOMUTE); 347 } else if (sc->sc_type & EMUXKI_AUDIGY) { 348 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 349 EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE); 350 } else { 351 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 352 EMU_HCFG_AUDIOENABLE | EMU_HCFG_JOYENABLE | 353 EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_AUTOMUTE); 354 } 355 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 356 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_INTE) | 357 EMU_INTE_VOLINCRENABLE | EMU_INTE_VOLDECRENABLE | 358 EMU_INTE_MUTEENABLE); 359 if (sc->sc_type & EMUXKI_AUDIGY2) { 360 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG, 361 EMU_A_IOCFG_GPOUT0 | 362 bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_A_IOCFG)); 363 } 364 365 /* No multiple voice support for now */ 366 sc->pvoice = sc->rvoice = NULL; 367 368 return 0; 369 } 370 371 static int 372 emuxki_ac97_init(struct emuxki_softc *sc) 373 { 374 sc->hostif.arg = sc; 375 sc->hostif.attach = emuxki_ac97_attach; 376 sc->hostif.read = emuxki_ac97_read; 377 sc->hostif.write = emuxki_ac97_write; 378 sc->hostif.reset = emuxki_ac97_reset; 379 sc->hostif.flags = emuxki_ac97_flags; 380 return ac97_attach(&sc->hostif, sc->sc_dev, &sc->sc_lock); 381 } 382 383 static int 384 emuxki_match(device_t parent, cfdata_t match, 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(device_t parent, device_t self, void *aux) 404 { 405 struct emuxki_softc *sc; 406 struct pci_attach_args *pa; 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 414 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 415 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO); 416 mutex_init(&sc->sc_index_lock, MUTEX_DEFAULT, IPL_AUDIO); 417 mutex_init(&sc->sc_ac97_index_lock, MUTEX_DEFAULT, IPL_AUDIO); 418 419 if (pci_mapreg_map(pa, EMU_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 420 &(sc->sc_iot), &(sc->sc_ioh), &(sc->sc_iob), 421 &(sc->sc_ios))) { 422 aprint_error(": can't map iospace\n"); 423 return; 424 } 425 426 pci_aprint_devinfo(pa, "Audio controller"); 427 428 sc->sc_pc = pa->pa_pc; 429 sc->sc_dmat = pa->pa_dmat; 430 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 431 pci_conf_read(pa->pa_pc, pa->pa_tag, 432 (PCI_COMMAND_STATUS_REG) | PCI_COMMAND_MASTER_ENABLE)); 433 434 if (pci_intr_map(pa, &ih)) { 435 aprint_error_dev(self, "couldn't map interrupt\n"); 436 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 437 return; 438 } 439 440 intrstr = pci_intr_string(pa->pa_pc, ih); 441 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO, emuxki_intr, 442 sc); 443 if (sc->sc_ih == NULL) { 444 aprint_error_dev(self, "couldn't establish interrupt"); 445 if (intrstr != NULL) 446 aprint_error(" at %s", intrstr); 447 aprint_error("\n"); 448 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 449 return; 450 } 451 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 452 453 /* XXX it's unknown whether APS is made from Audigy as well */ 454 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_AUDIGY) { 455 sc->sc_type = EMUXKI_AUDIGY; 456 if (PCI_REVISION(pa->pa_class) == 0x04) { 457 sc->sc_type |= EMUXKI_AUDIGY2; 458 strlcpy(sc->sc_audv.name, "Audigy2", sizeof sc->sc_audv.name); 459 } else { 460 strlcpy(sc->sc_audv.name, "Audigy", sizeof sc->sc_audv.name); 461 } 462 } else if (pci_conf_read(pa->pa_pc, pa->pa_tag, 463 PCI_SUBSYS_ID_REG) == EMU_SUBSYS_APS) { 464 sc->sc_type = EMUXKI_APS; 465 strlcpy(sc->sc_audv.name, "E-mu APS", sizeof sc->sc_audv.name); 466 } else { 467 sc->sc_type = EMUXKI_SBLIVE; 468 strlcpy(sc->sc_audv.name, "SB Live!", sizeof sc->sc_audv.name); 469 } 470 snprintf(sc->sc_audv.version, sizeof sc->sc_audv.version, "0x%02x", 471 PCI_REVISION(pa->pa_class)); 472 strlcpy(sc->sc_audv.config, "emuxki", sizeof sc->sc_audv.config); 473 474 if (emuxki_scinit(sc) || emuxki_ac97_init(sc) || 475 (sc->sc_audev = audio_attach_mi(&emuxki_hw_if, sc, self)) == NULL) { 476 emuxki_pci_shutdown(sc); 477 return; 478 } 479 #if 0 480 mutex_enter(&sc->lock); 481 sc->rsourcectl.dev = 482 sc->codecif->vtbl->get_portnum_by_name(sc->codec_if, AudioCrecord, 483 AudioNsource, NULL); 484 sc->rsourcectl.cp = AUDIO_MIXER_ENUM; 485 mutex_exit(&sc->lock); 486 #endif 487 } 488 489 static int 490 emuxki_detach(device_t self, int flags) 491 { 492 struct emuxki_softc *sc; 493 494 sc = device_private(self); 495 if (sc->sc_audev != NULL) /* Test in case audio didn't attach */ 496 config_detach(sc->sc_audev, 0); 497 498 /* All voices should be stopped now but add some code here if not */ 499 500 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_HCFG, 501 EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK | 502 EMU_HCFG_MUTEBUTTONENABLE); 503 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_INTE, 0); 504 505 mutex_enter(&sc->sc_lock); 506 emuxki_shutdown(sc); 507 mutex_exit(&sc->sc_lock); 508 509 emuxki_pci_shutdown(sc); 510 511 mutex_destroy(&sc->sc_lock); 512 mutex_destroy(&sc->sc_intr_lock); 513 mutex_destroy(&sc->sc_index_lock); 514 mutex_destroy(&sc->sc_ac97_index_lock); 515 516 return 0; 517 } 518 519 520 /* Misc stuff relative to emu10k1 */ 521 522 static uint32_t 523 emuxki_rate_to_pitch(uint32_t rate) 524 { 525 static const uint32_t logMagTable[128] = { 526 0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 527 0x13aa2, 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 528 0x2655d, 0x28ed5, 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 529 0x381b6, 0x3a93d, 0x3d081, 0x3f782, 0x41e42, 0x444c1, 0x46b01, 530 0x49101, 0x4b6c4, 0x4dc49, 0x50191, 0x5269e, 0x54b6f, 0x57006, 531 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7, 0x646ee, 0x66a00, 532 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829, 0x759d4, 533 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e, 534 0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 535 0x93d26, 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 536 0xa11d8, 0xa2f9d, 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 537 0xadf26, 0xafbe7, 0xb1885, 0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 538 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899, 0xc1404, 0xc2f50, 0xc4a7b, 539 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c, 0xceaec, 0xd053f, 540 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3, 0xdba4a, 541 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3, 542 0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 543 0xf2c83, 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 544 0xfd1a7, 0xfe8df 545 }; 546 static const uint8_t logSlopeTable[128] = { 547 0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58, 548 0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53, 549 0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f, 550 0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b, 551 0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47, 552 0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44, 553 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41, 554 0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e, 555 0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c, 556 0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39, 557 0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37, 558 0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35, 559 0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34, 560 0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32, 561 0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30, 562 0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f 563 }; 564 int8_t i; 565 566 if (rate == 0) 567 return 0; /* Bail out if no leading "1" */ 568 rate *= 11185; /* Scale 48000 to 0x20002380 */ 569 for (i = 31; i > 0; i--) { 570 if (rate & 0x80000000) { /* Detect leading "1" */ 571 return (((uint32_t) (i - 15) << 20) + 572 logMagTable[0x7f & (rate >> 24)] + 573 (0x7f & (rate >> 17)) * 574 logSlopeTable[0x7f & (rate >> 24)]); 575 } 576 rate <<= 1; 577 } 578 579 return 0; /* Should never reach this point */ 580 } 581 582 /* Emu10k1 Low level */ 583 584 static uint32_t 585 emuxki_read(struct emuxki_softc *sc, uint16_t chano, uint32_t reg) 586 { 587 uint32_t ptr, mask; 588 uint8_t size, offset; 589 590 mask = 0xffffffff; 591 offset = 0; 592 ptr = ((((u_int32_t) reg) << 16) & 593 (sc->sc_type & EMUXKI_AUDIGY ? 594 EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK)) | 595 (chano & EMU_PTR_CHNO_MASK); 596 if (reg & 0xff000000) { 597 size = (reg >> 24) & 0x3f; 598 offset = (reg >> 16) & 0x1f; 599 mask = ((1 << size) - 1) << offset; 600 } 601 602 mutex_spin_enter(&sc->sc_index_lock); 603 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_PTR, ptr); 604 ptr = (bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_DATA) & mask) 605 >> offset; 606 mutex_spin_exit(&sc->sc_index_lock); 607 608 return ptr; 609 } 610 611 static void 612 emuxki_write(struct emuxki_softc *sc, uint16_t chano, 613 uint32_t reg, uint32_t data) 614 { 615 uint32_t ptr, mask; 616 uint8_t size, offset; 617 618 ptr = ((((u_int32_t) reg) << 16) & 619 (sc->sc_type & EMUXKI_AUDIGY ? 620 EMU_A_PTR_ADDR_MASK : EMU_PTR_ADDR_MASK)) | 621 (chano & EMU_PTR_CHNO_MASK); 622 if (reg & 0xff000000) { 623 size = (reg >> 24) & 0x3f; 624 offset = (reg >> 16) & 0x1f; 625 mask = ((1 << size) - 1) << offset; 626 data = ((data << offset) & mask) | 627 (emuxki_read(sc, chano, reg & 0xffff) & ~mask); 628 } 629 630 mutex_spin_enter(&sc->sc_index_lock); 631 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_PTR, ptr); 632 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_DATA, data); 633 mutex_spin_exit(&sc->sc_index_lock); 634 } 635 636 /* Microcode should this go in /sys/dev/microcode ? */ 637 638 static void 639 emuxki_write_micro(struct emuxki_softc *sc, uint32_t pc, uint32_t data) 640 { 641 642 emuxki_write(sc, 0, 643 (sc->sc_type & EMUXKI_AUDIGY ? 644 EMU_A_MICROCODEBASE : EMU_MICROCODEBASE) + pc, 645 data); 646 } 647 648 static void 649 emuxki_dsp_addop(struct emuxki_softc *sc, uint16_t *pc, uint8_t op, 650 uint16_t r, uint16_t a, uint16_t x, uint16_t y) 651 { 652 653 if (sc->sc_type & EMUXKI_AUDIGY) { 654 emuxki_write_micro(sc, *pc << 1, 655 ((x << 12) & EMU_A_DSP_LOWORD_OPX_MASK) | 656 (y & EMU_A_DSP_LOWORD_OPY_MASK)); 657 emuxki_write_micro(sc, (*pc << 1) + 1, 658 ((op << 24) & EMU_A_DSP_HIWORD_OPCODE_MASK) | 659 ((r << 12) & EMU_A_DSP_HIWORD_RESULT_MASK) | 660 (a & EMU_A_DSP_HIWORD_OPA_MASK)); 661 } else { 662 emuxki_write_micro(sc, *pc << 1, 663 ((x << 10) & EMU_DSP_LOWORD_OPX_MASK) | 664 (y & EMU_DSP_LOWORD_OPY_MASK)); 665 emuxki_write_micro(sc, (*pc << 1) + 1, 666 ((op << 20) & EMU_DSP_HIWORD_OPCODE_MASK) | 667 ((r << 10) & EMU_DSP_HIWORD_RESULT_MASK) | 668 (a & EMU_DSP_HIWORD_OPA_MASK)); 669 } 670 (*pc)++; 671 } 672 673 /* init and shutdown */ 674 675 static void 676 emuxki_initfx(struct emuxki_softc *sc) 677 { 678 uint16_t pc; 679 680 /* Set all GPRs to 0 */ 681 for (pc = 0; pc < 256; pc++) 682 emuxki_write(sc, 0, EMU_DSP_GPR(pc), 0); 683 for (pc = 0; pc < 160; pc++) { 684 emuxki_write(sc, 0, EMU_TANKMEMDATAREGBASE + pc, 0); 685 emuxki_write(sc, 0, EMU_TANKMEMADDRREGBASE + pc, 0); 686 } 687 pc = 0; 688 689 if (sc->sc_type & EMUXKI_AUDIGY) { 690 /* AC97 Out (l/r) = AC97 In (l/r) + FX[0/1] * 4 */ 691 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 692 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT), 693 EMU_A_DSP_CST(0), 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_FRONT), 697 EMU_A_DSP_CST(0), 698 EMU_DSP_FX(1), EMU_A_DSP_CST(4)); 699 700 /* Rear channel OUT (l/r) = FX[2/3] * 4 */ 701 #if 0 702 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 703 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_REAR), 704 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_A_FRONT), 705 EMU_DSP_FX(0), EMU_A_DSP_CST(4)); 706 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 707 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_REAR), 708 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_A_FRONT), 709 EMU_DSP_FX(1), EMU_A_DSP_CST(4)); 710 #endif 711 /* ADC recording (l/r) = AC97 In (l/r) */ 712 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 713 EMU_A_DSP_OUTL(EMU_A_DSP_OUT_ADC), 714 EMU_A_DSP_INL(EMU_DSP_IN_AC97), 715 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0)); 716 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 717 EMU_A_DSP_OUTR(EMU_A_DSP_OUT_ADC), 718 EMU_A_DSP_INR(EMU_DSP_IN_AC97), 719 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0)); 720 721 /* zero out the rest of the microcode */ 722 while (pc < 512) 723 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 724 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0), 725 EMU_A_DSP_CST(0), EMU_A_DSP_CST(0)); 726 727 emuxki_write(sc, 0, EMU_A_DBG, 0); /* Is it really necessary ? */ 728 } else { 729 /* AC97 Out (l/r) = AC97 In (l/r) + FX[0/1] * 4 */ 730 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 731 EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT), 732 EMU_DSP_CST(0), 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_A_FRONT), 736 EMU_DSP_CST(0), 737 EMU_DSP_FX(1), EMU_DSP_CST(4)); 738 739 /* Rear channel OUT (l/r) = FX[2/3] * 4 */ 740 #if 0 741 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 742 EMU_DSP_OUTL(EMU_DSP_OUT_AD_REAR), 743 EMU_DSP_OUTL(EMU_DSP_OUT_A_FRONT), 744 EMU_DSP_FX(0), EMU_DSP_CST(4)); 745 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS, 746 EMU_DSP_OUTR(EMU_DSP_OUT_AD_REAR), 747 EMU_DSP_OUTR(EMU_DSP_OUT_A_FRONT), 748 EMU_DSP_FX(1), EMU_DSP_CST(4)); 749 #endif 750 /* ADC recording (l/r) = AC97 In (l/r) */ 751 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 752 EMU_DSP_OUTL(EMU_DSP_OUT_ADC), 753 EMU_DSP_INL(EMU_DSP_IN_AC97), 754 EMU_DSP_CST(0), EMU_DSP_CST(0)); 755 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 756 EMU_DSP_OUTR(EMU_DSP_OUT_ADC), 757 EMU_DSP_INR(EMU_DSP_IN_AC97), 758 EMU_DSP_CST(0), EMU_DSP_CST(0)); 759 760 /* zero out the rest of the microcode */ 761 while (pc < 512) 762 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3, 763 EMU_DSP_CST(0), EMU_DSP_CST(0), 764 EMU_DSP_CST(0), EMU_DSP_CST(0)); 765 766 emuxki_write(sc, 0, EMU_DBG, 0); /* Is it really necessary ? */ 767 } 768 } 769 770 static int 771 emuxki_init(struct emuxki_softc *sc) 772 { 773 uint16_t i; 774 uint32_t spcs, *ptb; 775 bus_addr_t silentpage; 776 777 /* disable any channel interrupt */ 778 emuxki_write(sc, 0, EMU_CLIEL, 0); 779 emuxki_write(sc, 0, EMU_CLIEH, 0); 780 emuxki_write(sc, 0, EMU_SOLEL, 0); 781 emuxki_write(sc, 0, EMU_SOLEH, 0); 782 783 /* Set recording buffers sizes to zero */ 784 emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE); 785 emuxki_write(sc, 0, EMU_MICBA, 0); 786 emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE); 787 emuxki_write(sc, 0, EMU_FXBA, 0); 788 emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE); 789 emuxki_write(sc, 0, EMU_ADCBA, 0); 790 791 if(sc->sc_type & EMUXKI_AUDIGY) { 792 emuxki_write(sc, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS); 793 emuxki_write(sc, 0, EMU_AC97SLOT, EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE); 794 } 795 796 /* Initialize all channels to stopped and no effects */ 797 for (i = 0; i < EMU_NUMCHAN; i++) { 798 emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0); 799 emuxki_write(sc, i, EMU_CHAN_IP, 0); 800 emuxki_write(sc, i, EMU_CHAN_VTFT, 0xffff); 801 emuxki_write(sc, i, EMU_CHAN_CVCF, 0xffff); 802 emuxki_write(sc, i, EMU_CHAN_PTRX, 0); 803 emuxki_write(sc, i, EMU_CHAN_CPF, 0); 804 emuxki_write(sc, i, EMU_CHAN_CCR, 0); 805 emuxki_write(sc, i, EMU_CHAN_PSST, 0); 806 emuxki_write(sc, i, EMU_CHAN_DSL, 0x10); /* Why 16 ? */ 807 emuxki_write(sc, i, EMU_CHAN_CCCA, 0); 808 emuxki_write(sc, i, EMU_CHAN_Z1, 0); 809 emuxki_write(sc, i, EMU_CHAN_Z2, 0); 810 emuxki_write(sc, i, EMU_CHAN_FXRT, 0x32100000); 811 emuxki_write(sc, i, EMU_CHAN_ATKHLDM, 0); 812 emuxki_write(sc, i, EMU_CHAN_DCYSUSM, 0); 813 emuxki_write(sc, i, EMU_CHAN_IFATN, 0xffff); 814 emuxki_write(sc, i, EMU_CHAN_PEFE, 0); 815 emuxki_write(sc, i, EMU_CHAN_FMMOD, 0); 816 emuxki_write(sc, i, EMU_CHAN_TREMFRQ, 24); 817 emuxki_write(sc, i, EMU_CHAN_FM2FRQ2, 24); 818 emuxki_write(sc, i, EMU_CHAN_TEMPENV, 0); 819 820 /* these are last so OFF prevents writing */ 821 emuxki_write(sc, i, EMU_CHAN_LFOVAL2, 0); 822 emuxki_write(sc, i, EMU_CHAN_LFOVAL1, 0); 823 emuxki_write(sc, i, EMU_CHAN_ATKHLDV, 0); 824 emuxki_write(sc, i, EMU_CHAN_ENVVOL, 0); 825 emuxki_write(sc, i, EMU_CHAN_ENVVAL, 0); 826 } 827 828 /* set digital outputs format */ 829 spcs = (EMU_SPCS_CLKACCY_1000PPM | EMU_SPCS_SAMPLERATE_48 | 830 EMU_SPCS_CHANNELNUM_LEFT | EMU_SPCS_SOURCENUM_UNSPEC | 831 EMU_SPCS_GENERATIONSTATUS | 0x00001200 /* Cat code. */ | 832 0x00000000 /* IEC-958 Mode */ | EMU_SPCS_EMPHASIS_NONE | 833 EMU_SPCS_COPYRIGHT); 834 emuxki_write(sc, 0, EMU_SPCS0, spcs); 835 emuxki_write(sc, 0, EMU_SPCS1, spcs); 836 emuxki_write(sc, 0, EMU_SPCS2, spcs); 837 838 if(sc->sc_type & EMUXKI_AUDIGY2) { 839 emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE, EMU_A2_SPDIF_UNKNOWN); 840 841 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCSEL); 842 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, 843 EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI); 844 845 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_PTR, EMU_A2_SRCMULTI); 846 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_A2_DATA, EMU_A2_SRCMULTI_ENABLE_INPUT); 847 } 848 849 850 /* Let's play with sound processor */ 851 emuxki_initfx(sc); 852 853 /* Here is our Page Table */ 854 if ((sc->ptb = dmamem_alloc(sc->sc_dmat, 855 EMU_MAXPTE * sizeof(u_int32_t), 856 EMU_DMA_ALIGN, EMU_DMAMEM_NSEG)) == NULL) 857 return ENOMEM; 858 859 /* This is necessary unless you like Metallic noise... */ 860 if ((sc->silentpage = dmamem_alloc(sc->sc_dmat, EMU_PTESIZE, 861 EMU_DMA_ALIGN, EMU_DMAMEM_NSEG))==NULL){ 862 dmamem_free(sc->ptb); 863 return ENOMEM; 864 } 865 866 /* Zero out the silent page */ 867 /* This might not be always true, it might be 128 for 8bit channels */ 868 memset(KERNADDR(sc->silentpage), 0, DMASIZE(sc->silentpage)); 869 870 /* 871 * Set all the PTB Entries to the silent page We shift the physical 872 * address by one and OR it with the page number. I don't know what 873 * the ORed index is for, might be a very useful unused feature... 874 */ 875 silentpage = DMAADDR(sc->silentpage) << 1; 876 ptb = KERNADDR(sc->ptb); 877 for (i = 0; i < EMU_MAXPTE; i++) 878 ptb[i] = htole32(silentpage | i); 879 880 /* Write PTB address and set TCB to none */ 881 emuxki_write(sc, 0, EMU_PTB, DMAADDR(sc->ptb)); 882 emuxki_write(sc, 0, EMU_TCBS, 0); /* This means 16K TCB */ 883 emuxki_write(sc, 0, EMU_TCB, 0); /* No TCB use for now */ 884 885 /* 886 * Set channels MAPs to the silent page. 887 * I don't know what MAPs are for. 888 */ 889 silentpage |= EMU_CHAN_MAP_PTI_MASK; 890 for (i = 0; i < EMU_NUMCHAN; i++) { 891 emuxki_write(sc, i, EMU_CHAN_MAPA, silentpage); 892 emuxki_write(sc, i, EMU_CHAN_MAPB, silentpage); 893 sc->channel[i] = NULL; 894 } 895 896 /* Init voices list */ 897 LIST_INIT(&(sc->voices)); 898 899 /* Timer is stopped */ 900 sc->timerstate &= ~EMU_TIMER_STATE_ENABLED; 901 return 0; 902 } 903 904 static void 905 emuxki_shutdown(struct emuxki_softc *sc) 906 { 907 uint32_t i; 908 909 /* Disable any Channels interrupts */ 910 emuxki_write(sc, 0, EMU_CLIEL, 0); 911 emuxki_write(sc, 0, EMU_CLIEH, 0); 912 emuxki_write(sc, 0, EMU_SOLEL, 0); 913 emuxki_write(sc, 0, EMU_SOLEH, 0); 914 915 /* 916 * Should do some voice(stream) stopping stuff here, that's what will 917 * stop and deallocate all channels. 918 */ 919 920 /* Stop all channels */ 921 /* XXX This shouldn't be necessary, I'll remove once everything works */ 922 for (i = 0; i < EMU_NUMCHAN; i++) 923 emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0); 924 for (i = 0; i < EMU_NUMCHAN; i++) { 925 emuxki_write(sc, i, EMU_CHAN_VTFT, 0); 926 emuxki_write(sc, i, EMU_CHAN_CVCF, 0); 927 emuxki_write(sc, i, EMU_CHAN_PTRX, 0); 928 emuxki_write(sc, i, EMU_CHAN_CPF, 0); 929 } 930 931 /* 932 * Deallocate Emu10k1 caches and recording buffers. Again it will be 933 * removed because it will be done in voice shutdown. 934 */ 935 emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE); 936 emuxki_write(sc, 0, EMU_MICBA, 0); 937 emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE); 938 emuxki_write(sc, 0, EMU_FXBA, 0); 939 if(sc->sc_type & EMUXKI_AUDIGY) { 940 emuxki_write(sc, 0, EMU_A_FXWC1, 0); 941 emuxki_write(sc, 0, EMU_A_FXWC2, 0); 942 } else { 943 emuxki_write(sc, 0, EMU_FXWC, 0); 944 } 945 emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE); 946 emuxki_write(sc, 0, EMU_ADCBA, 0); 947 948 /* 949 * XXX I don't know yet how I will handle tank cache buffer, 950 * I don't even clearly know what it is for. 951 */ 952 emuxki_write(sc, 0, EMU_TCB, 0); /* 16K again */ 953 emuxki_write(sc, 0, EMU_TCBS, 0); 954 955 emuxki_write(sc, 0, EMU_DBG, 0x8000); /* necessary ? */ 956 957 dmamem_free(sc->silentpage); 958 dmamem_free(sc->ptb); 959 } 960 961 /* Emu10k1 Memory management */ 962 963 static struct emuxki_mem * 964 emuxki_mem_new(struct emuxki_softc *sc, int ptbidx, size_t size) 965 { 966 struct emuxki_mem *mem; 967 968 if ((mem = kmem_alloc(sizeof(*mem), KM_SLEEP)) == NULL) 969 return NULL; 970 971 mem->ptbidx = ptbidx; 972 if ((mem->dmamem = dmamem_alloc(sc->sc_dmat, size, EMU_DMA_ALIGN, 973 EMU_DMAMEM_NSEG)) == NULL) { 974 kmem_free(mem, sizeof(*mem)); 975 return NULL; 976 } 977 return mem; 978 } 979 980 static void 981 emuxki_mem_delete(struct emuxki_mem *mem, size_t size) 982 { 983 984 dmamem_free(mem->dmamem); 985 kmem_free(mem, sizeof(*mem)); 986 } 987 988 static void * 989 emuxki_pmem_alloc(struct emuxki_softc *sc, size_t size) 990 { 991 int i, j; 992 size_t numblocks; 993 struct emuxki_mem *mem; 994 uint32_t *ptb, silentpage; 995 996 ptb = KERNADDR(sc->ptb); 997 silentpage = DMAADDR(sc->silentpage) << 1; 998 numblocks = size / EMU_PTESIZE; 999 if (size % EMU_PTESIZE) 1000 numblocks++; 1001 1002 for (i = 0; i < EMU_MAXPTE; i++) { 1003 mutex_spin_enter(&sc->sc_intr_lock); 1004 if ((le32toh(ptb[i]) & EMU_CHAN_MAP_PTE_MASK) == silentpage) { 1005 /* We look for a free PTE */ 1006 for (j = 0; j < numblocks; j++) 1007 if ((le32toh(ptb[i + j]) 1008 & EMU_CHAN_MAP_PTE_MASK) != silentpage) 1009 break; 1010 if (j == numblocks) { 1011 mutex_spin_exit(&sc->sc_intr_lock); 1012 if ((mem = emuxki_mem_new(sc, i, 1013 size)) == NULL) { 1014 return NULL; 1015 } 1016 mutex_spin_enter(&sc->sc_intr_lock); 1017 for (j = 0; j < numblocks; j++) 1018 ptb[i + j] = 1019 htole32((((DMAADDR(mem->dmamem) + 1020 j * EMU_PTESIZE)) << 1) | (i + j)); 1021 LIST_INSERT_HEAD(&(sc->mem), mem, next); 1022 mutex_spin_exit(&sc->sc_intr_lock); 1023 return (KERNADDR(mem->dmamem)); 1024 } else 1025 i += j; 1026 } 1027 mutex_spin_exit(&sc->sc_intr_lock); 1028 } 1029 return NULL; 1030 } 1031 1032 static void * 1033 emuxki_rmem_alloc(struct emuxki_softc *sc, size_t size) 1034 { 1035 struct emuxki_mem *mem; 1036 1037 mem = emuxki_mem_new(sc, EMU_RMEM, size); 1038 if (mem == NULL) 1039 return NULL; 1040 1041 mutex_spin_enter(&sc->sc_intr_lock); 1042 LIST_INSERT_HEAD(&(sc->mem), mem, next); 1043 mutex_spin_exit(&sc->sc_intr_lock); 1044 1045 return KERNADDR(mem->dmamem); 1046 } 1047 1048 /* 1049 * emuxki_channel_* : Channel management functions 1050 * emuxki_chanparms_* : Channel parameters modification functions 1051 */ 1052 1053 /* 1054 * is splaudio necessary here, can the same voice be manipulated by two 1055 * different threads at a time ? 1056 */ 1057 static void 1058 emuxki_chanparms_set_defaults(struct emuxki_channel *chan) 1059 { 1060 1061 chan->fxsend.a.level = chan->fxsend.b.level = 1062 chan->fxsend.c.level = chan->fxsend.d.level = 1063 /* for audigy */ 1064 chan->fxsend.e.level = chan->fxsend.f.level = 1065 chan->fxsend.g.level = chan->fxsend.h.level = 1066 chan->voice->sc->sc_type & EMUXKI_AUDIGY ? 1067 0xc0 : 0xff; /* not max */ 1068 1069 chan->fxsend.a.dest = 0x0; 1070 chan->fxsend.b.dest = 0x1; 1071 chan->fxsend.c.dest = 0x2; 1072 chan->fxsend.d.dest = 0x3; 1073 /* for audigy */ 1074 chan->fxsend.e.dest = 0x4; 1075 chan->fxsend.f.dest = 0x5; 1076 chan->fxsend.g.dest = 0x6; 1077 chan->fxsend.h.dest = 0x7; 1078 1079 chan->pitch.initial = 0x0000; /* shouldn't it be 0xE000 ? */ 1080 chan->pitch.current = 0x0000; /* should it be 0x0400 */ 1081 chan->pitch.target = 0x0000; /* the unity pitch shift ? */ 1082 chan->pitch.envelope_amount = 0x00; /* none */ 1083 1084 chan->initial_attenuation = 0x00; /* no attenuation */ 1085 chan->volume.current = 0x0000; /* no volume */ 1086 chan->volume.target = 0xffff; 1087 chan->volume.envelope.current_state = 0x8000; /* 0 msec delay */ 1088 chan->volume.envelope.hold_time = 0x7f; /* 0 msec */ 1089 chan->volume.envelope.attack_time = 0x7F; /* 5.5msec */ 1090 chan->volume.envelope.sustain_level = 0x7F; /* full */ 1091 chan->volume.envelope.decay_time = 0x7F; /* 22msec */ 1092 1093 chan->filter.initial_cutoff_frequency = 0xff; /* no filter */ 1094 chan->filter.current_cutoff_frequency = 0xffff; /* no filtering */ 1095 chan->filter.target_cutoff_frequency = 0xffff; /* no filtering */ 1096 chan->filter.lowpass_resonance_height = 0x0; 1097 chan->filter.interpolation_ROM = 0x1; /* full band */ 1098 chan->filter.envelope_amount = 0x7f; /* none */ 1099 chan->filter.LFO_modulation_depth = 0x00; /* none */ 1100 1101 chan->loop.start = 0x000000; 1102 chan->loop.end = 0x000010; /* Why ? */ 1103 1104 chan->modulation.envelope.current_state = 0x8000; 1105 chan->modulation.envelope.hold_time = 0x00; /* 127 better ? */ 1106 chan->modulation.envelope.attack_time = 0x00; /* infinite */ 1107 chan->modulation.envelope.sustain_level = 0x00; /* off */ 1108 chan->modulation.envelope.decay_time = 0x7f; /* 22 msec */ 1109 chan->modulation.LFO_state = 0x8000; 1110 1111 chan->vibrato_LFO.state = 0x8000; 1112 chan->vibrato_LFO.modulation_depth = 0x00; /* none */ 1113 chan->vibrato_LFO.vibrato_depth = 0x00; 1114 chan->vibrato_LFO.frequency = 0x00; /* Why set to 24 when 1115 * initialized ? */ 1116 1117 chan->tremolo_depth = 0x00; 1118 } 1119 1120 /* only call it at splaudio */ 1121 static struct emuxki_channel * 1122 emuxki_channel_new(struct emuxki_voice *voice, u_int8_t num) 1123 { 1124 struct emuxki_channel *chan; 1125 1126 chan = malloc(sizeof(struct emuxki_channel), M_DEVBUF, M_WAITOK); 1127 if (chan == NULL) 1128 return NULL; 1129 1130 chan->voice = voice; 1131 chan->num = num; 1132 emuxki_chanparms_set_defaults(chan); 1133 chan->voice->sc->channel[num] = chan; 1134 return chan; 1135 } 1136 1137 /* only call it at splaudio */ 1138 static void 1139 emuxki_channel_delete(struct emuxki_channel *chan) 1140 { 1141 1142 chan->voice->sc->channel[chan->num] = NULL; 1143 free(chan, M_DEVBUF); 1144 } 1145 1146 static void 1147 emuxki_channel_set_fxsend(struct emuxki_channel *chan, 1148 struct emuxki_chanparms_fxsend *fxsend) 1149 { 1150 /* Could do a memcpy ...*/ 1151 chan->fxsend.a.level = fxsend->a.level; 1152 chan->fxsend.b.level = fxsend->b.level; 1153 chan->fxsend.c.level = fxsend->c.level; 1154 chan->fxsend.d.level = fxsend->d.level; 1155 chan->fxsend.a.dest = fxsend->a.dest; 1156 chan->fxsend.b.dest = fxsend->b.dest; 1157 chan->fxsend.c.dest = fxsend->c.dest; 1158 chan->fxsend.d.dest = fxsend->d.dest; 1159 1160 /* for audigy */ 1161 chan->fxsend.e.level = fxsend->e.level; 1162 chan->fxsend.f.level = fxsend->f.level; 1163 chan->fxsend.g.level = fxsend->g.level; 1164 chan->fxsend.h.level = fxsend->h.level; 1165 chan->fxsend.e.dest = fxsend->e.dest; 1166 chan->fxsend.f.dest = fxsend->f.dest; 1167 chan->fxsend.g.dest = fxsend->g.dest; 1168 chan->fxsend.h.dest = fxsend->h.dest; 1169 } 1170 1171 static void 1172 emuxki_channel_set_srate(struct emuxki_channel *chan, uint32_t srate) 1173 { 1174 1175 chan->pitch.target = (srate << 8) / 375; 1176 chan->pitch.target = (chan->pitch.target >> 1) + 1177 (chan->pitch.target & 1); 1178 chan->pitch.target &= 0xffff; 1179 chan->pitch.current = chan->pitch.target; 1180 chan->pitch.initial = 1181 (emuxki_rate_to_pitch(srate) >> 8) & EMU_CHAN_IP_MASK; 1182 } 1183 1184 /* voice params must be set before calling this */ 1185 static void 1186 emuxki_channel_set_bufparms(struct emuxki_channel *chan, 1187 uint32_t start, uint32_t end) 1188 { 1189 1190 chan->loop.start = start & EMU_CHAN_PSST_LOOPSTARTADDR_MASK; 1191 chan->loop.end = end & EMU_CHAN_DSL_LOOPENDADDR_MASK; 1192 } 1193 1194 static void 1195 emuxki_channel_commit_fx(struct emuxki_channel *chan) 1196 { 1197 struct emuxki_softc *sc; 1198 u_int8_t chano; 1199 1200 sc = chan->voice->sc; 1201 chano = chan->num; 1202 if(sc->sc_type & EMUXKI_AUDIGY) { 1203 emuxki_write(sc, chano, EMU_A_CHAN_FXRT1, 1204 (chan->fxsend.d.dest << 24) | 1205 (chan->fxsend.c.dest << 16) | 1206 (chan->fxsend.b.dest << 8) | 1207 (chan->fxsend.a.dest)); 1208 emuxki_write(sc, chano, EMU_A_CHAN_FXRT2, 1209 (chan->fxsend.h.dest << 24) | 1210 (chan->fxsend.g.dest << 16) | 1211 (chan->fxsend.f.dest << 8) | 1212 (chan->fxsend.e.dest)); 1213 emuxki_write(sc, chano, EMU_A_CHAN_SENDAMOUNTS, 1214 (chan->fxsend.e.level << 24) | 1215 (chan->fxsend.f.level << 16) | 1216 (chan->fxsend.g.level << 8) | 1217 (chan->fxsend.h.level)); 1218 } else { 1219 emuxki_write(sc, chano, EMU_CHAN_FXRT, 1220 (chan->fxsend.d.dest << 28) | 1221 (chan->fxsend.c.dest << 24) | 1222 (chan->fxsend.b.dest << 20) | 1223 (chan->fxsend.a.dest << 16)); 1224 } 1225 1226 emuxki_write(sc, chano, 0x10000000 | EMU_CHAN_PTRX, 1227 (chan->fxsend.a.level << 8) | chan->fxsend.b.level); 1228 emuxki_write(sc, chano, EMU_CHAN_DSL, 1229 (chan->fxsend.d.level << 24) | chan->loop.end); 1230 emuxki_write(sc, chano, EMU_CHAN_PSST, 1231 (chan->fxsend.c.level << 24) | chan->loop.start); 1232 } 1233 1234 static void 1235 emuxki_channel_commit_parms(struct emuxki_channel *chan) 1236 { 1237 struct emuxki_voice *voice; 1238 struct emuxki_softc *sc; 1239 uint32_t start, mapval; 1240 uint8_t chano; 1241 1242 voice = chan->voice; 1243 sc = voice->sc; 1244 chano = chan->num; 1245 start = chan->loop.start + 1246 (voice->stereo ? 28 : 30) * (voice->b16 + 1); 1247 mapval = DMAADDR(sc->silentpage) << 1 | EMU_CHAN_MAP_PTI_MASK; 1248 1249 KASSERT(mutex_owned(&sc->sc_intr_lock)); 1250 emuxki_write(sc, chano, EMU_CHAN_CPF_STEREO, voice->stereo); 1251 1252 emuxki_channel_commit_fx(chan); 1253 1254 emuxki_write(sc, chano, EMU_CHAN_CCCA, 1255 (chan->filter.lowpass_resonance_height << 28) | 1256 (chan->filter.interpolation_ROM << 25) | 1257 (voice->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT) | start); 1258 emuxki_write(sc, chano, EMU_CHAN_Z1, 0); 1259 emuxki_write(sc, chano, EMU_CHAN_Z2, 0); 1260 emuxki_write(sc, chano, EMU_CHAN_MAPA, mapval); 1261 emuxki_write(sc, chano, EMU_CHAN_MAPB, mapval); 1262 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRFILTER, 1263 chan->filter.current_cutoff_frequency); 1264 emuxki_write(sc, chano, EMU_CHAN_VTFT_FILTERTARGET, 1265 chan->filter.target_cutoff_frequency); 1266 emuxki_write(sc, chano, EMU_CHAN_ATKHLDM, 1267 (chan->modulation.envelope.hold_time << 8) | 1268 chan->modulation.envelope.attack_time); 1269 emuxki_write(sc, chano, EMU_CHAN_DCYSUSM, 1270 (chan->modulation.envelope.sustain_level << 8) | 1271 chan->modulation.envelope.decay_time); 1272 emuxki_write(sc, chano, EMU_CHAN_LFOVAL1, 1273 chan->modulation.LFO_state); 1274 emuxki_write(sc, chano, EMU_CHAN_LFOVAL2, 1275 chan->vibrato_LFO.state); 1276 emuxki_write(sc, chano, EMU_CHAN_FMMOD, 1277 (chan->vibrato_LFO.modulation_depth << 8) | 1278 chan->filter.LFO_modulation_depth); 1279 emuxki_write(sc, chano, EMU_CHAN_TREMFRQ, 1280 (chan->tremolo_depth << 8)); 1281 emuxki_write(sc, chano, EMU_CHAN_FM2FRQ2, 1282 (chan->vibrato_LFO.vibrato_depth << 8) | 1283 chan->vibrato_LFO.frequency); 1284 emuxki_write(sc, chano, EMU_CHAN_ENVVAL, 1285 chan->modulation.envelope.current_state); 1286 emuxki_write(sc, chano, EMU_CHAN_ATKHLDV, 1287 (chan->volume.envelope.hold_time << 8) | 1288 chan->volume.envelope.attack_time); 1289 emuxki_write(sc, chano, EMU_CHAN_ENVVOL, 1290 chan->volume.envelope.current_state); 1291 emuxki_write(sc, chano, EMU_CHAN_PEFE, 1292 (chan->pitch.envelope_amount << 8) | 1293 chan->filter.envelope_amount); 1294 } 1295 1296 static void 1297 emuxki_channel_start(struct emuxki_channel *chan) 1298 { 1299 struct emuxki_voice *voice; 1300 struct emuxki_softc *sc; 1301 u_int8_t cache_sample, cache_invalid_size, chano; 1302 u_int32_t sample; 1303 1304 voice = chan->voice; 1305 sc = voice->sc; 1306 chano = chan->num; 1307 cache_sample = voice->stereo ? 4 : 2; 1308 sample = voice->b16 ? 0x00000000 : 0x80808080; 1309 cache_invalid_size = (voice->stereo ? 28 : 30) * (voice->b16 + 1); 1310 1311 KASSERT(mutex_owned(&sc->sc_intr_lock)); 1312 while (cache_sample--) { 1313 emuxki_write(sc, chano, EMU_CHAN_CD0 + cache_sample, 1314 sample); 1315 } 1316 emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0); 1317 emuxki_write(sc, chano, EMU_CHAN_CCR_READADDRESS, 64); 1318 emuxki_write(sc, chano, EMU_CHAN_CCR_CACHEINVALIDSIZE, 1319 cache_invalid_size); 1320 emuxki_write(sc, chano, EMU_CHAN_IFATN, 1321 (chan->filter.target_cutoff_frequency << 8) | 1322 chan->initial_attenuation); 1323 emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET, 1324 chan->volume.target); 1325 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL, 1326 chan->volume.current); 1327 emuxki_write(sc, 0, 1328 EMU_MKSUBREG(1, chano, EMU_SOLEL + (chano >> 5)), 1329 0); /* Clear stop on loop */ 1330 emuxki_write(sc, 0, 1331 EMU_MKSUBREG(1, chano, EMU_CLIEL + (chano >> 5)), 1332 0); /* Clear loop interrupt */ 1333 emuxki_write(sc, chano, EMU_CHAN_DCYSUSV, 1334 (chan->volume.envelope.sustain_level << 8) | 1335 chan->volume.envelope.decay_time); 1336 emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET, 1337 chan->pitch.target); 1338 emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH, 1339 chan->pitch.current); 1340 emuxki_write(sc, chano, EMU_CHAN_IP, chan->pitch.initial); 1341 } 1342 1343 static void 1344 emuxki_channel_stop(struct emuxki_channel *chan) 1345 { 1346 struct emuxki_softc *sc; 1347 u_int8_t chano; 1348 1349 sc = chan->voice->sc; 1350 chano = chan->num; 1351 KASSERT(mutex_owned(&sc->sc_intr_lock)); 1352 emuxki_write(sc, chano, EMU_CHAN_PTRX_PITCHTARGET, 0); 1353 emuxki_write(sc, chano, EMU_CHAN_CPF_PITCH, 0); 1354 emuxki_write(sc, chano, EMU_CHAN_IFATN_ATTENUATION, 0xff); 1355 emuxki_write(sc, chano, EMU_CHAN_VTFT_VOLUMETARGET, 0); 1356 emuxki_write(sc, chano, EMU_CHAN_CVCF_CURRVOL, 0); 1357 emuxki_write(sc, chano, EMU_CHAN_IP, 0); 1358 } 1359 1360 /* 1361 * Voices management 1362 * emuxki_voice_dataloc : use(play or rec) independent dataloc union helpers 1363 * emuxki_voice_channel_* : play part of dataloc union helpers 1364 * emuxki_voice_recsrc_* : rec part of dataloc union helpers 1365 */ 1366 1367 /* Allocate channels for voice in case of play voice */ 1368 static int 1369 emuxki_voice_channel_create(struct emuxki_voice *voice) 1370 { 1371 struct emuxki_channel **channel; 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 1381 if (stereo) { 1382 voice->dataloc.chan[1] = 1383 emuxki_channel_new(voice, i + 1); 1384 if (voice->dataloc.chan[1] == NULL) 1385 return ENOMEM; 1386 } 1387 voice->dataloc.chan[0] = emuxki_channel_new(voice, i); 1388 if (voice->dataloc.chan[0] == NULL) { 1389 if (stereo) { 1390 emuxki_channel_delete(voice->dataloc.chan[1]); 1391 voice->dataloc.chan[1] = NULL; 1392 } 1393 return ENOMEM; 1394 } 1395 return 0; 1396 } 1397 return EAGAIN; 1398 } 1399 1400 /* When calling this function we assume no one can access the voice */ 1401 static void 1402 emuxki_voice_channel_destroy(struct emuxki_voice *voice) 1403 { 1404 1405 emuxki_channel_delete(voice->dataloc.chan[0]); 1406 voice->dataloc.chan[0] = NULL; 1407 if (voice->stereo) 1408 emuxki_channel_delete(voice->dataloc.chan[1]); 1409 voice->dataloc.chan[1] = NULL; 1410 } 1411 1412 /* 1413 * Will come back when used in voice_dataloc_create 1414 */ 1415 static int 1416 emuxki_recsrc_reserve(struct emuxki_voice *voice, emuxki_recsrc_t source) 1417 { 1418 1419 if (source >= EMU_NUMRECSRCS) { 1420 #ifdef EMUXKI_DEBUG 1421 printf("Tried to reserve invalid source: %d\n", source); 1422 #endif 1423 return EINVAL; 1424 } 1425 if (voice->sc->recsrc[source] == voice) 1426 return 0; /* XXX */ 1427 if (voice->sc->recsrc[source] != NULL) 1428 return EBUSY; 1429 voice->sc->recsrc[source] = voice; 1430 return 0; 1431 } 1432 1433 /* When calling this function we assume the voice is stopped */ 1434 static void 1435 emuxki_voice_recsrc_release(struct emuxki_softc *sc, emuxki_recsrc_t source) 1436 { 1437 1438 sc->recsrc[source] = NULL; 1439 } 1440 1441 static int 1442 emuxki_voice_dataloc_create(struct emuxki_voice *voice) 1443 { 1444 int error; 1445 1446 if (voice->use & EMU_VOICE_USE_PLAY) { 1447 if ((error = emuxki_voice_channel_create(voice))) 1448 return error; 1449 } else { 1450 if ((error = 1451 emuxki_recsrc_reserve(voice, voice->dataloc.source))) 1452 return error; 1453 } 1454 return 0; 1455 } 1456 1457 static void 1458 emuxki_voice_dataloc_destroy(struct emuxki_voice *voice) 1459 { 1460 1461 if (voice->use & EMU_VOICE_USE_PLAY) { 1462 if (voice->dataloc.chan[0] != NULL) 1463 emuxki_voice_channel_destroy(voice); 1464 } else { 1465 if (voice->dataloc.source != EMU_RECSRC_NOTSET) { 1466 emuxki_voice_recsrc_release(voice->sc, 1467 voice->dataloc.source); 1468 voice->dataloc.source = EMU_RECSRC_NOTSET; 1469 } 1470 } 1471 } 1472 1473 static struct emuxki_voice * 1474 emuxki_voice_new(struct emuxki_softc *sc, uint8_t use) 1475 { 1476 struct emuxki_voice *voice; 1477 1478 KASSERT(mutex_owned(&sc->sc_intr_lock)); 1479 1480 voice = sc->lvoice; 1481 sc->lvoice = NULL; 1482 1483 if (!voice) { 1484 mutex_exit(&sc->sc_intr_lock); 1485 voice = kmem_alloc(sizeof(*voice), KM_SLEEP); 1486 mutex_enter(&sc->sc_intr_lock); 1487 if (!voice) 1488 return NULL; 1489 } else if (voice->use != use) { 1490 mutex_exit(&sc->sc_intr_lock); 1491 emuxki_voice_dataloc_destroy(voice); 1492 mutex_enter(&sc->sc_intr_lock); 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 LIST_INSERT_HEAD((&sc->voices), voice, next); 1515 1516 return voice; 1517 } 1518 1519 static void 1520 emuxki_voice_delete(struct emuxki_voice *voice) 1521 { 1522 struct emuxki_softc *sc; 1523 struct emuxki_voice *lvoice; 1524 1525 sc = voice->sc; 1526 if (voice->state & EMU_VOICE_STATE_STARTED) 1527 emuxki_voice_halt(voice); 1528 1529 LIST_REMOVE(voice, next); 1530 lvoice = sc->lvoice; 1531 sc->lvoice = voice; 1532 1533 if (lvoice) { 1534 mutex_exit(&sc->sc_lock); 1535 emuxki_voice_dataloc_destroy(lvoice); 1536 kmem_free(lvoice, sizeof(*lvoice)); 1537 mutex_enter(&sc->sc_lock); 1538 } 1539 } 1540 1541 static int 1542 emuxki_voice_set_stereo(struct emuxki_softc *sc, 1543 struct emuxki_voice *voice, uint8_t stereo) 1544 { 1545 int error; 1546 emuxki_recsrc_t source; 1547 struct emuxki_chanparms_fxsend fxsend; 1548 1549 source = 0; /* XXX: gcc */ 1550 if (! (voice->use & EMU_VOICE_USE_PLAY)) 1551 source = voice->dataloc.source; 1552 mutex_exit(&sc->sc_lock); 1553 emuxki_voice_dataloc_destroy(voice); 1554 if (! (voice->use & EMU_VOICE_USE_PLAY)) 1555 voice->dataloc.source = source; 1556 voice->stereo = stereo; 1557 error = emuxki_voice_dataloc_create(voice); 1558 mutex_enter(&sc->sc_lock); 1559 if (error) 1560 return error; 1561 if (voice->use & EMU_VOICE_USE_PLAY) { 1562 fxsend.a.dest = 0x0; 1563 fxsend.b.dest = 0x1; 1564 fxsend.c.dest = 0x2; 1565 fxsend.d.dest = 0x3; 1566 /* for audigy */ 1567 fxsend.e.dest = 0x4; 1568 fxsend.f.dest = 0x5; 1569 fxsend.g.dest = 0x6; 1570 fxsend.h.dest = 0x7; 1571 if (voice->stereo) { 1572 fxsend.a.level = fxsend.c.level = 0xc0; 1573 fxsend.b.level = fxsend.d.level = 0x00; 1574 fxsend.e.level = fxsend.g.level = 0xc0; 1575 fxsend.f.level = fxsend.h.level = 0x00; 1576 emuxki_channel_set_fxsend(voice->dataloc.chan[0], 1577 &fxsend); 1578 fxsend.a.level = fxsend.c.level = 0x00; 1579 fxsend.b.level = fxsend.d.level = 0xc0; 1580 fxsend.e.level = fxsend.g.level = 0x00; 1581 fxsend.f.level = fxsend.h.level = 0xc0; 1582 emuxki_channel_set_fxsend(voice->dataloc.chan[1], 1583 &fxsend); 1584 } /* No else : default is good for mono */ 1585 } 1586 return 0; 1587 } 1588 1589 static int 1590 emuxki_voice_set_srate(struct emuxki_voice *voice, uint32_t srate) 1591 { 1592 1593 if (voice->use & EMU_VOICE_USE_PLAY) { 1594 if ((srate < 4000) || (srate > 48000)) 1595 return EINVAL; 1596 voice->sample_rate = srate; 1597 emuxki_channel_set_srate(voice->dataloc.chan[0], srate); 1598 if (voice->stereo) 1599 emuxki_channel_set_srate(voice->dataloc.chan[1], 1600 srate); 1601 } else { 1602 if ((srate < 8000) || (srate > 48000)) 1603 return EINVAL; 1604 voice->sample_rate = srate; 1605 if (emuxki_voice_adc_rate(voice) < 0) { 1606 voice->sample_rate = 0; 1607 return EINVAL; 1608 } 1609 } 1610 return 0; 1611 } 1612 1613 static int 1614 emuxki_voice_set_audioparms(struct emuxki_softc *sc, 1615 struct emuxki_voice *voice, uint8_t stereo, uint8_t b16, uint32_t srate) 1616 { 1617 int error; 1618 1619 if (voice->stereo == stereo && voice->b16 == b16 && 1620 voice->sample_rate == srate) 1621 return 0; 1622 1623 #ifdef EMUXKI_DEBUG 1624 printf("Setting %s voice params : %s, %u bits, %u Hz\n", 1625 (voice->use & EMU_VOICE_USE_PLAY) ? "play" : "record", 1626 stereo ? "stereo" : "mono", (b16 + 1) * 8, srate); 1627 #endif 1628 error = 0; 1629 if (voice->stereo != stereo) { 1630 if ((error = emuxki_voice_set_stereo(sc, voice, stereo))) 1631 return error; 1632 } 1633 voice->b16 = b16; 1634 if (voice->sample_rate != srate) 1635 error = emuxki_voice_set_srate(voice, srate); 1636 return error; 1637 } 1638 1639 /* voice audio parms (see just before) must be set prior to this */ 1640 static int 1641 emuxki_voice_set_bufparms(struct emuxki_voice *voice, void *ptr, 1642 uint32_t bufsize, uint16_t blksize) 1643 { 1644 struct emuxki_mem *mem; 1645 struct emuxki_channel **chan; 1646 uint32_t start, end; 1647 uint8_t sample_size; 1648 int idx; 1649 int error; 1650 1651 error = EFAULT; 1652 LIST_FOREACH(mem, &voice->sc->mem, next) { 1653 if (KERNADDR(mem->dmamem) != ptr) 1654 continue; 1655 1656 voice->buffer = mem; 1657 sample_size = (voice->b16 + 1) * (voice->stereo + 1); 1658 voice->trigblk = 0; /* This shouldn't be needed */ 1659 voice->blkmod = bufsize / blksize; 1660 if (bufsize % blksize) /* This should not happen */ 1661 voice->blkmod++; 1662 error = 0; 1663 1664 if (voice->use & EMU_VOICE_USE_PLAY) { 1665 voice->blksize = blksize / sample_size; 1666 chan = voice->dataloc.chan; 1667 start = mem->ptbidx << 12; 1668 end = start + bufsize / sample_size; 1669 emuxki_channel_set_bufparms(chan[0], 1670 start, end); 1671 if (voice->stereo) 1672 emuxki_channel_set_bufparms(chan[1], 1673 start, end); 1674 voice->timerate = (uint32_t) 48000 * 1675 voice->blksize / voice->sample_rate; 1676 if (voice->timerate < 5) 1677 error = EINVAL; 1678 } else { 1679 voice->blksize = blksize; 1680 for(idx = sizeof(emuxki_recbuf_sz) / 1681 sizeof(emuxki_recbuf_sz[0]); --idx >= 0;) 1682 if (emuxki_recbuf_sz[idx] == bufsize) 1683 break; 1684 if (idx < 0) { 1685 #ifdef EMUXKI_DEBUG 1686 printf("Invalid bufsize: %d\n", bufsize); 1687 #endif 1688 return EINVAL; 1689 } 1690 emuxki_write(voice->sc, 0, 1691 emuxki_recsrc_szreg[voice->dataloc.source], idx); 1692 emuxki_write(voice->sc, 0, 1693 emuxki_recsrc_bufaddrreg[voice->dataloc.source], 1694 DMAADDR(mem->dmamem)); 1695 1696 /* Use timer to emulate DMA completion interrupt */ 1697 voice->timerate = (u_int32_t) 48000 * blksize / 1698 (voice->sample_rate * sample_size); 1699 if (voice->timerate < 5) { 1700 #ifdef EMUXKI_DEBUG 1701 printf("Invalid timerate: %d, blksize %d\n", 1702 voice->timerate, blksize); 1703 #endif 1704 error = EINVAL; 1705 } 1706 } 1707 1708 break; 1709 } 1710 1711 return error; 1712 } 1713 1714 static void 1715 emuxki_voice_commit_parms(struct emuxki_voice *voice) 1716 { 1717 if (voice->use & EMU_VOICE_USE_PLAY) { 1718 emuxki_channel_commit_parms(voice->dataloc.chan[0]); 1719 if (voice->stereo) 1720 emuxki_channel_commit_parms(voice->dataloc.chan[1]); 1721 } 1722 } 1723 1724 static uint32_t 1725 emuxki_voice_curaddr(struct emuxki_voice *voice) 1726 { 1727 int idxreg; 1728 int rv; 1729 1730 /* XXX different semantics in these cases */ 1731 if (voice->use & EMU_VOICE_USE_PLAY) { 1732 /* returns number of samples (an l/r pair counts 1) */ 1733 rv = emuxki_read(voice->sc, 1734 voice->dataloc.chan[0]->num, EMU_CHAN_CCCA_CURRADDR) - 1735 voice->dataloc.chan[0]->loop.start; 1736 } else { 1737 idxreg = 0; 1738 /* returns number of bytes */ 1739 switch (voice->dataloc.source) { 1740 case EMU_RECSRC_MIC: 1741 idxreg = (voice->sc->sc_type & EMUXKI_AUDIGY) ? 1742 EMU_A_MICIDX : EMU_MICIDX; 1743 break; 1744 case EMU_RECSRC_ADC: 1745 idxreg = (voice->sc->sc_type & EMUXKI_AUDIGY) ? 1746 EMU_A_ADCIDX : EMU_ADCIDX; 1747 break; 1748 case EMU_RECSRC_FX: 1749 idxreg = EMU_FXIDX; 1750 break; 1751 default: 1752 #ifdef EMUXKI_DEBUG 1753 printf("emu: bad recording source!\n"); 1754 #endif 1755 break; 1756 } 1757 rv = emuxki_read(voice->sc, 0, EMU_RECIDX(idxreg) 1758 & EMU_RECIDX_MASK); 1759 } 1760 return rv; 1761 } 1762 1763 static void 1764 emuxki_resched_timer(struct emuxki_softc *sc) 1765 { 1766 struct emuxki_voice *voice; 1767 uint16_t timerate; 1768 uint8_t active; 1769 1770 timerate = 1024; 1771 active = 0; 1772 KASSERT(mutex_owned(&sc->sc_intr_lock)); 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 } 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 KASSERT(mutex_owned(&sc->sc_intr_lock)); 1901 val = emu_rd(sc, INTE, 4); 1902 val |= emuxki_recsrc_intrmasks[voice->dataloc.source]; 1903 emu_wr(sc, INTE, val, 4); 1904 break; 1905 default: 1906 break; 1907 } 1908 #endif 1909 } 1910 voice->state |= EMU_VOICE_STATE_STARTED; 1911 emuxki_resched_timer(voice->sc); 1912 } 1913 1914 static void 1915 emuxki_voice_halt(struct emuxki_voice *voice) 1916 { 1917 1918 if (voice->use & EMU_VOICE_USE_PLAY) { 1919 emuxki_channel_stop(voice->dataloc.chan[0]); 1920 if (voice->stereo) 1921 emuxki_channel_stop(voice->dataloc.chan[1]); 1922 } else { 1923 switch (voice->dataloc.source) { 1924 case EMU_RECSRC_ADC: 1925 emuxki_write(voice->sc, 0, EMU_ADCCR, 0); 1926 break; 1927 case EMU_RECSRC_FX: 1928 case EMU_RECSRC_MIC: 1929 printf("unimplemented\n"); 1930 break; 1931 default: 1932 case EMU_RECSRC_NOTSET: 1933 printf("Bad dataloc.source %d\n", 1934 voice->dataloc.source); 1935 break; 1936 } 1937 1938 switch (voice->dataloc.source) { 1939 case EMU_RECSRC_ADC: 1940 case EMU_RECSRC_FX: 1941 case EMU_RECSRC_MIC: 1942 /* This should reset buffer pointer */ 1943 emuxki_write(voice->sc, 0, 1944 emuxki_recsrc_szreg[voice->dataloc.source], 1945 EMU_RECBS_BUFSIZE_NONE); 1946 #if 0 1947 KASSERT(mutex_owned(&sc->sc_intr_lock)); 1948 val = emu_rd(sc, INTE, 4); 1949 val &= ~emuxki_recsrc_intrmasks[voice->dataloc.source]; 1950 emu_wr(sc, INTE, val, 4); 1951 #endif 1952 break; 1953 default: 1954 break; 1955 } 1956 } 1957 voice->state &= ~EMU_VOICE_STATE_STARTED; 1958 emuxki_resched_timer(voice->sc); 1959 } 1960 1961 /* 1962 * The interrupt handler 1963 */ 1964 static int 1965 emuxki_intr(void *arg) 1966 { 1967 struct emuxki_softc *sc; 1968 struct emuxki_voice *voice; 1969 uint32_t ipr, curblk; 1970 int claim; 1971 1972 sc = arg; 1973 claim = 0; 1974 1975 mutex_spin_enter(&sc->sc_intr_lock); 1976 1977 while ((ipr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EMU_IPR))) { 1978 if (ipr & EMU_IPR_INTERVALTIMER) { 1979 LIST_FOREACH(voice, &sc->voices, next) { 1980 if ((voice->state & 1981 EMU_VOICE_STATE_STARTED) == 0) 1982 continue; 1983 1984 curblk = emuxki_voice_curaddr(voice) / 1985 voice->blksize; 1986 #if 0 1987 if (curblk == voice->trigblk) { 1988 voice->inth(voice->inthparam); 1989 voice->trigblk++; 1990 voice->trigblk %= voice->blkmod; 1991 } 1992 #else 1993 while ((curblk >= voice->trigblk && 1994 curblk < (voice->trigblk + voice->blkmod / 2)) || 1995 ((int)voice->trigblk - (int)curblk) > 1996 (voice->blkmod / 2 + 1)) { 1997 voice->inth(voice->inthparam); 1998 voice->trigblk++; 1999 voice->trigblk %= voice->blkmod; 2000 } 2001 #endif 2002 } 2003 } 2004 2005 /* Got interrupt */ 2006 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EMU_IPR, ipr); 2007 2008 claim = 1; 2009 } 2010 2011 mutex_spin_exit(&sc->sc_intr_lock); 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_softc *sc, struct emuxki_voice *voice, 2154 const audio_params_t *p, 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(sc, voice, p->channels == 2, 2167 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(sc, 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 { 2279 if (direction == AUMODE_PLAY) 2280 return emuxki_pmem_alloc(addr, size); 2281 else 2282 return emuxki_rmem_alloc(addr, size); 2283 } 2284 2285 static void 2286 emuxki_freem(void *addr, void *ptr, size_t size) 2287 { 2288 struct emuxki_softc *sc; 2289 struct emuxki_mem *mem; 2290 uint32_t *ptb, silentpage; 2291 size_t numblocks; 2292 int i; 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 mutex_spin_enter(&sc->sc_intr_lock); 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 mutex_spin_exit(&sc->sc_intr_lock); 2312 2313 emuxki_mem_delete(mem, size); 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 = device_private(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 2391 mutex_exit(&sc->sc_lock); 2392 LIST_FOREACH(mem, &sc->mem, next) { 2393 if (KERNADDR(mem->dmamem) == ptr) { 2394 struct dmamem *dm = mem->dmamem; 2395 2396 return bus_dmamem_mmap(dm->dmat, dm->segs, dm->nsegs, 2397 off, prot, BUS_DMA_WAITOK); 2398 } 2399 } 2400 mutex_enter(&sc->sc_lock); 2401 2402 return -1; 2403 } 2404 2405 static int 2406 emuxki_get_props(void *addr) 2407 { 2408 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | 2409 AUDIO_PROP_FULLDUPLEX; 2410 } 2411 2412 static int 2413 emuxki_trigger_output(void *addr, void *start, void *end, int blksize, 2414 void (*inth) (void *), void *inthparam, const audio_params_t *params) 2415 { 2416 struct emuxki_softc *sc; 2417 /* No multiple voice support for now */ 2418 struct emuxki_voice *voice; 2419 int error; 2420 2421 sc = addr; 2422 voice = sc->pvoice; 2423 if (voice == NULL) 2424 return ENXIO; 2425 if ((error = emuxki_voice_set_audioparms(sc, voice, params->channels == 2, 2426 params->precision == 16, params->sample_rate))) 2427 return error; 2428 if ((error = emuxki_voice_set_bufparms(voice, start, 2429 (char *)end - (char *)start, blksize))) 2430 return error; 2431 emuxki_voice_commit_parms(voice); 2432 emuxki_voice_start(voice, inth, inthparam); 2433 2434 return 0; 2435 } 2436 2437 static int 2438 emuxki_trigger_input(void *addr, void *start, void *end, int blksize, 2439 void (*inth) (void *), void *inthparam, const audio_params_t *params) 2440 { 2441 struct emuxki_softc *sc; 2442 /* No multiple voice support for now */ 2443 struct emuxki_voice *voice; 2444 int error; 2445 2446 sc = addr; 2447 voice = sc->rvoice; 2448 if (voice == NULL) 2449 return ENXIO; 2450 if ((error = emuxki_voice_set_audioparms(sc, voice, 2451 params->channels == 2, params->precision == 16, 2452 params->sample_rate))) 2453 return error; 2454 if ((error = emuxki_voice_set_bufparms(voice, start, 2455 (char *)end - (char *)start, blksize))) 2456 return error; 2457 emuxki_voice_start(voice, inth, inthparam); 2458 2459 return 0; 2460 } 2461 2462 /* 2463 * AC97 callbacks 2464 */ 2465 2466 static int 2467 emuxki_ac97_attach(void *arg, struct ac97_codec_if *codecif) 2468 { 2469 struct emuxki_softc *sc; 2470 2471 sc = arg; 2472 sc->codecif = codecif; 2473 return 0; 2474 } 2475 2476 static int 2477 emuxki_ac97_read(void *arg, uint8_t reg, uint16_t *val) 2478 { 2479 struct emuxki_softc *sc; 2480 2481 sc = arg; 2482 mutex_spin_enter(&sc->sc_ac97_index_lock); 2483 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg); 2484 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA); 2485 mutex_spin_exit(&sc->sc_ac97_index_lock); 2486 2487 return 0; 2488 } 2489 2490 static int 2491 emuxki_ac97_write(void *arg, uint8_t reg, uint16_t val) 2492 { 2493 struct emuxki_softc *sc; 2494 2495 sc = arg; 2496 mutex_spin_enter(&sc->sc_ac97_index_lock); 2497 bus_space_write_1(sc->sc_iot, sc->sc_ioh, EMU_AC97ADDR, reg); 2498 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EMU_AC97DATA, val); 2499 mutex_spin_exit(&sc->sc_ac97_index_lock); 2500 2501 return 0; 2502 } 2503 2504 static int 2505 emuxki_ac97_reset(void *arg) 2506 { 2507 2508 return 0; 2509 } 2510 2511 enum ac97_host_flags 2512 emuxki_ac97_flags(void *arg) 2513 { 2514 2515 return AC97_HOST_SWAPPED_CHANNELS; 2516 } 2517 2518 static void 2519 emuxki_get_locks(void *arg, kmutex_t **intr, kmutex_t **proc) 2520 { 2521 struct emuxki_softc *sc; 2522 2523 sc = arg; 2524 *intr = &sc->sc_intr_lock; 2525 *proc = &sc->sc_lock; 2526 } 2527