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