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