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