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