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