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