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