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