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