Home | History | Annotate | Line # | Download | only in pci
neo.c revision 1.4
      1 /*	$NetBSD: neo.c,v 1.4 2000/11/05 16:44:08 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  *    Figure out interaction with video stuff (look at Xfree86 driver?)
    111  *
    112  *    Power management (neoactivate)
    113  *
    114  *    Figure out how to shrink that huge table neo-coeff.h
    115  */
    116 
    117 #define	NM_BUFFSIZE	16384
    118 
    119 /* device private data */
    120 struct neo_softc {
    121 	struct          device dev;
    122 
    123 	bus_space_tag_t bufiot;
    124 	bus_space_handle_t  bufioh;
    125 
    126 	bus_space_tag_t regiot;
    127 	bus_space_handle_t  regioh;
    128 
    129 	u_int32_t 	type;
    130 	void            *ih;
    131 
    132 	void	(*pintr)(void *);	/* dma completion intr handler */
    133 	void	*parg;		/* arg for intr() */
    134 
    135 	void	(*rintr)(void *);	/* dma completion intr handler */
    136 	void	*rarg;		/* arg for intr() */
    137 
    138 	vaddr_t	buf_vaddr;
    139 	vaddr_t	rbuf_vaddr;
    140 	vaddr_t	pbuf_vaddr;
    141 	int	pbuf_allocated;
    142 	int	rbuf_allocated;
    143 
    144 	bus_addr_t buf_pciaddr;
    145 	bus_addr_t rbuf_pciaddr;
    146 	bus_addr_t pbuf_pciaddr;
    147 
    148 	u_int32_t 	ac97_base, ac97_status, ac97_busy;
    149 	u_int32_t	buftop, pbuf, rbuf, cbuf, acbuf;
    150 	u_int32_t	playint, recint, misc1int, misc2int;
    151 	u_int32_t	irsz, badintr;
    152 
    153         u_int32_t       pbufsize;
    154         u_int32_t       rbufsize;
    155 
    156 	u_int32_t       pblksize;
    157 	u_int32_t       rblksize;
    158 
    159         u_int32_t       pwmark;
    160         u_int32_t       rwmark;
    161 
    162 	struct ac97_codec_if *codec_if;
    163 	struct ac97_host_if host_if;
    164 };
    165 
    166 /* -------------------------------------------------------------------- */
    167 
    168 /*
    169  * prototypes
    170  */
    171 
    172 static int	nm_waitcd(struct neo_softc *sc);
    173 static int	nm_loadcoeff(struct neo_softc *sc, int dir, int num);
    174 static int	nm_init(struct neo_softc *);
    175 
    176 int	neo_match(struct device *, struct cfdata *, void *);
    177 void	neo_attach(struct device *, struct device *, void *);
    178 int	neo_intr(void *);
    179 
    180 int	neo_open(void *, int);
    181 void	neo_close(void *);
    182 int	neo_query_encoding(void *, struct audio_encoding *);
    183 int	neo_set_params(void *, int, int, struct audio_params *,
    184 	    struct audio_params *);
    185 int	neo_round_blocksize(void *, int);
    186 int	neo_trigger_output(void *, void *, void *, int, void (*)(void *),
    187 	    void *, struct audio_params *);
    188 int	neo_trigger_input(void *, void *, void *, int, void (*)(void *),
    189 	    void *, struct audio_params *);
    190 int	neo_halt_output(void *);
    191 int	neo_halt_input(void *);
    192 int	neo_getdev(void *, struct audio_device *);
    193 int	neo_mixer_set_port(void *, mixer_ctrl_t *);
    194 int	neo_mixer_get_port(void *, mixer_ctrl_t *);
    195 int     neo_attach_codec(void *sc, struct ac97_codec_if *);
    196 int	neo_read_codec(void *sc, u_int8_t a, u_int16_t *d);
    197 int	neo_write_codec(void *sc, u_int8_t a, u_int16_t d);
    198 void    neo_reset_codec(void *sc);
    199 enum ac97_host_flags neo_flags_codec(void *sc);
    200 int	neo_query_devinfo(void *, mixer_devinfo_t *);
    201 void   *neo_malloc(void *, int, size_t, int, int);
    202 void	neo_free(void *, void *, int);
    203 size_t	neo_round_buffersize(void *, int, size_t);
    204 paddr_t	neo_mappage(void *, void *, off_t, int);
    205 int	neo_get_props(void *);
    206 void	neo_set_mixer(struct neo_softc *sc, int a, int d);
    207 
    208 struct cfattach neo_ca = {
    209 	sizeof(struct neo_softc), neo_match, neo_attach
    210 };
    211 
    212 struct audio_device neo_device = {
    213 	"NeoMagic 256",
    214 	"",
    215 	"neo"
    216 };
    217 
    218 /* The actual rates supported by the card. */
    219 static const int samplerates[9] = {
    220 	8000,
    221 	11025,
    222 	16000,
    223 	22050,
    224 	24000,
    225 	32000,
    226 	44100,
    227 	48000,
    228 	99999999
    229 };
    230 
    231 /* -------------------------------------------------------------------- */
    232 
    233 struct audio_hw_if neo_hw_if = {
    234 	neo_open,
    235 	neo_close,
    236 	NULL,				/* drain */
    237 	neo_query_encoding,
    238 	neo_set_params,
    239 	neo_round_blocksize,
    240 	NULL,				/* commit_setting */
    241 	NULL,				/* init_output */
    242 	NULL,				/* init_input */
    243 	NULL,				/* start_output */
    244 	NULL,				/* start_input */
    245 	neo_halt_output,
    246 	neo_halt_input,
    247 	NULL,				/* speaker_ctl */
    248 	neo_getdev,
    249 	NULL,				/* getfd */
    250 	neo_mixer_set_port,
    251 	neo_mixer_get_port,
    252 	neo_query_devinfo,
    253 	neo_malloc,
    254 	neo_free,
    255 	neo_round_buffersize,
    256 	neo_mappage,
    257 	neo_get_props,
    258 	neo_trigger_output,
    259 	neo_trigger_input,
    260 };
    261 
    262 /* -------------------------------------------------------------------- */
    263 
    264 #define	nm_rd_1(sc, regno)						\
    265 	bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
    266 
    267 #define	nm_rd_2(sc, regno)						\
    268 	bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
    269 
    270 #define	nm_rd_4(sc, regno)						\
    271 	bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
    272 
    273 #define	nm_wr_1(sc, regno, val)						\
    274 	bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
    275 
    276 #define	nm_wr_2(sc, regno, val)						\
    277 	bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
    278 
    279 #define	nm_wr_4(sc, regno, val)						\
    280 	bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
    281 
    282 #define	nm_rdbuf_4(sc, regno)						\
    283 	bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
    284 
    285 #define	nm_wrbuf_1(sc, regno, val)					\
    286 	bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
    287 
    288 /* ac97 codec */
    289 static int
    290 nm_waitcd(struct neo_softc *sc)
    291 {
    292 	int cnt = 10;
    293 	int fail = 1;
    294 
    295 	while (cnt-- > 0) {
    296 		if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
    297 			DELAY(100);
    298 		else {
    299 		        fail = 0;
    300 			break;
    301 		}
    302 	}
    303 	return (fail);
    304 }
    305 
    306 
    307 static void
    308 nm_ackint(struct neo_softc *sc, u_int32_t num)
    309 {
    310 
    311 	switch (sc->type) {
    312 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    313 		nm_wr_2(sc, NM_INT_REG, num << 1);
    314 		break;
    315 
    316 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    317 		nm_wr_4(sc, NM_INT_REG, num);
    318 		break;
    319 	}
    320 }
    321 
    322 static int
    323 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
    324 {
    325 	int ofs, sz, i;
    326 	u_int32_t addr;
    327 
    328 	addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
    329 	if (dir == AUMODE_RECORD)
    330 		num += 8;
    331 	sz = coefficientSizes[num];
    332 	ofs = 0;
    333 	while (num-- > 0)
    334 		ofs+= coefficientSizes[num];
    335 	for (i = 0; i < sz; i++)
    336 		nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
    337 	nm_wr_4(sc, addr, sc->cbuf);
    338 	if (dir == AUMODE_PLAY)
    339 		sz--;
    340 	nm_wr_4(sc, addr + 4, sc->cbuf + sz);
    341 	return 0;
    342 }
    343 
    344 /* The interrupt handler */
    345 int
    346 neo_intr(void *p)
    347 {
    348 	struct neo_softc *sc = (struct neo_softc *)p;
    349 	int status, x, active;
    350 	int rv = 0;
    351 
    352 	active = (sc->pintr || sc->rintr);
    353 	status = (sc->irsz == 2) ?
    354 	    nm_rd_2(sc, NM_INT_REG) :
    355 	    nm_rd_4(sc, NM_INT_REG);
    356 
    357 	if (status & sc->playint) {
    358 		status &= ~sc->playint;
    359 
    360 		sc->pwmark += sc->pblksize;
    361 		sc->pwmark %= sc->pbufsize;
    362 
    363 		nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
    364 
    365 		nm_ackint(sc, sc->playint);
    366 
    367 		if (sc->pintr)
    368 			(*sc->pintr)(sc->parg);
    369 
    370 		rv = 1;
    371 	}
    372 	if (status & sc->recint) {
    373 		status &= ~sc->recint;
    374 
    375 		sc->rwmark += sc->rblksize;
    376 		sc->rwmark %= sc->rbufsize;
    377 
    378 		nm_ackint(sc, sc->recint);
    379 		if (sc->rintr)
    380 			(*sc->rintr)(sc->rarg);
    381 
    382 		rv = 1;
    383 	}
    384 	if (status & sc->misc1int) {
    385 		status &= ~sc->misc1int;
    386 		nm_ackint(sc, sc->misc1int);
    387 		x = nm_rd_1(sc, 0x400);
    388 		nm_wr_1(sc, 0x400, x | 2);
    389 		printf("%s: misc int 1\n", sc->dev.dv_xname);
    390 		rv = 1;
    391 	}
    392 	if (status & sc->misc2int) {
    393 		status &= ~sc->misc2int;
    394 		nm_ackint(sc, sc->misc2int);
    395 		x = nm_rd_1(sc, 0x400);
    396 		nm_wr_1(sc, 0x400, x & ~2);
    397 		printf("%s: misc int 2\n", sc->dev.dv_xname);
    398 		rv = 1;
    399 	}
    400 	if (status) {
    401 		status &= ~sc->misc2int;
    402 		nm_ackint(sc, sc->misc2int);
    403 		printf("%s: unknown int\n", sc->dev.dv_xname);
    404 		rv = 1;
    405 	}
    406 
    407 	return (rv);
    408 }
    409 
    410 /* -------------------------------------------------------------------- */
    411 
    412 /*
    413  * Probe and attach the card
    414  */
    415 
    416 static int
    417 nm_init(struct neo_softc *sc)
    418 {
    419 	u_int32_t ofs, i;
    420 
    421 	switch (sc->type) {
    422 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    423 		sc->ac97_base = NM_MIXER_OFFSET;
    424 		sc->ac97_status = NM_MIXER_STATUS_OFFSET;
    425 		sc->ac97_busy = NM_MIXER_READY_MASK;
    426 
    427 		sc->buftop = 2560 * 1024;
    428 
    429 		sc->irsz = 2;
    430 		sc->playint = NM_PLAYBACK_INT;
    431 		sc->recint = NM_RECORD_INT;
    432 		sc->misc1int = NM_MISC_INT_1;
    433 		sc->misc2int = NM_MISC_INT_2;
    434 		break;
    435 
    436 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    437 		sc->ac97_base = NM_MIXER_OFFSET;
    438 		sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
    439 		sc->ac97_busy = NM2_MIXER_READY_MASK;
    440 
    441 		sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
    442 
    443 		sc->irsz = 4;
    444 		sc->playint = NM2_PLAYBACK_INT;
    445 		sc->recint = NM2_RECORD_INT;
    446 		sc->misc1int = NM2_MISC_INT_1;
    447 		sc->misc2int = NM2_MISC_INT_2;
    448 		break;
    449 #ifdef DIAGNOSTIC
    450 	default:
    451 		panic("nm_init: impossible");
    452 #endif
    453 	}
    454 
    455 	sc->badintr = 0;
    456 	ofs = sc->buftop - 0x0400;
    457 	sc->buftop -= 0x1400;
    458 
    459  	if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
    460 		i = nm_rdbuf_4(sc, ofs + 4);
    461 		if (i != 0 && i != 0xffffffff)
    462 			sc->buftop = i;
    463 	}
    464 
    465 	sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
    466 	sc->rbuf = sc->cbuf - NM_BUFFSIZE;
    467 	sc->pbuf = sc->rbuf - NM_BUFFSIZE;
    468 	sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
    469 
    470 	sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
    471 	sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
    472 	sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
    473 
    474 	sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
    475 	sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
    476 
    477 	nm_wr_1(sc, 0, 0x11);
    478 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
    479 	nm_wr_2(sc, 0x214, 0);
    480 
    481 	return 0;
    482 }
    483 
    484 int
    485 neo_match(struct device *parent, struct cfdata *match, void *aux)
    486 {
    487 	struct pci_attach_args *pa = aux;
    488 	pcireg_t subdev;
    489 
    490 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
    491 		return (0);
    492 
    493 	subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    494 
    495 	switch (PCI_PRODUCT(pa->pa_id)) {
    496 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    497 		/*
    498 		 * We have to weed-out the non-AC'97 versions of
    499 		 * the chip (i.e. the ones that are known to work
    500 		 * in WSS emulation mode), as they won't work with
    501 		 * this driver.
    502 		 */
    503 		switch (PCI_VENDOR(subdev)) {
    504 		case PCI_VENDOR_DELL:
    505 			switch (PCI_PRODUCT(subdev)) {
    506 			case 0x008f:
    507 				return (0);
    508 			}
    509 			break;
    510 
    511 		case PCI_VENDOR_HP:
    512 			switch (PCI_PRODUCT(subdev)) {
    513 			case 0x0007:
    514 				return (0);
    515 			}
    516 		}
    517 		return (1);
    518 
    519 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    520 		return (1);
    521 	}
    522 
    523 	return (0);
    524 }
    525 
    526 void
    527 neo_attach(struct device *parent, struct device *self, void *aux)
    528 {
    529 	struct neo_softc *sc = (struct neo_softc *)self;
    530 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    531 	pci_chipset_tag_t pc = pa->pa_pc;
    532 	char const *intrstr;
    533 	pci_intr_handle_t ih;
    534 	pcireg_t csr;
    535 	int error;
    536 
    537 	sc->type = PCI_PRODUCT(pa->pa_id);
    538 
    539 	printf(": NeoMagic 256%s audio\n",
    540 	    sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
    541 
    542 	/* Map I/O register */
    543 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
    544 			   &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
    545 		printf("%s: can't map buffer\n", sc->dev.dv_xname);
    546 		return;
    547 	}
    548 
    549 	if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM, 0,
    550 			   &sc->regiot, &sc->regioh, NULL, NULL)) {
    551 		printf("%s: can't map registers\n", sc->dev.dv_xname);
    552 		return;
    553 	}
    554 
    555 	/* Map and establish the interrupt. */
    556 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    557 			 pa->pa_intrline, &ih)) {
    558 		printf("%s: couldn't map interrupt\n", sc->dev.dv_xname);
    559 		return;
    560 	}
    561 
    562 	intrstr = pci_intr_string(pc, ih);
    563 	sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc);
    564 
    565 	if (sc->ih == NULL) {
    566 		printf("%s: couldn't establish interrupt",
    567 		       sc->dev.dv_xname);
    568 		if (intrstr != NULL)
    569 			printf(" at %s", intrstr);
    570 		printf("\n");
    571 		return;
    572 	}
    573 	printf("%s: interruping at %s\n", sc->dev.dv_xname, intrstr);
    574 
    575 	if ((error = nm_init(sc)) != 0)
    576 		return;
    577 
    578 	/* Enable the device. */
    579 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    580 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    581 		       csr | PCI_COMMAND_MASTER_ENABLE);
    582 
    583 	sc->host_if.arg = sc;
    584 
    585 	sc->host_if.attach = neo_attach_codec;
    586 	sc->host_if.read   = neo_read_codec;
    587 	sc->host_if.write  = neo_write_codec;
    588 	sc->host_if.reset  = neo_reset_codec;
    589 	sc->host_if.flags  = neo_flags_codec;
    590 
    591 	if ((error = ac97_attach(&sc->host_if)) != 0)
    592 		return;
    593 
    594 	audio_attach_mi(&neo_hw_if, sc, &sc->dev);
    595 }
    596 
    597 int
    598 neo_read_codec(void *v, u_int8_t a, u_int16_t *d)
    599 {
    600 	struct neo_softc *sc = v;
    601 
    602 	if (!nm_waitcd(sc)) {
    603 		*d = nm_rd_2(sc, sc->ac97_base + a);
    604 		DELAY(1000);
    605 		return 0;
    606 	}
    607 
    608 	return (ENXIO);
    609 }
    610 
    611 
    612 int
    613 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
    614 {
    615 	struct neo_softc *sc = v;
    616 	int cnt = 3;
    617 
    618 	if (!nm_waitcd(sc)) {
    619 		while (cnt-- > 0) {
    620 			nm_wr_2(sc, sc->ac97_base + a, d);
    621 			if (!nm_waitcd(sc)) {
    622 				DELAY(1000);
    623 				return (0);
    624 			}
    625 		}
    626 	}
    627 
    628         return (ENXIO);
    629 }
    630 
    631 int
    632 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
    633 {
    634 	struct neo_softc *sc = v;
    635 
    636 	sc->codec_if = codec_if;
    637 	return (0);
    638 }
    639 
    640 void
    641 neo_reset_codec(void *v)
    642 {
    643 	struct neo_softc *sc = v;
    644 
    645 	nm_wr_1(sc, 0x6c0, 0x01);
    646 	nm_wr_1(sc, 0x6cc, 0x87);
    647 	nm_wr_1(sc, 0x6cc, 0x80);
    648 	nm_wr_1(sc, 0x6cc, 0x00);
    649 }
    650 
    651 enum ac97_host_flags
    652 neo_flags_codec(void *v)
    653 {
    654 
    655 	return (AC97_HOST_DONT_READ);
    656 }
    657 
    658 int
    659 neo_open(void *addr, int flags)
    660 {
    661 
    662 	return (0);
    663 }
    664 
    665 /*
    666  * Close function is called at splaudio().
    667  */
    668 void
    669 neo_close(void *addr)
    670 {
    671 	struct neo_softc *sc = addr;
    672 
    673 	neo_halt_output(sc);
    674 	neo_halt_input(sc);
    675 
    676 	sc->pintr = 0;
    677 	sc->rintr = 0;
    678 }
    679 
    680 int
    681 neo_query_encoding(void *addr, struct audio_encoding *fp)
    682 {
    683 
    684 	switch (fp->index) {
    685 	case 0:
    686 		strcpy(fp->name, AudioEulinear);
    687 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    688 		fp->precision = 8;
    689 		fp->flags = 0;
    690 		return (0);
    691 	case 1:
    692 		strcpy(fp->name, AudioEmulaw);
    693 		fp->encoding = AUDIO_ENCODING_ULAW;
    694 		fp->precision = 8;
    695 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    696 		return (0);
    697 	case 2:
    698 		strcpy(fp->name, AudioEalaw);
    699 		fp->encoding = AUDIO_ENCODING_ALAW;
    700 		fp->precision = 8;
    701 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    702 		return (0);
    703 	case 3:
    704 		strcpy(fp->name, AudioEslinear);
    705 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    706 		fp->precision = 8;
    707 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    708 		return (0);
    709 	case 4:
    710 		strcpy(fp->name, AudioEslinear_le);
    711 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    712 		fp->precision = 16;
    713 		fp->flags = 0;
    714 		return (0);
    715 	case 5:
    716 		strcpy(fp->name, AudioEulinear_le);
    717 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    718 		fp->precision = 16;
    719 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    720 		return (0);
    721 	case 6:
    722 		strcpy(fp->name, AudioEslinear_be);
    723 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    724 		fp->precision = 16;
    725 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    726 		return (0);
    727 	case 7:
    728 		strcpy(fp->name, AudioEulinear_be);
    729 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    730 		fp->precision = 16;
    731 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    732 		return (0);
    733 	default:
    734 		return (EINVAL);
    735 	}
    736 }
    737 
    738 /* Todo: don't commit settings to card until we've verified all parameters */
    739 int
    740 neo_set_params(void *addr, int setmode, int usemode, struct audio_params *play,
    741     struct audio_params *rec)
    742 {
    743 	struct neo_softc *sc = addr;
    744 	u_int32_t base;
    745 	u_int8_t x;
    746 	int mode;
    747 	struct audio_params *p;
    748 
    749 	for (mode = AUMODE_RECORD; mode != -1;
    750 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    751 		if ((setmode & mode) == 0)
    752 			continue;
    753 
    754 		p = mode == AUMODE_PLAY ? play : rec;
    755 
    756 		if (p == NULL) continue;
    757 
    758 		for (x = 0; x < 8; x++) {
    759 			if (p->sample_rate <
    760 			    (samplerates[x] + samplerates[x + 1]) / 2)
    761 				break;
    762 		}
    763 		if (x == 8)
    764 			return (EINVAL);
    765 
    766 		p->sample_rate = samplerates[x];
    767 		nm_loadcoeff(sc, mode, x);
    768 
    769 		x <<= 4;
    770 		x &= NM_RATE_MASK;
    771 		if (p->precision == 16)
    772 			x |= NM_RATE_BITS_16;
    773 		if (p->channels == 2)
    774 			x |= NM_RATE_STEREO;
    775 
    776 		base = (mode == AUMODE_PLAY)?
    777 		    NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
    778 		nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
    779 
    780 		p->factor = 1;
    781 		p->sw_code = 0;
    782 		switch (p->encoding) {
    783 		case AUDIO_ENCODING_SLINEAR_BE:
    784 			if (p->precision == 16)
    785 				p->sw_code = swap_bytes;
    786 			else
    787 				p->sw_code = change_sign8;
    788 			break;
    789 		case AUDIO_ENCODING_SLINEAR_LE:
    790 			if (p->precision != 16)
    791 				p->sw_code = change_sign8;
    792 			break;
    793 		case AUDIO_ENCODING_ULINEAR_BE:
    794 			if (p->precision == 16) {
    795 				if (mode == AUMODE_PLAY)
    796 					p->sw_code =
    797 					    swap_bytes_change_sign16_le;
    798 				else
    799 					p->sw_code =
    800 					    change_sign16_swap_bytes_le;
    801 			}
    802 			break;
    803 		case AUDIO_ENCODING_ULINEAR_LE:
    804 			if (p->precision == 16)
    805 				p->sw_code = change_sign16_le;
    806 			break;
    807 		case AUDIO_ENCODING_ULAW:
    808 			if (mode == AUMODE_PLAY) {
    809 				p->factor = 2;
    810 				p->sw_code = mulaw_to_slinear16_le;
    811 			} else
    812 				p->sw_code = ulinear8_to_mulaw;
    813 			break;
    814 		case AUDIO_ENCODING_ALAW:
    815 			if (mode == AUMODE_PLAY) {
    816 				p->factor = 2;
    817 				p->sw_code = alaw_to_slinear16_le;
    818 			} else
    819 				p->sw_code = ulinear8_to_alaw;
    820 			break;
    821 		default:
    822 			return (EINVAL);
    823 		}
    824 	}
    825 
    826 
    827 	return (0);
    828 }
    829 
    830 int
    831 neo_round_blocksize(void *addr, int blk)
    832 {
    833 
    834 	return (NM_BUFFSIZE / 2);
    835 }
    836 
    837 int
    838 neo_trigger_output(void *addr, void *start, void *end, int blksize,
    839     void (*intr)(void *), void *arg, struct audio_params *param)
    840 {
    841 	struct neo_softc *sc = addr;
    842 	int ssz;
    843 
    844 	sc->pintr = intr;
    845 	sc->parg = arg;
    846 
    847 	ssz = (param->precision * param->factor == 16) ? 2 : 1;
    848 	if (param->channels == 2)
    849 		ssz <<= 1;
    850 
    851 	sc->pbufsize = ((char*)end - (char *)start);
    852 	sc->pblksize = blksize;
    853 	sc->pwmark = blksize;
    854 
    855 	nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
    856 	nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
    857 	nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
    858 	nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
    859 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
    860 	    NM_PLAYBACK_ENABLE_FLAG);
    861 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
    862 
    863 	return (0);
    864 }
    865 
    866 int
    867 neo_trigger_input(void *addr, void *start, void *end, int blksize,
    868     void (*intr)(void *), void *arg, struct audio_params *param)
    869 {
    870 	struct neo_softc *sc = addr;
    871 	int ssz;
    872 
    873 	sc->rintr = intr;
    874 	sc->rarg = arg;
    875 
    876 	ssz = (param->precision * param->factor == 16) ? 2 : 1;
    877 	if (param->channels == 2)
    878 		ssz <<= 1;
    879 
    880 	sc->rbufsize = ((char*)end - (char *)start);
    881 	sc->rblksize = blksize;
    882 	sc->rwmark = blksize;
    883 
    884 	nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
    885 	nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
    886 	nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
    887 	nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
    888 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
    889 	    NM_RECORD_ENABLE_FLAG);
    890 
    891 	return (0);
    892 }
    893 
    894 int
    895 neo_halt_output(void *addr)
    896 {
    897 	struct neo_softc *sc = (struct neo_softc *)addr;
    898 
    899 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
    900 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
    901 
    902 	return (0);
    903 }
    904 
    905 int
    906 neo_halt_input(void *addr)
    907 {
    908 	struct neo_softc *sc = (struct neo_softc *)addr;
    909 
    910 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
    911 
    912 	return (0);
    913 }
    914 
    915 int
    916 neo_getdev(void *addr, struct audio_device *retp)
    917 {
    918 
    919 	*retp = neo_device;
    920 	return (0);
    921 }
    922 
    923 int
    924 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
    925 {
    926 	struct neo_softc *sc = addr;
    927 
    928 	return ((sc->codec_if->vtbl->mixer_set_port)(sc->codec_if, cp));
    929 }
    930 
    931 int
    932 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
    933 {
    934 	struct neo_softc *sc = addr;
    935 
    936 	return ((sc->codec_if->vtbl->mixer_get_port)(sc->codec_if, cp));
    937 }
    938 
    939 int
    940 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
    941 {
    942 	struct neo_softc *sc = addr;
    943 
    944 	return ((sc->codec_if->vtbl->query_devinfo)(sc->codec_if, dip));
    945 }
    946 
    947 void *
    948 neo_malloc(void *addr, int direction, size_t size, int pool, int flags)
    949 {
    950 	struct neo_softc *sc = addr;
    951 	void *rv = NULL;
    952 
    953 	switch (direction) {
    954 	case AUMODE_PLAY:
    955 		if (sc->pbuf_allocated == 0) {
    956 			rv = (void *) sc->pbuf_vaddr;
    957 			sc->pbuf_allocated = 1;
    958 		}
    959 		break;
    960 
    961 	case AUMODE_RECORD:
    962 		if (sc->rbuf_allocated == 0) {
    963 			rv = (void *) sc->rbuf_vaddr;
    964 			sc->rbuf_allocated = 1;
    965 		}
    966 		break;
    967 	}
    968 
    969 	return (rv);
    970 }
    971 
    972 void
    973 neo_free(void *addr, void *ptr, int pool)
    974 {
    975 	struct neo_softc *sc = addr;
    976 	vaddr_t v = (vaddr_t) ptr;
    977 
    978 	if (v == sc->pbuf_vaddr)
    979 		sc->pbuf_allocated = 0;
    980 	else if (v == sc->rbuf_vaddr)
    981 		sc->rbuf_allocated = 0;
    982 	else
    983 		printf("neo_free: bad address %p\n", ptr);
    984 }
    985 
    986 size_t
    987 neo_round_buffersize(void *addr, int direction, size_t size)
    988 {
    989 
    990 	return (NM_BUFFSIZE);
    991 }
    992 
    993 paddr_t
    994 neo_mappage(void *addr, void *mem, off_t off, int prot)
    995 {
    996 	struct neo_softc *sc = addr;
    997 	vaddr_t v = (vaddr_t) mem;
    998 	bus_addr_t pciaddr;
    999 
   1000 	/* XXX Need new mapping code. */
   1001 
   1002 	if (v == sc->pbuf_vaddr)
   1003 		pciaddr = sc->pbuf_pciaddr;
   1004 	else if (v == sc->rbuf_vaddr)
   1005 		pciaddr = sc->rbuf_pciaddr;
   1006 	else
   1007 		return (-1);
   1008 
   1009 #ifdef __i386__
   1010 	return (i386_btop(pciaddr + off));
   1011 #else
   1012 	return (-1);
   1013 #endif
   1014 }
   1015 
   1016 int
   1017 neo_get_props(void *addr)
   1018 {
   1019 
   1020 	return (AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
   1021                 AUDIO_PROP_FULLDUPLEX);
   1022 }
   1023