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