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