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