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