Home | History | Annotate | Line # | Download | only in pci
neo.c revision 1.3
      1 /*	$NetBSD: neo.c,v 1.3 2000/11/05 16:24:45 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999 Cameron Grant <gandalf (at) vilnya.demon.co.uk>
      5  * All rights reserved.
      6  *
      7  * Derived from the public domain Linux driver
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
     28  * SUCH DAMAGE.
     29  *
     30  * FreeBSD: src/sys/dev/sound/pci/neomagic.c,v 1.8 2000/03/20 15:30:50 cg Exp
     31  * OpenBSD: neo.c,v 1.4 2000/07/19 09:04:37 csapuntz Exp
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/kernel.h>
     37 #include <sys/malloc.h>
     38 #include <sys/device.h>
     39 
     40 #include <machine/bus.h>
     41 
     42 #include <dev/pci/pcidevs.h>
     43 #include <dev/pci/pcivar.h>
     44 
     45 #include <dev/pci/neoreg.h>
     46 #include <dev/pci/neo-coeff.h>
     47 
     48 #include <sys/audioio.h>
     49 #include <dev/audio_if.h>
     50 #include <dev/mulaw.h>
     51 #include <dev/auconv.h>
     52 
     53 #include <dev/ic/ac97var.h>
     54 
     55 
     56 /* -------------------------------------------------------------------- */
     57 /*
     58  * As of 04/13/00, public documentation on the Neomagic 256 is not available.
     59  * These comments were gleaned by looking at the driver carefully.
     60  *
     61  * The Neomagic 256 AV/ZX chips provide both video and audio capabilities
     62  * on one chip. About 2-6 megabytes of memory are associated with
     63  * the chip. Most of this goes to video frame buffers, but some is used for
     64  * audio buffering
     65  *
     66  * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA.
     67  * Instead, the chip allows you to carve out two ring buffers out of its
     68  * memory. However you carve this and how much you can carve seems to be
     69  * voodoo. The algorithm is in nm_init.
     70  *
     71  * Most Neomagic audio chips use the AC-97 codec interface. However, there
     72  * seem to be a select few chips 256AV chips that do not support AC-97.
     73  * This driver does not support them but there are rumors that it
     74  * mgiht work with wss isa drivers. This might require some playing around
     75  * with your BIOS.
     76  *
     77  * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of
     78  * them describe a memory region. The frame buffer is the first region
     79  * and the register set is the secodn region.
     80  *
     81  * The register manipulation logic is taken from the Linux driver,
     82  * which is in the public domain.
     83  *
     84  * The Neomagic is even nice enough to map the AC-97 codec registers into
     85  * the register space to allow direct manipulation. Watch out, accessing
     86  * AC-97 registers on the Neomagic requires great delicateness, otherwise
     87  * the thing will hang the PCI bus, rendering your system frozen.
     88  *
     89  * For one, it seems the Neomagic status register that reports AC-97
     90  * readiness should NOT be polled more often than once each 1ms.
     91  *
     92  * Also, writes to the AC-97 register space may take order 40us to
     93  * complete.
     94  *
     95  * Unlike many sound engines, the Neomagic does not support (as fas as
     96  * we know :) the notion of interrupting every n bytes transferred,
     97  * unlike many DMA engines.  Instead, it allows you to specify one
     98  * location in each ring buffer (called the watermark). When the chip
     99  * passes that location while playing, it signals an interrupt.
    100  *
    101  * The ring buffer size is currently 16k. That is about 100ms of audio
    102  * at 44.1khz/stero/16 bit. However, to keep the buffer full, interrupts
    103  * are generated more often than that, so 20-40 interrupts per second
    104  * should not be unexpected. Increasing BUFFSIZE should help minimize
    105  * of glitches due to drivers that spend to much time looping at high
    106  * privelege levels as well as the impact of badly written audio
    107  * interface clients.
    108  *
    109  * TO-DO list:
    110  *    Add mmap support.
    111  *
    112  *    Figure out interaction with video stuff (look at Xfree86 driver?)
    113  *
    114  *    Power management (neoactivate)
    115  *
    116  *    Fix detect of Neo devices that don't work this driver (see neo_attach)
    117  *
    118  *    Figure out how to shrink that huge table neo-coeff.h
    119  */
    120 
    121 #define	NM_BUFFSIZE	16384
    122 
    123 /* device private data */
    124 struct neo_softc {
    125 	struct          device dev;
    126 
    127 	bus_space_tag_t bufiot;
    128 	bus_space_handle_t  bufioh;
    129 
    130 	bus_space_tag_t regiot;
    131 	bus_space_handle_t  regioh;
    132 
    133 	u_int32_t 	type;
    134 	void            *ih;
    135 
    136 	void	(*pintr)(void *);	/* dma completion intr handler */
    137 	void	*parg;		/* arg for intr() */
    138 
    139 	void	(*rintr)(void *);	/* dma completion intr handler */
    140 	void	*rarg;		/* arg for intr() */
    141 
    142 	vaddr_t	buf_vaddr;
    143 	vaddr_t	rbuf_vaddr;
    144 	vaddr_t	pbuf_vaddr;
    145 	int	pbuf_allocated;
    146 	int	rbuf_allocated;
    147 
    148 	bus_addr_t buf_pciaddr;
    149 	bus_addr_t rbuf_pciaddr;
    150 	bus_addr_t pbuf_pciaddr;
    151 
    152 	u_int32_t 	ac97_base, ac97_status, ac97_busy;
    153 	u_int32_t	buftop, pbuf, rbuf, cbuf, acbuf;
    154 	u_int32_t	playint, recint, misc1int, misc2int;
    155 	u_int32_t	irsz, badintr;
    156 
    157         u_int32_t       pbufsize;
    158         u_int32_t       rbufsize;
    159 
    160 	u_int32_t       pblksize;
    161 	u_int32_t       rblksize;
    162 
    163         u_int32_t       pwmark;
    164         u_int32_t       rwmark;
    165 
    166 	struct ac97_codec_if *codec_if;
    167 	struct ac97_host_if host_if;
    168 };
    169 
    170 /* -------------------------------------------------------------------- */
    171 
    172 /*
    173  * prototypes
    174  */
    175 
    176 static int	nm_waitcd(struct neo_softc *sc);
    177 static int	nm_loadcoeff(struct neo_softc *sc, int dir, int num);
    178 static int	nm_init(struct neo_softc *);
    179 
    180 int	neo_match(struct device *, struct cfdata *, void *);
    181 void	neo_attach(struct device *, struct device *, void *);
    182 int	neo_intr(void *);
    183 
    184 int	neo_open(void *, int);
    185 void	neo_close(void *);
    186 int	neo_query_encoding(void *, struct audio_encoding *);
    187 int	neo_set_params(void *, int, int, struct audio_params *,
    188 	    struct audio_params *);
    189 int	neo_round_blocksize(void *, int);
    190 int	neo_trigger_output(void *, void *, void *, int, void (*)(void *),
    191 	    void *, struct audio_params *);
    192 int	neo_trigger_input(void *, void *, void *, int, void (*)(void *),
    193 	    void *, struct audio_params *);
    194 int	neo_halt_output(void *);
    195 int	neo_halt_input(void *);
    196 int	neo_getdev(void *, struct audio_device *);
    197 int	neo_mixer_set_port(void *, mixer_ctrl_t *);
    198 int	neo_mixer_get_port(void *, mixer_ctrl_t *);
    199 int     neo_attach_codec(void *sc, struct ac97_codec_if *);
    200 int	neo_read_codec(void *sc, u_int8_t a, u_int16_t *d);
    201 int	neo_write_codec(void *sc, u_int8_t a, u_int16_t d);
    202 void    neo_reset_codec(void *sc);
    203 enum ac97_host_flags neo_flags_codec(void *sc);
    204 int	neo_query_devinfo(void *, mixer_devinfo_t *);
    205 void   *neo_malloc(void *, int, size_t, int, int);
    206 void	neo_free(void *, void *, int);
    207 size_t	neo_round_buffersize(void *, int, size_t);
    208 paddr_t	neo_mappage(void *, void *, off_t, int);
    209 int	neo_get_props(void *);
    210 void	neo_set_mixer(struct neo_softc *sc, int a, int d);
    211 
    212 struct cfattach neo_ca = {
    213 	sizeof(struct neo_softc), neo_match, neo_attach
    214 };
    215 
    216 struct audio_device neo_device = {
    217 	"NeoMagic 256",
    218 	"",
    219 	"neo"
    220 };
    221 
    222 /* The actual rates supported by the card. */
    223 static const int samplerates[9] = {
    224 	8000,
    225 	11025,
    226 	16000,
    227 	22050,
    228 	24000,
    229 	32000,
    230 	44100,
    231 	48000,
    232 	99999999
    233 };
    234 
    235 /* -------------------------------------------------------------------- */
    236 
    237 struct audio_hw_if neo_hw_if = {
    238 	neo_open,
    239 	neo_close,
    240 	NULL,				/* drain */
    241 	neo_query_encoding,
    242 	neo_set_params,
    243 	neo_round_blocksize,
    244 	NULL,				/* commit_setting */
    245 	NULL,				/* init_output */
    246 	NULL,				/* init_input */
    247 	NULL,				/* start_output */
    248 	NULL,				/* start_input */
    249 	neo_halt_output,
    250 	neo_halt_input,
    251 	NULL,				/* speaker_ctl */
    252 	neo_getdev,
    253 	NULL,				/* getfd */
    254 	neo_mixer_set_port,
    255 	neo_mixer_get_port,
    256 	neo_query_devinfo,
    257 	neo_malloc,
    258 	neo_free,
    259 	neo_round_buffersize,
    260 	neo_mappage,
    261 	neo_get_props,
    262 	neo_trigger_output,
    263 	neo_trigger_input,
    264 };
    265 
    266 /* -------------------------------------------------------------------- */
    267 
    268 #define	nm_rd_1(sc, regno)						\
    269 	bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
    270 
    271 #define	nm_rd_2(sc, regno)						\
    272 	bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
    273 
    274 #define	nm_rd_4(sc, regno)						\
    275 	bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
    276 
    277 #define	nm_wr_1(sc, regno, val)						\
    278 	bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
    279 
    280 #define	nm_wr_2(sc, regno, val)						\
    281 	bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
    282 
    283 #define	nm_wr_4(sc, regno, val)						\
    284 	bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
    285 
    286 #define	nm_rdbuf_4(sc, regno)						\
    287 	bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
    288 
    289 #define	nm_wrbuf_1(sc, regno, val)					\
    290 	bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
    291 
    292 /* ac97 codec */
    293 static int
    294 nm_waitcd(struct neo_softc *sc)
    295 {
    296 	int cnt = 10;
    297 	int fail = 1;
    298 
    299 	while (cnt-- > 0) {
    300 		if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
    301 			DELAY(100);
    302 		else {
    303 		        fail = 0;
    304 			break;
    305 		}
    306 	}
    307 	return (fail);
    308 }
    309 
    310 
    311 static void
    312 nm_ackint(struct neo_softc *sc, u_int32_t num)
    313 {
    314 
    315 	switch (sc->type) {
    316 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    317 		nm_wr_2(sc, NM_INT_REG, num << 1);
    318 		break;
    319 
    320 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    321 		nm_wr_4(sc, NM_INT_REG, num);
    322 		break;
    323 	}
    324 }
    325 
    326 static int
    327 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
    328 {
    329 	int ofs, sz, i;
    330 	u_int32_t addr;
    331 
    332 	addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
    333 	if (dir == AUMODE_RECORD)
    334 		num += 8;
    335 	sz = coefficientSizes[num];
    336 	ofs = 0;
    337 	while (num-- > 0)
    338 		ofs+= coefficientSizes[num];
    339 	for (i = 0; i < sz; i++)
    340 		nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
    341 	nm_wr_4(sc, addr, sc->cbuf);
    342 	if (dir == AUMODE_PLAY)
    343 		sz--;
    344 	nm_wr_4(sc, addr + 4, sc->cbuf + sz);
    345 	return 0;
    346 }
    347 
    348 /* The interrupt handler */
    349 int
    350 neo_intr(void *p)
    351 {
    352 	struct neo_softc *sc = (struct neo_softc *)p;
    353 	int status, x, active;
    354 	int rv = 0;
    355 
    356 	active = (sc->pintr || sc->rintr);
    357 	status = (sc->irsz == 2) ?
    358 	    nm_rd_2(sc, NM_INT_REG) :
    359 	    nm_rd_4(sc, NM_INT_REG);
    360 
    361 	if (status & sc->playint) {
    362 		status &= ~sc->playint;
    363 
    364 		sc->pwmark += sc->pblksize;
    365 		sc->pwmark %= sc->pbufsize;
    366 
    367 		nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
    368 
    369 		nm_ackint(sc, sc->playint);
    370 
    371 		if (sc->pintr)
    372 			(*sc->pintr)(sc->parg);
    373 
    374 		rv = 1;
    375 	}
    376 	if (status & sc->recint) {
    377 		status &= ~sc->recint;
    378 
    379 		sc->rwmark += sc->rblksize;
    380 		sc->rwmark %= sc->rbufsize;
    381 
    382 		nm_ackint(sc, sc->recint);
    383 		if (sc->rintr)
    384 			(*sc->rintr)(sc->rarg);
    385 
    386 		rv = 1;
    387 	}
    388 	if (status & sc->misc1int) {
    389 		status &= ~sc->misc1int;
    390 		nm_ackint(sc, sc->misc1int);
    391 		x = nm_rd_1(sc, 0x400);
    392 		nm_wr_1(sc, 0x400, x | 2);
    393 		printf("%s: misc int 1\n", sc->dev.dv_xname);
    394 		rv = 1;
    395 	}
    396 	if (status & sc->misc2int) {
    397 		status &= ~sc->misc2int;
    398 		nm_ackint(sc, sc->misc2int);
    399 		x = nm_rd_1(sc, 0x400);
    400 		nm_wr_1(sc, 0x400, x & ~2);
    401 		printf("%s: misc int 2\n", sc->dev.dv_xname);
    402 		rv = 1;
    403 	}
    404 	if (status) {
    405 		status &= ~sc->misc2int;
    406 		nm_ackint(sc, sc->misc2int);
    407 		printf("%s: unknown int\n", sc->dev.dv_xname);
    408 		rv = 1;
    409 	}
    410 
    411 	return (rv);
    412 }
    413 
    414 /* -------------------------------------------------------------------- */
    415 
    416 /*
    417  * Probe and attach the card
    418  */
    419 
    420 static int
    421 nm_init(struct neo_softc *sc)
    422 {
    423 	u_int32_t ofs, i;
    424 
    425 	switch (sc->type) {
    426 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    427 		sc->ac97_base = NM_MIXER_OFFSET;
    428 		sc->ac97_status = NM_MIXER_STATUS_OFFSET;
    429 		sc->ac97_busy = NM_MIXER_READY_MASK;
    430 
    431 		sc->buftop = 2560 * 1024;
    432 
    433 		sc->irsz = 2;
    434 		sc->playint = NM_PLAYBACK_INT;
    435 		sc->recint = NM_RECORD_INT;
    436 		sc->misc1int = NM_MISC_INT_1;
    437 		sc->misc2int = NM_MISC_INT_2;
    438 		break;
    439 
    440 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    441 		sc->ac97_base = NM_MIXER_OFFSET;
    442 		sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
    443 		sc->ac97_busy = NM2_MIXER_READY_MASK;
    444 
    445 		sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
    446 
    447 		sc->irsz = 4;
    448 		sc->playint = NM2_PLAYBACK_INT;
    449 		sc->recint = NM2_RECORD_INT;
    450 		sc->misc1int = NM2_MISC_INT_1;
    451 		sc->misc2int = NM2_MISC_INT_2;
    452 		break;
    453 #ifdef DIAGNOSTIC
    454 	default:
    455 		panic("nm_init: impossible");
    456 #endif
    457 	}
    458 
    459 	sc->badintr = 0;
    460 	ofs = sc->buftop - 0x0400;
    461 	sc->buftop -= 0x1400;
    462 
    463  	if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
    464 		i = nm_rdbuf_4(sc, ofs + 4);
    465 		if (i != 0 && i != 0xffffffff)
    466 			sc->buftop = i;
    467 	}
    468 
    469 	sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
    470 	sc->rbuf = sc->cbuf - NM_BUFFSIZE;
    471 	sc->pbuf = sc->rbuf - NM_BUFFSIZE;
    472 	sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
    473 
    474 	sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
    475 	sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
    476 	sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
    477 
    478 	sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
    479 	sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
    480 
    481 	nm_wr_1(sc, 0, 0x11);
    482 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
    483 	nm_wr_2(sc, 0x214, 0);
    484 
    485 	return 0;
    486 }
    487 
    488 int
    489 neo_match(struct device *parent, struct cfdata *match, void *aux)
    490 {
    491 	struct pci_attach_args *pa = aux;
    492 	pcireg_t subdev;
    493 
    494 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
    495 		return (0);
    496 
    497 	subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    498 
    499 	switch (PCI_PRODUCT(pa->pa_id)) {
    500 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    501 		/*
    502 		 * We have to weed-out the non-AC'97 versions of
    503 		 * the chip (i.e. the ones that are known to work
    504 		 * in WSS emulation mode), as they won't work with
    505 		 * this driver.
    506 		 */
    507 		switch (PCI_VENDOR(subdev)) {
    508 		case PCI_VENDOR_DELL:
    509 			switch (PCI_PRODUCT(subdev)) {
    510 			case 0x008f:
    511 				return (0);
    512 			}
    513 			break;
    514 
    515 		case PCI_VENDOR_HP:
    516 			switch (PCI_PRODUCT(subdev)) {
    517 			case 0x0007:
    518 				return (0);
    519 			}
    520 		}
    521 		return (1);
    522 
    523 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    524 		return (1);
    525 	}
    526 
    527 	return (0);
    528 }
    529 
    530 void
    531 neo_attach(struct device *parent, struct device *self, void *aux)
    532 {
    533 	struct neo_softc *sc = (struct neo_softc *)self;
    534 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    535 	pci_chipset_tag_t pc = pa->pa_pc;
    536 	char const *intrstr;
    537 	pci_intr_handle_t ih;
    538 	pcireg_t csr;
    539 	int error;
    540 
    541 	sc->type = PCI_PRODUCT(pa->pa_id);
    542 
    543 	printf(": NeoMagic 256%s audio\n",
    544 	    sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
    545 
    546 	/* Map I/O register */
    547 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
    548 			   &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
    549 		printf("%s: can't map buffer\n", sc->dev.dv_xname);
    550 		return;
    551 	}
    552 
    553 	if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM, 0,
    554 			   &sc->regiot, &sc->regioh, NULL, NULL)) {
    555 		printf("%s: can't map registers\n", sc->dev.dv_xname);
    556 		return;
    557 	}
    558 
    559 	/* Map and establish the interrupt. */
    560 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    561 			 pa->pa_intrline, &ih)) {
    562 		printf("%s: couldn't map interrupt\n", sc->dev.dv_xname);
    563 		return;
    564 	}
    565 
    566 	intrstr = pci_intr_string(pc, ih);
    567 	sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc);
    568 
    569 	if (sc->ih == NULL) {
    570 		printf("%s: couldn't establish interrupt",
    571 		       sc->dev.dv_xname);
    572 		if (intrstr != NULL)
    573 			printf(" at %s", intrstr);
    574 		printf("\n");
    575 		return;
    576 	}
    577 	printf("%s: interruping at %s\n", sc->dev.dv_xname, intrstr);
    578 
    579 	if ((error = nm_init(sc)) != 0)
    580 		return;
    581 
    582 	/* Enable the device. */
    583 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    584 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    585 		       csr | PCI_COMMAND_MASTER_ENABLE);
    586 
    587 	sc->host_if.arg = sc;
    588 
    589 	sc->host_if.attach = neo_attach_codec;
    590 	sc->host_if.read   = neo_read_codec;
    591 	sc->host_if.write  = neo_write_codec;
    592 	sc->host_if.reset  = neo_reset_codec;
    593 	sc->host_if.flags  = neo_flags_codec;
    594 
    595 	if ((error = ac97_attach(&sc->host_if)) != 0)
    596 		return;
    597 
    598 	audio_attach_mi(&neo_hw_if, sc, &sc->dev);
    599 }
    600 
    601 int
    602 neo_read_codec(void *v, u_int8_t a, u_int16_t *d)
    603 {
    604 	struct neo_softc *sc = v;
    605 
    606 	if (!nm_waitcd(sc)) {
    607 		*d = nm_rd_2(sc, sc->ac97_base + a);
    608 		DELAY(1000);
    609 		return 0;
    610 	}
    611 
    612 	return (ENXIO);
    613 }
    614 
    615 
    616 int
    617 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
    618 {
    619 	struct neo_softc *sc = v;
    620 	int cnt = 3;
    621 
    622 	if (!nm_waitcd(sc)) {
    623 		while (cnt-- > 0) {
    624 			nm_wr_2(sc, sc->ac97_base + a, d);
    625 			if (!nm_waitcd(sc)) {
    626 				DELAY(1000);
    627 				return (0);
    628 			}
    629 		}
    630 	}
    631 
    632         return (ENXIO);
    633 }
    634 
    635 int
    636 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
    637 {
    638 	struct neo_softc *sc = v;
    639 
    640 	sc->codec_if = codec_if;
    641 	return (0);
    642 }
    643 
    644 void
    645 neo_reset_codec(void *v)
    646 {
    647 	struct neo_softc *sc = v;
    648 
    649 	nm_wr_1(sc, 0x6c0, 0x01);
    650 	nm_wr_1(sc, 0x6cc, 0x87);
    651 	nm_wr_1(sc, 0x6cc, 0x80);
    652 	nm_wr_1(sc, 0x6cc, 0x00);
    653 }
    654 
    655 enum ac97_host_flags
    656 neo_flags_codec(void *v)
    657 {
    658 
    659 	return (AC97_HOST_DONT_READ);
    660 }
    661 
    662 int
    663 neo_open(void *addr, int flags)
    664 {
    665 
    666 	return (0);
    667 }
    668 
    669 /*
    670  * Close function is called at splaudio().
    671  */
    672 void
    673 neo_close(void *addr)
    674 {
    675 	struct neo_softc *sc = addr;
    676 
    677 	neo_halt_output(sc);
    678 	neo_halt_input(sc);
    679 
    680 	sc->pintr = 0;
    681 	sc->rintr = 0;
    682 }
    683 
    684 int
    685 neo_query_encoding(void *addr, struct audio_encoding *fp)
    686 {
    687 
    688 	switch (fp->index) {
    689 	case 0:
    690 		strcpy(fp->name, AudioEulinear);
    691 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    692 		fp->precision = 8;
    693 		fp->flags = 0;
    694 		return (0);
    695 	case 1:
    696 		strcpy(fp->name, AudioEmulaw);
    697 		fp->encoding = AUDIO_ENCODING_ULAW;
    698 		fp->precision = 8;
    699 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    700 		return (0);
    701 	case 2:
    702 		strcpy(fp->name, AudioEalaw);
    703 		fp->encoding = AUDIO_ENCODING_ALAW;
    704 		fp->precision = 8;
    705 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    706 		return (0);
    707 	case 3:
    708 		strcpy(fp->name, AudioEslinear);
    709 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    710 		fp->precision = 8;
    711 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    712 		return (0);
    713 	case 4:
    714 		strcpy(fp->name, AudioEslinear_le);
    715 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    716 		fp->precision = 16;
    717 		fp->flags = 0;
    718 		return (0);
    719 	case 5:
    720 		strcpy(fp->name, AudioEulinear_le);
    721 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    722 		fp->precision = 16;
    723 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    724 		return (0);
    725 	case 6:
    726 		strcpy(fp->name, AudioEslinear_be);
    727 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    728 		fp->precision = 16;
    729 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    730 		return (0);
    731 	case 7:
    732 		strcpy(fp->name, AudioEulinear_be);
    733 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    734 		fp->precision = 16;
    735 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    736 		return (0);
    737 	default:
    738 		return (EINVAL);
    739 	}
    740 }
    741 
    742 /* Todo: don't commit settings to card until we've verified all parameters */
    743 int
    744 neo_set_params(void *addr, int setmode, int usemode, struct audio_params *play,
    745     struct audio_params *rec)
    746 {
    747 	struct neo_softc *sc = addr;
    748 	u_int32_t base;
    749 	u_int8_t x;
    750 	int mode;
    751 	struct audio_params *p;
    752 
    753 	for (mode = AUMODE_RECORD; mode != -1;
    754 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    755 		if ((setmode & mode) == 0)
    756 			continue;
    757 
    758 		p = mode == AUMODE_PLAY ? play : rec;
    759 
    760 		if (p == NULL) continue;
    761 
    762 		for (x = 0; x < 8; x++) {
    763 			if (p->sample_rate <
    764 			    (samplerates[x] + samplerates[x + 1]) / 2)
    765 				break;
    766 		}
    767 		if (x == 8)
    768 			return (EINVAL);
    769 
    770 		p->sample_rate = samplerates[x];
    771 		nm_loadcoeff(sc, mode, x);
    772 
    773 		x <<= 4;
    774 		x &= NM_RATE_MASK;
    775 		if (p->precision == 16)
    776 			x |= NM_RATE_BITS_16;
    777 		if (p->channels == 2)
    778 			x |= NM_RATE_STEREO;
    779 
    780 		base = (mode == AUMODE_PLAY)?
    781 		    NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
    782 		nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
    783 
    784 		p->factor = 1;
    785 		p->sw_code = 0;
    786 		switch (p->encoding) {
    787 		case AUDIO_ENCODING_SLINEAR_BE:
    788 			if (p->precision == 16)
    789 				p->sw_code = swap_bytes;
    790 			else
    791 				p->sw_code = change_sign8;
    792 			break;
    793 		case AUDIO_ENCODING_SLINEAR_LE:
    794 			if (p->precision != 16)
    795 				p->sw_code = change_sign8;
    796 			break;
    797 		case AUDIO_ENCODING_ULINEAR_BE:
    798 			if (p->precision == 16) {
    799 				if (mode == AUMODE_PLAY)
    800 					p->sw_code =
    801 					    swap_bytes_change_sign16_le;
    802 				else
    803 					p->sw_code =
    804 					    change_sign16_swap_bytes_le;
    805 			}
    806 			break;
    807 		case AUDIO_ENCODING_ULINEAR_LE:
    808 			if (p->precision == 16)
    809 				p->sw_code = change_sign16_le;
    810 			break;
    811 		case AUDIO_ENCODING_ULAW:
    812 			if (mode == AUMODE_PLAY) {
    813 				p->factor = 2;
    814 				p->sw_code = mulaw_to_slinear16_le;
    815 			} else
    816 				p->sw_code = ulinear8_to_mulaw;
    817 			break;
    818 		case AUDIO_ENCODING_ALAW:
    819 			if (mode == AUMODE_PLAY) {
    820 				p->factor = 2;
    821 				p->sw_code = alaw_to_slinear16_le;
    822 			} else
    823 				p->sw_code = ulinear8_to_alaw;
    824 			break;
    825 		default:
    826 			return (EINVAL);
    827 		}
    828 	}
    829 
    830 
    831 	return (0);
    832 }
    833 
    834 int
    835 neo_round_blocksize(void *addr, int blk)
    836 {
    837 
    838 	return (NM_BUFFSIZE / 2);
    839 }
    840 
    841 int
    842 neo_trigger_output(void *addr, void *start, void *end, int blksize,
    843     void (*intr)(void *), void *arg, struct audio_params *param)
    844 {
    845 	struct neo_softc *sc = addr;
    846 	int ssz;
    847 
    848 	sc->pintr = intr;
    849 	sc->parg = arg;
    850 
    851 	ssz = (param->precision * param->factor == 16) ? 2 : 1;
    852 	if (param->channels == 2)
    853 		ssz <<= 1;
    854 
    855 	sc->pbufsize = ((char*)end - (char *)start);
    856 	sc->pblksize = blksize;
    857 	sc->pwmark = blksize;
    858 
    859 	nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
    860 	nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
    861 	nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
    862 	nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
    863 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
    864 	    NM_PLAYBACK_ENABLE_FLAG);
    865 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
    866 
    867 	return (0);
    868 }
    869 
    870 int
    871 neo_trigger_input(void *addr, void *start, void *end, int blksize,
    872     void (*intr)(void *), void *arg, struct audio_params *param)
    873 {
    874 	struct neo_softc *sc = addr;
    875 	int ssz;
    876 
    877 	sc->rintr = intr;
    878 	sc->rarg = arg;
    879 
    880 	ssz = (param->precision * param->factor == 16) ? 2 : 1;
    881 	if (param->channels == 2)
    882 		ssz <<= 1;
    883 
    884 	sc->rbufsize = ((char*)end - (char *)start);
    885 	sc->rblksize = blksize;
    886 	sc->rwmark = blksize;
    887 
    888 	nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
    889 	nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
    890 	nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
    891 	nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
    892 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
    893 	    NM_RECORD_ENABLE_FLAG);
    894 
    895 	return (0);
    896 }
    897 
    898 int
    899 neo_halt_output(void *addr)
    900 {
    901 	struct neo_softc *sc = (struct neo_softc *)addr;
    902 
    903 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
    904 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
    905 
    906 	return (0);
    907 }
    908 
    909 int
    910 neo_halt_input(void *addr)
    911 {
    912 	struct neo_softc *sc = (struct neo_softc *)addr;
    913 
    914 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
    915 
    916 	return (0);
    917 }
    918 
    919 int
    920 neo_getdev(void *addr, struct audio_device *retp)
    921 {
    922 
    923 	*retp = neo_device;
    924 	return (0);
    925 }
    926 
    927 int
    928 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
    929 {
    930 	struct neo_softc *sc = addr;
    931 
    932 	return ((sc->codec_if->vtbl->mixer_set_port)(sc->codec_if, cp));
    933 }
    934 
    935 int
    936 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
    937 {
    938 	struct neo_softc *sc = addr;
    939 
    940 	return ((sc->codec_if->vtbl->mixer_get_port)(sc->codec_if, cp));
    941 }
    942 
    943 int
    944 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
    945 {
    946 	struct neo_softc *sc = addr;
    947 
    948 	return ((sc->codec_if->vtbl->query_devinfo)(sc->codec_if, dip));
    949 }
    950 
    951 void *
    952 neo_malloc(void *addr, int direction, size_t size, int pool, int flags)
    953 {
    954 	struct neo_softc *sc = addr;
    955 	void *rv = NULL;
    956 
    957 	switch (direction) {
    958 	case AUMODE_PLAY:
    959 		if (sc->pbuf_allocated == 0) {
    960 			rv = (void *) sc->pbuf_vaddr;
    961 			sc->pbuf_allocated = 1;
    962 		}
    963 		break;
    964 
    965 	case AUMODE_RECORD:
    966 		if (sc->rbuf_allocated == 0) {
    967 			rv = (void *) sc->rbuf_vaddr;
    968 			sc->rbuf_allocated = 1;
    969 		}
    970 		break;
    971 	}
    972 
    973 	return (rv);
    974 }
    975 
    976 void
    977 neo_free(void *addr, void *ptr, int pool)
    978 {
    979 	struct neo_softc *sc = addr;
    980 	vaddr_t v = (vaddr_t) ptr;
    981 
    982 	if (v == sc->pbuf_vaddr)
    983 		sc->pbuf_allocated = 0;
    984 	else if (v == sc->rbuf_vaddr)
    985 		sc->rbuf_allocated = 0;
    986 	else
    987 		printf("neo_free: bad address %p\n", ptr);
    988 }
    989 
    990 size_t
    991 neo_round_buffersize(void *addr, int direction, size_t size)
    992 {
    993 
    994 	return (NM_BUFFSIZE);
    995 }
    996 
    997 paddr_t
    998 neo_mappage(void *addr, void *mem, off_t off, int prot)
    999 {
   1000 	struct neo_softc *sc = addr;
   1001 	vaddr_t v = (vaddr_t) mem;
   1002 	bus_addr_t pciaddr;
   1003 
   1004 	/* XXX Need new mapping code. */
   1005 
   1006 	if (v == sc->pbuf_vaddr)
   1007 		pciaddr = sc->pbuf_pciaddr;
   1008 	else if (v == sc->rbuf_vaddr)
   1009 		pciaddr = sc->rbuf_pciaddr;
   1010 	else
   1011 		return (-1);
   1012 
   1013 #ifdef __i386__
   1014 	return (i386_btop(pciaddr + off));
   1015 #else
   1016 	return (-1);
   1017 #endif
   1018 }
   1019 
   1020 int
   1021 neo_get_props(void *addr)
   1022 {
   1023 
   1024 	return (AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
   1025                 AUDIO_PROP_FULLDUPLEX);
   1026 }
   1027