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