xref: /netbsd-src/sys/dev/pci/sv.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*      $NetBSD: sv.c,v 1.31 2005/12/11 12:22:50 christos Exp $ */
2 /*      $OpenBSD: sv.c,v 1.2 1998/07/13 01:50:15 csapuntz Exp $ */
3 
4 /*
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Charles M. Hannum.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1998 Constantine Paul Sapuntzakis
42  * All rights reserved
43  *
44  * Author: Constantine Paul Sapuntzakis (csapuntz@cvs.openbsd.org)
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. The author's name or those of the contributors may be used to
55  *    endorse or promote products derived from this software without
56  *    specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS
59  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68  * POSSIBILITY OF SUCH DAMAGE.
69  */
70 
71 /*
72  * S3 SonicVibes driver
73  *   Heavily based on the eap driver by Lennart Augustsson
74  */
75 
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: sv.c,v 1.31 2005/12/11 12:22:50 christos Exp $");
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/malloc.h>
83 #include <sys/device.h>
84 
85 #include <dev/pci/pcireg.h>
86 #include <dev/pci/pcivar.h>
87 #include <dev/pci/pcidevs.h>
88 
89 #include <sys/audioio.h>
90 #include <dev/audio_if.h>
91 #include <dev/mulaw.h>
92 #include <dev/auconv.h>
93 
94 #include <dev/ic/i8237reg.h>
95 #include <dev/pci/svreg.h>
96 #include <dev/pci/svvar.h>
97 
98 #include <machine/bus.h>
99 
100 /* XXX
101  * The SonicVibes DMA is broken and only works on 24-bit addresses.
102  * As long as bus_dmamem_alloc_range() is missing we use the ISA
103  * DMA tag on i386.
104  */
105 #if defined(i386)
106 #include "isa.h"
107 #if NISA > 0
108 #include <dev/isa/isavar.h>
109 #endif
110 #endif
111 
112 #ifdef AUDIO_DEBUG
113 #define DPRINTF(x)	if (svdebug) printf x
114 #define DPRINTFN(n,x)	if (svdebug>(n)) printf x
115 int	svdebug = 0;
116 #else
117 #define DPRINTF(x)
118 #define DPRINTFN(n,x)
119 #endif
120 
121 static int	sv_match(struct device *, struct cfdata *, void *);
122 static void	sv_attach(struct device *, struct device *, void *);
123 static int	sv_intr(void *);
124 
125 struct sv_dma {
126 	bus_dmamap_t map;
127 	caddr_t addr;
128 	bus_dma_segment_t segs[1];
129 	int nsegs;
130 	size_t size;
131 	struct sv_dma *next;
132 };
133 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
134 #define KERNADDR(p) ((void *)((p)->addr))
135 
136 CFATTACH_DECL(sv, sizeof(struct sv_softc),
137     sv_match, sv_attach, NULL, NULL);
138 
139 static struct audio_device sv_device = {
140 	"S3 SonicVibes",
141 	"",
142 	"sv"
143 };
144 
145 #define ARRAY_SIZE(foo)  ((sizeof(foo)) / sizeof(foo[0]))
146 
147 static int	sv_allocmem(struct sv_softc *, size_t, size_t, int,
148 			    struct sv_dma *);
149 static int	sv_freemem(struct sv_softc *, struct sv_dma *);
150 
151 static void	sv_init_mixer(struct sv_softc *);
152 
153 static int	sv_open(void *, int);
154 static int	sv_query_encoding(void *, struct audio_encoding *);
155 static int	sv_set_params(void *, int, int, audio_params_t *,
156 			      audio_params_t *, stream_filter_list_t *,
157 			      stream_filter_list_t *);
158 static int	sv_round_blocksize(void *, int, int, const audio_params_t *);
159 static int	sv_trigger_output(void *, void *, void *, int, void (*)(void *),
160 				  void *, const audio_params_t *);
161 static int	sv_trigger_input(void *, void *, void *, int, void (*)(void *),
162 				 void *, const audio_params_t *);
163 static int	sv_halt_output(void *);
164 static int	sv_halt_input(void *);
165 static int	sv_getdev(void *, struct audio_device *);
166 static int	sv_mixer_set_port(void *, mixer_ctrl_t *);
167 static int	sv_mixer_get_port(void *, mixer_ctrl_t *);
168 static int	sv_query_devinfo(void *, mixer_devinfo_t *);
169 static void *	sv_malloc(void *, int, size_t, struct malloc_type *, int);
170 static void	sv_free(void *, void *, struct malloc_type *);
171 static size_t	sv_round_buffersize(void *, int, size_t);
172 static paddr_t	sv_mappage(void *, void *, off_t, int);
173 static int	sv_get_props(void *);
174 
175 #ifdef AUDIO_DEBUG
176 void    sv_dumpregs(struct sv_softc *sc);
177 #endif
178 
179 static const struct audio_hw_if sv_hw_if = {
180 	sv_open,
181 	NULL,			/* close */
182 	NULL,
183 	sv_query_encoding,
184 	sv_set_params,
185 	sv_round_blocksize,
186 	NULL,
187 	NULL,
188 	NULL,
189 	NULL,
190 	NULL,
191 	sv_halt_output,
192 	sv_halt_input,
193 	NULL,
194 	sv_getdev,
195 	NULL,
196 	sv_mixer_set_port,
197 	sv_mixer_get_port,
198 	sv_query_devinfo,
199 	sv_malloc,
200 	sv_free,
201 	sv_round_buffersize,
202 	sv_mappage,
203 	sv_get_props,
204 	sv_trigger_output,
205 	sv_trigger_input,
206 	NULL,
207 };
208 
209 #define SV_NFORMATS	4
210 static const struct audio_format sv_formats[SV_NFORMATS] = {
211 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
212 	 2, AUFMT_STEREO, 0, {2000, 48000}},
213 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
214 	 1, AUFMT_MONAURAL, 0, {2000, 48000}},
215 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
216 	 2, AUFMT_STEREO, 0, {2000, 48000}},
217 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
218 	 1, AUFMT_MONAURAL, 0, {2000, 48000}},
219 };
220 
221 
222 static void
223 sv_write(struct sv_softc *sc, uint8_t reg, uint8_t val)
224 {
225 
226 	DPRINTFN(8,("sv_write(0x%x, 0x%x)\n", reg, val));
227 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val);
228 }
229 
230 static uint8_t
231 sv_read(struct sv_softc *sc, uint8_t reg)
232 {
233 	uint8_t val;
234 
235 	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, reg);
236 	DPRINTFN(8,("sv_read(0x%x) = 0x%x\n", reg, val));
237 	return val;
238 }
239 
240 static uint8_t
241 sv_read_indirect(struct sv_softc *sc, uint8_t reg)
242 {
243 	uint8_t val;
244 	int s;
245 
246 	s = splaudio();
247 	sv_write(sc, SV_CODEC_IADDR, reg & SV_IADDR_MASK);
248 	val = sv_read(sc, SV_CODEC_IDATA);
249 	splx(s);
250 	return val;
251 }
252 
253 static void
254 sv_write_indirect(struct sv_softc *sc, uint8_t reg, uint8_t val)
255 {
256 	uint8_t iaddr;
257 	int s;
258 
259 	iaddr = reg & SV_IADDR_MASK;
260 	s = splaudio();
261 	if (reg == SV_DMA_DATA_FORMAT)
262 		iaddr |= SV_IADDR_MCE;
263 
264 	sv_write(sc, SV_CODEC_IADDR, iaddr);
265 	sv_write(sc, SV_CODEC_IDATA, val);
266 	splx(s);
267 }
268 
269 static int
270 sv_match(struct device *parent, struct cfdata *match, void *aux)
271 {
272 	struct pci_attach_args *pa;
273 
274 	pa = aux;
275 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_S3 &&
276 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_S3_SONICVIBES)
277 		return 1;
278 
279 	return 0;
280 }
281 
282 static pcireg_t pci_io_alloc_low, pci_io_alloc_high;
283 
284 static int
285 pci_alloc_io(pci_chipset_tag_t pc, pcitag_t pt, int pcioffs,
286     bus_space_tag_t iot, bus_size_t size, bus_size_t align,
287     bus_size_t bound, int flags, bus_space_handle_t *ioh)
288 {
289 	bus_addr_t addr;
290 	int error;
291 
292 	error = bus_space_alloc(iot, pci_io_alloc_low, pci_io_alloc_high,
293 				size, align, bound, flags, &addr, ioh);
294 	if (error)
295 		return error;
296 
297 	pci_conf_write(pc, pt, pcioffs, addr);
298 	return 0;
299 }
300 
301 /*
302  * Allocate IO addresses when all other configuration is done.
303  */
304 static void
305 sv_defer(struct device *self)
306 {
307 	struct sv_softc *sc;
308 	pci_chipset_tag_t pc;
309 	pcitag_t pt;
310 	pcireg_t dmaio;
311 
312 	sc = (struct sv_softc *)self;
313 	pc = sc->sc_pa.pa_pc;
314 	pt = sc->sc_pa.pa_tag;
315 	DPRINTF(("sv_defer: %p\n", sc));
316 
317 	/* XXX
318 	 * Get a reasonable default for the I/O range.
319 	 * Assume the range around SB_PORTBASE is valid on this PCI bus.
320 	 */
321 	pci_io_alloc_low = pci_conf_read(pc, pt, SV_SB_PORTBASE_SLOT);
322 	pci_io_alloc_high = pci_io_alloc_low + 0x1000;
323 
324 	if (pci_alloc_io(pc, pt, SV_DMAA_CONFIG_OFF,
325 			  sc->sc_iot, SV_DMAA_SIZE, SV_DMAA_ALIGN, 0,
326 			  0, &sc->sc_dmaa_ioh)) {
327 		printf("sv_attach: cannot allocate DMA A range\n");
328 		return;
329 	}
330 	dmaio = pci_conf_read(pc, pt, SV_DMAA_CONFIG_OFF);
331 	DPRINTF(("sv_attach: addr a dmaio=0x%lx\n", (u_long)dmaio));
332 	pci_conf_write(pc, pt, SV_DMAA_CONFIG_OFF,
333 		       dmaio | SV_DMA_CHANNEL_ENABLE | SV_DMAA_EXTENDED_ADDR);
334 
335 	if (pci_alloc_io(pc, pt, SV_DMAC_CONFIG_OFF,
336 			  sc->sc_iot, SV_DMAC_SIZE, SV_DMAC_ALIGN, 0,
337 			  0, &sc->sc_dmac_ioh)) {
338 		printf("sv_attach: cannot allocate DMA C range\n");
339 		return;
340 	}
341 	dmaio = pci_conf_read(pc, pt, SV_DMAC_CONFIG_OFF);
342 	DPRINTF(("sv_attach: addr c dmaio=0x%lx\n", (u_long)dmaio));
343 	pci_conf_write(pc, pt, SV_DMAC_CONFIG_OFF,
344 		       dmaio | SV_DMA_CHANNEL_ENABLE);
345 
346 	sc->sc_dmaset = 1;
347 }
348 
349 static void
350 sv_attach(struct device *parent, struct device *self, void *aux)
351 {
352 	struct sv_softc *sc;
353 	struct pci_attach_args *pa;
354 	pci_chipset_tag_t pc;
355 	pcitag_t pt;
356 	pci_intr_handle_t ih;
357 	pcireg_t csr;
358 	char const *intrstr;
359 	uint8_t reg;
360 	struct audio_attach_args arg;
361 
362 	sc = (struct sv_softc *)self;
363 	pa = aux;
364 	pc = pa->pa_pc;
365 	pt = pa->pa_tag;
366 	printf ("\n");
367 
368 	/* Map I/O registers */
369 	if (pci_mapreg_map(pa, SV_ENHANCED_PORTBASE_SLOT,
370 			   PCI_MAPREG_TYPE_IO, 0,
371 			   &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
372 		printf("%s: can't map enhanced i/o space\n",
373 		       sc->sc_dev.dv_xname);
374 		return;
375 	}
376 	if (pci_mapreg_map(pa, SV_FM_PORTBASE_SLOT,
377 			   PCI_MAPREG_TYPE_IO, 0,
378 			   &sc->sc_opliot, &sc->sc_oplioh, NULL, NULL)) {
379 		printf("%s: can't map FM i/o space\n", sc->sc_dev.dv_xname);
380 		return;
381 	}
382 	if (pci_mapreg_map(pa, SV_MIDI_PORTBASE_SLOT,
383 			   PCI_MAPREG_TYPE_IO, 0,
384 			   &sc->sc_midiiot, &sc->sc_midiioh, NULL, NULL)) {
385 		printf("%s: can't map MIDI i/o space\n", sc->sc_dev.dv_xname);
386 		return;
387 	}
388 	DPRINTF(("sv: IO ports: enhanced=0x%x, OPL=0x%x, MIDI=0x%x\n",
389 		 (int)sc->sc_ioh, (int)sc->sc_oplioh, (int)sc->sc_midiioh));
390 
391 #if defined(alpha)
392 	/* XXX Force allocation through the SGMAP. */
393 	sc->sc_dmatag = alphabus_dma_get_tag(pa->pa_dmat, ALPHA_BUS_ISA);
394 #elif defined(i386) && NISA > 0
395 /* XXX
396  * The SonicVibes DMA is broken and only works on 24-bit addresses.
397  * As long as bus_dmamem_alloc_range() is missing we use the ISA
398  * DMA tag on i386.
399  */
400 	sc->sc_dmatag = &isa_bus_dma_tag;
401 #else
402 	sc->sc_dmatag = pa->pa_dmat;
403 #endif
404 
405 	pci_conf_write(pc, pt, SV_DMAA_CONFIG_OFF, SV_DMAA_EXTENDED_ADDR);
406 	pci_conf_write(pc, pt, SV_DMAC_CONFIG_OFF, 0);
407 
408 	/* Enable the device. */
409 	csr = pci_conf_read(pc, pt, PCI_COMMAND_STATUS_REG);
410 	pci_conf_write(pc, pt, PCI_COMMAND_STATUS_REG,
411 		       csr | PCI_COMMAND_MASTER_ENABLE);
412 
413 	sv_write_indirect(sc, SV_ANALOG_POWER_DOWN_CONTROL, 0);
414 	sv_write_indirect(sc, SV_DIGITAL_POWER_DOWN_CONTROL, 0);
415 
416 	/* initialize codec registers */
417 	reg = sv_read(sc, SV_CODEC_CONTROL);
418 	reg |= SV_CTL_RESET;
419 	sv_write(sc, SV_CODEC_CONTROL, reg);
420 	delay(50);
421 
422 	reg = sv_read(sc, SV_CODEC_CONTROL);
423 	reg &= ~SV_CTL_RESET;
424 	reg |= SV_CTL_INTA | SV_CTL_ENHANCED;
425 
426 	/* This write clears the reset */
427 	sv_write(sc, SV_CODEC_CONTROL, reg);
428 	delay(50);
429 
430 	/* This write actually shoves the new values in */
431 	sv_write(sc, SV_CODEC_CONTROL, reg);
432 
433 	DPRINTF(("sv_attach: control=0x%x\n", sv_read(sc, SV_CODEC_CONTROL)));
434 
435 	/* Enable DMA interrupts */
436 	reg = sv_read(sc, SV_CODEC_INTMASK);
437 	reg &= ~(SV_INTMASK_DMAA | SV_INTMASK_DMAC);
438 	reg |= SV_INTMASK_UD | SV_INTMASK_SINT | SV_INTMASK_MIDI;
439 	sv_write(sc, SV_CODEC_INTMASK, reg);
440 
441 	sv_read(sc, SV_CODEC_STATUS);
442 
443 	/* Map and establish the interrupt. */
444 	if (pci_intr_map(pa, &ih)) {
445 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
446 		return;
447 	}
448 	intrstr = pci_intr_string(pc, ih);
449 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, sv_intr, sc);
450 	if (sc->sc_ih == NULL) {
451 		printf("%s: couldn't establish interrupt",
452 		       sc->sc_dev.dv_xname);
453 		if (intrstr != NULL)
454 			printf(" at %s", intrstr);
455 		printf("\n");
456 		return;
457 	}
458 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
459 	printf("%s: rev %d", sc->sc_dev.dv_xname,
460 	       sv_read_indirect(sc, SV_REVISION_LEVEL));
461 	if (sv_read(sc, SV_CODEC_CONTROL) & SV_CTL_MD1)
462 		printf(", reverb SRAM present");
463 	if (!(sv_read_indirect(sc, SV_WAVETABLE_SOURCE_SELECT) & SV_WSS_WT0))
464 		printf(", wavetable ROM present");
465 	printf("\n");
466 
467 	sv_init_mixer(sc);
468 
469 	audio_attach_mi(&sv_hw_if, sc, &sc->sc_dev);
470 
471 	arg.type = AUDIODEV_TYPE_OPL;
472 	arg.hwif = 0;
473 	arg.hdl = 0;
474 	(void)config_found(&sc->sc_dev, &arg, audioprint);
475 
476 	sc->sc_pa = *pa;	/* for deferred setup */
477 	config_defer(self, sv_defer);
478 }
479 
480 #ifdef AUDIO_DEBUG
481 void
482 sv_dumpregs(struct sv_softc *sc)
483 {
484 	int idx;
485 
486 #if 0
487 	for (idx = 0; idx < 0x50; idx += 4)
488 		printf ("%02x = %x\n", idx,
489 			pci_conf_read(pa->pa_pc, pa->pa_tag, idx));
490 #endif
491 
492 	for (idx = 0; idx < 6; idx++)
493 		printf ("REG %02x = %02x\n", idx, sv_read(sc, idx));
494 
495 	for (idx = 0; idx < 0x32; idx++)
496 		printf ("IREG %02x = %02x\n", idx, sv_read_indirect(sc, idx));
497 
498 	for (idx = 0; idx < 0x10; idx++)
499 		printf ("DMA %02x = %02x\n", idx,
500 			bus_space_read_1(sc->sc_iot, sc->sc_dmaa_ioh, idx));
501 }
502 #endif
503 
504 static int
505 sv_intr(void *p)
506 {
507 	struct sv_softc *sc;
508 	uint8_t intr;
509 
510 	sc = p;
511 	intr = sv_read(sc, SV_CODEC_STATUS);
512 	DPRINTFN(5,("sv_intr: intr=0x%x\n", intr));
513 
514 	if (!(intr & (SV_INTSTATUS_DMAA | SV_INTSTATUS_DMAC)))
515 		return 0;
516 
517 	if (intr & SV_INTSTATUS_DMAA) {
518 		if (sc->sc_pintr)
519 			sc->sc_pintr(sc->sc_parg);
520 	}
521 
522 	if (intr & SV_INTSTATUS_DMAC) {
523 		if (sc->sc_rintr)
524 			sc->sc_rintr(sc->sc_rarg);
525 	}
526 
527 	return 1;
528 }
529 
530 static int
531 sv_allocmem(struct sv_softc *sc, size_t size, size_t align,
532     int direction, struct sv_dma *p)
533 {
534 	int error;
535 
536 	p->size = size;
537 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
538 	    p->segs, ARRAY_SIZE(p->segs), &p->nsegs, BUS_DMA_NOWAIT);
539 	if (error)
540 		return error;
541 
542 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
543 	    &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
544 	if (error)
545 		goto free;
546 
547 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
548 	    0, BUS_DMA_NOWAIT, &p->map);
549 	if (error)
550 		goto unmap;
551 
552 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
553 	    BUS_DMA_NOWAIT | (direction == AUMODE_RECORD) ? BUS_DMA_READ : BUS_DMA_WRITE);
554 	if (error)
555 		goto destroy;
556 	DPRINTF(("sv_allocmem: pa=%lx va=%lx pba=%lx\n",
557 	    (long)p->segs[0].ds_addr, (long)KERNADDR(p), (long)DMAADDR(p)));
558 	return 0;
559 
560 destroy:
561 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
562 unmap:
563 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
564 free:
565 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
566 	return error;
567 }
568 
569 static int
570 sv_freemem(struct sv_softc *sc, struct sv_dma *p)
571 {
572 
573 	bus_dmamap_unload(sc->sc_dmatag, p->map);
574 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
575 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
576 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
577 	return 0;
578 }
579 
580 static int
581 sv_open(void *addr, int flags)
582 {
583 	struct sv_softc *sc;
584 
585 	sc = addr;
586 	DPRINTF(("sv_open\n"));
587 	if (!sc->sc_dmaset)
588 		return ENXIO;
589 
590 	return 0;
591 }
592 
593 static int
594 sv_query_encoding(void *addr, struct audio_encoding *fp)
595 {
596 
597 	switch (fp->index) {
598 	case 0:
599 		strcpy(fp->name, AudioEulinear);
600 		fp->encoding = AUDIO_ENCODING_ULINEAR;
601 		fp->precision = 8;
602 		fp->flags = 0;
603 		return 0;
604 	case 1:
605 		strcpy(fp->name, AudioEmulaw);
606 		fp->encoding = AUDIO_ENCODING_ULAW;
607 		fp->precision = 8;
608 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
609 		return 0;
610 	case 2:
611 		strcpy(fp->name, AudioEalaw);
612 		fp->encoding = AUDIO_ENCODING_ALAW;
613 		fp->precision = 8;
614 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
615 		return 0;
616 	case 3:
617 		strcpy(fp->name, AudioEslinear);
618 		fp->encoding = AUDIO_ENCODING_SLINEAR;
619 		fp->precision = 8;
620 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
621 		return 0;
622 	case 4:
623 		strcpy(fp->name, AudioEslinear_le);
624 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
625 		fp->precision = 16;
626 		fp->flags = 0;
627 		return 0;
628 	case 5:
629 		strcpy(fp->name, AudioEulinear_le);
630 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
631 		fp->precision = 16;
632 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
633 		return 0;
634 	case 6:
635 		strcpy(fp->name, AudioEslinear_be);
636 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
637 		fp->precision = 16;
638 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
639 		return 0;
640 	case 7:
641 		strcpy(fp->name, AudioEulinear_be);
642 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
643 		fp->precision = 16;
644 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
645 		return 0;
646 	default:
647 		return EINVAL;
648 	}
649 }
650 
651 static int
652 sv_set_params(void *addr, int setmode, int usemode, audio_params_t *play,
653     audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
654 {
655 	struct sv_softc *sc;
656 	audio_params_t *p;
657 	uint32_t val;
658 
659 	sc = addr;
660 	p = NULL;
661 	/*
662 	 * This device only has one clock, so make the sample rates match.
663 	 */
664 	if (play->sample_rate != rec->sample_rate &&
665 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
666 		if (setmode == AUMODE_PLAY) {
667 			rec->sample_rate = play->sample_rate;
668 			setmode |= AUMODE_RECORD;
669 		} else if (setmode == AUMODE_RECORD) {
670 			play->sample_rate = rec->sample_rate;
671 			setmode |= AUMODE_PLAY;
672 		} else
673 			return EINVAL;
674 	}
675 
676 	if (setmode & AUMODE_RECORD) {
677 		p = rec;
678 		if (auconv_set_converter(sv_formats, SV_NFORMATS,
679 					 AUMODE_RECORD, rec, FALSE, rfil) < 0)
680 			return EINVAL;
681 	}
682 	if (setmode & AUMODE_PLAY) {
683 		p = play;
684 		if (auconv_set_converter(sv_formats, SV_NFORMATS,
685 					 AUMODE_PLAY, play, FALSE, pfil) < 0)
686 			return EINVAL;
687 	}
688 
689 	if (p == NULL)
690 		return 0;
691 
692 	val = p->sample_rate * 65536 / 48000;
693 	/*
694 	 * If the sample rate is exactly 48KHz, the fraction would overflow the
695 	 * register, so we have to bias it.  This causes a little clock drift.
696 	 * The drift is below normal crystal tolerance (.0001%), so although
697 	 * this seems a little silly, we can pretty much ignore it.
698 	 * (I tested the output speed with values of 1-20, just to be sure this
699 	 * register isn't *supposed* to have a bias.  It isn't.)
700 	 * - mycroft
701 	 */
702 	if (val > 65535)
703 		val = 65535;
704 
705 	sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_0, val & 0xff);
706 	sv_write_indirect(sc, SV_PCM_SAMPLE_RATE_1, val >> 8);
707 
708 #define F_REF 24576000
709 
710 #define ABS(x) (((x) < 0) ? (-x) : (x))
711 
712 	if (setmode & AUMODE_RECORD) {
713 		/* The ADC reference frequency (f_out) is 512 * sample rate */
714 
715 		/* f_out is dervied from the 24.576MHz crystal by three values:
716 		   M & N & R. The equation is as follows:
717 
718 		   f_out = (m + 2) * f_ref / ((n + 2) * (2 ^ a))
719 
720 		   with the constraint that:
721 
722 		   80 MHz < (m + 2) / (n + 2) * f_ref <= 150MHz
723 		   and n, m >= 1
724 		*/
725 
726 		int  goal_f_out;
727 		int  a, n, m, best_n, best_m, best_error;
728 		int  pll_sample;
729 		int  error;
730 
731 		goal_f_out = 512 * rec->sample_rate;
732 		best_n = 0;
733 		best_m = 0;
734 		best_error = 10000000;
735 		for (a = 0; a < 8; a++) {
736 			if ((goal_f_out * (1 << a)) >= 80000000)
737 				break;
738 		}
739 
740 		/* a != 8 because sample_rate >= 2000 */
741 
742 		for (n = 33; n > 2; n--) {
743 			m = (goal_f_out * n * (1 << a)) / F_REF;
744 			if ((m > 257) || (m < 3))
745 				continue;
746 
747 			pll_sample = (m * F_REF) / (n * (1 << a));
748 			pll_sample /= 512;
749 
750 			/* Threshold might be good here */
751 			error = pll_sample - rec->sample_rate;
752 			error = ABS(error);
753 
754 			if (error < best_error) {
755 				best_error = error;
756 				best_n = n;
757 				best_m = m;
758 				if (error == 0) break;
759 			}
760 		}
761 
762 		best_n -= 2;
763 		best_m -= 2;
764 
765 		sv_write_indirect(sc, SV_ADC_PLL_M, best_m);
766 		sv_write_indirect(sc, SV_ADC_PLL_N,
767 				  best_n | (a << SV_PLL_R_SHIFT));
768 	}
769 
770 	return 0;
771 }
772 
773 static int
774 sv_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
775 {
776 
777 	return blk & -32;	/* keep good alignment */
778 }
779 
780 static int
781 sv_trigger_output(void *addr, void *start, void *end, int blksize,
782     void (*intr)(void *), void *arg, const audio_params_t *param)
783 {
784 	struct sv_softc *sc;
785 	struct sv_dma *p;
786 	uint8_t mode;
787 	int dma_count;
788 
789 	DPRINTFN(1, ("sv_trigger_output: sc=%p start=%p end=%p blksize=%d "
790 	    "intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
791 	sc = addr;
792 	sc->sc_pintr = intr;
793 	sc->sc_parg = arg;
794 
795 	mode = sv_read_indirect(sc, SV_DMA_DATA_FORMAT);
796 	mode &= ~(SV_DMAA_FORMAT16 | SV_DMAA_STEREO);
797 	if (param->precision == 16)
798 		mode |= SV_DMAA_FORMAT16;
799 	if (param->channels == 2)
800 		mode |= SV_DMAA_STEREO;
801 	sv_write_indirect(sc, SV_DMA_DATA_FORMAT, mode);
802 
803 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
804 		continue;
805 	if (p == NULL) {
806 		printf("sv_trigger_output: bad addr %p\n", start);
807 		return EINVAL;
808 	}
809 
810 	dma_count = ((char *)end - (char *)start) - 1;
811 	DPRINTF(("sv_trigger_output: DMA start loop input addr=%x cc=%d\n",
812 	    (int)DMAADDR(p), dma_count));
813 
814 	bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0,
815 			  DMAADDR(p));
816 	bus_space_write_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_COUNT0,
817 			  dma_count);
818 	bus_space_write_1(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_MODE,
819 			  DMA37MD_READ | DMA37MD_LOOP);
820 
821 	DPRINTF(("sv_trigger_output: current addr=%x\n",
822 	    bus_space_read_4(sc->sc_iot, sc->sc_dmaa_ioh, SV_DMA_ADDR0)));
823 
824 	dma_count = blksize - 1;
825 
826 	sv_write_indirect(sc, SV_DMAA_COUNT1, dma_count >> 8);
827 	sv_write_indirect(sc, SV_DMAA_COUNT0, dma_count & 0xFF);
828 
829 	mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
830 	sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode | SV_PLAY_ENABLE);
831 
832 	return 0;
833 }
834 
835 static int
836 sv_trigger_input(void *addr, void *start, void *end, int blksize,
837     void (*intr)(void *), void *arg, const audio_params_t *param)
838 {
839 	struct sv_softc *sc;
840 	struct sv_dma *p;
841 	uint8_t mode;
842 	int dma_count;
843 
844 	DPRINTFN(1, ("sv_trigger_input: sc=%p start=%p end=%p blksize=%d "
845 	    "intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
846 	sc = addr;
847 	sc->sc_rintr = intr;
848 	sc->sc_rarg = arg;
849 
850 	mode = sv_read_indirect(sc, SV_DMA_DATA_FORMAT);
851 	mode &= ~(SV_DMAC_FORMAT16 | SV_DMAC_STEREO);
852 	if (param->precision == 16)
853 		mode |= SV_DMAC_FORMAT16;
854 	if (param->channels == 2)
855 		mode |= SV_DMAC_STEREO;
856 	sv_write_indirect(sc, SV_DMA_DATA_FORMAT, mode);
857 
858 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
859 		continue;
860 	if (!p) {
861 		printf("sv_trigger_input: bad addr %p\n", start);
862 		return EINVAL;
863 	}
864 
865 	dma_count = (((char *)end - (char *)start) >> 1) - 1;
866 	DPRINTF(("sv_trigger_input: DMA start loop input addr=%x cc=%d\n",
867 	    (int)DMAADDR(p), dma_count));
868 
869 	bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0,
870 			  DMAADDR(p));
871 	bus_space_write_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_COUNT0,
872 			  dma_count);
873 	bus_space_write_1(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_MODE,
874 			  DMA37MD_WRITE | DMA37MD_LOOP);
875 
876 	DPRINTF(("sv_trigger_input: current addr=%x\n",
877 	    bus_space_read_4(sc->sc_iot, sc->sc_dmac_ioh, SV_DMA_ADDR0)));
878 
879 	dma_count = (blksize >> 1) - 1;
880 
881 	sv_write_indirect(sc, SV_DMAC_COUNT1, dma_count >> 8);
882 	sv_write_indirect(sc, SV_DMAC_COUNT0, dma_count & 0xFF);
883 
884 	mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
885 	sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode | SV_RECORD_ENABLE);
886 
887 	return 0;
888 }
889 
890 static int
891 sv_halt_output(void *addr)
892 {
893 	struct sv_softc *sc;
894 	uint8_t mode;
895 
896 	DPRINTF(("sv: sv_halt_output\n"));
897 	sc = addr;
898 	mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
899 	sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode & ~SV_PLAY_ENABLE);
900 	sc->sc_pintr = 0;
901 
902 	return 0;
903 }
904 
905 static int
906 sv_halt_input(void *addr)
907 {
908 	struct sv_softc *sc;
909 	uint8_t mode;
910 
911 	DPRINTF(("sv: sv_halt_input\n"));
912 	sc = addr;
913 	mode = sv_read_indirect(sc, SV_PLAY_RECORD_ENABLE);
914 	sv_write_indirect(sc, SV_PLAY_RECORD_ENABLE, mode & ~SV_RECORD_ENABLE);
915 	sc->sc_rintr = 0;
916 
917 	return 0;
918 }
919 
920 static int
921 sv_getdev(void *addr, struct audio_device *retp)
922 {
923 
924 	*retp = sv_device;
925 	return 0;
926 }
927 
928 
929 /*
930  * Mixer related code is here
931  *
932  */
933 
934 #define SV_INPUT_CLASS 0
935 #define SV_OUTPUT_CLASS 1
936 #define SV_RECORD_CLASS 2
937 
938 #define SV_LAST_CLASS 2
939 
940 static const char *mixer_classes[] =
941 	{ AudioCinputs, AudioCoutputs, AudioCrecord };
942 
943 static const struct {
944 	uint8_t   l_port;
945 	uint8_t   r_port;
946 	uint8_t   mask;
947 	uint8_t   class;
948 	const char *audio;
949 } ports[] = {
950   { SV_LEFT_AUX1_INPUT_CONTROL, SV_RIGHT_AUX1_INPUT_CONTROL, SV_AUX1_MASK,
951     SV_INPUT_CLASS, "aux1" },
952   { SV_LEFT_CD_INPUT_CONTROL, SV_RIGHT_CD_INPUT_CONTROL, SV_CD_MASK,
953     SV_INPUT_CLASS, AudioNcd },
954   { SV_LEFT_LINE_IN_INPUT_CONTROL, SV_RIGHT_LINE_IN_INPUT_CONTROL, SV_LINE_IN_MASK,
955     SV_INPUT_CLASS, AudioNline },
956   { SV_MIC_INPUT_CONTROL, 0, SV_MIC_MASK, SV_INPUT_CLASS, AudioNmicrophone },
957   { SV_LEFT_SYNTH_INPUT_CONTROL, SV_RIGHT_SYNTH_INPUT_CONTROL,
958     SV_SYNTH_MASK, SV_INPUT_CLASS, AudioNfmsynth },
959   { SV_LEFT_AUX2_INPUT_CONTROL, SV_RIGHT_AUX2_INPUT_CONTROL, SV_AUX2_MASK,
960     SV_INPUT_CLASS, "aux2" },
961   { SV_LEFT_PCM_INPUT_CONTROL, SV_RIGHT_PCM_INPUT_CONTROL, SV_PCM_MASK,
962     SV_INPUT_CLASS, AudioNdac },
963   { SV_LEFT_MIXER_OUTPUT_CONTROL, SV_RIGHT_MIXER_OUTPUT_CONTROL,
964     SV_MIXER_OUT_MASK, SV_OUTPUT_CLASS, AudioNmaster }
965 };
966 
967 
968 static const struct {
969 	int idx;
970 	const char *name;
971 } record_sources[] = {
972 	{ SV_REC_CD, AudioNcd },
973 	{ SV_REC_DAC, AudioNdac },
974 	{ SV_REC_AUX2, "aux2" },
975 	{ SV_REC_LINE, AudioNline },
976 	{ SV_REC_AUX1, "aux1" },
977 	{ SV_REC_MIC, AudioNmicrophone },
978 	{ SV_REC_MIXER, AudioNmixerout }
979 };
980 
981 
982 #define SV_DEVICES_PER_PORT 2
983 #define SV_FIRST_MIXER (SV_LAST_CLASS + 1)
984 #define SV_LAST_MIXER (SV_DEVICES_PER_PORT * (ARRAY_SIZE(ports)) + SV_LAST_CLASS)
985 #define SV_RECORD_SOURCE (SV_LAST_MIXER + 1)
986 #define SV_MIC_BOOST (SV_LAST_MIXER + 2)
987 #define SV_RECORD_GAIN (SV_LAST_MIXER + 3)
988 #define SV_SRS_MODE (SV_LAST_MIXER + 4)
989 
990 static int
991 sv_query_devinfo(void *addr, mixer_devinfo_t *dip)
992 {
993 	int i;
994 
995 	/* It's a class */
996 	if (dip->index <= SV_LAST_CLASS) {
997 		dip->type = AUDIO_MIXER_CLASS;
998 		dip->mixer_class = dip->index;
999 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1000 		strcpy(dip->label.name, mixer_classes[dip->index]);
1001 		return 0;
1002 	}
1003 
1004 	if (dip->index >= SV_FIRST_MIXER &&
1005 	    dip->index <= SV_LAST_MIXER) {
1006 		int off, mute ,idx;
1007 
1008 		off = dip->index - SV_FIRST_MIXER;
1009 		mute = (off % SV_DEVICES_PER_PORT);
1010 		idx = off / SV_DEVICES_PER_PORT;
1011 		dip->mixer_class = ports[idx].class;
1012 		strcpy(dip->label.name, ports[idx].audio);
1013 
1014 		if (!mute) {
1015 			dip->type = AUDIO_MIXER_VALUE;
1016 			dip->prev = AUDIO_MIXER_LAST;
1017 			dip->next = dip->index + 1;
1018 
1019 			if (ports[idx].r_port != 0)
1020 				dip->un.v.num_channels = 2;
1021 			else
1022 				dip->un.v.num_channels = 1;
1023 
1024 			strcpy(dip->un.v.units.name, AudioNvolume);
1025 		} else {
1026 			dip->type = AUDIO_MIXER_ENUM;
1027 			dip->prev = dip->index - 1;
1028 			dip->next = AUDIO_MIXER_LAST;
1029 
1030 			strcpy(dip->label.name, AudioNmute);
1031 			dip->un.e.num_mem = 2;
1032 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
1033 			dip->un.e.member[0].ord = 0;
1034 			strcpy(dip->un.e.member[1].label.name, AudioNon);
1035 			dip->un.e.member[1].ord = 1;
1036 		}
1037 
1038 		return 0;
1039 	}
1040 
1041 	switch (dip->index) {
1042 	case SV_RECORD_SOURCE:
1043 		dip->mixer_class = SV_RECORD_CLASS;
1044 		dip->prev = AUDIO_MIXER_LAST;
1045 		dip->next = SV_RECORD_GAIN;
1046 		strcpy(dip->label.name, AudioNsource);
1047 		dip->type = AUDIO_MIXER_ENUM;
1048 
1049 		dip->un.e.num_mem = ARRAY_SIZE(record_sources);
1050 		for (i = 0; i < ARRAY_SIZE(record_sources); i++) {
1051 			strcpy(dip->un.e.member[i].label.name,
1052 			       record_sources[i].name);
1053 			dip->un.e.member[i].ord = record_sources[i].idx;
1054 		}
1055 		return 0;
1056 
1057 	case SV_RECORD_GAIN:
1058 		dip->mixer_class = SV_RECORD_CLASS;
1059 		dip->prev = SV_RECORD_SOURCE;
1060 		dip->next = AUDIO_MIXER_LAST;
1061 		strcpy(dip->label.name, "gain");
1062 		dip->type = AUDIO_MIXER_VALUE;
1063 		dip->un.v.num_channels = 1;
1064 		strcpy(dip->un.v.units.name, AudioNvolume);
1065 		return 0;
1066 
1067 	case SV_MIC_BOOST:
1068 		dip->mixer_class = SV_RECORD_CLASS;
1069 		dip->prev = AUDIO_MIXER_LAST;
1070 		dip->next = AUDIO_MIXER_LAST;
1071 		strcpy(dip->label.name, "micboost");
1072 		goto on_off;
1073 
1074 	case SV_SRS_MODE:
1075 		dip->mixer_class = SV_OUTPUT_CLASS;
1076 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1077 		strcpy(dip->label.name, AudioNspatial);
1078 
1079 	on_off:
1080 		dip->type = AUDIO_MIXER_ENUM;
1081 		dip->un.e.num_mem = 2;
1082 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
1083 		dip->un.e.member[0].ord = 0;
1084 		strcpy(dip->un.e.member[1].label.name, AudioNon);
1085 		dip->un.e.member[1].ord = 1;
1086 		return 0;
1087 	}
1088 
1089 	return ENXIO;
1090 }
1091 
1092 static int
1093 sv_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1094 {
1095 	struct sv_softc *sc;
1096 	uint8_t reg;
1097 	int idx;
1098 
1099 	sc = addr;
1100 	if (cp->dev >= SV_FIRST_MIXER &&
1101 	    cp->dev <= SV_LAST_MIXER) {
1102 		int off, mute;
1103 
1104 		off = cp->dev - SV_FIRST_MIXER;
1105 		mute = (off % SV_DEVICES_PER_PORT);
1106 		idx = off / SV_DEVICES_PER_PORT;
1107 
1108 		if (mute) {
1109 			if (cp->type != AUDIO_MIXER_ENUM)
1110 				return EINVAL;
1111 
1112 			reg = sv_read_indirect(sc, ports[idx].l_port);
1113 			if (cp->un.ord)
1114 				reg |= SV_MUTE_BIT;
1115 			else
1116 				reg &= ~SV_MUTE_BIT;
1117 			sv_write_indirect(sc, ports[idx].l_port, reg);
1118 
1119 			if (ports[idx].r_port) {
1120 				reg = sv_read_indirect(sc, ports[idx].r_port);
1121 				if (cp->un.ord)
1122 					reg |= SV_MUTE_BIT;
1123 				else
1124 					reg &= ~SV_MUTE_BIT;
1125 				sv_write_indirect(sc, ports[idx].r_port, reg);
1126 			}
1127 		} else {
1128 			int  lval, rval;
1129 
1130 			if (cp->type != AUDIO_MIXER_VALUE)
1131 				return EINVAL;
1132 
1133 			if (cp->un.value.num_channels != 1 &&
1134 			    cp->un.value.num_channels != 2)
1135 				return (EINVAL);
1136 
1137 			if (ports[idx].r_port == 0) {
1138 				if (cp->un.value.num_channels != 1)
1139 					return (EINVAL);
1140 				lval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1141 				rval = 0; /* shut up GCC */
1142 			} else {
1143 				if (cp->un.value.num_channels != 2)
1144 					return (EINVAL);
1145 
1146 				lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1147 				rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1148 			}
1149 
1150 
1151 			reg = sv_read_indirect(sc, ports[idx].l_port);
1152 			reg &= ~(ports[idx].mask);
1153 			lval = (AUDIO_MAX_GAIN - lval) * ports[idx].mask /
1154 				AUDIO_MAX_GAIN;
1155 			reg |= lval;
1156 			sv_write_indirect(sc, ports[idx].l_port, reg);
1157 
1158 			if (ports[idx].r_port != 0) {
1159 				reg = sv_read_indirect(sc, ports[idx].r_port);
1160 				reg &= ~(ports[idx].mask);
1161 
1162 				rval = (AUDIO_MAX_GAIN - rval) * ports[idx].mask /
1163 					AUDIO_MAX_GAIN;
1164 				reg |= rval;
1165 
1166 				sv_write_indirect(sc, ports[idx].r_port, reg);
1167 			}
1168 
1169 			sv_read_indirect(sc, ports[idx].l_port);
1170 		}
1171 
1172 		return 0;
1173 	}
1174 
1175 
1176 	switch (cp->dev) {
1177 	case SV_RECORD_SOURCE:
1178 		if (cp->type != AUDIO_MIXER_ENUM)
1179 			return EINVAL;
1180 
1181 		for (idx = 0; idx < ARRAY_SIZE(record_sources); idx++) {
1182 			if (record_sources[idx].idx == cp->un.ord)
1183 				goto found;
1184 		}
1185 
1186 		return EINVAL;
1187 
1188 	found:
1189 		reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1190 		reg &= ~SV_REC_SOURCE_MASK;
1191 		reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK);
1192 		sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1193 
1194 		reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL);
1195 		reg &= ~SV_REC_SOURCE_MASK;
1196 		reg |= (((cp->un.ord) << SV_REC_SOURCE_SHIFT) & SV_REC_SOURCE_MASK);
1197 		sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg);
1198 		return 0;
1199 
1200 	case SV_RECORD_GAIN:
1201 	{
1202 		int val;
1203 
1204 		if (cp->type != AUDIO_MIXER_VALUE)
1205 			return EINVAL;
1206 
1207 		if (cp->un.value.num_channels != 1)
1208 			return EINVAL;
1209 
1210 		val = (cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]
1211 		    * SV_REC_GAIN_MASK) / AUDIO_MAX_GAIN;
1212 
1213 		reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1214 		reg &= ~SV_REC_GAIN_MASK;
1215 		reg |= val;
1216 		sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1217 
1218 		reg = sv_read_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL);
1219 		reg &= ~SV_REC_GAIN_MASK;
1220 		reg |= val;
1221 		sv_write_indirect(sc, SV_RIGHT_ADC_INPUT_CONTROL, reg);
1222 	}
1223 	return (0);
1224 
1225 	case SV_MIC_BOOST:
1226 		if (cp->type != AUDIO_MIXER_ENUM)
1227 			return EINVAL;
1228 
1229 		reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1230 		if (cp->un.ord) {
1231 			reg |= SV_MIC_BOOST_BIT;
1232 		} else {
1233 			reg &= ~SV_MIC_BOOST_BIT;
1234 		}
1235 
1236 		sv_write_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL, reg);
1237 		return 0;
1238 
1239 	case SV_SRS_MODE:
1240 		if (cp->type != AUDIO_MIXER_ENUM)
1241 			return EINVAL;
1242 
1243 		reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL);
1244 		if (cp->un.ord) {
1245 			reg &= ~SV_SRS_SPACE_ONOFF;
1246 		} else {
1247 			reg |= SV_SRS_SPACE_ONOFF;
1248 		}
1249 
1250 		sv_write_indirect(sc, SV_SRS_SPACE_CONTROL, reg);
1251 		return 0;
1252 	}
1253 
1254 	return EINVAL;
1255 }
1256 
1257 static int
1258 sv_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1259 {
1260 	struct sv_softc *sc;
1261 	int val;
1262 	uint8_t reg;
1263 
1264 	sc = addr;
1265 	if (cp->dev >= SV_FIRST_MIXER &&
1266 	    cp->dev <= SV_LAST_MIXER) {
1267 		int off = cp->dev - SV_FIRST_MIXER;
1268 		int mute = (off % 2);
1269 		int idx = off / 2;
1270 
1271 		off = cp->dev - SV_FIRST_MIXER;
1272 		mute = (off % 2);
1273 		idx = off / 2;
1274 		if (mute) {
1275 			if (cp->type != AUDIO_MIXER_ENUM)
1276 				return EINVAL;
1277 
1278 			reg = sv_read_indirect(sc, ports[idx].l_port);
1279 			cp->un.ord = ((reg & SV_MUTE_BIT) ? 1 : 0);
1280 		} else {
1281 			if (cp->type != AUDIO_MIXER_VALUE)
1282 				return EINVAL;
1283 
1284 			if (cp->un.value.num_channels != 1 &&
1285 			    cp->un.value.num_channels != 2)
1286 				return EINVAL;
1287 
1288 			if ((ports[idx].r_port == 0 &&
1289 			     cp->un.value.num_channels != 1) ||
1290 			    (ports[idx].r_port != 0 &&
1291 			     cp->un.value.num_channels != 2))
1292 				return EINVAL;
1293 
1294 			reg = sv_read_indirect(sc, ports[idx].l_port);
1295 			reg &= ports[idx].mask;
1296 
1297 			val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN) / ports[idx].mask);
1298 
1299 			if (ports[idx].r_port != 0) {
1300 				cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = val;
1301 
1302 				reg = sv_read_indirect(sc, ports[idx].r_port);
1303 				reg &= ports[idx].mask;
1304 
1305 				val = AUDIO_MAX_GAIN - ((reg * AUDIO_MAX_GAIN)
1306 				    / ports[idx].mask);
1307 				cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = val;
1308 			} else
1309 				cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = val;
1310 		}
1311 
1312 		return 0;
1313 	}
1314 
1315 	switch (cp->dev) {
1316 	case SV_RECORD_SOURCE:
1317 		if (cp->type != AUDIO_MIXER_ENUM)
1318 			return EINVAL;
1319 
1320 		reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1321 		cp->un.ord = ((reg & SV_REC_SOURCE_MASK) >> SV_REC_SOURCE_SHIFT);
1322 
1323 		return 0;
1324 
1325 	case SV_RECORD_GAIN:
1326 		if (cp->type != AUDIO_MIXER_VALUE)
1327 			return EINVAL;
1328 		if (cp->un.value.num_channels != 1)
1329 			return EINVAL;
1330 
1331 		reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL) & SV_REC_GAIN_MASK;
1332 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1333 			(((unsigned int)reg) * AUDIO_MAX_GAIN) / SV_REC_GAIN_MASK;
1334 
1335 		return 0;
1336 
1337 	case SV_MIC_BOOST:
1338 		if (cp->type != AUDIO_MIXER_ENUM)
1339 			return EINVAL;
1340 		reg = sv_read_indirect(sc, SV_LEFT_ADC_INPUT_CONTROL);
1341 		cp->un.ord = ((reg & SV_MIC_BOOST_BIT) ? 1 : 0);
1342 		return 0;
1343 
1344 	case SV_SRS_MODE:
1345 		if (cp->type != AUDIO_MIXER_ENUM)
1346 			return EINVAL;
1347 		reg = sv_read_indirect(sc, SV_SRS_SPACE_CONTROL);
1348 		cp->un.ord = ((reg & SV_SRS_SPACE_ONOFF) ? 0 : 1);
1349 		return 0;
1350 	}
1351 
1352 	return EINVAL;
1353 }
1354 
1355 static void
1356 sv_init_mixer(struct sv_softc *sc)
1357 {
1358 	mixer_ctrl_t cp;
1359 	int i;
1360 
1361 	cp.type = AUDIO_MIXER_ENUM;
1362 	cp.dev = SV_SRS_MODE;
1363 	cp.un.ord = 0;
1364 
1365 	sv_mixer_set_port(sc, &cp);
1366 
1367 	for (i = 0; i < ARRAY_SIZE(ports); i++) {
1368 		if (ports[i].audio == AudioNdac) {
1369 			cp.type = AUDIO_MIXER_ENUM;
1370 			cp.dev = SV_FIRST_MIXER + i * SV_DEVICES_PER_PORT + 1;
1371 			cp.un.ord = 0;
1372 			sv_mixer_set_port(sc, &cp);
1373 			break;
1374 		}
1375 	}
1376 }
1377 
1378 static void *
1379 sv_malloc(void *addr, int direction, size_t size,
1380     struct malloc_type *pool, int flags)
1381 {
1382 	struct sv_softc *sc;
1383 	struct sv_dma *p;
1384 	int error;
1385 
1386 	sc = addr;
1387 	p = malloc(sizeof(*p), pool, flags);
1388 	if (p == NULL)
1389 		return NULL;
1390 	error = sv_allocmem(sc, size, 16, direction, p);
1391 	if (error) {
1392 		free(p, pool);
1393 		return 0;
1394 	}
1395 	p->next = sc->sc_dmas;
1396 	sc->sc_dmas = p;
1397 	return KERNADDR(p);
1398 }
1399 
1400 static void
1401 sv_free(void *addr, void *ptr, struct malloc_type *pool)
1402 {
1403 	struct sv_softc *sc;
1404 	struct sv_dma **pp, *p;
1405 
1406 	sc = addr;
1407 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1408 		if (KERNADDR(p) == ptr) {
1409 			sv_freemem(sc, p);
1410 			*pp = p->next;
1411 			free(p, pool);
1412 			return;
1413 		}
1414 	}
1415 }
1416 
1417 static size_t
1418 sv_round_buffersize(void *addr, int direction, size_t size)
1419 {
1420 
1421 	return size;
1422 }
1423 
1424 static paddr_t
1425 sv_mappage(void *addr, void *mem, off_t off, int prot)
1426 {
1427 	struct sv_softc *sc;
1428 	struct sv_dma *p;
1429 
1430 	sc = addr;
1431 	if (off < 0)
1432 		return -1;
1433 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1434 		continue;
1435 	if (p == NULL)
1436 		return -1;
1437 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1438 			       off, prot, BUS_DMA_WAITOK);
1439 }
1440 
1441 static int
1442 sv_get_props(void *addr)
1443 {
1444 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
1445 }
1446