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