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