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