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