Home | History | Annotate | Line # | Download | only in pci
auvia.c revision 1.26
      1  1.25      kent /*	$NetBSD: auvia.c,v 1.26 2002/10/08 13:10:24 kent Exp $	*/
      2   1.1    tsarna 
      3   1.1    tsarna /*-
      4   1.1    tsarna  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5   1.1    tsarna  * All rights reserved.
      6   1.1    tsarna  *
      7   1.1    tsarna  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1    tsarna  * by Tyler C. Sarna
      9   1.1    tsarna  *
     10   1.1    tsarna  * Redistribution and use in source and binary forms, with or without
     11   1.1    tsarna  * modification, are permitted provided that the following conditions
     12   1.1    tsarna  * are met:
     13   1.1    tsarna  * 1. Redistributions of source code must retain the above copyright
     14   1.1    tsarna  *    notice, this list of conditions and the following disclaimer.
     15   1.1    tsarna  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    tsarna  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    tsarna  *    documentation and/or other materials provided with the distribution.
     18   1.1    tsarna  * 3. All advertising materials mentioning features or use of this software
     19   1.1    tsarna  *    must display the following acknowledgement:
     20   1.1    tsarna  *	This product includes software developed by the NetBSD
     21   1.1    tsarna  *	Foundation, Inc. and its contributors.
     22   1.1    tsarna  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1    tsarna  *    contributors may be used to endorse or promote products derived
     24   1.1    tsarna  *    from this software without specific prior written permission.
     25   1.1    tsarna  *
     26   1.1    tsarna  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1    tsarna  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1    tsarna  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1    tsarna  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1    tsarna  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1    tsarna  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1    tsarna  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1    tsarna  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1    tsarna  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1    tsarna  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1    tsarna  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1    tsarna  */
     38   1.1    tsarna 
     39   1.1    tsarna /*
     40  1.22      kent  * VIA Technologies VT82C686A / VT8233 / VT8235 Southbridge Audio Driver
     41   1.1    tsarna  *
     42   1.1    tsarna  * Documentation links:
     43   1.1    tsarna  *
     44   1.1    tsarna  * ftp://ftp.alsa-project.org/pub/manuals/via/686a.pdf
     45   1.1    tsarna  * ftp://ftp.alsa-project.org/pub/manuals/general/ac97r21.pdf
     46   1.1    tsarna  * ftp://ftp.alsa-project.org/pub/manuals/ad/AD1881_0.pdf (example AC'97 codec)
     47   1.1    tsarna  */
     48  1.14     lukem 
     49  1.14     lukem #include <sys/cdefs.h>
     50  1.25      kent __KERNEL_RCSID(0, "$NetBSD: auvia.c,v 1.26 2002/10/08 13:10:24 kent Exp $");
     51   1.1    tsarna 
     52   1.1    tsarna #include <sys/param.h>
     53   1.1    tsarna #include <sys/systm.h>
     54   1.1    tsarna #include <sys/malloc.h>
     55   1.1    tsarna #include <sys/device.h>
     56   1.1    tsarna #include <sys/audioio.h>
     57   1.1    tsarna 
     58   1.6   thorpej #include <uvm/uvm_extern.h>
     59   1.6   thorpej 
     60   1.1    tsarna #include <dev/pci/pcidevs.h>
     61   1.1    tsarna #include <dev/pci/pcivar.h>
     62   1.1    tsarna 
     63   1.1    tsarna #include <dev/audio_if.h>
     64   1.1    tsarna #include <dev/mulaw.h>
     65   1.1    tsarna #include <dev/auconv.h>
     66   1.1    tsarna 
     67  1.10  augustss #include <dev/ic/ac97reg.h>
     68   1.3   thorpej #include <dev/ic/ac97var.h>
     69   1.1    tsarna 
     70   1.1    tsarna #include <dev/pci/auviavar.h>
     71   1.1    tsarna 
     72   1.1    tsarna struct auvia_dma {
     73   1.1    tsarna 	struct auvia_dma *next;
     74   1.1    tsarna 	caddr_t addr;
     75   1.1    tsarna 	size_t size;
     76   1.1    tsarna 	bus_dmamap_t map;
     77   1.1    tsarna 	bus_dma_segment_t seg;
     78   1.1    tsarna };
     79   1.1    tsarna 
     80   1.1    tsarna struct auvia_dma_op {
     81   1.1    tsarna 	u_int32_t ptr;
     82   1.1    tsarna 	u_int32_t flags;
     83   1.1    tsarna #define AUVIA_DMAOP_EOL		0x80000000
     84   1.1    tsarna #define AUVIA_DMAOP_FLAG	0x40000000
     85   1.1    tsarna #define AUVIA_DMAOP_STOP	0x20000000
     86   1.1    tsarna #define AUVIA_DMAOP_COUNT(x)	((x)&0x00FFFFFF)
     87   1.1    tsarna };
     88   1.1    tsarna 
     89   1.1    tsarna int	auvia_match(struct device *, struct cfdata *, void *);
     90   1.1    tsarna void	auvia_attach(struct device *, struct device *, void *);
     91   1.1    tsarna int	auvia_open(void *, int);
     92   1.1    tsarna void	auvia_close(void *);
     93   1.1    tsarna int	auvia_query_encoding(void *addr, struct audio_encoding *fp);
     94   1.1    tsarna int	auvia_set_params(void *, int, int, struct audio_params *,
     95   1.1    tsarna 	struct audio_params *);
     96   1.1    tsarna int	auvia_round_blocksize(void *, int);
     97   1.1    tsarna int	auvia_halt_output(void *);
     98   1.1    tsarna int	auvia_halt_input(void *);
     99   1.1    tsarna int	auvia_getdev(void *, struct audio_device *);
    100   1.1    tsarna int	auvia_set_port(void *, mixer_ctrl_t *);
    101   1.1    tsarna int	auvia_get_port(void *, mixer_ctrl_t *);
    102   1.1    tsarna int	auvia_query_devinfo(void *, mixer_devinfo_t *);
    103   1.1    tsarna void *	auvia_malloc(void *, int, size_t, int, int);
    104   1.1    tsarna void	auvia_free(void *, void *, int);
    105   1.1    tsarna size_t	auvia_round_buffersize(void *, int, size_t);
    106   1.4    simonb paddr_t	auvia_mappage(void *, void *, off_t, int);
    107   1.1    tsarna int	auvia_get_props(void *);
    108   1.1    tsarna int	auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
    109   1.1    tsarna 	struct auvia_dma *, void *, void *, int);
    110   1.1    tsarna int	auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
    111   1.1    tsarna 	void *, struct audio_params *);
    112   1.1    tsarna int	auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
    113   1.1    tsarna 	void *, struct audio_params *);
    114   1.1    tsarna 
    115   1.1    tsarna int	auvia_intr __P((void *));
    116   1.1    tsarna 
    117  1.20   thorpej CFATTACH_DECL(auvia, sizeof (struct auvia_softc),
    118  1.21   thorpej     auvia_match, auvia_attach, NULL, NULL);
    119   1.1    tsarna 
    120   1.1    tsarna #define AUVIA_PCICONF_JUNK	0x40
    121   1.1    tsarna #define		AUVIA_PCICONF_ENABLES	 0x00FF0000	/* reg 42 mask */
    122   1.1    tsarna #define		AUVIA_PCICONF_ACLINKENAB 0x00008000	/* ac link enab */
    123   1.1    tsarna #define		AUVIA_PCICONF_ACNOTRST	 0x00004000	/* ~(ac reset) */
    124   1.1    tsarna #define		AUVIA_PCICONF_ACSYNC	 0x00002000	/* ac sync */
    125   1.1    tsarna #define		AUVIA_PCICONF_ACVSR	 0x00000800	/* var. samp. rate */
    126   1.1    tsarna #define		AUVIA_PCICONF_ACSGD	 0x00000400	/* SGD enab */
    127   1.1    tsarna #define		AUVIA_PCICONF_ACFM	 0x00000200	/* FM enab */
    128   1.1    tsarna #define		AUVIA_PCICONF_ACSB	 0x00000100	/* SB enab */
    129   1.1    tsarna 
    130  1.24      kent #define	AUVIA_PLAY_BASE			0x00
    131  1.24      kent #define	AUVIA_RECORD_BASE		0x10
    132  1.22      kent 
    133  1.24      kent #define	AUVIA_RP_STAT			0x00
    134   1.1    tsarna #define		AUVIA_RPSTAT_INTR		0x03
    135  1.24      kent #define	AUVIA_RP_CONTROL		0x01
    136   1.1    tsarna #define		AUVIA_RPCTRL_START		0x80
    137   1.1    tsarna #define		AUVIA_RPCTRL_TERMINATE		0x40
    138  1.22      kent #define		AUVIA_RPCTRL_AUTOSTART		0x20
    139  1.22      kent /* The following are 8233 specific */
    140  1.22      kent #define		AUVIA_RPCTRL_STOP		0x04
    141  1.22      kent #define		AUVIA_RPCTRL_EOL		0x02
    142  1.22      kent #define		AUVIA_RPCTRL_FLAG		0x01
    143  1.24      kent #define	AUVIA_RP_MODE			0x02
    144   1.1    tsarna #define		AUVIA_RPMODE_INTR_FLAG		0x01
    145   1.1    tsarna #define		AUVIA_RPMODE_INTR_EOL		0x02
    146   1.1    tsarna #define		AUVIA_RPMODE_STEREO		0x10
    147   1.1    tsarna #define		AUVIA_RPMODE_16BIT		0x20
    148   1.1    tsarna #define		AUVIA_RPMODE_AUTOSTART		0x80
    149  1.24      kent #define	AUVIA_RP_DMAOPS_BASE		0x04
    150  1.22      kent 
    151  1.24      kent #define	VIA8233_RP_DXS_LVOL		0x02
    152  1.24      kent #define	VIA8233_RP_DXS_RVOL		0x03
    153  1.24      kent #define	VIA8233_RP_RATEFMT		0x08
    154  1.22      kent #define		VIA8233_RATEFMT_48K		0xfffff
    155  1.22      kent #define		VIA8233_RATEFMT_STEREO		0x00100000
    156  1.22      kent #define		VIA8233_RATEFMT_16BIT		0x00200000
    157  1.22      kent 
    158  1.24      kent #define	VIA_RP_DMAOPS_COUNT		0x0c
    159   1.1    tsarna 
    160   1.1    tsarna #define	AUVIA_CODEC_CTL			0x80
    161   1.1    tsarna #define		AUVIA_CODEC_READ		0x00800000
    162   1.1    tsarna #define		AUVIA_CODEC_BUSY		0x01000000
    163   1.1    tsarna #define		AUVIA_CODEC_PRIVALID		0x02000000
    164   1.1    tsarna #define		AUVIA_CODEC_INDEX(x)		((x)<<16)
    165   1.1    tsarna 
    166   1.1    tsarna #define TIMEOUT	50
    167   1.1    tsarna 
    168   1.1    tsarna struct audio_hw_if auvia_hw_if = {
    169   1.1    tsarna 	auvia_open,
    170   1.1    tsarna 	auvia_close,
    171   1.1    tsarna 	NULL, /* drain */
    172   1.1    tsarna 	auvia_query_encoding,
    173   1.1    tsarna 	auvia_set_params,
    174   1.1    tsarna 	auvia_round_blocksize,
    175   1.1    tsarna 	NULL, /* commit_settings */
    176   1.1    tsarna 	NULL, /* init_output */
    177   1.1    tsarna 	NULL, /* init_input */
    178   1.1    tsarna 	NULL, /* start_output */
    179   1.1    tsarna 	NULL, /* start_input */
    180   1.1    tsarna 	auvia_halt_output,
    181   1.1    tsarna 	auvia_halt_input,
    182   1.1    tsarna 	NULL, /* speaker_ctl */
    183   1.1    tsarna 	auvia_getdev,
    184   1.1    tsarna 	NULL, /* setfd */
    185   1.1    tsarna 	auvia_set_port,
    186   1.1    tsarna 	auvia_get_port,
    187   1.1    tsarna 	auvia_query_devinfo,
    188   1.1    tsarna 	auvia_malloc,
    189   1.1    tsarna 	auvia_free,
    190   1.1    tsarna 	auvia_round_buffersize,
    191   1.1    tsarna 	auvia_mappage,
    192   1.1    tsarna 	auvia_get_props,
    193   1.1    tsarna 	auvia_trigger_output,
    194   1.1    tsarna 	auvia_trigger_input,
    195  1.13  augustss 	NULL, /* dev_ioctl */
    196   1.1    tsarna };
    197   1.1    tsarna 
    198   1.1    tsarna int	auvia_attach_codec(void *, struct ac97_codec_if *);
    199   1.1    tsarna int	auvia_write_codec(void *, u_int8_t, u_int16_t);
    200   1.1    tsarna int	auvia_read_codec(void *, u_int8_t, u_int16_t *);
    201   1.1    tsarna void	auvia_reset_codec(void *);
    202   1.1    tsarna int	auvia_waitready_codec(struct auvia_softc *sc);
    203   1.1    tsarna int	auvia_waitvalid_codec(struct auvia_softc *sc);
    204   1.1    tsarna 
    205   1.1    tsarna 
    206   1.1    tsarna int
    207   1.1    tsarna auvia_match(struct device *parent, struct cfdata *match, void *aux)
    208   1.1    tsarna {
    209   1.1    tsarna 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    210   1.1    tsarna 
    211   1.1    tsarna 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
    212   1.1    tsarna 		return 0;
    213  1.15  jmcneill 	switch (PCI_PRODUCT(pa->pa_id)) {
    214  1.15  jmcneill 	case PCI_PRODUCT_VIATECH_VT82C686A_AC97:
    215  1.22      kent 	case PCI_PRODUCT_VIATECH_VT8233_AC97:
    216  1.15  jmcneill 		break;
    217  1.15  jmcneill 	default:
    218   1.1    tsarna 		return 0;
    219  1.15  jmcneill 	}
    220   1.1    tsarna 
    221   1.1    tsarna 	return 1;
    222   1.1    tsarna }
    223   1.1    tsarna 
    224   1.1    tsarna 
    225   1.1    tsarna void
    226   1.1    tsarna auvia_attach(struct device *parent, struct device *self, void *aux)
    227   1.1    tsarna {
    228   1.1    tsarna 	struct pci_attach_args *pa = aux;
    229   1.1    tsarna 	struct auvia_softc *sc = (struct auvia_softc *) self;
    230  1.22      kent 	const char *intrstr = NULL;
    231   1.1    tsarna 	struct mixer_ctrl ctl;
    232   1.1    tsarna 	pci_chipset_tag_t pc = pa->pa_pc;
    233  1.22      kent 	pcitag_t pt = pa->pa_tag;
    234  1.22      kent 	pci_intr_handle_t ih;
    235  1.22      kent 	bus_size_t iosize;
    236   1.1    tsarna 	pcireg_t pr;
    237  1.22      kent 	int r, i;
    238  1.22      kent 
    239  1.22      kent 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT8233_AC97)
    240  1.22      kent 		sc->sc_flags |= AUVIA_FLAGS_VT8233;
    241  1.22      kent 
    242  1.22      kent 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
    243  1.22      kent 		&sc->sc_ioh, NULL, &iosize)) {
    244  1.22      kent 		printf(": can't map i/o space\n");
    245  1.22      kent 		return;
    246  1.22      kent 	}
    247  1.22      kent 
    248  1.22      kent 	sc->sc_dmat = pa->pa_dmat;
    249  1.22      kent 	sc->sc_pc = pc;
    250  1.22      kent 	sc->sc_pt = pt;
    251   1.1    tsarna 
    252   1.1    tsarna 	r = PCI_REVISION(pa->pa_class);
    253  1.22      kent 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    254  1.22      kent 		sprintf(sc->sc_revision, "0x%02X", r);
    255  1.22      kent 		if (r < 0x50) {
    256  1.22      kent 			printf(": VIA VT8233 AC'97 Audio (rev %s)\n",
    257  1.22      kent 			       sc->sc_revision);
    258  1.22      kent 		} else {
    259  1.22      kent 			printf(": VIA VT8235 AC'97 Audio (rev %s)\n",
    260  1.22      kent 			       sc->sc_revision);
    261  1.22      kent 		}
    262   1.1    tsarna 	} else {
    263  1.22      kent 		sc->sc_revision[1] = '\0';
    264  1.22      kent 		if (r == 0x20) {
    265  1.22      kent 			sc->sc_revision[0] = 'H';
    266  1.22      kent 		} else if ((r >= 0x10) && (r <= 0x14)) {
    267  1.22      kent 			sc->sc_revision[0] = 'A' + (r - 0x10);
    268  1.22      kent 		} else {
    269  1.22      kent 			sprintf(sc->sc_revision, "0x%02X", r);
    270  1.22      kent 		}
    271  1.22      kent 
    272  1.22      kent 		printf(": VIA VT82C686A AC'97 Audio (rev %s)\n",
    273  1.22      kent 		       sc->sc_revision);
    274   1.1    tsarna 	}
    275   1.1    tsarna 
    276   1.9  sommerfe 	if (pci_intr_map(pa, &ih)) {
    277  1.22      kent 		printf(": couldn't map interrupt\n");
    278  1.22      kent 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
    279   1.1    tsarna 		return;
    280   1.1    tsarna 	}
    281   1.1    tsarna 	intrstr = pci_intr_string(pc, ih);
    282   1.1    tsarna 
    283   1.1    tsarna 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
    284   1.1    tsarna 	if (sc->sc_ih == NULL) {
    285   1.1    tsarna 		printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
    286   1.1    tsarna 		if (intrstr != NULL)
    287   1.1    tsarna 			printf(" at %s", intrstr);
    288   1.1    tsarna 		printf("\n");
    289  1.22      kent 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
    290   1.1    tsarna 		return;
    291   1.1    tsarna 	}
    292   1.1    tsarna 
    293   1.1    tsarna 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    294   1.1    tsarna 
    295   1.1    tsarna 	/* disable SBPro compat & others */
    296   1.1    tsarna 	pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
    297   1.1    tsarna 
    298   1.1    tsarna 	pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
    299   1.1    tsarna 	/* XXX what to do about MIDI, FM, joystick? */
    300   1.1    tsarna 
    301   1.1    tsarna 	pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
    302   1.1    tsarna 		| AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
    303   1.1    tsarna 
    304   1.1    tsarna 	pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
    305   1.1    tsarna 
    306   1.1    tsarna 	pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
    307   1.1    tsarna 
    308   1.1    tsarna 	sc->host_if.arg = sc;
    309   1.1    tsarna 	sc->host_if.attach = auvia_attach_codec;
    310   1.1    tsarna 	sc->host_if.read = auvia_read_codec;
    311   1.1    tsarna 	sc->host_if.write = auvia_write_codec;
    312   1.1    tsarna 	sc->host_if.reset = auvia_reset_codec;
    313   1.1    tsarna 
    314   1.1    tsarna 	if ((r = ac97_attach(&sc->host_if)) != 0) {
    315   1.1    tsarna 		printf("%s: can't attach codec (error 0x%X)\n",
    316   1.1    tsarna 			sc->sc_dev.dv_xname, r);
    317  1.22      kent 		pci_intr_disestablish(pc, sc->sc_ih);
    318  1.22      kent 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
    319   1.1    tsarna 		return;
    320   1.1    tsarna 	}
    321   1.1    tsarna 
    322   1.1    tsarna 	/* disable mutes */
    323   1.1    tsarna 	for (i = 0; i < 4; i++) {
    324   1.1    tsarna 		static struct {
    325   1.1    tsarna 			char *class, *device;
    326   1.1    tsarna 		} d[] = {
    327   1.1    tsarna 			{ AudioCoutputs, AudioNmaster},
    328   1.1    tsarna 			{ AudioCinputs, AudioNdac},
    329   1.1    tsarna 			{ AudioCinputs, AudioNcd},
    330  1.11      fvdl 			{ AudioCinputs, AudioNline},
    331   1.1    tsarna 			{ AudioCrecord, AudioNvolume},
    332   1.1    tsarna 		};
    333  1.22      kent 
    334   1.1    tsarna 		ctl.type = AUDIO_MIXER_ENUM;
    335   1.1    tsarna 		ctl.un.ord = 0;
    336   1.1    tsarna 
    337   1.1    tsarna 		ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
    338   1.1    tsarna 			d[i].class, d[i].device, AudioNmute);
    339   1.1    tsarna 		auvia_set_port(sc, &ctl);
    340   1.1    tsarna 	}
    341   1.1    tsarna 
    342   1.1    tsarna 	/* set a reasonable default volume */
    343   1.1    tsarna 
    344   1.1    tsarna 	ctl.type = AUDIO_MIXER_VALUE;
    345   1.1    tsarna 	ctl.un.value.num_channels = 2;
    346   1.1    tsarna 	ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \
    347   1.1    tsarna 	ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 199;
    348   1.1    tsarna 
    349   1.1    tsarna 	ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
    350   1.1    tsarna 		AudioCoutputs, AudioNmaster, NULL);
    351   1.1    tsarna 	auvia_set_port(sc, &ctl);
    352  1.22      kent 
    353  1.22      kent 	audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
    354   1.1    tsarna }
    355   1.1    tsarna 
    356   1.1    tsarna 
    357   1.1    tsarna int
    358   1.1    tsarna auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
    359   1.1    tsarna {
    360   1.1    tsarna 	struct auvia_softc *sc = addr;
    361   1.1    tsarna 
    362   1.1    tsarna 	sc->codec_if = cif;
    363   1.1    tsarna 
    364   1.1    tsarna 	return 0;
    365   1.1    tsarna }
    366   1.1    tsarna 
    367   1.1    tsarna 
    368   1.1    tsarna void
    369   1.1    tsarna auvia_reset_codec(void *addr)
    370   1.1    tsarna {
    371   1.1    tsarna #ifdef notyet /* XXX seems to make codec become unready... ??? */
    372   1.1    tsarna 	struct auvia_softc *sc = addr;
    373   1.1    tsarna 	pcireg_t r;
    374   1.1    tsarna 
    375   1.1    tsarna 	/* perform a codec cold reset */
    376   1.1    tsarna 
    377   1.1    tsarna 	r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
    378   1.1    tsarna 
    379   1.1    tsarna 	r &= ~AUVIA_PCICONF_ACNOTRST;	/* enable RESET (active low) */
    380   1.1    tsarna 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    381   1.1    tsarna 	delay(2);
    382   1.1    tsarna 
    383  1.22      kent 	r |= AUVIA_PCICONF_ACNOTRST;	/* disable RESET (inactive high) */
    384   1.1    tsarna 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    385   1.1    tsarna 	delay(200);
    386   1.1    tsarna 
    387   1.1    tsarna 	auvia_waitready_codec(sc);
    388   1.1    tsarna #endif
    389   1.1    tsarna }
    390   1.1    tsarna 
    391   1.1    tsarna 
    392   1.1    tsarna int
    393   1.1    tsarna auvia_waitready_codec(struct auvia_softc *sc)
    394   1.1    tsarna {
    395   1.1    tsarna 	int i;
    396   1.1    tsarna 
    397   1.1    tsarna 	/* poll until codec not busy */
    398   1.1    tsarna 	for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    399   1.1    tsarna 		AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
    400   1.1    tsarna 		delay(1);
    401   1.1    tsarna 	if (i >= TIMEOUT) {
    402   1.1    tsarna 		printf("%s: codec busy\n", sc->sc_dev.dv_xname);
    403   1.1    tsarna 		return 1;
    404   1.1    tsarna 	}
    405   1.1    tsarna 
    406   1.1    tsarna 	return 0;
    407   1.1    tsarna }
    408   1.1    tsarna 
    409   1.1    tsarna 
    410   1.1    tsarna int
    411   1.1    tsarna auvia_waitvalid_codec(struct auvia_softc *sc)
    412   1.1    tsarna {
    413   1.1    tsarna 	int i;
    414   1.1    tsarna 
    415   1.1    tsarna 	/* poll until codec valid */
    416   1.1    tsarna 	for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    417   1.1    tsarna 		AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
    418   1.1    tsarna 			delay(1);
    419   1.1    tsarna 	if (i >= TIMEOUT) {
    420   1.1    tsarna 		printf("%s: codec invalid\n", sc->sc_dev.dv_xname);
    421   1.1    tsarna 		return 1;
    422   1.1    tsarna 	}
    423   1.1    tsarna 
    424   1.1    tsarna 	return 0;
    425   1.1    tsarna }
    426   1.1    tsarna 
    427   1.1    tsarna 
    428   1.1    tsarna int
    429   1.1    tsarna auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
    430   1.1    tsarna {
    431   1.1    tsarna 	struct auvia_softc *sc = addr;
    432   1.1    tsarna 
    433   1.1    tsarna 	if (auvia_waitready_codec(sc))
    434   1.1    tsarna 		return 1;
    435   1.1    tsarna 
    436   1.1    tsarna 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    437   1.1    tsarna 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
    438   1.1    tsarna 
    439   1.1    tsarna 	return 0;
    440   1.1    tsarna }
    441   1.1    tsarna 
    442   1.1    tsarna 
    443   1.1    tsarna int
    444   1.1    tsarna auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
    445   1.1    tsarna {
    446   1.1    tsarna 	struct auvia_softc *sc = addr;
    447   1.1    tsarna 
    448   1.1    tsarna 	if (auvia_waitready_codec(sc))
    449   1.1    tsarna 		return 1;
    450   1.1    tsarna 
    451   1.1    tsarna 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    452   1.1    tsarna 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
    453   1.1    tsarna 
    454   1.1    tsarna 	if (auvia_waitready_codec(sc))
    455   1.1    tsarna 		return 1;
    456   1.1    tsarna 
    457   1.1    tsarna 	if (auvia_waitvalid_codec(sc))
    458   1.1    tsarna 		return 1;
    459   1.1    tsarna 
    460   1.1    tsarna 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
    461   1.1    tsarna 
    462   1.1    tsarna 	return 0;
    463   1.1    tsarna }
    464   1.1    tsarna 
    465   1.1    tsarna 
    466   1.1    tsarna int
    467   1.1    tsarna auvia_open(void *addr, int flags)
    468   1.1    tsarna {
    469   1.1    tsarna 	return 0;
    470   1.1    tsarna }
    471   1.1    tsarna 
    472   1.1    tsarna 
    473   1.1    tsarna void
    474   1.1    tsarna auvia_close(void *addr)
    475   1.1    tsarna {
    476   1.2    tsarna 	struct auvia_softc *sc = addr;
    477  1.22      kent 
    478   1.2    tsarna 	auvia_halt_output(sc);
    479   1.2    tsarna 	auvia_halt_input(sc);
    480   1.2    tsarna 
    481   1.2    tsarna 	sc->sc_play.sc_intr = NULL;
    482   1.2    tsarna 	sc->sc_record.sc_intr = NULL;
    483   1.1    tsarna }
    484   1.1    tsarna 
    485   1.1    tsarna 
    486   1.1    tsarna int
    487   1.1    tsarna auvia_query_encoding(void *addr, struct audio_encoding *fp)
    488   1.1    tsarna {
    489   1.1    tsarna 	switch (fp->index) {
    490   1.1    tsarna 	case 0:
    491   1.1    tsarna 		strcpy(fp->name, AudioEulinear);
    492   1.1    tsarna 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    493   1.1    tsarna 		fp->precision = 8;
    494   1.1    tsarna 		fp->flags = 0;
    495   1.1    tsarna 		return (0);
    496   1.1    tsarna 	case 1:
    497   1.1    tsarna 		strcpy(fp->name, AudioEmulaw);
    498   1.1    tsarna 		fp->encoding = AUDIO_ENCODING_ULAW;
    499   1.1    tsarna 		fp->precision = 8;
    500   1.1    tsarna 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    501   1.1    tsarna 		return (0);
    502   1.1    tsarna 	case 2:
    503   1.1    tsarna 		strcpy(fp->name, AudioEalaw);
    504   1.1    tsarna 		fp->encoding = AUDIO_ENCODING_ALAW;
    505   1.1    tsarna 		fp->precision = 8;
    506   1.1    tsarna 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    507   1.1    tsarna 		return (0);
    508   1.1    tsarna 	case 3:
    509   1.1    tsarna 		strcpy(fp->name, AudioEslinear);
    510   1.1    tsarna 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    511   1.1    tsarna 		fp->precision = 8;
    512   1.1    tsarna 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    513   1.1    tsarna 		return (0);
    514   1.1    tsarna 	case 4:
    515   1.1    tsarna 		strcpy(fp->name, AudioEslinear_le);
    516   1.1    tsarna 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    517   1.1    tsarna 		fp->precision = 16;
    518   1.1    tsarna 		fp->flags = 0;
    519   1.1    tsarna 		return (0);
    520   1.1    tsarna 	case 5:
    521   1.1    tsarna 		strcpy(fp->name, AudioEulinear_le);
    522   1.1    tsarna 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    523   1.1    tsarna 		fp->precision = 16;
    524   1.1    tsarna 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    525   1.1    tsarna 		return (0);
    526   1.1    tsarna 	case 6:
    527   1.1    tsarna 		strcpy(fp->name, AudioEslinear_be);
    528   1.1    tsarna 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    529   1.1    tsarna 		fp->precision = 16;
    530   1.1    tsarna 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    531   1.1    tsarna 		return (0);
    532   1.1    tsarna 	case 7:
    533   1.1    tsarna 		strcpy(fp->name, AudioEulinear_be);
    534   1.1    tsarna 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    535   1.1    tsarna 		fp->precision = 16;
    536   1.1    tsarna 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    537   1.1    tsarna 		return (0);
    538   1.1    tsarna 	default:
    539   1.1    tsarna 		return (EINVAL);
    540   1.1    tsarna 	}
    541   1.1    tsarna }
    542   1.1    tsarna 
    543   1.1    tsarna 
    544   1.1    tsarna int
    545   1.1    tsarna auvia_set_params(void *addr, int setmode, int usemode,
    546   1.1    tsarna 	struct audio_params *play, struct audio_params *rec)
    547   1.1    tsarna {
    548   1.1    tsarna 	struct auvia_softc *sc = addr;
    549   1.1    tsarna 	struct audio_params *p;
    550   1.1    tsarna 	u_int16_t regval;
    551  1.22      kent 	int reg, mode, base;
    552   1.1    tsarna 
    553   1.1    tsarna 	/* for mode in (RECORD, PLAY) */
    554  1.22      kent 	for (mode = AUMODE_RECORD; mode != -1;
    555   1.1    tsarna 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    556   1.1    tsarna 		if ((setmode & mode) == 0)
    557   1.1    tsarna 			continue;
    558   1.1    tsarna 
    559  1.22      kent 		if (mode == AUMODE_PLAY ) {
    560  1.22      kent 			p = play;
    561  1.22      kent 			base = AUVIA_PLAY_BASE;
    562  1.22      kent 		} else {
    563  1.22      kent 			p = rec;
    564  1.22      kent 			base = AUVIA_RECORD_BASE;
    565  1.22      kent 		}
    566  1.22      kent 
    567  1.22      kent 		if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    568  1.22      kent 			u_int32_t v = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    569  1.22      kent 				base + VIA8233_RP_RATEFMT) & ~(VIA8233_RATEFMT_48K
    570  1.22      kent 				| VIA8233_RATEFMT_STEREO | VIA8233_RATEFMT_16BIT);
    571  1.22      kent 
    572  1.26      kent 			v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20)
    573  1.26      kent 				/ (48000 / 20);
    574  1.22      kent 
    575  1.22      kent 			if (p->channels == 2)
    576  1.22      kent 				v |= VIA8233_RATEFMT_STEREO;
    577  1.22      kent 			if( p->precision == 16)
    578  1.22      kent 				v |= VIA8233_RATEFMT_16BIT;
    579  1.22      kent 
    580  1.22      kent 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    581  1.22      kent 				base + VIA8233_RP_RATEFMT, v);
    582  1.22      kent 		}
    583   1.1    tsarna 
    584   1.1    tsarna 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
    585   1.1    tsarna 		    (p->precision != 8 && p->precision != 16) ||
    586   1.1    tsarna 		    (p->channels != 1 && p->channels != 2))
    587   1.1    tsarna 			return (EINVAL);
    588   1.1    tsarna 
    589   1.1    tsarna 		reg = mode == AUMODE_PLAY ?
    590  1.10  augustss 			AC97_REG_PCM_FRONT_DAC_RATE : AC97_REG_PCM_LR_ADC_RATE;
    591   1.1    tsarna 
    592  1.26      kent 		if (IS_FIXED_RATE(sc->codec_if)) {
    593  1.26      kent 			/* Enable aurateconv */
    594  1.26      kent 			p->hw_sample_rate = AC97_SINGLE_RATE;
    595  1.26      kent 		} else {
    596  1.26      kent 			if (sc->codec_if->vtbl->set_rate(sc->codec_if, reg,
    597  1.26      kent 							 &p->sample_rate))
    598  1.26      kent 				return (EINVAL);
    599  1.26      kent 		}
    600   1.1    tsarna 
    601   1.1    tsarna 		p->factor = 1;
    602   1.1    tsarna 		p->sw_code = 0;
    603   1.1    tsarna 		switch (p->encoding) {
    604   1.1    tsarna 		case AUDIO_ENCODING_SLINEAR_BE:
    605   1.1    tsarna 			if (p->precision == 16)
    606   1.1    tsarna 				p->sw_code = swap_bytes;
    607   1.1    tsarna 			else
    608   1.1    tsarna 				p->sw_code = change_sign8;
    609   1.1    tsarna 			break;
    610   1.1    tsarna 		case AUDIO_ENCODING_SLINEAR_LE:
    611   1.1    tsarna 			if (p->precision != 16)
    612   1.1    tsarna 				p->sw_code = change_sign8;
    613   1.1    tsarna 			break;
    614   1.1    tsarna 		case AUDIO_ENCODING_ULINEAR_BE:
    615   1.1    tsarna 			if (p->precision == 16) {
    616   1.1    tsarna 				if (mode == AUMODE_PLAY)
    617   1.1    tsarna 					p->sw_code = swap_bytes_change_sign16_le;
    618   1.1    tsarna 				else
    619   1.1    tsarna 					p->sw_code = change_sign16_swap_bytes_le;
    620   1.1    tsarna 			}
    621   1.1    tsarna 			break;
    622   1.1    tsarna 		case AUDIO_ENCODING_ULINEAR_LE:
    623   1.1    tsarna 			if (p->precision == 16)
    624   1.1    tsarna 				p->sw_code = change_sign16_le;
    625   1.1    tsarna 			break;
    626   1.1    tsarna 		case AUDIO_ENCODING_ULAW:
    627   1.1    tsarna 			if (mode == AUMODE_PLAY) {
    628   1.1    tsarna 				p->factor = 2;
    629   1.1    tsarna 				p->sw_code = mulaw_to_slinear16_le;
    630   1.1    tsarna 			} else
    631   1.1    tsarna 				p->sw_code = ulinear8_to_mulaw;
    632   1.1    tsarna 			break;
    633   1.1    tsarna 		case AUDIO_ENCODING_ALAW:
    634   1.1    tsarna 			if (mode == AUMODE_PLAY) {
    635   1.1    tsarna 				p->factor = 2;
    636   1.1    tsarna 				p->sw_code = alaw_to_slinear16_le;
    637   1.1    tsarna 			} else
    638   1.1    tsarna 				p->sw_code = ulinear8_to_alaw;
    639   1.1    tsarna 			break;
    640   1.1    tsarna 		default:
    641   1.1    tsarna 			return (EINVAL);
    642   1.1    tsarna 		}
    643   1.1    tsarna 
    644   1.1    tsarna 		regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
    645   1.1    tsarna 			| (p->precision * p->factor == 16 ?
    646   1.1    tsarna 				AUVIA_RPMODE_16BIT : 0)
    647   1.1    tsarna 			| AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
    648   1.1    tsarna 			| AUVIA_RPMODE_AUTOSTART;
    649   1.1    tsarna 
    650   1.1    tsarna 		if (mode == AUMODE_PLAY) {
    651   1.1    tsarna 			sc->sc_play.sc_reg = regval;
    652   1.1    tsarna 		} else {
    653   1.1    tsarna 			sc->sc_record.sc_reg = regval;
    654   1.1    tsarna 		}
    655   1.1    tsarna 	}
    656   1.1    tsarna 
    657   1.1    tsarna 	return 0;
    658   1.1    tsarna }
    659   1.1    tsarna 
    660   1.1    tsarna 
    661   1.1    tsarna int
    662   1.1    tsarna auvia_round_blocksize(void *addr, int blk)
    663   1.1    tsarna {
    664   1.1    tsarna 	return (blk & -32);
    665   1.1    tsarna }
    666   1.1    tsarna 
    667   1.1    tsarna 
    668   1.1    tsarna int
    669   1.1    tsarna auvia_halt_output(void *addr)
    670   1.1    tsarna {
    671  1.22      kent 	struct auvia_softc *sc = addr;
    672   1.1    tsarna 
    673  1.22      kent 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    674  1.22      kent 		AUVIA_PLAY_BASE + AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    675   1.1    tsarna 
    676   1.1    tsarna 	return 0;
    677   1.1    tsarna }
    678   1.1    tsarna 
    679   1.1    tsarna 
    680   1.1    tsarna int
    681   1.1    tsarna auvia_halt_input(void *addr)
    682   1.1    tsarna {
    683  1.22      kent 	struct auvia_softc *sc = addr;
    684   1.1    tsarna 
    685  1.22      kent 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    686  1.22      kent 		AUVIA_PLAY_BASE + AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    687   1.1    tsarna 
    688   1.1    tsarna 	return 0;
    689   1.1    tsarna }
    690   1.1    tsarna 
    691   1.1    tsarna 
    692   1.1    tsarna int
    693   1.1    tsarna auvia_getdev(void *addr, struct audio_device *retp)
    694   1.1    tsarna {
    695  1.22      kent 	struct auvia_softc *sc = addr;
    696   1.1    tsarna 
    697   1.1    tsarna 	if (retp) {
    698  1.22      kent 		if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    699  1.26      kent 			strncpy(retp->name, "VIA VT8233/8235",
    700  1.22      kent 				sizeof(retp->name));
    701  1.22      kent 		} else {
    702  1.22      kent 			strncpy(retp->name, "VIA VT82C686A",
    703  1.22      kent 				sizeof(retp->name));
    704  1.22      kent 		}
    705   1.1    tsarna 		strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
    706   1.1    tsarna 		strncpy(retp->config, "auvia", sizeof(retp->config));
    707   1.1    tsarna 	}
    708   1.1    tsarna 
    709   1.1    tsarna 	return 0;
    710   1.1    tsarna }
    711   1.1    tsarna 
    712   1.1    tsarna 
    713   1.1    tsarna int
    714   1.1    tsarna auvia_set_port(void *addr, mixer_ctrl_t *cp)
    715   1.1    tsarna {
    716   1.1    tsarna 	struct auvia_softc *sc = addr;
    717   1.1    tsarna 
    718   1.1    tsarna 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
    719   1.1    tsarna }
    720   1.1    tsarna 
    721   1.1    tsarna 
    722   1.1    tsarna int
    723   1.1    tsarna auvia_get_port(void *addr, mixer_ctrl_t *cp)
    724   1.1    tsarna {
    725   1.1    tsarna 	struct auvia_softc *sc = addr;
    726   1.1    tsarna 
    727   1.1    tsarna 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
    728   1.1    tsarna }
    729   1.1    tsarna 
    730   1.1    tsarna 
    731   1.1    tsarna int
    732   1.1    tsarna auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
    733   1.1    tsarna {
    734   1.1    tsarna 	struct auvia_softc *sc = addr;
    735   1.1    tsarna 
    736   1.1    tsarna 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
    737   1.1    tsarna }
    738   1.1    tsarna 
    739   1.1    tsarna 
    740   1.1    tsarna void *
    741   1.1    tsarna auvia_malloc(void *addr, int direction, size_t size, int pool, int flags)
    742   1.1    tsarna {
    743   1.1    tsarna 	struct auvia_softc *sc = addr;
    744   1.1    tsarna 	struct auvia_dma *p;
    745   1.1    tsarna 	int error;
    746   1.1    tsarna 	int rseg;
    747   1.1    tsarna 
    748   1.1    tsarna 	p = malloc(sizeof(*p), pool, flags);
    749   1.1    tsarna 	if (!p)
    750   1.1    tsarna 		return 0;
    751   1.1    tsarna 
    752   1.2    tsarna 	p->size = size;
    753   1.6   thorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
    754   1.6   thorpej 				      1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    755  1.22      kent 		printf("%s: unable to allocate dma, error = %d\n",
    756   1.1    tsarna 		       sc->sc_dev.dv_xname, error);
    757   1.1    tsarna 		goto fail_alloc;
    758   1.1    tsarna 	}
    759   1.1    tsarna 
    760   1.1    tsarna 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
    761   1.1    tsarna 				    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    762  1.22      kent 		printf("%s: unable to map dma, error = %d\n",
    763   1.1    tsarna 		       sc->sc_dev.dv_xname, error);
    764   1.1    tsarna 		goto fail_map;
    765   1.1    tsarna 	}
    766   1.1    tsarna 
    767  1.22      kent 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    768   1.1    tsarna 				       BUS_DMA_NOWAIT, &p->map)) != 0) {
    769   1.1    tsarna 		printf("%s: unable to create dma map, error = %d\n",
    770   1.1    tsarna 		       sc->sc_dev.dv_xname, error);
    771   1.1    tsarna 		goto fail_create;
    772   1.1    tsarna 	}
    773   1.1    tsarna 
    774   1.1    tsarna 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
    775   1.1    tsarna 				     BUS_DMA_NOWAIT)) != 0) {
    776   1.1    tsarna 		printf("%s: unable to load dma map, error = %d\n",
    777   1.1    tsarna 		       sc->sc_dev.dv_xname, error);
    778   1.1    tsarna 		goto fail_load;
    779   1.1    tsarna 	}
    780   1.1    tsarna 
    781   1.1    tsarna 	p->next = sc->sc_dmas;
    782   1.1    tsarna 	sc->sc_dmas = p;
    783   1.1    tsarna 
    784   1.1    tsarna 	return p->addr;
    785   1.1    tsarna 
    786   1.1    tsarna 
    787   1.1    tsarna fail_load:
    788   1.1    tsarna 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    789   1.1    tsarna fail_create:
    790   1.1    tsarna 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
    791   1.1    tsarna fail_map:
    792   1.1    tsarna 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    793   1.1    tsarna fail_alloc:
    794   1.1    tsarna 	free(p, pool);
    795   1.1    tsarna 	return 0;
    796   1.1    tsarna }
    797   1.1    tsarna 
    798   1.1    tsarna 
    799   1.1    tsarna void
    800   1.1    tsarna auvia_free(void *addr, void *ptr, int pool)
    801   1.1    tsarna {
    802   1.1    tsarna 	struct auvia_softc *sc = addr;
    803   1.2    tsarna 	struct auvia_dma **pp, *p;
    804   1.1    tsarna 
    805   1.2    tsarna 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
    806   1.2    tsarna 		if (p->addr == ptr) {
    807   1.2    tsarna 			bus_dmamap_unload(sc->sc_dmat, p->map);
    808   1.2    tsarna 			bus_dmamap_destroy(sc->sc_dmat, p->map);
    809   1.2    tsarna 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    810   1.2    tsarna 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    811  1.22      kent 
    812   1.2    tsarna 			*pp = p->next;
    813   1.2    tsarna 			free(p, pool);
    814   1.2    tsarna 			return;
    815   1.2    tsarna 		}
    816   1.1    tsarna 
    817   1.2    tsarna 	panic("auvia_free: trying to free unallocated memory");
    818   1.1    tsarna }
    819   1.1    tsarna 
    820   1.1    tsarna 
    821   1.1    tsarna size_t
    822   1.1    tsarna auvia_round_buffersize(void *addr, int direction, size_t size)
    823   1.1    tsarna {
    824   1.1    tsarna 	return size;
    825   1.1    tsarna }
    826   1.1    tsarna 
    827   1.1    tsarna 
    828   1.4    simonb paddr_t
    829   1.4    simonb auvia_mappage(void *addr, void *mem, off_t off, int prot)
    830   1.1    tsarna {
    831   1.1    tsarna 	struct auvia_softc *sc = addr;
    832   1.1    tsarna 	struct auvia_dma *p;
    833   1.1    tsarna 
    834   1.1    tsarna 	if (off < 0)
    835   1.1    tsarna 		return -1;
    836   1.1    tsarna 
    837   1.1    tsarna 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
    838   1.1    tsarna 		;
    839   1.1    tsarna 
    840   1.1    tsarna 	if (!p)
    841   1.1    tsarna 		return -1;
    842   1.1    tsarna 
    843  1.22      kent 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
    844   1.1    tsarna 	       BUS_DMA_WAITOK);
    845   1.1    tsarna }
    846   1.1    tsarna 
    847   1.1    tsarna 
    848   1.1    tsarna int
    849   1.1    tsarna auvia_get_props(void *addr)
    850   1.1    tsarna {
    851  1.26      kent 	struct auvia_softc *sc = addr;
    852  1.26      kent 	int props;
    853  1.26      kent 
    854  1.26      kent 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    855  1.26      kent 	/*
    856  1.26      kent 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
    857  1.26      kent 	 * rate because of aurateconv.  Applications can't know what rate the
    858  1.26      kent 	 * device can process in the case of mmap().
    859  1.26      kent 	 */
    860  1.26      kent 	if (!IS_FIXED_RATE(sc->codec_if))
    861  1.26      kent 		props |= AUDIO_PROP_MMAP;
    862  1.26      kent 	return props;
    863   1.1    tsarna }
    864   1.1    tsarna 
    865   1.1    tsarna 
    866   1.1    tsarna int
    867   1.1    tsarna auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    868   1.1    tsarna 	struct auvia_dma *p, void *start, void *end, int blksize)
    869   1.1    tsarna {
    870   1.1    tsarna 	struct auvia_dma_op *op;
    871   1.1    tsarna 	struct auvia_dma *dp;
    872   1.1    tsarna 	bus_addr_t s, e;
    873   1.1    tsarna 	size_t l;
    874   1.1    tsarna 	int segs;
    875   1.1    tsarna 
    876   1.1    tsarna 	s = p->map->dm_segs[0].ds_addr;
    877   1.1    tsarna 	l = ((char *)end - (char *)start);
    878   1.1    tsarna 	e = s + l;
    879   1.1    tsarna 	segs = (l + blksize - 1) / blksize;
    880   1.1    tsarna 
    881   1.1    tsarna 	if (segs > (ch->sc_dma_op_count)) {
    882   1.1    tsarna 		/* if old list was too small, free it */
    883   1.1    tsarna 		if (ch->sc_dma_ops) {
    884   1.1    tsarna 			auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
    885   1.1    tsarna 		}
    886   1.1    tsarna 
    887   1.1    tsarna 		ch->sc_dma_ops = auvia_malloc(sc, 0,
    888   1.1    tsarna 			sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
    889   1.1    tsarna 
    890   1.1    tsarna 		if (ch->sc_dma_ops == NULL) {
    891   1.1    tsarna 			printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
    892   1.1    tsarna 			return 1;
    893   1.1    tsarna 		}
    894   1.1    tsarna 
    895   1.1    tsarna 		for (dp = sc->sc_dmas;
    896   1.1    tsarna 			dp && dp->addr != (void *)(ch->sc_dma_ops);
    897   1.1    tsarna 			dp = dp->next)
    898   1.1    tsarna 				;
    899   1.1    tsarna 
    900   1.1    tsarna 		if (!dp)
    901   1.1    tsarna 			panic("%s: build_dma_ops: where'd my memory go??? "
    902   1.1    tsarna 				"address (%p)\n", sc->sc_dev.dv_xname,
    903   1.1    tsarna 				ch->sc_dma_ops);
    904   1.1    tsarna 
    905   1.1    tsarna 		ch->sc_dma_op_count = segs;
    906   1.1    tsarna 		ch->sc_dma_ops_dma = dp;
    907   1.1    tsarna 	}
    908   1.1    tsarna 
    909   1.1    tsarna 	dp = ch->sc_dma_ops_dma;
    910   1.1    tsarna 	op = ch->sc_dma_ops;
    911   1.1    tsarna 
    912   1.1    tsarna 	while (l) {
    913   1.1    tsarna 		op->ptr = s;
    914   1.1    tsarna 		l = l - blksize;
    915   1.1    tsarna 		if (!l) {
    916   1.1    tsarna 			/* if last block */
    917   1.1    tsarna 			op->flags = AUVIA_DMAOP_EOL | blksize;
    918   1.1    tsarna 		} else {
    919   1.1    tsarna 			op->flags = AUVIA_DMAOP_FLAG | blksize;
    920   1.1    tsarna 		}
    921   1.1    tsarna 		s += blksize;
    922   1.1    tsarna 		op++;
    923   1.1    tsarna 	}
    924   1.1    tsarna 
    925   1.1    tsarna 	return 0;
    926   1.1    tsarna }
    927   1.1    tsarna 
    928   1.1    tsarna 
    929   1.1    tsarna int
    930   1.1    tsarna auvia_trigger_output(void *addr, void *start, void *end,
    931   1.1    tsarna 	int blksize, void (*intr)(void *), void *arg,
    932   1.1    tsarna 	struct audio_params *param)
    933   1.1    tsarna {
    934   1.1    tsarna 	struct auvia_softc *sc = addr;
    935   1.1    tsarna 	struct auvia_softc_chan *ch = &(sc->sc_play);
    936   1.1    tsarna 	struct auvia_dma *p;
    937   1.1    tsarna 
    938   1.1    tsarna 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
    939   1.1    tsarna 		;
    940   1.1    tsarna 
    941   1.1    tsarna 	if (!p)
    942   1.1    tsarna 		panic("auvia_trigger_output: request with bad start "
    943  1.18    provos 			"address (%p)", start);
    944   1.1    tsarna 
    945   1.1    tsarna 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
    946   1.1    tsarna 		return 1;
    947   1.1    tsarna 	}
    948   1.1    tsarna 
    949   1.1    tsarna 	ch->sc_intr = intr;
    950   1.1    tsarna 	ch->sc_arg = arg;
    951   1.1    tsarna 
    952  1.22      kent 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    953  1.22      kent 			AUVIA_PLAY_BASE + AUVIA_RP_DMAOPS_BASE,
    954   1.1    tsarna 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
    955   1.1    tsarna 
    956  1.22      kent 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    957  1.22      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    958  1.25      kent 			AUVIA_PLAY_BASE + VIA8233_RP_DXS_LVOL, 0);
    959  1.25      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    960  1.25      kent 			AUVIA_PLAY_BASE + VIA8233_RP_DXS_RVOL, 0);
    961  1.25      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    962  1.22      kent 			AUVIA_PLAY_BASE + AUVIA_RP_CONTROL,
    963  1.22      kent 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
    964  1.22      kent 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
    965  1.24      kent 	} else {
    966  1.24      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    967  1.24      kent 			AUVIA_PLAY_BASE + AUVIA_RP_MODE, ch->sc_reg);
    968  1.22      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    969  1.22      kent 			AUVIA_PLAY_BASE + AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
    970  1.24      kent 	}
    971   1.1    tsarna 
    972   1.1    tsarna 	return 0;
    973   1.1    tsarna }
    974   1.1    tsarna 
    975   1.1    tsarna 
    976   1.1    tsarna int
    977   1.1    tsarna auvia_trigger_input(void *addr, void *start, void *end,
    978   1.1    tsarna 	int blksize, void (*intr)(void *), void *arg,
    979   1.1    tsarna 	struct audio_params *param)
    980   1.1    tsarna {
    981   1.1    tsarna 	struct auvia_softc *sc = addr;
    982   1.1    tsarna 	struct auvia_softc_chan *ch = &(sc->sc_record);
    983   1.1    tsarna 	struct auvia_dma *p;
    984   1.1    tsarna 
    985   1.1    tsarna 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
    986   1.1    tsarna 		;
    987   1.1    tsarna 
    988   1.1    tsarna 	if (!p)
    989   1.1    tsarna 		panic("auvia_trigger_input: request with bad start "
    990  1.18    provos 			"address (%p)", start);
    991   1.1    tsarna 
    992   1.1    tsarna 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
    993   1.1    tsarna 		return 1;
    994   1.1    tsarna 	}
    995   1.1    tsarna 
    996   1.1    tsarna 	ch->sc_intr = intr;
    997   1.1    tsarna 	ch->sc_arg = arg;
    998   1.1    tsarna 
    999  1.22      kent 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
   1000  1.22      kent 		AUVIA_RECORD_BASE + AUVIA_RP_DMAOPS_BASE,
   1001   1.1    tsarna 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1002   1.1    tsarna 
   1003  1.24      kent 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1004  1.25      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1005  1.25      kent 			AUVIA_RECORD_BASE + VIA8233_RP_DXS_LVOL, 0);
   1006  1.25      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1007  1.25      kent 			AUVIA_RECORD_BASE + VIA8233_RP_DXS_RVOL, 0);
   1008  1.22      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1009  1.22      kent 			AUVIA_RECORD_BASE + AUVIA_RP_CONTROL,
   1010  1.22      kent 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1011  1.22      kent 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1012  1.24      kent 	} else {
   1013  1.24      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1014  1.24      kent 			AUVIA_RECORD_BASE + AUVIA_RP_MODE, ch->sc_reg);
   1015  1.22      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1016  1.22      kent 			AUVIA_RECORD_BASE + AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1017  1.24      kent 	}
   1018   1.1    tsarna 
   1019   1.1    tsarna 	return 0;
   1020   1.1    tsarna }
   1021   1.1    tsarna 
   1022   1.1    tsarna 
   1023   1.1    tsarna int
   1024   1.1    tsarna auvia_intr(void *arg)
   1025   1.1    tsarna {
   1026   1.1    tsarna 	struct auvia_softc *sc = arg;
   1027   1.1    tsarna 	u_int8_t r;
   1028  1.17      fvdl 	int rval;
   1029  1.17      fvdl 
   1030  1.17      fvdl 	rval = 0;
   1031   1.1    tsarna 
   1032  1.22      kent 	r = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
   1033  1.22      kent 		AUVIA_RECORD_BASE + AUVIA_RP_STAT);
   1034   1.1    tsarna 	if (r & AUVIA_RPSTAT_INTR) {
   1035   1.2    tsarna 		if (sc->sc_record.sc_intr)
   1036   1.2    tsarna 			sc->sc_record.sc_intr(sc->sc_record.sc_arg);
   1037   1.1    tsarna 
   1038   1.1    tsarna 		/* clear interrupts */
   1039  1.22      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1040  1.22      kent 			AUVIA_RECORD_BASE + AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1041  1.17      fvdl 		rval = 1;
   1042   1.1    tsarna 	}
   1043  1.22      kent 
   1044  1.22      kent 	r = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
   1045  1.22      kent 		AUVIA_PLAY_BASE + AUVIA_RP_STAT);
   1046   1.1    tsarna 	if (r & AUVIA_RPSTAT_INTR) {
   1047   1.2    tsarna 		if (sc->sc_play.sc_intr)
   1048   1.2    tsarna 			sc->sc_play.sc_intr(sc->sc_play.sc_arg);
   1049   1.1    tsarna 
   1050   1.1    tsarna 		/* clear interrupts */
   1051  1.22      kent 		bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1052  1.22      kent 			AUVIA_PLAY_BASE + AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1053  1.17      fvdl 		rval = 1;
   1054   1.1    tsarna 	}
   1055   1.1    tsarna 
   1056  1.17      fvdl 	return rval;
   1057   1.1    tsarna }
   1058