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