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