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