Home | History | Annotate | Line # | Download | only in pci
emuxki.c revision 1.75
      1 /*	$NetBSD: emuxki.c,v 1.75 2022/09/07 01:00:37 khorben Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2007 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, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * EMU10K1 single voice driver
     34  * o. only 1 voice playback, 1 recording
     35  * o. only s16le 2ch 48k
     36  * This makes it simple to control buffers and interrupts
     37  * while satisfying playback and recording quality.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: emuxki.c,v 1.75 2022/09/07 01:00:37 khorben Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/device.h>
     45 #include <sys/module.h>
     46 #include <sys/errno.h>
     47 #include <sys/systm.h>
     48 #include <sys/audioio.h>
     49 #include <sys/mutex.h>
     50 #include <sys/kmem.h>
     51 #include <sys/malloc.h>
     52 #include <sys/fcntl.h>
     53 
     54 #include <sys/bus.h>
     55 #include <sys/intr.h>
     56 
     57 #include <dev/pci/emuxkireg.h>
     58 #include <dev/pci/emuxkivar.h>
     59 
     60 /* #define EMUXKI_DEBUG 1 */
     61 #ifdef EMUXKI_DEBUG
     62 #define emudebug EMUXKI_DEBUG
     63 # define DPRINTF(fmt...)	do { if (emudebug) printf(fmt); } while (0)
     64 # define DPRINTFN(n,fmt...)	do { if (emudebug>=(n)) printf(fmt); } while (0)
     65 #else
     66 # define DPRINTF(fmt...)	__nothing
     67 # define DPRINTFN(n,fmt...)	__nothing
     68 #endif
     69 
     70 /*
     71  * PCI
     72  * Note: emuxki's page table entry uses only 31bit addressing.
     73  *       (Maybe, later chip has 32bit mode, but it isn't used now.)
     74  */
     75 
     76 #define EMU_PCI_CBIO		(0x10)
     77 #define EMU_SUBSYS_APS		(0x40011102)
     78 
     79 /* blackmagic */
     80 #define X1(x)		((sc->sc_type & EMUXKI_AUDIGY) ? EMU_A_##x : EMU_##x)
     81 #define X2(x, y)	((sc->sc_type & EMUXKI_AUDIGY) \
     82     ? EMU_A_##x(EMU_A_##y) : EMU_##x(EMU_##y))
     83 #define EMU_A_DSP_FX		EMU_DSP_FX
     84 #define EMU_A_DSP_IN_AC97	EMU_DSP_IN_AC97
     85 
     86 /* prototypes */
     87 static struct dmamem *dmamem_alloc(struct emuxki_softc *, size_t);
     88 static void	dmamem_free(struct dmamem *);
     89 static void	dmamem_sync(struct dmamem *, int);
     90 static uint8_t	emuxki_readio_1(struct emuxki_softc *, int) __unused;
     91 static uint16_t	emuxki_readio_2(struct emuxki_softc *, int);
     92 static uint32_t	emuxki_readio_4(struct emuxki_softc *, int);
     93 static void	emuxki_writeio_1(struct emuxki_softc *, int, uint8_t);
     94 static void	emuxki_writeio_2(struct emuxki_softc *, int, uint16_t);
     95 static void	emuxki_writeio_4(struct emuxki_softc *, int, uint32_t);
     96 static uint32_t	emuxki_readptr(struct emuxki_softc *, int, int, int);
     97 static void	emuxki_writeptr(struct emuxki_softc *, int, int, int, uint32_t);
     98 static uint32_t	emuxki_read(struct emuxki_softc *, int, int);
     99 static void	emuxki_write(struct emuxki_softc *, int, int, uint32_t);
    100 static int	emuxki_match(device_t, cfdata_t, void *);
    101 static void	emuxki_attach(device_t, device_t, void *);
    102 static int	emuxki_detach(device_t, int);
    103 static int	emuxki_init(struct emuxki_softc *);
    104 static void	emuxki_dsp_addop(struct emuxki_softc *, uint16_t *, uint8_t,
    105 		    uint16_t, uint16_t, uint16_t, uint16_t);
    106 static void	emuxki_initfx(struct emuxki_softc *);
    107 static void	emuxki_play_start(struct emuxki_softc *, int, uint32_t,
    108 		    uint32_t);
    109 static void	emuxki_play_stop(struct emuxki_softc *, int);
    110 
    111 static int	emuxki_query_format(void *, audio_format_query_t *);
    112 static int	emuxki_set_format(void *, int,
    113 		    const audio_params_t *, const audio_params_t *,
    114 		    audio_filter_reg_t *, audio_filter_reg_t *);
    115 static int	emuxki_halt_output(void *);
    116 static int	emuxki_halt_input(void *);
    117 static int	emuxki_intr(void *);
    118 static int	emuxki_getdev(void *, struct audio_device *);
    119 static int	emuxki_set_port(void *, mixer_ctrl_t *);
    120 static int	emuxki_get_port(void *, mixer_ctrl_t *);
    121 static int	emuxki_query_devinfo(void *, mixer_devinfo_t *);
    122 static void	*emuxki_allocm(void *, int, size_t);
    123 static void	emuxki_freem(void *, void *, size_t);
    124 static int	emuxki_round_blocksize(void *, int, int,
    125 		    const audio_params_t *);
    126 static size_t	emuxki_round_buffersize(void *, int, size_t);
    127 static int	emuxki_get_props(void *);
    128 static int	emuxki_trigger_output(void *, void *, void *, int,
    129 		    void (*)(void *), void *, const audio_params_t *);
    130 static int	emuxki_trigger_input(void *, void *, void *, int,
    131 		    void (*)(void *), void *, const audio_params_t *);
    132 static void	emuxki_get_locks(void *, kmutex_t **, kmutex_t **);
    133 
    134 static int	emuxki_ac97_init(struct emuxki_softc *);
    135 static int	emuxki_ac97_attach(void *, struct ac97_codec_if *);
    136 static int	emuxki_ac97_read(void *, uint8_t, uint16_t *);
    137 static int	emuxki_ac97_write(void *, uint8_t, uint16_t);
    138 static int	emuxki_ac97_reset(void *);
    139 static enum ac97_host_flags	emuxki_ac97_flags(void *);
    140 
    141 
    142 CFATTACH_DECL_NEW(emuxki, sizeof(struct emuxki_softc),
    143     emuxki_match, emuxki_attach, emuxki_detach, NULL);
    144 
    145 static const struct audio_hw_if emuxki_hw_if = {
    146 	.query_format		= emuxki_query_format,
    147 	.set_format		= emuxki_set_format,
    148 	.round_blocksize	= emuxki_round_blocksize,
    149 	.halt_output		= emuxki_halt_output,
    150 	.halt_input		= emuxki_halt_input,
    151 	.getdev			= emuxki_getdev,
    152 	.set_port		= emuxki_set_port,
    153 	.get_port		= emuxki_get_port,
    154 	.query_devinfo		= emuxki_query_devinfo,
    155 	.allocm			= emuxki_allocm,
    156 	.freem			= emuxki_freem,
    157 	.round_buffersize	= emuxki_round_buffersize,
    158 	.get_props		= emuxki_get_props,
    159 	.trigger_output		= emuxki_trigger_output,
    160 	.trigger_input		= emuxki_trigger_input,
    161 	.get_locks		= emuxki_get_locks,
    162 };
    163 
    164 static const struct audio_format emuxki_formats[] = {
    165 	{
    166 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
    167 		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
    168 		.validbits	= 16,
    169 		.precision	= 16,
    170 		.channels	= 2,
    171 		.channel_mask	= AUFMT_STEREO,
    172 		.frequency_type	= 1,
    173 		.frequency	= { 48000 },
    174 	}
    175 };
    176 #define EMUXKI_NFORMATS	__arraycount(emuxki_formats)
    177 
    178 /*
    179  * dma memory
    180  */
    181 
    182 static struct dmamem *
    183 dmamem_alloc(struct emuxki_softc *sc, size_t size)
    184 {
    185 	struct dmamem *mem;
    186 
    187 	KASSERT(!mutex_owned(&sc->sc_intr_lock));
    188 
    189 	/* Allocate memory for structure */
    190 	mem = kmem_alloc(sizeof(*mem), KM_SLEEP);
    191 	mem->dmat = sc->sc_dmat;
    192 	mem->size = size;
    193 	mem->align = EMU_DMA_ALIGN;
    194 	mem->nsegs = EMU_DMA_NSEGS;
    195 	mem->bound = 0;
    196 
    197 	mem->segs = kmem_alloc(mem->nsegs * sizeof(*(mem->segs)), KM_SLEEP);
    198 
    199 	if (bus_dmamem_alloc(mem->dmat, mem->size, mem->align, mem->bound,
    200 	    mem->segs, mem->nsegs, &mem->rsegs, BUS_DMA_WAITOK)) {
    201 		device_printf(sc->sc_dev,
    202 		    "%s bus_dmamem_alloc failed\n", __func__);
    203 		goto memfree;
    204 	}
    205 
    206 	if (bus_dmamem_map(mem->dmat, mem->segs, mem->nsegs, mem->size,
    207 	    &mem->kaddr, BUS_DMA_WAITOK | BUS_DMA_COHERENT)) {
    208 		device_printf(sc->sc_dev,
    209 		    "%s bus_dmamem_map failed\n", __func__);
    210 		goto free;
    211 	}
    212 
    213 	if (bus_dmamap_create(mem->dmat, mem->size, mem->nsegs, mem->size,
    214 	    mem->bound, BUS_DMA_WAITOK, &mem->map)) {
    215 		device_printf(sc->sc_dev,
    216 		    "%s bus_dmamap_create failed\n", __func__);
    217 		goto unmap;
    218 	}
    219 
    220 	if (bus_dmamap_load(mem->dmat, mem->map, mem->kaddr,
    221 	    mem->size, NULL, BUS_DMA_WAITOK)) {
    222 		device_printf(sc->sc_dev,
    223 		    "%s bus_dmamap_load failed\n", __func__);
    224 		goto destroy;
    225 	}
    226 
    227 	return mem;
    228 
    229 destroy:
    230 	bus_dmamap_destroy(mem->dmat, mem->map);
    231 unmap:
    232 	bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
    233 free:
    234 	bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
    235 memfree:
    236 	kmem_free(mem->segs, mem->nsegs * sizeof(*(mem->segs)));
    237 	kmem_free(mem, sizeof(*mem));
    238 
    239 	return NULL;
    240 }
    241 
    242 static void
    243 dmamem_free(struct dmamem *mem)
    244 {
    245 
    246 	bus_dmamap_unload(mem->dmat, mem->map);
    247 	bus_dmamap_destroy(mem->dmat, mem->map);
    248 	bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
    249 	bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
    250 
    251 	kmem_free(mem->segs, mem->nsegs * sizeof(*(mem->segs)));
    252 	kmem_free(mem, sizeof(*mem));
    253 }
    254 
    255 static void
    256 dmamem_sync(struct dmamem *mem, int ops)
    257 {
    258 
    259 	bus_dmamap_sync(mem->dmat, mem->map, 0, mem->size, ops);
    260 }
    261 
    262 
    263 /*
    264  * I/O register access
    265  */
    266 
    267 static uint8_t
    268 emuxki_readio_1(struct emuxki_softc *sc, int addr)
    269 {
    270 
    271 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, addr);
    272 }
    273 
    274 static void
    275 emuxki_writeio_1(struct emuxki_softc *sc, int addr, uint8_t data)
    276 {
    277 
    278 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, addr, data);
    279 }
    280 
    281 static uint16_t
    282 emuxki_readio_2(struct emuxki_softc *sc, int addr)
    283 {
    284 
    285 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, addr);
    286 }
    287 
    288 static void
    289 emuxki_writeio_2(struct emuxki_softc *sc, int addr, uint16_t data)
    290 {
    291 
    292 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, addr, data);
    293 }
    294 
    295 static uint32_t
    296 emuxki_readio_4(struct emuxki_softc *sc, int addr)
    297 {
    298 
    299 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, addr);
    300 }
    301 
    302 static void
    303 emuxki_writeio_4(struct emuxki_softc *sc, int addr, uint32_t data)
    304 {
    305 
    306 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, addr, data);
    307 }
    308 
    309 static uint32_t
    310 emuxki_readptr(struct emuxki_softc *sc, int aptr, int dptr, int addr)
    311 {
    312 	uint32_t data;
    313 
    314 	mutex_spin_enter(&sc->sc_index_lock);
    315 	emuxki_writeio_4(sc, aptr, addr);
    316 	data = emuxki_readio_4(sc, dptr);
    317 	mutex_spin_exit(&sc->sc_index_lock);
    318 	return data;
    319 }
    320 
    321 static void
    322 emuxki_writeptr(struct emuxki_softc *sc, int aptr, int dptr, int addr,
    323     uint32_t data)
    324 {
    325 
    326 	mutex_spin_enter(&sc->sc_index_lock);
    327 	emuxki_writeio_4(sc, aptr, addr);
    328 	emuxki_writeio_4(sc, dptr, data);
    329 	mutex_spin_exit(&sc->sc_index_lock);
    330 }
    331 
    332 static uint32_t
    333 emuxki_read(struct emuxki_softc *sc, int ch, int addr)
    334 {
    335 
    336 	/* Original HENTAI addressing is never supported. */
    337 	KASSERT((addr & 0xff000000) == 0);
    338 
    339 	return emuxki_readptr(sc, EMU_PTR, EMU_DATA, (addr << 16) + ch);
    340 }
    341 
    342 static void
    343 emuxki_write(struct emuxki_softc *sc, int ch, int addr, uint32_t data)
    344 {
    345 
    346 	/* Original HENTAI addressing is never supported. */
    347 	KASSERT((addr & 0xff000000) == 0);
    348 
    349 	emuxki_writeptr(sc, EMU_PTR, EMU_DATA, (addr << 16) + ch, data);
    350 }
    351 
    352 /*
    353  * MD driver
    354  */
    355 
    356 static int
    357 emuxki_match(device_t parent, cfdata_t match, void *aux)
    358 {
    359 	struct pci_attach_args *pa;
    360 
    361 	pa = aux;
    362 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CREATIVELABS)
    363 		return 0;
    364 
    365 	switch (PCI_PRODUCT(pa->pa_id)) {
    366 	case PCI_PRODUCT_CREATIVELABS_SBLIVE:
    367 	case PCI_PRODUCT_CREATIVELABS_SBLIVE2:
    368 	case PCI_PRODUCT_CREATIVELABS_AUDIGY:
    369 	case PCI_PRODUCT_CREATIVELABS_SBAUDIGY4:
    370 		return 1;
    371 	default:
    372 		return 0;
    373 	}
    374 }
    375 
    376 static void
    377 emuxki_attach(device_t parent, device_t self, void *aux)
    378 {
    379 	struct emuxki_softc *sc;
    380 	struct pci_attach_args *pa;
    381 	pci_intr_handle_t ih;
    382 	const char *intrstr;
    383 	char intrbuf[PCI_INTRSTR_LEN];
    384 	pcireg_t reg;
    385 
    386 	sc = device_private(self);
    387 	sc->sc_dev = self;
    388 	pa = aux;
    389 
    390 	pci_aprint_devinfo(pa, "Audio controller");
    391 	DPRINTF("dmat=%p\n", (char *)pa->pa_dmat);
    392 
    393 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    394 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    395 	mutex_init(&sc->sc_index_lock, MUTEX_DEFAULT, IPL_AUDIO);
    396 
    397 	sc->sc_pc   = pa->pa_pc;
    398 
    399 	/* EMU10K1 can only address 31 bits (2GB) */
    400 	if (bus_dmatag_subregion(pa->pa_dmat, 0, ((uint32_t)1 << 31) - 1,
    401 	    &(sc->sc_dmat), BUS_DMA_NOWAIT) != 0) {
    402 		aprint_error_dev(self,
    403 		    "WARNING: failed to restrict dma range,"
    404 		    " falling back to parent bus dma range\n");
    405 		sc->sc_dmat = pa->pa_dmat;
    406 	}
    407 
    408 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    409 	reg |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE |
    410 	    PCI_COMMAND_MEM_ENABLE;
    411 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    412 
    413 	if (pci_mapreg_map(pa, EMU_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    414 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
    415 		aprint_error(": can't map iospace\n");
    416 		return;
    417 	}
    418 
    419 	if (pci_intr_map(pa, &ih)) {
    420 		aprint_error_dev(self, "couldn't map interrupt\n");
    421 		goto unmap;
    422 	}
    423 
    424 	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
    425 	sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_AUDIO,
    426 	    emuxki_intr, sc, device_xname(self));
    427 	if (sc->sc_ih == NULL) {
    428 		aprint_error_dev(self, "couldn't establish interrupt");
    429 		if (intrstr != NULL)
    430 			aprint_error(" at %s", intrstr);
    431 		aprint_error("\n");
    432 		goto unmap;
    433 	}
    434 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    435 
    436 	/* XXX it's unknown whether APS is made from Audigy as well */
    437 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBAUDIGY4) {
    438 		sc->sc_type = EMUXKI_AUDIGY;
    439 		sc->sc_type |= EMUXKI_AUDIGY2;
    440 		sc->sc_type |= EMUXKI_AUDIGY2_VALUE;
    441 		strlcpy(sc->sc_audv.name, "Audigy2 (value)",
    442 		    sizeof(sc->sc_audv.name));
    443 	} else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_AUDIGY) {
    444 		sc->sc_type = EMUXKI_AUDIGY;
    445 		if (PCI_REVISION(pa->pa_class) == 0x04) {
    446 			sc->sc_type |= EMUXKI_AUDIGY2;
    447 			strlcpy(sc->sc_audv.name, "Audigy2",
    448 			    sizeof(sc->sc_audv.name));
    449 		} else {
    450 			strlcpy(sc->sc_audv.name, "Audigy",
    451 			    sizeof(sc->sc_audv.name));
    452 		}
    453 	} else if (pci_conf_read(pa->pa_pc, pa->pa_tag,
    454 	    PCI_SUBSYS_ID_REG) == EMU_SUBSYS_APS) {
    455 		sc->sc_type = EMUXKI_APS;
    456 		strlcpy(sc->sc_audv.name, "E-mu APS", sizeof(sc->sc_audv.name));
    457 	} else {
    458 		sc->sc_type = EMUXKI_SBLIVE;
    459 		strlcpy(sc->sc_audv.name, "SB Live!", sizeof(sc->sc_audv.name));
    460 	}
    461 	snprintf(sc->sc_audv.version, sizeof(sc->sc_audv.version), "0x%02x",
    462 	    PCI_REVISION(pa->pa_class));
    463 	strlcpy(sc->sc_audv.config, "emuxki", sizeof(sc->sc_audv.config));
    464 
    465 	if (emuxki_init(sc)) {
    466 		aprint_error("emuxki_init error\n");
    467 		goto intrdis;
    468 	}
    469 	if (emuxki_ac97_init(sc)) {
    470 		aprint_error("emuxki_ac97_init error\n");
    471 		goto intrdis;
    472 	}
    473 
    474 	sc->sc_audev = audio_attach_mi(&emuxki_hw_if, sc, self);
    475 	if (sc->sc_audev == NULL) {
    476 		aprint_error("audio_attach_mi error\n");
    477 		goto intrdis;
    478 	}
    479 
    480 	return;
    481 
    482 intrdis:
    483 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    484 unmap:
    485 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    486 	return;
    487 }
    488 
    489 static int
    490 emuxki_detach(device_t self, int flags)
    491 {
    492 	struct emuxki_softc *sc;
    493 
    494 	sc = device_private(self);
    495 	if (sc->sc_audev != NULL) /* Test in case audio didn't attach */
    496 		config_detach(sc->sc_audev, 0);
    497 
    498 	/* All voices should be stopped now but add some code here if not */
    499 	emuxki_writeio_4(sc, EMU_HCFG,
    500 	    EMU_HCFG_LOCKSOUNDCACHE |
    501 	    EMU_HCFG_LOCKTANKCACHE_MASK |
    502 	    EMU_HCFG_MUTEBUTTONENABLE);
    503 	emuxki_writeio_4(sc, EMU_INTE, 0);
    504 
    505 	/* Disable any Channels interrupts */
    506 	emuxki_write(sc, 0, EMU_CLIEL, 0);
    507 	emuxki_write(sc, 0, EMU_CLIEH, 0);
    508 	emuxki_write(sc, 0, EMU_SOLEL, 0);
    509 	emuxki_write(sc, 0, EMU_SOLEH, 0);
    510 
    511 	/* stop DSP */
    512 	emuxki_write(sc, 0, X1(DBG), X1(DBG_SINGLE_STEP));
    513 
    514 	dmamem_free(sc->ptb);
    515 
    516 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    517 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    518 
    519 	mutex_destroy(&sc->sc_lock);
    520 	mutex_destroy(&sc->sc_intr_lock);
    521 	mutex_destroy(&sc->sc_index_lock);
    522 
    523 	return 0;
    524 }
    525 
    526 static int
    527 emuxki_init(struct emuxki_softc *sc)
    528 {
    529 	int i;
    530 	uint32_t spcs;
    531 	uint32_t hcfg;
    532 
    533 	/* clear AUDIO bit */
    534 	emuxki_writeio_4(sc, EMU_HCFG,
    535 	    EMU_HCFG_LOCKSOUNDCACHE |
    536 	    EMU_HCFG_LOCKTANKCACHE_MASK |
    537 	    EMU_HCFG_MUTEBUTTONENABLE);
    538 
    539 	/* mask interrupt without PCIERR */
    540 	emuxki_writeio_4(sc, EMU_INTE,
    541 	    EMU_INTE_SAMPLERATER | /* always on this bit */
    542 	    EMU_INTE_PCIERRENABLE);
    543 
    544 	/* disable all channel interrupt */
    545 	emuxki_write(sc, 0, EMU_CLIEL, 0);
    546 	emuxki_write(sc, 0, EMU_CLIEH, 0);
    547 	emuxki_write(sc, 0, EMU_SOLEL, 0);
    548 	emuxki_write(sc, 0, EMU_SOLEH, 0);
    549 
    550 	/* Set recording buffers sizes to zero */
    551 	emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
    552 	emuxki_write(sc, 0, EMU_MICBA, 0);
    553 	emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
    554 	emuxki_write(sc, 0, EMU_FXBA, 0);
    555 	emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
    556 	emuxki_write(sc, 0, EMU_ADCBA, 0);
    557 
    558 	if(sc->sc_type & EMUXKI_AUDIGY) {
    559 		emuxki_write(sc, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS);
    560 		emuxki_write(sc, 0, EMU_AC97SLOT,
    561 		    EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
    562 	}
    563 
    564 	/* Initialize all channels to stopped and no effects */
    565 	for (i = 0; i < EMU_NUMCHAN; i++) {
    566 		emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0x7f7f);
    567 		emuxki_write(sc, i, EMU_CHAN_IP, EMU_CHAN_IP_UNITY);
    568 		emuxki_write(sc, i, EMU_CHAN_VTFT, 0xffff);
    569 		emuxki_write(sc, i, EMU_CHAN_CVCF, 0xffff);
    570 		emuxki_write(sc, i, EMU_CHAN_PTRX, 0);
    571 		emuxki_write(sc, i, EMU_CHAN_CPF, 0);
    572 		emuxki_write(sc, i, EMU_CHAN_CCR, 0);
    573 		emuxki_write(sc, i, EMU_CHAN_PSST, 0);
    574 		emuxki_write(sc, i, EMU_CHAN_DSL, 0);
    575 		emuxki_write(sc, i, EMU_CHAN_CCCA, EMU_CHAN_CCCA_INTERPROM_1);
    576 		emuxki_write(sc, i, EMU_CHAN_Z1, 0);
    577 		emuxki_write(sc, i, EMU_CHAN_Z2, 0);
    578 		emuxki_write(sc, i, EMU_CHAN_MAPA, 0xffffffff);
    579 		emuxki_write(sc, i, EMU_CHAN_MAPB, 0xffffffff);
    580 		emuxki_write(sc, i, EMU_CHAN_FXRT, 0x32100000);
    581 		emuxki_write(sc, i, EMU_CHAN_ATKHLDM, 0);
    582 		emuxki_write(sc, i, EMU_CHAN_DCYSUSM, 0);
    583 		emuxki_write(sc, i, EMU_CHAN_IFATN, 0xffff);
    584 		emuxki_write(sc, i, EMU_CHAN_PEFE, 0x007f);
    585 		emuxki_write(sc, i, EMU_CHAN_FMMOD, 0);
    586 		emuxki_write(sc, i, EMU_CHAN_TREMFRQ, 0);
    587 		emuxki_write(sc, i, EMU_CHAN_FM2FRQ2, 0);
    588 		emuxki_write(sc, i, EMU_CHAN_TEMPENV, 0);
    589 
    590 		/* these are last so OFF prevents writing */
    591 		emuxki_write(sc, i, EMU_CHAN_LFOVAL2, 0x8000);
    592 		emuxki_write(sc, i, EMU_CHAN_LFOVAL1, 0x8000);
    593 		emuxki_write(sc, i, EMU_CHAN_ATKHLDV, 0x7f7f);
    594 		emuxki_write(sc, i, EMU_CHAN_ENVVOL, 0);
    595 		emuxki_write(sc, i, EMU_CHAN_ENVVAL, 0x8000);
    596 	}
    597 
    598 	/* set digital outputs format */
    599 	spcs = EMU_SPCS_CLKACCY_1000PPM |
    600 	       EMU_SPCS_SAMPLERATE_48 |
    601 	       EMU_SPCS_CHANNELNUM_LEFT |
    602 	       EMU_SPCS_SOURCENUM_UNSPEC |
    603 	       EMU_SPCS_GENERATIONSTATUS |
    604 	       0x00001200 /* Cat code. */ |
    605 	       0x00000000 /* IEC-958 Mode */ |
    606 	       EMU_SPCS_EMPHASIS_NONE |
    607 	       EMU_SPCS_COPYRIGHT;
    608 	emuxki_write(sc, 0, EMU_SPCS0, spcs);
    609 	emuxki_write(sc, 0, EMU_SPCS1, spcs);
    610 	emuxki_write(sc, 0, EMU_SPCS2, spcs);
    611 
    612 	if (sc->sc_type & EMUXKI_AUDIGY2_VALUE) {
    613 		/* Setup SRCMulti_I2S SamplingRate */
    614 		emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE,
    615 		    emuxki_read(sc, 0, EMU_A2_SPDIF_SAMPLERATE) & 0xfffff1ff);
    616 
    617 		/* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */
    618 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCSEL,
    619 		    EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
    620 
    621 		/* Setup SRCMulti Input Audio Enable */
    622 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA,
    623 		    0x7b0000, 0xff000000);
    624 
    625 		/* Setup SPDIF Out Audio Enable
    626 		 * The Audigy 2 Value has a separate SPDIF out,
    627 		 * so no need for a mixer switch */
    628 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA,
    629 		    0x7a0000, 0xff000000);
    630 		emuxki_writeio_4(sc, EMU_A_IOCFG,
    631 		    emuxki_readio_4(sc, EMU_A_IOCFG) & ~0x8); /* clear bit 3 */
    632 	} else if (sc->sc_type & EMUXKI_AUDIGY2) {
    633 		emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE,
    634 		    EMU_A2_SPDIF_UNKNOWN);
    635 
    636 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCSEL,
    637 		    EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
    638 
    639 		emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCMULTI,
    640 		    EMU_A2_SRCMULTI_ENABLE_INPUT);
    641 	}
    642 
    643 	/* page table */
    644 	sc->ptb = dmamem_alloc(sc, EMU_MAXPTE * sizeof(uint32_t));
    645 	if (sc->ptb == NULL) {
    646 		device_printf(sc->sc_dev, "ptb allocation error\n");
    647 		return ENOMEM;
    648 	}
    649 	emuxki_write(sc, 0, EMU_PTB, DMAADDR(sc->ptb));
    650 
    651 	emuxki_write(sc, 0, EMU_TCBS, 0);	/* This means 16K TCB */
    652 	emuxki_write(sc, 0, EMU_TCB, 0);	/* No TCB use for now */
    653 
    654 	/* Let's play with sound processor */
    655 	emuxki_initfx(sc);
    656 
    657 	/* enable interrupt */
    658 	emuxki_writeio_4(sc, EMU_INTE,
    659 	    emuxki_readio_4(sc, EMU_INTE) |
    660 	    EMU_INTE_VOLINCRENABLE |
    661 	    EMU_INTE_VOLDECRENABLE |
    662 	    EMU_INTE_MUTEENABLE);
    663 
    664 	if (sc->sc_type & EMUXKI_AUDIGY2_VALUE) {
    665 		emuxki_writeio_4(sc, EMU_A_IOCFG,
    666 		    0x0060 | emuxki_readio_4(sc, EMU_A_IOCFG));
    667 	} else if (sc->sc_type & EMUXKI_AUDIGY2) {
    668 		emuxki_writeio_4(sc, EMU_A_IOCFG,
    669 		    EMU_A_IOCFG_GPOUT0 | emuxki_readio_4(sc, EMU_A_IOCFG));
    670 	}
    671 
    672 	/* enable AUDIO bit */
    673 	hcfg = EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE;
    674 
    675 	if (sc->sc_type & EMUXKI_AUDIGY2) {
    676 		hcfg |= EMU_HCFG_AC3ENABLE_CDSPDIF |
    677 		        EMU_HCFG_AC3ENABLE_GPSPDIF;
    678 	} else if (sc->sc_type & EMUXKI_AUDIGY) {
    679 	} else {
    680 		hcfg |= EMU_HCFG_LOCKTANKCACHE_MASK;
    681 	}
    682 	/* joystick not supported now */
    683 	emuxki_writeio_4(sc, EMU_HCFG, hcfg);
    684 
    685 	return 0;
    686 }
    687 
    688 /*
    689  * dsp programming
    690  */
    691 
    692 static void
    693 emuxki_dsp_addop(struct emuxki_softc *sc, uint16_t *pc, uint8_t op,
    694     uint16_t r, uint16_t a, uint16_t x, uint16_t y)
    695 {
    696 	uint32_t loword;
    697 	uint32_t hiword;
    698 	int reg;
    699 
    700 	if (sc->sc_type & EMUXKI_AUDIGY) {
    701 		reg = EMU_A_MICROCODEBASE;
    702 		loword = (x << 12) & EMU_A_DSP_LOWORD_OPX_MASK;
    703 		loword |= y & EMU_A_DSP_LOWORD_OPY_MASK;
    704 		hiword = (op << 24) & EMU_A_DSP_HIWORD_OPCODE_MASK;
    705 		hiword |= (r << 12) & EMU_A_DSP_HIWORD_RESULT_MASK;
    706 		hiword |= a & EMU_A_DSP_HIWORD_OPA_MASK;
    707 	} else {
    708 		reg = EMU_MICROCODEBASE;
    709 		loword = (x << 10) & EMU_DSP_LOWORD_OPX_MASK;
    710 		loword |= y & EMU_DSP_LOWORD_OPY_MASK;
    711 		hiword = (op << 20) & EMU_DSP_HIWORD_OPCODE_MASK;
    712 		hiword |= (r << 10) & EMU_DSP_HIWORD_RESULT_MASK;
    713 		hiword |= a & EMU_DSP_HIWORD_OPA_MASK;
    714 	}
    715 
    716 	reg += (*pc) * 2;
    717 	/* must ordering; lo, hi */
    718 	emuxki_write(sc, 0, reg, loword);
    719 	emuxki_write(sc, 0, reg + 1, hiword);
    720 
    721 	(*pc)++;
    722 }
    723 
    724 static void
    725 emuxki_initfx(struct emuxki_softc *sc)
    726 {
    727 	uint16_t pc;
    728 
    729 	/* Set all GPRs to 0 */
    730 	for (pc = 0; pc < 256; pc++)
    731 		emuxki_write(sc, 0, EMU_DSP_GPR(pc), 0);
    732 	for (pc = 0; pc < 160; pc++) {
    733 		emuxki_write(sc, 0, EMU_TANKMEMDATAREGBASE + pc, 0);
    734 		emuxki_write(sc, 0, EMU_TANKMEMADDRREGBASE + pc, 0);
    735 	}
    736 
    737 	/* stop DSP, single step mode */
    738 	emuxki_write(sc, 0, X1(DBG), X1(DBG_SINGLE_STEP));
    739 
    740 	/* XXX: delay (48kHz equiv. 21us) if needed */
    741 
    742 	/* start DSP programming */
    743 	pc = 0;
    744 
    745 	/* OUT[L/R] = 0 + FX[L/R] * 1 */
    746 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
    747 	    X2(DSP_OUTL, DSP_OUT_A_FRONT),
    748 	    X1(DSP_CST(0)),
    749 	    X1(DSP_FX(0)),
    750 	    X1(DSP_CST(1)));
    751 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
    752 	    X2(DSP_OUTR, DSP_OUT_A_FRONT),
    753 	    X1(DSP_CST(0)),
    754 	    X1(DSP_FX(1)),
    755 	    X1(DSP_CST(1)));
    756 #if 0
    757 	/* XXX: rear feature??? */
    758 	/* Rear OUT[L/R] = 0 + FX[L/R] * 1 */
    759 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
    760 	    X2(DSP_OUTL, DSP_OUT_A_REAR),
    761 	    X1(DSP_CST(0)),
    762 	    X1(DSP_FX(0)),
    763 	    X1(DSP_CST(1)));
    764 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
    765 	    X2(DSP_OUTR, DSP_OUT_A_REAR),
    766 	    X1(DSP_CST(0)),
    767 	    X1(DSP_FX(1)),
    768 	    X1(DSP_CST(1)));
    769 #endif
    770 	/* ADC recording[L/R] = AC97 In[L/R] */
    771 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
    772 	    X2(DSP_OUTL, DSP_OUT_ADC),
    773 	    X2(DSP_INL, DSP_IN_AC97),
    774 	    X1(DSP_CST(0)),
    775 	    X1(DSP_CST(0)));
    776 	emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
    777 	    X2(DSP_OUTR, DSP_OUT_ADC),
    778 	    X2(DSP_INR, DSP_IN_AC97),
    779 	    X1(DSP_CST(0)),
    780 	    X1(DSP_CST(0)));
    781 
    782 	/* fill NOP the rest of the microcode */
    783 	while (pc < 512) {
    784 		emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
    785 		    X1(DSP_CST(0)),
    786 		    X1(DSP_CST(0)),
    787 		    X1(DSP_CST(0)),
    788 		    X1(DSP_CST(0)));
    789 	}
    790 
    791 	/* clear single step flag, run DSP */
    792 	emuxki_write(sc, 0, X1(DBG), 0);
    793 }
    794 
    795 /*
    796  * operations
    797  */
    798 
    799 static void
    800 emuxki_play_start(struct emuxki_softc *sc, int ch, uint32_t start, uint32_t end)
    801 {
    802 	uint32_t pitch;
    803 	uint32_t volume;
    804 
    805 	/* 48kHz:16384 = 128/375 */
    806 	pitch = sc->play.sample_rate * 128 / 375;
    807 	volume = 32767;
    808 
    809 	emuxki_write(sc, ch, EMU_CHAN_DSL,
    810 	    (0 << 24) |	/* send amound D = 0 */
    811 	    end);
    812 
    813 	emuxki_write(sc, ch, EMU_CHAN_PSST,
    814 	    (0 << 24) |	/* send amount C = 0 */
    815 	    start);
    816 
    817 	emuxki_write(sc, ch, EMU_CHAN_VTFT,
    818 	    (volume << 16) |
    819 	    (0xffff));	/* cutoff filter = none */
    820 
    821 	emuxki_write(sc, ch, EMU_CHAN_CVCF,
    822 	    (volume << 16) |
    823 	    (0xffff));	/* cutoff filter = none */
    824 
    825 	emuxki_write(sc, ch, EMU_CHAN_PTRX,
    826 	    (pitch << 16) |
    827 	    ((ch == 0 ? 0x7f : 0) << 8) |	/* send amount A = 255,0(L) */
    828 	    ((ch == 0 ? 0 : 0x7f)));		/* send amount B = 0,255(R) */
    829 
    830 	/* set the pitch to start */
    831 	emuxki_write(sc, ch, EMU_CHAN_CPF,
    832 	    (pitch << 16) |
    833 	    EMU_CHAN_CPF_STEREO_MASK);	/* stereo only */
    834 }
    835 
    836 static void
    837 emuxki_play_stop(struct emuxki_softc *sc, int ch)
    838 {
    839 
    840 	/* pitch = 0 to stop playing */
    841 	emuxki_write(sc, ch, EMU_CHAN_CPF, EMU_CHAN_CPF_STOP_MASK);
    842 	/* volume = 0 */
    843 	emuxki_write(sc, ch, EMU_CHAN_CVCF, 0);
    844 }
    845 
    846 static void
    847 emuxki_timer_start(struct emuxki_softc *sc)
    848 {
    849 	uint32_t timer;
    850 
    851 	/* frame count of half PTE at 16bit, 2ch, 48kHz */
    852 	timer = EMU_PTESIZE / 4 / 2;
    853 
    854 	/* EMU_TIMER is 16bit register */
    855 	emuxki_writeio_2(sc, EMU_TIMER, timer);
    856 	emuxki_writeio_4(sc, EMU_INTE,
    857 	    emuxki_readio_4(sc, EMU_INTE) |
    858 	        EMU_INTE_INTERTIMERENB);
    859 	DPRINTF("timer start\n");
    860 }
    861 
    862 static void
    863 emuxki_timer_stop(struct emuxki_softc *sc)
    864 {
    865 
    866 	emuxki_writeio_4(sc, EMU_INTE,
    867 	    emuxki_readio_4(sc, EMU_INTE) &
    868 	        ~EMU_INTE_INTERTIMERENB);
    869 	/* EMU_TIMER is 16bit register */
    870 	emuxki_writeio_2(sc, EMU_TIMER, 0);
    871 	DPRINTF("timer stop\n");
    872 }
    873 
    874 /*
    875  * audio interface
    876  */
    877 
    878 static int
    879 emuxki_query_format(void *hdl, audio_format_query_t *afp)
    880 {
    881 
    882 	return audio_query_format(emuxki_formats, EMUXKI_NFORMATS, afp);
    883 }
    884 
    885 static int
    886 emuxki_set_format(void *hdl, int setmode,
    887     const audio_params_t *play, const audio_params_t *rec,
    888     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    889 {
    890 	struct emuxki_softc *sc = hdl;
    891 
    892 	if ((setmode & AUMODE_PLAY))
    893 		sc->play = *play;
    894 	if ((setmode & AUMODE_RECORD))
    895 		sc->rec = *rec;
    896 	return 0;
    897 }
    898 
    899 static int
    900 emuxki_halt_output(void *hdl)
    901 {
    902 	struct emuxki_softc *sc = hdl;
    903 
    904 	emuxki_timer_stop(sc);
    905 	emuxki_play_stop(sc, 0);
    906 	emuxki_play_stop(sc, 1);
    907 	return 0;
    908 }
    909 
    910 static int
    911 emuxki_halt_input(void *hdl)
    912 {
    913 	struct emuxki_softc *sc = hdl;
    914 
    915 	/* stop ADC */
    916 	emuxki_write(sc, 0, EMU_ADCCR, 0);
    917 
    918 	/* disable interrupt */
    919 	emuxki_writeio_4(sc, EMU_INTE,
    920 	    emuxki_readio_4(sc, EMU_INTE) &
    921 	        ~EMU_INTE_ADCBUFENABLE);
    922 
    923 	return 0;
    924 }
    925 
    926 static int
    927 emuxki_intr(void *hdl)
    928 {
    929 	struct emuxki_softc *sc = hdl;
    930 	uint32_t ipr;
    931 	uint32_t curaddr;
    932 	int handled = 0;
    933 
    934 	mutex_spin_enter(&sc->sc_intr_lock);
    935 
    936 	ipr = emuxki_readio_4(sc, EMU_IPR);
    937 	DPRINTFN(3, "emuxki: ipr=%08x\n", ipr);
    938 	if (sc->pintr && (ipr & EMU_IPR_INTERVALTIMER)) {
    939 		/* read ch 0 */
    940 		curaddr = emuxki_read(sc, 0, EMU_CHAN_CCCA) &
    941 		    EMU_CHAN_CCCA_CURRADDR_MASK;
    942 		DPRINTFN(3, "curaddr=%08x\n", curaddr);
    943 		curaddr *= sc->pframesize;
    944 
    945 		if (curaddr < sc->poffset)
    946 			curaddr += sc->plength;
    947 		if (curaddr >= sc->poffset + sc->pblksize) {
    948 			dmamem_sync(sc->pmem, BUS_DMASYNC_POSTWRITE);
    949 			sc->pintr(sc->pintrarg);
    950 			sc->poffset += sc->pblksize;
    951 			if (sc->poffset >= sc->plength) {
    952 				sc->poffset -= sc->plength;
    953 			}
    954 			dmamem_sync(sc->pmem, BUS_DMASYNC_PREWRITE);
    955 		}
    956 		handled = 1;
    957 	}
    958 
    959 	if (sc->rintr &&
    960 	    (ipr & (EMU_IPR_ADCBUFHALFFULL | EMU_IPR_ADCBUFFULL))) {
    961 		char *src;
    962 		char *dst;
    963 
    964 		/* Record DMA buffer has just 2 blocks */
    965 		src = KERNADDR(sc->rmem);
    966 		if (ipr & EMU_IPR_ADCBUFFULL) {
    967 			/* 2nd block */
    968 			src += EMU_REC_DMABLKSIZE;
    969 		}
    970 		dst = (char *)sc->rptr + sc->rcurrent;
    971 
    972 		dmamem_sync(sc->rmem, BUS_DMASYNC_POSTREAD);
    973 		memcpy(dst, src, EMU_REC_DMABLKSIZE);
    974 		/* for next trans */
    975 		dmamem_sync(sc->rmem, BUS_DMASYNC_PREREAD);
    976 		sc->rcurrent += EMU_REC_DMABLKSIZE;
    977 
    978 		if (sc->rcurrent >= sc->roffset + sc->rblksize) {
    979 			sc->rintr(sc->rintrarg);
    980 			sc->roffset += sc->rblksize;
    981 			if (sc->roffset >= sc->rlength) {
    982 				sc->roffset = 0;
    983 				sc->rcurrent = 0;
    984 			}
    985 		}
    986 
    987 		handled = 1;
    988 	}
    989 
    990 #if defined(EMUXKI_DEBUG)
    991 	if (!handled) {
    992 		char buf[1024];
    993 		snprintb(buf, sizeof(buf),
    994 		    "\20"
    995 		    "\x19""RATETRCHANGE"
    996 		    "\x18""FXDSP"
    997 		    "\x17""FORCEINT"
    998 		    "\x16""PCIERROR"
    999 		    "\x15""VOLINCR"
   1000 		    "\x14""VOLDECR"
   1001 		    "\x13""MUTE"
   1002 		    "\x12""MICBUFFULL"
   1003 		    "\x11""MICBUFHALFFULL"
   1004 		    "\x10""ADCBUFFULL"
   1005 		    "\x0f""ADCBUFHALFFULL"
   1006 		    "\x0e""EFXBUFFULL"
   1007 		    "\x0d""EFXBUFHALFFULL"
   1008 		    "\x0c""GPSPDIFSTCHANGE"
   1009 		    "\x0b""CDROMSTCHANGE"
   1010 		    /*     INTERVALTIMER */
   1011 		    "\x09""MIDITRANSBUFE"
   1012 		    "\x08""MIDIRECVBUFE"
   1013 		    "\x07""CHANNELLOOP"
   1014 		    , ipr);
   1015 		DPRINTF("unexpected intr: %s\n", buf);
   1016 
   1017 		/* for debugging (must not handle if !DEBUG) */
   1018 		handled = 1;
   1019 	}
   1020 #endif
   1021 
   1022 	/* Reset interrupt bit */
   1023 	emuxki_writeio_4(sc, EMU_IPR, ipr);
   1024 
   1025 	mutex_spin_exit(&sc->sc_intr_lock);
   1026 
   1027 	/* Interrupt handler must return !=0 if handled */
   1028 	return handled;
   1029 }
   1030 
   1031 static int
   1032 emuxki_getdev(void *hdl, struct audio_device *dev)
   1033 {
   1034 	struct emuxki_softc *sc = hdl;
   1035 
   1036 	*dev = sc->sc_audv;
   1037 	return 0;
   1038 }
   1039 
   1040 static int
   1041 emuxki_set_port(void *hdl, mixer_ctrl_t *mctl)
   1042 {
   1043 	struct emuxki_softc *sc = hdl;
   1044 
   1045 	return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl);
   1046 }
   1047 
   1048 static int
   1049 emuxki_get_port(void *hdl, mixer_ctrl_t *mctl)
   1050 {
   1051 	struct emuxki_softc *sc = hdl;
   1052 
   1053 	return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl);
   1054 }
   1055 
   1056 static int
   1057 emuxki_query_devinfo(void *hdl, mixer_devinfo_t *minfo)
   1058 {
   1059 	struct emuxki_softc *sc = hdl;
   1060 
   1061 	return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo);
   1062 }
   1063 
   1064 static void *
   1065 emuxki_allocm(void *hdl, int direction, size_t size)
   1066 {
   1067 	struct emuxki_softc *sc = hdl;
   1068 
   1069 	if (direction == AUMODE_PLAY) {
   1070 		if (sc->pmem) {
   1071 			panic("pmem already allocated\n");
   1072 			return NULL;
   1073 		}
   1074 		sc->pmem = dmamem_alloc(sc, size);
   1075 		return KERNADDR(sc->pmem);
   1076 	} else {
   1077 		/* rmem is fixed size internal DMA buffer */
   1078 		if (sc->rmem) {
   1079 			panic("rmem already allocated\n");
   1080 			return NULL;
   1081 		}
   1082 		/* rmem fixed size */
   1083 		sc->rmem = dmamem_alloc(sc, EMU_REC_DMASIZE);
   1084 
   1085 		/* recording MI buffer is normal kmem, software trans. */
   1086 		sc->rptr = kmem_alloc(size, KM_SLEEP);
   1087 		return sc->rptr;
   1088 	}
   1089 }
   1090 
   1091 static void
   1092 emuxki_freem(void *hdl, void *ptr, size_t size)
   1093 {
   1094 	struct emuxki_softc *sc = hdl;
   1095 
   1096 	if (sc->pmem && ptr == KERNADDR(sc->pmem)) {
   1097 		dmamem_free(sc->pmem);
   1098 		sc->pmem = NULL;
   1099 	}
   1100 	if (sc->rmem && ptr == sc->rptr) {
   1101 		dmamem_free(sc->rmem);
   1102 		sc->rmem = NULL;
   1103 		kmem_free(sc->rptr, size);
   1104 		sc->rptr = NULL;
   1105 	}
   1106 }
   1107 
   1108 /*
   1109  * blocksize rounding to EMU_PTESIZE. It is for easy to drive.
   1110  */
   1111 static int
   1112 emuxki_round_blocksize(void *hdl, int blksize,
   1113     int mode, const audio_params_t* param)
   1114 {
   1115 
   1116 	/*
   1117 	 * This is not necessary for recording, but symmetric for easy.
   1118 	 * For recording buffer/block size requirements of hardware,
   1119 	 * see EMU_RECBS_BUFSIZE_*
   1120 	 */
   1121 	if (blksize < EMU_PTESIZE)
   1122 		blksize = EMU_PTESIZE;
   1123 	return rounddown(blksize, EMU_PTESIZE);
   1124 }
   1125 
   1126 static size_t
   1127 emuxki_round_buffersize(void *hdl, int direction, size_t bsize)
   1128 {
   1129 
   1130 	/* This is not necessary for recording, but symmetric for easy */
   1131 	if (bsize < EMU_MINPTE * EMU_PTESIZE) {
   1132 		bsize = EMU_MINPTE * EMU_PTESIZE;
   1133 	} else if (bsize > EMU_MAXPTE * EMU_PTESIZE) {
   1134 		bsize = EMU_MAXPTE * EMU_PTESIZE;
   1135 	}
   1136 	return roundup(bsize, EMU_PTESIZE);
   1137 }
   1138 
   1139 static int
   1140 emuxki_get_props(void *hdl)
   1141 {
   1142 
   1143 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
   1144 	    AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
   1145 }
   1146 
   1147 static int
   1148 emuxki_trigger_output(void *hdl, void *start, void *end, int blksize,
   1149     void (*intr)(void *), void *arg, const audio_params_t *params)
   1150 {
   1151 	struct emuxki_softc *sc = hdl;
   1152 	int npage;
   1153 	uint32_t *kptb;
   1154 	bus_addr_t dpmem;
   1155 	int i;
   1156 	uint32_t hwstart;
   1157 	uint32_t hwend;
   1158 
   1159 	if (sc->pmem == NULL)
   1160 		panic("pmem == NULL\n");
   1161 	if (start != KERNADDR(sc->pmem))
   1162 		panic("start != KERNADDR(sc->pmem)\n");
   1163 
   1164 	sc->pframesize = 4;	/* channels * bit / 8 = 2*16/8=4 */
   1165 	sc->pblksize = blksize;
   1166 	sc->plength = (char *)end - (char *)start;
   1167 	sc->poffset = 0;
   1168 	npage = roundup(sc->plength, EMU_PTESIZE);
   1169 
   1170 	kptb = KERNADDR(sc->ptb);
   1171 	dpmem = DMAADDR(sc->pmem);
   1172 	for (i = 0; i < npage; i++) {
   1173 		kptb[i] = htole32(dpmem << 1);
   1174 		dpmem += EMU_PTESIZE;
   1175 	}
   1176 	dmamem_sync(sc->ptb, BUS_DMASYNC_PREWRITE);
   1177 
   1178 	hwstart = 0;
   1179 	hwend = hwstart + sc->plength / sc->pframesize;
   1180 
   1181 	sc->pintr = intr;
   1182 	sc->pintrarg = arg;
   1183 
   1184 	dmamem_sync(sc->pmem, BUS_DMASYNC_PREWRITE);
   1185 
   1186 	emuxki_play_start(sc, 0, hwstart, hwend);
   1187 	emuxki_play_start(sc, 1, hwstart, hwend);
   1188 
   1189 	emuxki_timer_start(sc);
   1190 
   1191 	return 0;
   1192 }
   1193 
   1194 /*
   1195  * Recording uses temporary buffer.  Because it can use ADC_HALF/FULL
   1196  * interrupts and this method doesn't conflict with playback.
   1197  */
   1198 
   1199 static int
   1200 emuxki_trigger_input(void *hdl, void *start, void *end, int blksize,
   1201     void (*intr)(void *), void *arg, const audio_params_t *params)
   1202 {
   1203 	struct emuxki_softc *sc = hdl;
   1204 
   1205 	if (sc->rmem == NULL)
   1206 		panic("rmem == NULL\n");
   1207 	if (start != sc->rptr)
   1208 		panic("start != sc->rptr\n");
   1209 
   1210 	sc->rframesize = 4;	/* channels * bit / 8 = 2*16/8=4 */
   1211 	sc->rblksize = blksize;
   1212 	sc->rlength = (char *)end - (char *)start;
   1213 	sc->roffset = 0;
   1214 	sc->rcurrent = 0;
   1215 
   1216 	sc->rintr = intr;
   1217 	sc->rintrarg = arg;
   1218 
   1219 	/*
   1220 	 * Memo:
   1221 	 *  recording source is selected by AC97
   1222 	 *  AC97 input source routes to ADC by FX(DSP)
   1223 	 *
   1224 	 * Must keep following sequence order
   1225 	 */
   1226 
   1227 	/* first, stop ADC */
   1228 	emuxki_write(sc, 0, EMU_ADCCR, 0);
   1229 	emuxki_write(sc, 0, EMU_ADCBA, 0);
   1230 	emuxki_write(sc, 0, EMU_ADCBS, 0);
   1231 
   1232 	dmamem_sync(sc->rmem, BUS_DMASYNC_PREREAD);
   1233 
   1234 	/* ADC interrupt enable */
   1235 	emuxki_writeio_4(sc, EMU_INTE,
   1236 	    emuxki_readio_4(sc, EMU_INTE) |
   1237 	        EMU_INTE_ADCBUFENABLE);
   1238 
   1239 	/* ADC Enable */
   1240 	/* stereo, 48kHz, enable */
   1241 	emuxki_write(sc, 0, EMU_ADCCR,
   1242 	    X1(ADCCR_LCHANENABLE) | X1(ADCCR_RCHANENABLE));
   1243 
   1244 	/* ADC buffer address */
   1245 	emuxki_write(sc, 0, X1(ADCIDX), 0);
   1246 	emuxki_write(sc, 0, EMU_ADCBA, DMAADDR(sc->rmem));
   1247 
   1248 	/* ADC buffer size, to start */
   1249 	emuxki_write(sc, 0, EMU_ADCBS, EMU_REC_BUFSIZE_RECBS);
   1250 
   1251 	return 0;
   1252 }
   1253 
   1254 static void
   1255 emuxki_get_locks(void *hdl, kmutex_t **intr, kmutex_t **proc)
   1256 {
   1257 	struct emuxki_softc *sc = hdl;
   1258 
   1259 	*intr = &sc->sc_intr_lock;
   1260 	*proc = &sc->sc_lock;
   1261 }
   1262 
   1263 /*
   1264  * AC97
   1265  */
   1266 
   1267 static int
   1268 emuxki_ac97_init(struct emuxki_softc *sc)
   1269 {
   1270 
   1271 	sc->hostif.arg = sc;
   1272 	sc->hostif.attach = emuxki_ac97_attach;
   1273 	sc->hostif.read = emuxki_ac97_read;
   1274 	sc->hostif.write = emuxki_ac97_write;
   1275 	sc->hostif.reset = emuxki_ac97_reset;
   1276 	sc->hostif.flags = emuxki_ac97_flags;
   1277 	return ac97_attach(&sc->hostif, sc->sc_dev, &sc->sc_lock);
   1278 }
   1279 
   1280 /*
   1281  * AC97 callbacks
   1282  */
   1283 
   1284 static int
   1285 emuxki_ac97_attach(void *hdl, struct ac97_codec_if *codecif)
   1286 {
   1287 	struct emuxki_softc *sc = hdl;
   1288 
   1289 	sc->codecif = codecif;
   1290 	return 0;
   1291 }
   1292 
   1293 static int
   1294 emuxki_ac97_read(void *hdl, uint8_t reg, uint16_t *val)
   1295 {
   1296 	struct emuxki_softc *sc = hdl;
   1297 
   1298 	mutex_spin_enter(&sc->sc_index_lock);
   1299 	emuxki_writeio_1(sc, EMU_AC97ADDR, reg);
   1300 	*val = emuxki_readio_2(sc, EMU_AC97DATA);
   1301 	mutex_spin_exit(&sc->sc_index_lock);
   1302 
   1303 	return 0;
   1304 }
   1305 
   1306 static int
   1307 emuxki_ac97_write(void *hdl, uint8_t reg, uint16_t val)
   1308 {
   1309 	struct emuxki_softc *sc = hdl;
   1310 
   1311 	mutex_spin_enter(&sc->sc_index_lock);
   1312 	emuxki_writeio_1(sc, EMU_AC97ADDR, reg);
   1313 	emuxki_writeio_2(sc, EMU_AC97DATA, val);
   1314 	mutex_spin_exit(&sc->sc_index_lock);
   1315 
   1316 	return 0;
   1317 }
   1318 
   1319 static int
   1320 emuxki_ac97_reset(void *hdl)
   1321 {
   1322 
   1323 	return 0;
   1324 }
   1325 
   1326 static enum ac97_host_flags
   1327 emuxki_ac97_flags(void *hdl)
   1328 {
   1329 
   1330 	return AC97_HOST_SWAPPED_CHANNELS;
   1331 }
   1332 
   1333 MODULE(MODULE_CLASS_DRIVER, emuxki, "pci,audio");
   1334 
   1335 #ifdef _MODULE
   1336 #include "ioconf.c"
   1337 #endif
   1338 
   1339 static int
   1340 emuxki_modcmd(modcmd_t cmd, void *opaque)
   1341 {
   1342 	int error = 0;
   1343 
   1344 	switch (cmd) {
   1345 	case MODULE_CMD_INIT:
   1346 #ifdef _MODULE
   1347 		error = config_init_component(cfdriver_ioconf_emuxki,
   1348 		    cfattach_ioconf_emuxki, cfdata_ioconf_emuxki);
   1349 #endif
   1350 		return error;
   1351 	case MODULE_CMD_FINI:
   1352 #ifdef _MODULE
   1353 		error = config_fini_component(cfdriver_ioconf_emuxki,
   1354 		    cfattach_ioconf_emuxki, cfdata_ioconf_emuxki);
   1355 #endif
   1356 		return error;
   1357 	default:
   1358 		return ENOTTY;
   1359 	}
   1360 }
   1361