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