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