Home | History | Annotate | Line # | Download | only in pci
auvia.c revision 1.82.2.4
      1  1.82.2.4     isaki /*	$NetBSD: auvia.c,v 1.82.2.4 2019/05/04 07:20:10 isaki Exp $	*/
      2       1.1    tsarna 
      3       1.1    tsarna /*-
      4      1.74  jmcneill  * Copyright (c) 2000, 2008 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.74  jmcneill  * 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  *
     19       1.1    tsarna  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1    tsarna  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1    tsarna  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1    tsarna  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1    tsarna  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1    tsarna  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1    tsarna  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1    tsarna  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1    tsarna  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1    tsarna  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1    tsarna  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1    tsarna  */
     31       1.1    tsarna 
     32       1.1    tsarna /*
     33      1.22      kent  * VIA Technologies VT82C686A / VT8233 / VT8235 Southbridge Audio Driver
     34       1.1    tsarna  *
     35       1.1    tsarna  * Documentation links:
     36       1.1    tsarna  *
     37       1.1    tsarna  * ftp://ftp.alsa-project.org/pub/manuals/via/686a.pdf
     38       1.1    tsarna  * ftp://ftp.alsa-project.org/pub/manuals/general/ac97r21.pdf
     39       1.1    tsarna  * ftp://ftp.alsa-project.org/pub/manuals/ad/AD1881_0.pdf (example AC'97 codec)
     40       1.1    tsarna  */
     41      1.14     lukem 
     42      1.14     lukem #include <sys/cdefs.h>
     43  1.82.2.4     isaki __KERNEL_RCSID(0, "$NetBSD: auvia.c,v 1.82.2.4 2019/05/04 07:20:10 isaki Exp $");
     44       1.1    tsarna 
     45       1.1    tsarna #include <sys/param.h>
     46       1.1    tsarna #include <sys/systm.h>
     47      1.74  jmcneill #include <sys/kmem.h>
     48       1.1    tsarna #include <sys/device.h>
     49       1.1    tsarna #include <sys/audioio.h>
     50       1.1    tsarna 
     51       1.1    tsarna #include <dev/pci/pcidevs.h>
     52       1.1    tsarna #include <dev/pci/pcivar.h>
     53       1.1    tsarna 
     54  1.82.2.4     isaki #include <dev/audio/audio_if.h>
     55       1.1    tsarna 
     56      1.10  augustss #include <dev/ic/ac97reg.h>
     57       1.3   thorpej #include <dev/ic/ac97var.h>
     58       1.1    tsarna 
     59       1.1    tsarna #include <dev/pci/auviavar.h>
     60       1.1    tsarna 
     61      1.74  jmcneill #define	AUVIA_MINBLKSZ		512
     62      1.74  jmcneill 
     63       1.1    tsarna struct auvia_dma {
     64       1.1    tsarna 	struct auvia_dma *next;
     65      1.60  christos 	void *addr;
     66       1.1    tsarna 	size_t size;
     67       1.1    tsarna 	bus_dmamap_t map;
     68       1.1    tsarna 	bus_dma_segment_t seg;
     69       1.1    tsarna };
     70       1.1    tsarna 
     71       1.1    tsarna struct auvia_dma_op {
     72      1.51      kent 	uint32_t ptr;
     73      1.51      kent 	uint32_t flags;
     74       1.1    tsarna #define AUVIA_DMAOP_EOL		0x80000000
     75       1.1    tsarna #define AUVIA_DMAOP_FLAG	0x40000000
     76       1.1    tsarna #define AUVIA_DMAOP_STOP	0x20000000
     77       1.1    tsarna #define AUVIA_DMAOP_COUNT(x)	((x)&0x00FFFFFF)
     78       1.1    tsarna };
     79       1.1    tsarna 
     80      1.68    cegger static int	auvia_match(device_t, cfdata_t, void *);
     81      1.63    dyoung static void	auvia_attach(device_t, device_t, void *);
     82      1.63    dyoung static int	auvia_detach(device_t, int);
     83      1.63    dyoung static void	auvia_childdet(device_t, device_t);
     84      1.54      kent static int	auvia_open(void *, int);
     85      1.54      kent static void	auvia_close(void *);
     86  1.82.2.2     isaki static int	auvia_query_format(void *, audio_format_query_t *);
     87  1.82.2.2     isaki static void	auvia_set_format_sub(struct auvia_softc *,
     88      1.52   thorpej 				     struct auvia_softc_chan *,
     89      1.52   thorpej 				     const audio_params_t *);
     90  1.82.2.2     isaki static int	auvia_set_format(void *, int,
     91  1.82.2.2     isaki 				 const audio_params_t *, const audio_params_t *,
     92  1.82.2.2     isaki 				 audio_filter_reg_t *, audio_filter_reg_t *);
     93      1.52   thorpej static int	auvia_round_blocksize(void *, int, int, const audio_params_t *);
     94      1.52   thorpej static int	auvia_halt_output(void *);
     95      1.52   thorpej static int	auvia_halt_input(void *);
     96      1.52   thorpej static int	auvia_getdev(void *, struct audio_device *);
     97      1.52   thorpej static int	auvia_set_port(void *, mixer_ctrl_t *);
     98      1.52   thorpej static int	auvia_get_port(void *, mixer_ctrl_t *);
     99      1.52   thorpej static int	auvia_query_devinfo(void *, mixer_devinfo_t *);
    100      1.74  jmcneill static void *	auvia_malloc(void *, int, size_t);
    101      1.74  jmcneill static void	auvia_free(void *, void *, size_t);
    102      1.52   thorpej static int	auvia_get_props(void *);
    103      1.52   thorpej static int	auvia_build_dma_ops(struct auvia_softc *,
    104      1.52   thorpej 				    struct auvia_softc_chan *,
    105      1.52   thorpej 				    struct auvia_dma *, void *, void *, int);
    106      1.52   thorpej static int	auvia_trigger_output(void *, void *, void *, int,
    107      1.52   thorpej 				     void (*)(void *), void *,
    108      1.52   thorpej 				     const audio_params_t *);
    109      1.52   thorpej static int	auvia_trigger_input(void *, void *, void *, int,
    110      1.52   thorpej 				    void (*)(void *), void *,
    111      1.52   thorpej 				    const audio_params_t *);
    112      1.74  jmcneill static void	auvia_get_locks(void *, kmutex_t **, kmutex_t **);
    113      1.72    dyoung static bool	auvia_resume(device_t, const pmf_qual_t *);
    114      1.74  jmcneill 
    115      1.52   thorpej static int	auvia_intr(void *);
    116      1.74  jmcneill static void *	auvia_malloc_dmamem(void *, int, size_t);
    117      1.74  jmcneill static int	auvia_malloc_channel(struct auvia_softc *, struct auvia_softc_chan *,
    118      1.74  jmcneill 				     size_t);
    119      1.54      kent static int	auvia_attach_codec(void *, struct ac97_codec_if *);
    120      1.54      kent static int	auvia_write_codec(void *, uint8_t, uint16_t);
    121      1.54      kent static int	auvia_read_codec(void *, uint8_t, uint16_t *);
    122      1.54      kent static int	auvia_reset_codec(void *);
    123      1.54      kent static int	auvia_waitready_codec(struct auvia_softc *);
    124      1.54      kent static int	auvia_waitvalid_codec(struct auvia_softc *);
    125      1.59   thorpej static void	auvia_spdif_event(void *, bool);
    126      1.54      kent 
    127      1.69    cegger CFATTACH_DECL2_NEW(auvia, sizeof (struct auvia_softc),
    128      1.63    dyoung     auvia_match, auvia_attach, auvia_detach, NULL, NULL, auvia_childdet);
    129       1.1    tsarna 
    130      1.34    martin /* VIA VT823xx revision number */
    131      1.53    rpaulo #define VIA_REV_8233PRE	0x10
    132      1.34    martin #define VIA_REV_8233C	0x20
    133      1.34    martin #define VIA_REV_8233	0x30
    134      1.34    martin #define VIA_REV_8233A	0x40
    135      1.34    martin #define VIA_REV_8235	0x50
    136      1.45      kent #define VIA_REV_8237	0x60
    137      1.34    martin 
    138       1.1    tsarna #define AUVIA_PCICONF_JUNK	0x40
    139       1.1    tsarna #define		AUVIA_PCICONF_ENABLES	 0x00FF0000	/* reg 42 mask */
    140       1.1    tsarna #define		AUVIA_PCICONF_ACLINKENAB 0x00008000	/* ac link enab */
    141       1.1    tsarna #define		AUVIA_PCICONF_ACNOTRST	 0x00004000	/* ~(ac reset) */
    142       1.1    tsarna #define		AUVIA_PCICONF_ACSYNC	 0x00002000	/* ac sync */
    143       1.1    tsarna #define		AUVIA_PCICONF_ACVSR	 0x00000800	/* var. samp. rate */
    144       1.1    tsarna #define		AUVIA_PCICONF_ACSGD	 0x00000400	/* SGD enab */
    145       1.1    tsarna #define		AUVIA_PCICONF_ACFM	 0x00000200	/* FM enab */
    146       1.1    tsarna #define		AUVIA_PCICONF_ACSB	 0x00000100	/* SB enab */
    147      1.32      jmmv #define		AUVIA_PCICONF_PRIVALID	 0x00000001	/* primary codec rdy */
    148       1.1    tsarna 
    149      1.24      kent #define	AUVIA_PLAY_BASE			0x00
    150      1.24      kent #define	AUVIA_RECORD_BASE		0x10
    151      1.22      kent 
    152      1.27      kent /* *_RP_* are offsets from AUVIA_PLAY_BASE or AUVIA_RECORD_BASE */
    153      1.24      kent #define	AUVIA_RP_STAT			0x00
    154       1.1    tsarna #define		AUVIA_RPSTAT_INTR		0x03
    155      1.24      kent #define	AUVIA_RP_CONTROL		0x01
    156       1.1    tsarna #define		AUVIA_RPCTRL_START		0x80
    157       1.1    tsarna #define		AUVIA_RPCTRL_TERMINATE		0x40
    158      1.22      kent #define		AUVIA_RPCTRL_AUTOSTART		0x20
    159      1.22      kent /* The following are 8233 specific */
    160      1.22      kent #define		AUVIA_RPCTRL_STOP		0x04
    161      1.22      kent #define		AUVIA_RPCTRL_EOL		0x02
    162      1.22      kent #define		AUVIA_RPCTRL_FLAG		0x01
    163      1.27      kent #define	AUVIA_RP_MODE			0x02		/* 82c686 specific */
    164       1.1    tsarna #define		AUVIA_RPMODE_INTR_FLAG		0x01
    165       1.1    tsarna #define		AUVIA_RPMODE_INTR_EOL		0x02
    166       1.1    tsarna #define		AUVIA_RPMODE_STEREO		0x10
    167       1.1    tsarna #define		AUVIA_RPMODE_16BIT		0x20
    168       1.1    tsarna #define		AUVIA_RPMODE_AUTOSTART		0x80
    169      1.24      kent #define	AUVIA_RP_DMAOPS_BASE		0x04
    170      1.22      kent 
    171      1.24      kent #define	VIA8233_RP_DXS_LVOL		0x02
    172      1.24      kent #define	VIA8233_RP_DXS_RVOL		0x03
    173      1.24      kent #define	VIA8233_RP_RATEFMT		0x08
    174      1.22      kent #define		VIA8233_RATEFMT_48K		0xfffff
    175      1.22      kent #define		VIA8233_RATEFMT_STEREO		0x00100000
    176      1.22      kent #define		VIA8233_RATEFMT_16BIT		0x00200000
    177      1.22      kent 
    178      1.24      kent #define	VIA_RP_DMAOPS_COUNT		0x0c
    179       1.1    tsarna 
    180      1.27      kent #define VIA8233_MP_BASE			0x40
    181      1.27      kent 	/* STAT, CONTROL, DMAOPS_BASE, DMAOPS_COUNT are valid */
    182      1.27      kent #define VIA8233_OFF_MP_FORMAT		0x02
    183      1.27      kent #define		VIA8233_MP_FORMAT_8BIT		0x00
    184      1.27      kent #define		VIA8233_MP_FORMAT_16BIT		0x80
    185      1.27      kent #define		VIA8233_MP_FORMAT_CHANNLE_MASK	0x70 /* 1, 2, 4, 6 */
    186      1.27      kent #define VIA8233_OFF_MP_SCRATCH		0x03
    187      1.27      kent #define VIA8233_OFF_MP_STOP		0x08
    188      1.27      kent 
    189      1.66  jmcneill #define VIA8233_WR_BASE			0x60
    190      1.66  jmcneill 
    191       1.1    tsarna #define	AUVIA_CODEC_CTL			0x80
    192       1.1    tsarna #define		AUVIA_CODEC_READ		0x00800000
    193       1.1    tsarna #define		AUVIA_CODEC_BUSY		0x01000000
    194       1.1    tsarna #define		AUVIA_CODEC_PRIVALID		0x02000000
    195       1.1    tsarna #define		AUVIA_CODEC_INDEX(x)		((x)<<16)
    196       1.1    tsarna 
    197      1.27      kent #define CH_WRITE1(sc, ch, off, v)	\
    198      1.27      kent 	bus_space_write_1((sc)->sc_iot,	(sc)->sc_ioh, (ch)->sc_base + (off), v)
    199      1.27      kent #define CH_WRITE4(sc, ch, off, v)	\
    200      1.27      kent 	bus_space_write_4((sc)->sc_iot,	(sc)->sc_ioh, (ch)->sc_base + (off), v)
    201      1.27      kent #define CH_READ1(sc, ch, off)		\
    202      1.27      kent 	bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
    203      1.27      kent #define CH_READ4(sc, ch, off)		\
    204      1.27      kent 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
    205      1.27      kent 
    206       1.1    tsarna #define TIMEOUT	50
    207       1.1    tsarna 
    208      1.52   thorpej static const struct audio_hw_if auvia_hw_if = {
    209      1.82     isaki 	.open			= auvia_open,
    210      1.82     isaki 	.close			= auvia_close,
    211  1.82.2.2     isaki 	.query_format		= auvia_query_format,
    212  1.82.2.2     isaki 	.set_format		= auvia_set_format,
    213      1.82     isaki 	.round_blocksize	= auvia_round_blocksize,
    214      1.82     isaki 	.halt_output		= auvia_halt_output,
    215      1.82     isaki 	.halt_input		= auvia_halt_input,
    216      1.82     isaki 	.getdev			= auvia_getdev,
    217      1.82     isaki 	.set_port		= auvia_set_port,
    218      1.82     isaki 	.get_port		= auvia_get_port,
    219      1.82     isaki 	.query_devinfo		= auvia_query_devinfo,
    220      1.82     isaki 	.allocm			= auvia_malloc,
    221      1.82     isaki 	.freem			= auvia_free,
    222      1.82     isaki 	.get_props		= auvia_get_props,
    223      1.82     isaki 	.trigger_output		= auvia_trigger_output,
    224      1.82     isaki 	.trigger_input		= auvia_trigger_input,
    225      1.82     isaki 	.get_locks		= auvia_get_locks,
    226       1.1    tsarna };
    227       1.1    tsarna 
    228  1.82.2.2     isaki #define AUVIA_FORMATS_4CH	2
    229  1.82.2.2     isaki #define AUVIA_FORMATS_6CH	3
    230  1.82.2.2     isaki #define AUVIA_FORMAT(aumode, ch, chmask) \
    231  1.82.2.1     isaki 	{ \
    232  1.82.2.1     isaki 		.mode		= (aumode), \
    233  1.82.2.1     isaki 		.encoding	= AUDIO_ENCODING_SLINEAR_LE, \
    234  1.82.2.1     isaki 		.validbits	= 16, \
    235  1.82.2.1     isaki 		.precision	= 16, \
    236  1.82.2.1     isaki 		.channels	= (ch), \
    237  1.82.2.1     isaki 		.channel_mask	= (chmask), \
    238  1.82.2.1     isaki 		.frequency_type	= 0, \
    239  1.82.2.1     isaki 		.frequency	= { 4000, 48000 }, \
    240  1.82.2.1     isaki 	}
    241      1.48      kent static const struct audio_format auvia_formats[AUVIA_NFORMATS] = {
    242  1.82.2.2     isaki 	AUVIA_FORMAT(AUMODE_PLAY | AUMODE_RECORD, 1, AUFMT_MONAURAL),
    243  1.82.2.2     isaki 	AUVIA_FORMAT(AUMODE_PLAY | AUMODE_RECORD, 2, AUFMT_STEREO),
    244  1.82.2.2     isaki 	AUVIA_FORMAT(AUMODE_PLAY                , 4, AUFMT_SURROUND4),
    245  1.82.2.2     isaki 	AUVIA_FORMAT(AUMODE_PLAY                , 6, AUFMT_DOLBY_5_1),
    246      1.48      kent };
    247      1.48      kent 
    248      1.54      kent #define	AUVIA_SPDIF_NFORMATS	1
    249      1.54      kent static const struct audio_format auvia_spdif_formats[AUVIA_SPDIF_NFORMATS] = {
    250  1.82.2.1     isaki 	{
    251  1.82.2.1     isaki 		.mode		= AUMODE_PLAY,
    252  1.82.2.1     isaki 		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
    253  1.82.2.1     isaki 		.validbits	= 16,
    254  1.82.2.1     isaki 		.precision	= 16,
    255  1.82.2.1     isaki 		.channels	= 2,
    256  1.82.2.1     isaki 		.channel_mask	= AUFMT_STEREO,
    257  1.82.2.1     isaki 		.frequency_type	= 1,
    258  1.82.2.1     isaki 		.frequency	= { 48000 },
    259  1.82.2.1     isaki 	},
    260      1.54      kent };
    261       1.1    tsarna 
    262       1.1    tsarna 
    263      1.52   thorpej static int
    264      1.68    cegger auvia_match(device_t parent, cfdata_t match, void *aux)
    265       1.1    tsarna {
    266      1.51      kent 	struct pci_attach_args *pa;
    267       1.1    tsarna 
    268      1.51      kent 	pa = (struct pci_attach_args *) aux;
    269       1.1    tsarna 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
    270       1.1    tsarna 		return 0;
    271      1.15  jmcneill 	switch (PCI_PRODUCT(pa->pa_id)) {
    272      1.15  jmcneill 	case PCI_PRODUCT_VIATECH_VT82C686A_AC97:
    273      1.22      kent 	case PCI_PRODUCT_VIATECH_VT8233_AC97:
    274      1.15  jmcneill 		break;
    275      1.15  jmcneill 	default:
    276       1.1    tsarna 		return 0;
    277      1.15  jmcneill 	}
    278       1.1    tsarna 
    279       1.1    tsarna 	return 1;
    280       1.1    tsarna }
    281       1.1    tsarna 
    282      1.52   thorpej static void
    283      1.63    dyoung auvia_childdet(device_t self, device_t child)
    284      1.63    dyoung {
    285      1.63    dyoung 	/* we hold no child references, so do nothing */
    286      1.63    dyoung }
    287      1.63    dyoung 
    288      1.63    dyoung static int
    289      1.63    dyoung auvia_detach(device_t self, int flags)
    290      1.63    dyoung {
    291      1.63    dyoung 	int rc;
    292      1.63    dyoung 	struct auvia_softc *sc = device_private(self);
    293      1.63    dyoung 
    294      1.63    dyoung 	if ((rc = config_detach_children(self, flags)) != 0)
    295      1.63    dyoung 		return rc;
    296      1.63    dyoung 	pmf_device_deregister(self);
    297      1.63    dyoung 
    298      1.74  jmcneill 	mutex_enter(&sc->sc_lock);
    299      1.63    dyoung 	if (sc->codec_if != NULL)
    300      1.63    dyoung 		sc->codec_if->vtbl->detach(sc->codec_if);
    301      1.74  jmcneill 	mutex_exit(&sc->sc_lock);
    302      1.63    dyoung 
    303      1.63    dyoung 	/* XXX restore compatibility? */
    304      1.63    dyoung 	if (sc->sc_ih != NULL)
    305      1.63    dyoung 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    306      1.63    dyoung 
    307      1.63    dyoung 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    308      1.74  jmcneill 	if (sc->sc_play.sc_dma_ops != NULL) {
    309      1.74  jmcneill 		auvia_free(sc, sc->sc_play.sc_dma_ops,
    310      1.74  jmcneill 		    sc->sc_play.sc_dma_op_count *
    311      1.74  jmcneill 		    sizeof(struct auvia_dma_op));
    312      1.74  jmcneill 	}
    313      1.74  jmcneill 	if (sc->sc_record.sc_dma_ops != NULL) {
    314      1.74  jmcneill 		auvia_free(sc, sc->sc_record.sc_dma_ops,
    315      1.74  jmcneill 		    sc->sc_play.sc_dma_op_count *
    316      1.74  jmcneill 		    sizeof(struct auvia_dma_op));
    317      1.74  jmcneill 	}
    318      1.74  jmcneill 	mutex_destroy(&sc->sc_lock);
    319      1.74  jmcneill 	mutex_destroy(&sc->sc_intr_lock);
    320      1.63    dyoung 
    321      1.63    dyoung 	return 0;
    322      1.63    dyoung }
    323      1.63    dyoung 
    324      1.63    dyoung static void
    325      1.63    dyoung auvia_attach(device_t parent, device_t self, void *aux)
    326       1.1    tsarna {
    327      1.51      kent 	struct pci_attach_args *pa;
    328      1.51      kent 	struct auvia_softc *sc;
    329      1.51      kent 	const char *intrstr;
    330      1.51      kent 	pci_chipset_tag_t pc;
    331      1.51      kent 	pcitag_t pt;
    332      1.22      kent 	pci_intr_handle_t ih;
    333       1.1    tsarna 	pcireg_t pr;
    334      1.27      kent 	int r;
    335      1.51      kent 	const char *revnum;	/* VT823xx revision number */
    336      1.77  christos 	char intrbuf[PCI_INTRSTR_LEN];
    337      1.51      kent 
    338      1.51      kent 	pa = aux;
    339      1.63    dyoung 	sc = device_private(self);
    340      1.69    cegger 	sc->sc_dev = self;
    341      1.51      kent 	intrstr = NULL;
    342      1.51      kent 	pc = pa->pa_pc;
    343      1.51      kent 	pt = pa->pa_tag;
    344      1.51      kent 	revnum = NULL;
    345      1.22      kent 
    346      1.29   thorpej 	aprint_naive(": Audio controller\n");
    347      1.29   thorpej 
    348      1.27      kent 	sc->sc_play.sc_base = AUVIA_PLAY_BASE;
    349      1.27      kent 	sc->sc_record.sc_base = AUVIA_RECORD_BASE;
    350      1.27      kent 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT8233_AC97) {
    351      1.22      kent 		sc->sc_flags |= AUVIA_FLAGS_VT8233;
    352      1.27      kent 		sc->sc_play.sc_base = VIA8233_MP_BASE;
    353      1.66  jmcneill 		sc->sc_record.sc_base = VIA8233_WR_BASE;
    354      1.27      kent 	}
    355      1.22      kent 
    356      1.22      kent 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
    357      1.63    dyoung 		&sc->sc_ioh, NULL, &sc->sc_iosize)) {
    358      1.29   thorpej 		aprint_error(": can't map i/o space\n");
    359      1.22      kent 		return;
    360      1.22      kent 	}
    361      1.22      kent 
    362      1.22      kent 	sc->sc_dmat = pa->pa_dmat;
    363      1.22      kent 	sc->sc_pc = pc;
    364      1.22      kent 	sc->sc_pt = pt;
    365       1.1    tsarna 
    366       1.1    tsarna 	r = PCI_REVISION(pa->pa_class);
    367      1.22      kent 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    368      1.39    itojun 		snprintf(sc->sc_revision, sizeof(sc->sc_revision), "0x%02X", r);
    369      1.34    martin 		switch(r) {
    370      1.53    rpaulo 		case VIA_REV_8233PRE:
    371      1.53    rpaulo 			/* same as 8233, but should not be in the market */
    372      1.53    rpaulo 			revnum = "3-Pre";
    373      1.53    rpaulo 			break;
    374      1.34    martin 		case VIA_REV_8233C:
    375      1.34    martin 			/* 2 rec, 4 pb, 1 multi-pb */
    376      1.34    martin 			revnum = "3C";
    377      1.34    martin 			break;
    378      1.34    martin 		case VIA_REV_8233:
    379      1.34    martin 			/* 2 rec, 4 pb, 1 multi-pb, spdif */
    380      1.34    martin 			revnum = "3";
    381      1.34    martin 			break;
    382      1.34    martin 		case VIA_REV_8233A:
    383      1.34    martin 			/* 1 rec, 1 multi-pb, spdif */
    384      1.34    martin 			revnum = "3A";
    385      1.34    martin 			break;
    386      1.34    martin 		default:
    387      1.36   xtraeme 			break;
    388      1.22      kent 		}
    389      1.49   xtraeme 		if (r >= VIA_REV_8237)
    390      1.45      kent 			revnum = "7";
    391      1.45      kent 		else if (r >= VIA_REV_8235) /* 2 rec, 4 pb, 1 multi-pb, spdif */
    392      1.35   xtraeme 			revnum = "5";
    393      1.38      jmmv 		aprint_normal(": VIA Technologies VT823%s AC'97 Audio "
    394      1.38      jmmv 		    "(rev %s)\n", revnum, sc->sc_revision);
    395       1.1    tsarna 	} else {
    396      1.22      kent 		sc->sc_revision[1] = '\0';
    397      1.22      kent 		if (r == 0x20) {
    398      1.22      kent 			sc->sc_revision[0] = 'H';
    399      1.22      kent 		} else if ((r >= 0x10) && (r <= 0x14)) {
    400      1.22      kent 			sc->sc_revision[0] = 'A' + (r - 0x10);
    401      1.22      kent 		} else {
    402      1.39    itojun 			snprintf(sc->sc_revision, sizeof(sc->sc_revision),
    403      1.39    itojun 			    "0x%02X", r);
    404      1.22      kent 		}
    405      1.22      kent 
    406      1.38      jmmv 		aprint_normal(": VIA Technologies VT82C686A AC'97 Audio "
    407      1.38      jmmv 		    "(rev %s)\n", sc->sc_revision);
    408       1.1    tsarna 	}
    409       1.1    tsarna 
    410       1.9  sommerfe 	if (pci_intr_map(pa, &ih)) {
    411      1.29   thorpej 		aprint_error(": couldn't map interrupt\n");
    412      1.63    dyoung 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    413       1.1    tsarna 		return;
    414       1.1    tsarna 	}
    415      1.77  christos 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    416       1.1    tsarna 
    417      1.74  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    418      1.75       mrg 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    419      1.74  jmcneill 
    420      1.80  jdolecek 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, auvia_intr, sc,
    421      1.80  jdolecek 	    device_xname(self));
    422       1.1    tsarna 	if (sc->sc_ih == NULL) {
    423      1.69    cegger 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
    424       1.1    tsarna 		if (intrstr != NULL)
    425      1.70     njoly 			aprint_error(" at %s", intrstr);
    426      1.70     njoly 		aprint_error("\n");
    427      1.63    dyoung 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    428      1.74  jmcneill 		mutex_destroy(&sc->sc_lock);
    429      1.74  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    430       1.1    tsarna 		return;
    431       1.1    tsarna 	}
    432       1.1    tsarna 
    433      1.69    cegger 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    434       1.1    tsarna 
    435       1.1    tsarna 	/* disable SBPro compat & others */
    436       1.1    tsarna 	pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
    437       1.1    tsarna 
    438       1.1    tsarna 	pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
    439       1.1    tsarna 	/* XXX what to do about MIDI, FM, joystick? */
    440       1.1    tsarna 
    441       1.1    tsarna 	pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
    442       1.1    tsarna 		| AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
    443       1.1    tsarna 
    444       1.1    tsarna 	pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
    445       1.1    tsarna 
    446       1.1    tsarna 	pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
    447       1.1    tsarna 
    448       1.1    tsarna 	sc->host_if.arg = sc;
    449       1.1    tsarna 	sc->host_if.attach = auvia_attach_codec;
    450       1.1    tsarna 	sc->host_if.read = auvia_read_codec;
    451       1.1    tsarna 	sc->host_if.write = auvia_write_codec;
    452       1.1    tsarna 	sc->host_if.reset = auvia_reset_codec;
    453      1.54      kent 	sc->host_if.spdif_event = auvia_spdif_event;
    454       1.1    tsarna 
    455      1.74  jmcneill 	if ((r = ac97_attach(&sc->host_if, self, &sc->sc_lock)) != 0) {
    456      1.69    cegger 		aprint_error_dev(sc->sc_dev, "can't attach codec (error 0x%X)\n", r);
    457      1.22      kent 		pci_intr_disestablish(pc, sc->sc_ih);
    458      1.63    dyoung 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    459      1.74  jmcneill 		mutex_destroy(&sc->sc_lock);
    460      1.74  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    461       1.1    tsarna 		return;
    462       1.1    tsarna 	}
    463       1.1    tsarna 
    464      1.48      kent 	/* setup audio_format */
    465      1.48      kent 	memcpy(sc->sc_formats, auvia_formats, sizeof(auvia_formats));
    466      1.74  jmcneill 	mutex_enter(&sc->sc_lock);
    467      1.48      kent 	if (sc->sc_play.sc_base != VIA8233_MP_BASE || !AC97_IS_4CH(sc->codec_if)) {
    468  1.82.2.2     isaki 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_4CH]);
    469      1.48      kent 	}
    470      1.48      kent 	if (sc->sc_play.sc_base != VIA8233_MP_BASE || !AC97_IS_6CH(sc->codec_if)) {
    471  1.82.2.2     isaki 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_6CH]);
    472      1.48      kent 	}
    473      1.48      kent 	if (AC97_IS_FIXED_RATE(sc->codec_if)) {
    474      1.48      kent 		for (r = 0; r < AUVIA_NFORMATS; r++) {
    475      1.48      kent 			sc->sc_formats[r].frequency_type = 1;
    476      1.48      kent 			sc->sc_formats[r].frequency[0] = 48000;
    477      1.48      kent 		}
    478      1.48      kent 	}
    479      1.74  jmcneill 	mutex_exit(&sc->sc_lock);
    480      1.48      kent 
    481      1.61  jmcneill 	if (!pmf_device_register(self, NULL, auvia_resume))
    482      1.61  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    483      1.43      kent 
    484      1.69    cegger 	audio_attach_mi(&auvia_hw_if, sc, sc->sc_dev);
    485      1.74  jmcneill 	mutex_enter(&sc->sc_lock);
    486      1.54      kent 	sc->codec_if->vtbl->unlock(sc->codec_if);
    487      1.74  jmcneill 	mutex_exit(&sc->sc_lock);
    488      1.48      kent 	return;
    489       1.1    tsarna }
    490       1.1    tsarna 
    491      1.52   thorpej static int
    492       1.1    tsarna auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
    493       1.1    tsarna {
    494      1.51      kent 	struct auvia_softc *sc;
    495       1.1    tsarna 
    496      1.51      kent 	sc = addr;
    497       1.1    tsarna 	sc->codec_if = cif;
    498       1.1    tsarna 	return 0;
    499       1.1    tsarna }
    500       1.1    tsarna 
    501      1.52   thorpej static int
    502       1.1    tsarna auvia_reset_codec(void *addr)
    503       1.1    tsarna {
    504      1.51      kent 	struct auvia_softc *sc;
    505      1.51      kent 	pcireg_t r;
    506      1.32      jmmv 	int i;
    507       1.1    tsarna 
    508       1.1    tsarna 	/* perform a codec cold reset */
    509      1.51      kent 	sc = addr;
    510       1.1    tsarna 	r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
    511       1.1    tsarna 
    512       1.1    tsarna 	r &= ~AUVIA_PCICONF_ACNOTRST;	/* enable RESET (active low) */
    513       1.1    tsarna 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    514       1.1    tsarna 	delay(2);
    515       1.1    tsarna 
    516      1.22      kent 	r |= AUVIA_PCICONF_ACNOTRST;	/* disable RESET (inactive high) */
    517       1.1    tsarna 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    518       1.1    tsarna 	delay(200);
    519       1.1    tsarna 
    520      1.32      jmmv 	for (i = 500000; i != 0 && !(pci_conf_read(sc->sc_pc, sc->sc_pt,
    521      1.32      jmmv 		AUVIA_PCICONF_JUNK) & AUVIA_PCICONF_PRIVALID); i--)
    522      1.32      jmmv 		DELAY(1);
    523      1.42      kent 	if (i == 0) {
    524      1.69    cegger 		printf("%s: codec reset timed out\n", device_xname(sc->sc_dev));
    525      1.42      kent 		return ETIMEDOUT;
    526      1.42      kent 	}
    527      1.42      kent 	return 0;
    528       1.1    tsarna }
    529       1.1    tsarna 
    530      1.52   thorpej static int
    531       1.1    tsarna auvia_waitready_codec(struct auvia_softc *sc)
    532       1.1    tsarna {
    533       1.1    tsarna 	int i;
    534       1.1    tsarna 
    535       1.1    tsarna 	/* poll until codec not busy */
    536       1.1    tsarna 	for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    537       1.1    tsarna 		AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
    538       1.1    tsarna 		delay(1);
    539       1.1    tsarna 	if (i >= TIMEOUT) {
    540      1.69    cegger 		printf("%s: codec busy\n", device_xname(sc->sc_dev));
    541       1.1    tsarna 		return 1;
    542       1.1    tsarna 	}
    543       1.1    tsarna 
    544       1.1    tsarna 	return 0;
    545       1.1    tsarna }
    546       1.1    tsarna 
    547      1.52   thorpej static int
    548       1.1    tsarna auvia_waitvalid_codec(struct auvia_softc *sc)
    549       1.1    tsarna {
    550       1.1    tsarna 	int i;
    551       1.1    tsarna 
    552       1.1    tsarna 	/* poll until codec valid */
    553       1.1    tsarna 	for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    554       1.1    tsarna 		AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
    555       1.1    tsarna 			delay(1);
    556       1.1    tsarna 	if (i >= TIMEOUT) {
    557      1.69    cegger 		printf("%s: codec invalid\n", device_xname(sc->sc_dev));
    558       1.1    tsarna 		return 1;
    559       1.1    tsarna 	}
    560       1.1    tsarna 
    561       1.1    tsarna 	return 0;
    562       1.1    tsarna }
    563       1.1    tsarna 
    564      1.52   thorpej static int
    565       1.1    tsarna auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
    566       1.1    tsarna {
    567      1.51      kent 	struct auvia_softc *sc;
    568       1.1    tsarna 
    569      1.51      kent 	sc = addr;
    570       1.1    tsarna 	if (auvia_waitready_codec(sc))
    571       1.1    tsarna 		return 1;
    572       1.1    tsarna 
    573       1.1    tsarna 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    574       1.1    tsarna 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
    575       1.1    tsarna 
    576       1.1    tsarna 	return 0;
    577       1.1    tsarna }
    578       1.1    tsarna 
    579      1.52   thorpej static int
    580       1.1    tsarna auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
    581       1.1    tsarna {
    582      1.51      kent 	struct auvia_softc *sc;
    583       1.1    tsarna 
    584      1.51      kent 	sc = addr;
    585       1.1    tsarna 	if (auvia_waitready_codec(sc))
    586       1.1    tsarna 		return 1;
    587       1.1    tsarna 
    588       1.1    tsarna 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    589       1.1    tsarna 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
    590       1.1    tsarna 
    591       1.1    tsarna 	if (auvia_waitready_codec(sc))
    592       1.1    tsarna 		return 1;
    593       1.1    tsarna 
    594       1.1    tsarna 	if (auvia_waitvalid_codec(sc))
    595       1.1    tsarna 		return 1;
    596       1.1    tsarna 
    597       1.1    tsarna 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
    598       1.1    tsarna 
    599       1.1    tsarna 	return 0;
    600       1.1    tsarna }
    601       1.1    tsarna 
    602      1.54      kent static void
    603      1.59   thorpej auvia_spdif_event(void *addr, bool flag)
    604      1.54      kent {
    605      1.54      kent 	struct auvia_softc *sc;
    606      1.54      kent 
    607      1.54      kent 	sc = addr;
    608      1.54      kent 	sc->sc_spdif = flag;
    609      1.54      kent }
    610      1.54      kent 
    611      1.54      kent static int
    612      1.58  christos auvia_open(void *addr, int flags)
    613      1.54      kent {
    614      1.54      kent 	struct auvia_softc *sc;
    615      1.54      kent 
    616      1.54      kent 	sc = (struct auvia_softc *)addr;
    617      1.74  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    618      1.54      kent 	sc->codec_if->vtbl->lock(sc->codec_if);
    619      1.74  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    620      1.54      kent 	return 0;
    621      1.54      kent }
    622      1.54      kent 
    623      1.54      kent static void
    624      1.54      kent auvia_close(void *addr)
    625      1.54      kent {
    626      1.54      kent 	struct auvia_softc *sc;
    627      1.54      kent 
    628      1.54      kent 	sc = (struct auvia_softc *)addr;
    629      1.74  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    630      1.54      kent 	sc->codec_if->vtbl->unlock(sc->codec_if);
    631      1.74  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    632      1.54      kent }
    633      1.54      kent 
    634      1.52   thorpej static int
    635  1.82.2.2     isaki auvia_query_format(void *addr, audio_format_query_t *afp)
    636       1.1    tsarna {
    637      1.48      kent 	struct auvia_softc *sc;
    638      1.48      kent 
    639      1.48      kent 	sc = (struct auvia_softc *)addr;
    640  1.82.2.2     isaki 	if (sc->sc_spdif) {
    641  1.82.2.2     isaki 		return audio_query_format(auvia_spdif_formats,
    642  1.82.2.2     isaki 		    AUVIA_SPDIF_NFORMATS, afp);
    643  1.82.2.2     isaki 	} else {
    644  1.82.2.2     isaki 		return audio_query_format(sc->sc_formats,
    645  1.82.2.2     isaki 		    AUVIA_NFORMATS, afp);
    646  1.82.2.2     isaki 	}
    647       1.1    tsarna }
    648       1.1    tsarna 
    649      1.52   thorpej static void
    650  1.82.2.2     isaki auvia_set_format_sub(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    651      1.50      kent 		     const audio_params_t *p)
    652      1.27      kent {
    653      1.51      kent 	uint32_t v;
    654      1.51      kent 	uint16_t regval;
    655      1.27      kent 
    656      1.27      kent 	if (!(sc->sc_flags & AUVIA_FLAGS_VT8233)) {
    657      1.50      kent 		regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
    658      1.50      kent 			| (p->precision  == 16 ?
    659      1.27      kent 				AUVIA_RPMODE_16BIT : 0)
    660      1.27      kent 			| AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
    661      1.27      kent 			| AUVIA_RPMODE_AUTOSTART;
    662      1.27      kent 		ch->sc_reg = regval;
    663      1.27      kent 	} else if (ch->sc_base != VIA8233_MP_BASE) {
    664      1.27      kent 		v = CH_READ4(sc, ch, VIA8233_RP_RATEFMT);
    665      1.27      kent 		v &= ~(VIA8233_RATEFMT_48K | VIA8233_RATEFMT_STEREO
    666      1.27      kent 			| VIA8233_RATEFMT_16BIT);
    667      1.27      kent 
    668      1.50      kent 		v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20)
    669      1.27      kent 			/ (48000 / 20);
    670      1.50      kent 		if (p->channels == 2)
    671      1.27      kent 			v |= VIA8233_RATEFMT_STEREO;
    672      1.50      kent 		if (p->precision == 16)
    673      1.27      kent 			v |= VIA8233_RATEFMT_16BIT;
    674      1.27      kent 
    675      1.27      kent 		CH_WRITE4(sc, ch, VIA8233_RP_RATEFMT, v);
    676      1.27      kent 	} else {
    677      1.27      kent 		static const u_int32_t slottab[7] =
    678      1.27      kent 			{ 0, 0xff000011, 0xff000021, 0,
    679      1.27      kent 			  0xff004321, 0, 0xff436521};
    680      1.27      kent 
    681      1.50      kent 		regval = (p->precision == 16
    682      1.27      kent 			? VIA8233_MP_FORMAT_16BIT : VIA8233_MP_FORMAT_8BIT)
    683      1.50      kent 			| (p->channels << 4);
    684      1.27      kent 		CH_WRITE1(sc, ch, VIA8233_OFF_MP_FORMAT, regval);
    685      1.50      kent 		CH_WRITE4(sc, ch, VIA8233_OFF_MP_STOP, slottab[p->channels]);
    686      1.27      kent 	}
    687      1.27      kent }
    688       1.1    tsarna 
    689      1.52   thorpej static int
    690  1.82.2.2     isaki auvia_set_format(void *addr, int setmode,
    691  1.82.2.2     isaki     const audio_params_t *play, const audio_params_t *rec,
    692  1.82.2.2     isaki     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    693       1.1    tsarna {
    694      1.51      kent 	struct auvia_softc *sc;
    695      1.27      kent 	struct auvia_softc_chan *ch;
    696  1.82.2.2     isaki 	const struct audio_params *p;
    697      1.27      kent 	struct ac97_codec_if* codec;
    698      1.27      kent 	int reg, mode;
    699  1.82.2.2     isaki 	int rate;
    700       1.1    tsarna 
    701      1.51      kent 	sc = addr;
    702      1.27      kent 	codec = sc->codec_if;
    703       1.1    tsarna 	/* for mode in (RECORD, PLAY) */
    704      1.22      kent 	for (mode = AUMODE_RECORD; mode != -1;
    705       1.1    tsarna 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    706       1.1    tsarna 		if ((setmode & mode) == 0)
    707       1.1    tsarna 			continue;
    708       1.1    tsarna 
    709  1.82.2.2     isaki 		if (mode == AUMODE_PLAY) {
    710      1.22      kent 			p = play;
    711      1.27      kent 			ch = &sc->sc_play;
    712      1.28      kent 			reg = AC97_REG_PCM_FRONT_DAC_RATE;
    713      1.22      kent 		} else {
    714      1.22      kent 			p = rec;
    715      1.27      kent 			ch = &sc->sc_record;
    716      1.28      kent 			reg = AC97_REG_PCM_LR_ADC_RATE;
    717      1.22      kent 		}
    718      1.22      kent 
    719      1.48      kent 		if (!AC97_IS_FIXED_RATE(codec)) {
    720  1.82.2.2     isaki 			rate = p->sample_rate;
    721  1.82.2.2     isaki 			if (codec->vtbl->set_rate(codec, reg, &rate))
    722      1.48      kent 				return EINVAL;
    723      1.27      kent 			reg = AC97_REG_PCM_SURR_DAC_RATE;
    724  1.82.2.2     isaki 			rate = p->sample_rate;
    725      1.50      kent 			if (p->channels >= 4
    726  1.82.2.2     isaki 			    && codec->vtbl->set_rate(codec, reg, &rate))
    727      1.48      kent 				return EINVAL;
    728      1.27      kent 			reg = AC97_REG_PCM_LFE_DAC_RATE;
    729  1.82.2.2     isaki 			rate = p->sample_rate;
    730      1.50      kent 			if (p->channels == 6
    731  1.82.2.2     isaki 			    && codec->vtbl->set_rate(codec, reg, &rate))
    732      1.48      kent 				return EINVAL;
    733       1.1    tsarna 		}
    734  1.82.2.2     isaki 		auvia_set_format_sub(sc, ch, p);
    735       1.1    tsarna 	}
    736       1.1    tsarna 
    737       1.1    tsarna 	return 0;
    738       1.1    tsarna }
    739       1.1    tsarna 
    740      1.52   thorpej static int
    741      1.50      kent auvia_round_blocksize(void *addr, int blk,
    742      1.58  christos     int mode, const audio_params_t *param)
    743       1.1    tsarna {
    744      1.51      kent 	struct auvia_softc *sc;
    745      1.37  jmcneill 
    746      1.51      kent 	sc = addr;
    747      1.74  jmcneill 
    748      1.37  jmcneill 	/* XXX VT823x might have the limitation of dma_ops size */
    749      1.37  jmcneill 	if (sc->sc_flags & AUVIA_FLAGS_VT8233 && blk < 288)
    750      1.37  jmcneill 		blk = 288;
    751      1.37  jmcneill 
    752      1.74  jmcneill 	/* Avoid too many dma_ops. */
    753      1.79  riastrad 	return uimin((blk & -32), AUVIA_MINBLKSZ);
    754       1.1    tsarna }
    755       1.1    tsarna 
    756      1.52   thorpej static int
    757       1.1    tsarna auvia_halt_output(void *addr)
    758       1.1    tsarna {
    759      1.51      kent 	struct auvia_softc *sc;
    760      1.51      kent 	struct auvia_softc_chan *ch;
    761       1.1    tsarna 
    762      1.51      kent 	sc = addr;
    763      1.51      kent 	ch = &(sc->sc_play);
    764      1.27      kent 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    765      1.41   mycroft 	ch->sc_intr = NULL;
    766       1.1    tsarna 	return 0;
    767       1.1    tsarna }
    768       1.1    tsarna 
    769      1.52   thorpej static int
    770       1.1    tsarna auvia_halt_input(void *addr)
    771       1.1    tsarna {
    772      1.51      kent 	struct auvia_softc *sc;
    773      1.51      kent 	struct auvia_softc_chan *ch;
    774       1.1    tsarna 
    775      1.51      kent 	sc = addr;
    776      1.51      kent 	ch = &(sc->sc_record);
    777      1.27      kent 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    778      1.41   mycroft 	ch->sc_intr = NULL;
    779       1.1    tsarna 	return 0;
    780       1.1    tsarna }
    781       1.1    tsarna 
    782      1.52   thorpej static int
    783       1.1    tsarna auvia_getdev(void *addr, struct audio_device *retp)
    784       1.1    tsarna {
    785      1.51      kent 	struct auvia_softc *sc;
    786       1.1    tsarna 
    787       1.1    tsarna 	if (retp) {
    788      1.51      kent 		sc = addr;
    789      1.22      kent 		if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    790      1.45      kent 			strncpy(retp->name, "VIA VT823x",
    791      1.22      kent 				sizeof(retp->name));
    792      1.22      kent 		} else {
    793      1.22      kent 			strncpy(retp->name, "VIA VT82C686A",
    794      1.22      kent 				sizeof(retp->name));
    795      1.22      kent 		}
    796       1.1    tsarna 		strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
    797       1.1    tsarna 		strncpy(retp->config, "auvia", sizeof(retp->config));
    798       1.1    tsarna 	}
    799       1.1    tsarna 
    800       1.1    tsarna 	return 0;
    801       1.1    tsarna }
    802       1.1    tsarna 
    803      1.52   thorpej static int
    804       1.1    tsarna auvia_set_port(void *addr, mixer_ctrl_t *cp)
    805       1.1    tsarna {
    806      1.51      kent 	struct auvia_softc *sc;
    807       1.1    tsarna 
    808      1.51      kent 	sc = addr;
    809      1.51      kent 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    810       1.1    tsarna }
    811       1.1    tsarna 
    812      1.52   thorpej static int
    813       1.1    tsarna auvia_get_port(void *addr, mixer_ctrl_t *cp)
    814       1.1    tsarna {
    815      1.51      kent 	struct auvia_softc *sc;
    816       1.1    tsarna 
    817      1.51      kent 	sc = addr;
    818      1.51      kent 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
    819       1.1    tsarna }
    820       1.1    tsarna 
    821      1.52   thorpej static int
    822       1.1    tsarna auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
    823       1.1    tsarna {
    824      1.51      kent 	struct auvia_softc *sc;
    825       1.1    tsarna 
    826      1.51      kent 	sc = addr;
    827      1.51      kent 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
    828       1.1    tsarna }
    829       1.1    tsarna 
    830      1.74  jmcneill static int
    831      1.74  jmcneill auvia_malloc_channel(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    832      1.74  jmcneill 		     size_t size)
    833      1.74  jmcneill {
    834      1.74  jmcneill 	struct auvia_dma *dp;
    835      1.74  jmcneill 	int segs;
    836      1.74  jmcneill 
    837      1.74  jmcneill 	/* if old list is large enough, nothing to do */
    838      1.74  jmcneill 	segs = (size + AUVIA_MINBLKSZ - 1) / AUVIA_MINBLKSZ;
    839      1.74  jmcneill 	if (segs <= ch->sc_dma_op_count) {
    840      1.74  jmcneill 		return 0;
    841      1.74  jmcneill 	}
    842      1.74  jmcneill 
    843      1.74  jmcneill 	if (ch->sc_dma_ops) {
    844      1.74  jmcneill 		auvia_free(sc, ch->sc_dma_ops,
    845      1.74  jmcneill 		    ch->sc_dma_op_count * sizeof(*dp));
    846      1.74  jmcneill 	}
    847      1.74  jmcneill 
    848      1.74  jmcneill 	ch->sc_dma_ops = auvia_malloc_dmamem(sc, 0,
    849      1.74  jmcneill 	    sizeof(struct auvia_dma_op) * segs);
    850      1.74  jmcneill 
    851      1.74  jmcneill 	if (ch->sc_dma_ops == NULL) {
    852      1.74  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't build dmaops\n");
    853      1.74  jmcneill 		return ENOMEM;
    854      1.74  jmcneill 	}
    855      1.74  jmcneill 
    856      1.74  jmcneill 	for (dp = sc->sc_dmas;
    857      1.74  jmcneill 	    dp && dp->addr != (void *)(ch->sc_dma_ops);
    858      1.74  jmcneill 	    dp = dp->next)
    859      1.74  jmcneill 		continue;
    860      1.74  jmcneill 
    861      1.74  jmcneill 	if (!dp)
    862      1.74  jmcneill 		panic("%s: build_dma_ops: where'd my memory go??? "
    863      1.74  jmcneill 			"address (%p)\n", device_xname(sc->sc_dev),
    864      1.74  jmcneill 			ch->sc_dma_ops);
    865      1.74  jmcneill 
    866      1.74  jmcneill 	ch->sc_dma_op_count = segs;
    867      1.74  jmcneill 	ch->sc_dma_ops_dma = dp;
    868      1.74  jmcneill 
    869      1.74  jmcneill 	return 0;
    870      1.74  jmcneill }
    871      1.74  jmcneill 
    872      1.52   thorpej static void *
    873      1.74  jmcneill auvia_malloc_dmamem(void *addr, int direction, size_t size)
    874       1.1    tsarna {
    875      1.51      kent 	struct auvia_softc *sc;
    876       1.1    tsarna 	struct auvia_dma *p;
    877       1.1    tsarna 	int error;
    878       1.1    tsarna 	int rseg;
    879       1.1    tsarna 
    880      1.74  jmcneill 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
    881      1.51      kent 	sc = addr;
    882       1.2    tsarna 	p->size = size;
    883       1.6   thorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
    884      1.74  jmcneill 				      1, &rseg, BUS_DMA_WAITOK)) != 0) {
    885      1.69    cegger 		aprint_error_dev(sc->sc_dev, "unable to allocate DMA, error = %d\n", error);
    886       1.1    tsarna 		goto fail_alloc;
    887       1.1    tsarna 	}
    888       1.1    tsarna 
    889       1.1    tsarna 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
    890      1.74  jmcneill 				    BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
    891      1.69    cegger 		aprint_error_dev(sc->sc_dev, "unable to map DMA, error = %d\n",
    892      1.64    cegger 		       error);
    893       1.1    tsarna 		goto fail_map;
    894       1.1    tsarna 	}
    895       1.1    tsarna 
    896      1.22      kent 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    897      1.74  jmcneill 				       BUS_DMA_WAITOK, &p->map)) != 0) {
    898      1.69    cegger 		aprint_error_dev(sc->sc_dev, "unable to create DMA map, error = %d\n",
    899      1.64    cegger 		       error);
    900       1.1    tsarna 		goto fail_create;
    901       1.1    tsarna 	}
    902       1.1    tsarna 
    903       1.1    tsarna 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
    904      1.74  jmcneill 				     BUS_DMA_WAITOK)) != 0) {
    905      1.69    cegger 		aprint_error_dev(sc->sc_dev, "unable to load DMA map, error = %d\n",
    906      1.64    cegger 		       error);
    907       1.1    tsarna 		goto fail_load;
    908       1.1    tsarna 	}
    909       1.1    tsarna 
    910       1.1    tsarna 	p->next = sc->sc_dmas;
    911       1.1    tsarna 	sc->sc_dmas = p;
    912       1.1    tsarna 
    913       1.1    tsarna 	return p->addr;
    914       1.1    tsarna 
    915       1.1    tsarna fail_load:
    916       1.1    tsarna 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    917       1.1    tsarna fail_create:
    918       1.1    tsarna 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
    919       1.1    tsarna fail_map:
    920       1.1    tsarna 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    921       1.1    tsarna fail_alloc:
    922      1.74  jmcneill 	kmem_free(p, sizeof(*p));
    923      1.51      kent 	return NULL;
    924       1.1    tsarna }
    925       1.1    tsarna 
    926      1.74  jmcneill static void *
    927      1.74  jmcneill auvia_malloc(void *addr, int direction, size_t size)
    928      1.74  jmcneill {
    929      1.74  jmcneill 	struct auvia_softc *sc;
    930      1.74  jmcneill 	void *p;
    931      1.74  jmcneill 
    932      1.74  jmcneill 	sc = addr;
    933      1.74  jmcneill 
    934      1.74  jmcneill 	p = auvia_malloc_dmamem(addr, direction, size);
    935      1.74  jmcneill 	if (p == NULL) {
    936      1.74  jmcneill 		return NULL;
    937      1.74  jmcneill 	}
    938      1.74  jmcneill 	if (auvia_malloc_channel(sc, &sc->sc_play, size) != 0) {
    939      1.74  jmcneill 		auvia_free(addr, p, size);
    940      1.74  jmcneill 		return NULL;
    941      1.74  jmcneill 	}
    942      1.74  jmcneill 	if (auvia_malloc_channel(sc, &sc->sc_record, size) != 0) {
    943      1.74  jmcneill 		auvia_free(addr, p, size);
    944      1.74  jmcneill 		return NULL;
    945      1.74  jmcneill 	}
    946      1.74  jmcneill 	return p;
    947      1.74  jmcneill }
    948      1.74  jmcneill 
    949      1.52   thorpej static void
    950      1.74  jmcneill auvia_free(void *addr, void *ptr, size_t size)
    951       1.1    tsarna {
    952      1.51      kent 	struct auvia_softc *sc;
    953       1.2    tsarna 	struct auvia_dma **pp, *p;
    954       1.1    tsarna 
    955      1.51      kent 	sc = addr;
    956       1.2    tsarna 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
    957       1.2    tsarna 		if (p->addr == ptr) {
    958       1.2    tsarna 			bus_dmamap_unload(sc->sc_dmat, p->map);
    959       1.2    tsarna 			bus_dmamap_destroy(sc->sc_dmat, p->map);
    960       1.2    tsarna 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    961       1.2    tsarna 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    962      1.22      kent 
    963       1.2    tsarna 			*pp = p->next;
    964      1.74  jmcneill 			kmem_free(p, sizeof(*p));
    965       1.2    tsarna 			return;
    966       1.2    tsarna 		}
    967       1.1    tsarna 
    968       1.2    tsarna 	panic("auvia_free: trying to free unallocated memory");
    969       1.1    tsarna }
    970       1.1    tsarna 
    971      1.52   thorpej static int
    972       1.1    tsarna auvia_get_props(void *addr)
    973       1.1    tsarna {
    974      1.51      kent 	struct auvia_softc *sc;
    975      1.26      kent 	int props;
    976      1.26      kent 
    977      1.26      kent 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    978      1.51      kent 	sc = addr;
    979      1.26      kent 	/*
    980      1.26      kent 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
    981      1.26      kent 	 * rate because of aurateconv.  Applications can't know what rate the
    982      1.26      kent 	 * device can process in the case of mmap().
    983      1.26      kent 	 */
    984      1.46      kent 	if (!AC97_IS_FIXED_RATE(sc->codec_if))
    985      1.26      kent 		props |= AUDIO_PROP_MMAP;
    986      1.26      kent 	return props;
    987       1.1    tsarna }
    988       1.1    tsarna 
    989      1.74  jmcneill static void
    990      1.74  jmcneill auvia_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
    991      1.74  jmcneill {
    992      1.74  jmcneill 	struct auvia_softc *sc;
    993      1.74  jmcneill 
    994      1.74  jmcneill 	sc = addr;
    995      1.74  jmcneill 	*intr = &sc->sc_intr_lock;
    996      1.74  jmcneill 	*thread = &sc->sc_lock;
    997      1.74  jmcneill }
    998      1.74  jmcneill 
    999      1.52   thorpej static int
   1000       1.1    tsarna auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
   1001       1.1    tsarna 	struct auvia_dma *p, void *start, void *end, int blksize)
   1002       1.1    tsarna {
   1003       1.1    tsarna 	struct auvia_dma_op *op;
   1004      1.33    simonb 	bus_addr_t s;
   1005       1.1    tsarna 	size_t l;
   1006       1.1    tsarna 
   1007      1.74  jmcneill 	op = ch->sc_dma_ops;
   1008       1.1    tsarna 	s = p->map->dm_segs[0].ds_addr;
   1009       1.1    tsarna 	l = ((char *)end - (char *)start);
   1010       1.1    tsarna 
   1011       1.1    tsarna 	while (l) {
   1012      1.62   tsutsui 		op->ptr = htole32(s);
   1013       1.1    tsarna 		l = l - blksize;
   1014       1.1    tsarna 		if (!l) {
   1015       1.1    tsarna 			/* if last block */
   1016      1.62   tsutsui 			op->flags = htole32(AUVIA_DMAOP_EOL | blksize);
   1017       1.1    tsarna 		} else {
   1018      1.62   tsutsui 			op->flags = htole32(AUVIA_DMAOP_FLAG | blksize);
   1019       1.1    tsarna 		}
   1020       1.1    tsarna 		s += blksize;
   1021       1.1    tsarna 		op++;
   1022       1.1    tsarna 	}
   1023       1.1    tsarna 
   1024       1.1    tsarna 	return 0;
   1025       1.1    tsarna }
   1026       1.1    tsarna 
   1027       1.1    tsarna 
   1028      1.52   thorpej static int
   1029      1.57  christos auvia_trigger_output(void *addr, void *start, void *end, int blksize,
   1030      1.58  christos     void (*intr)(void *), void *arg, const audio_params_t *param)
   1031       1.1    tsarna {
   1032      1.51      kent 	struct auvia_softc *sc;
   1033      1.51      kent 	struct auvia_softc_chan *ch;
   1034       1.1    tsarna 	struct auvia_dma *p;
   1035       1.1    tsarna 
   1036      1.51      kent 	sc = addr;
   1037      1.51      kent 	ch = &(sc->sc_play);
   1038       1.1    tsarna 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
   1039      1.51      kent 		continue;
   1040       1.1    tsarna 
   1041       1.1    tsarna 	if (!p)
   1042       1.1    tsarna 		panic("auvia_trigger_output: request with bad start "
   1043      1.18    provos 			"address (%p)", start);
   1044       1.1    tsarna 
   1045       1.1    tsarna 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
   1046       1.1    tsarna 		return 1;
   1047       1.1    tsarna 	}
   1048       1.1    tsarna 
   1049       1.1    tsarna 	ch->sc_intr = intr;
   1050       1.1    tsarna 	ch->sc_arg = arg;
   1051       1.1    tsarna 
   1052      1.27      kent 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
   1053       1.1    tsarna 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1054       1.1    tsarna 
   1055      1.22      kent 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1056      1.27      kent 		if (ch->sc_base != VIA8233_MP_BASE) {
   1057      1.27      kent 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
   1058      1.27      kent 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
   1059      1.27      kent 		}
   1060      1.27      kent 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
   1061      1.22      kent 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1062      1.22      kent 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1063      1.24      kent 	} else {
   1064      1.27      kent 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
   1065      1.27      kent 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1066      1.24      kent 	}
   1067       1.1    tsarna 
   1068       1.1    tsarna 	return 0;
   1069       1.1    tsarna }
   1070       1.1    tsarna 
   1071      1.52   thorpej static int
   1072      1.57  christos auvia_trigger_input(void *addr, void *start, void *end, int blksize,
   1073      1.58  christos     void (*intr)(void *), void *arg, const audio_params_t *param)
   1074       1.1    tsarna {
   1075      1.51      kent 	struct auvia_softc *sc;
   1076      1.51      kent 	struct auvia_softc_chan *ch;
   1077       1.1    tsarna 	struct auvia_dma *p;
   1078       1.1    tsarna 
   1079      1.51      kent 	sc = addr;
   1080      1.51      kent 	ch = &(sc->sc_record);
   1081       1.1    tsarna 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
   1082      1.51      kent 		continue;
   1083       1.1    tsarna 
   1084       1.1    tsarna 	if (!p)
   1085       1.1    tsarna 		panic("auvia_trigger_input: request with bad start "
   1086      1.18    provos 			"address (%p)", start);
   1087       1.1    tsarna 
   1088       1.1    tsarna 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
   1089       1.1    tsarna 		return 1;
   1090       1.1    tsarna 	}
   1091       1.1    tsarna 
   1092       1.1    tsarna 	ch->sc_intr = intr;
   1093       1.1    tsarna 	ch->sc_arg = arg;
   1094       1.1    tsarna 
   1095      1.27      kent 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
   1096      1.27      kent 		  ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1097       1.1    tsarna 
   1098      1.24      kent 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1099      1.27      kent 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
   1100      1.27      kent 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
   1101      1.27      kent 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
   1102      1.22      kent 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1103      1.22      kent 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1104      1.24      kent 	} else {
   1105      1.27      kent 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
   1106      1.27      kent 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1107      1.24      kent 	}
   1108       1.1    tsarna 
   1109       1.1    tsarna 	return 0;
   1110       1.1    tsarna }
   1111       1.1    tsarna 
   1112      1.52   thorpej static int
   1113       1.1    tsarna auvia_intr(void *arg)
   1114       1.1    tsarna {
   1115      1.51      kent 	struct auvia_softc *sc;
   1116      1.27      kent 	struct auvia_softc_chan *ch;
   1117       1.1    tsarna 	u_int8_t r;
   1118      1.17      fvdl 	int rval;
   1119      1.17      fvdl 
   1120      1.51      kent 	sc = arg;
   1121      1.17      fvdl 	rval = 0;
   1122      1.74  jmcneill 	ch = &sc->sc_record;
   1123       1.1    tsarna 
   1124      1.74  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
   1125      1.27      kent 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
   1126       1.1    tsarna 	if (r & AUVIA_RPSTAT_INTR) {
   1127       1.2    tsarna 		if (sc->sc_record.sc_intr)
   1128       1.2    tsarna 			sc->sc_record.sc_intr(sc->sc_record.sc_arg);
   1129       1.1    tsarna 
   1130       1.1    tsarna 		/* clear interrupts */
   1131      1.27      kent 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1132      1.17      fvdl 		rval = 1;
   1133       1.1    tsarna 	}
   1134      1.22      kent 
   1135      1.27      kent 	ch = &sc->sc_play;
   1136      1.27      kent 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
   1137       1.1    tsarna 	if (r & AUVIA_RPSTAT_INTR) {
   1138       1.2    tsarna 		if (sc->sc_play.sc_intr)
   1139       1.2    tsarna 			sc->sc_play.sc_intr(sc->sc_play.sc_arg);
   1140       1.1    tsarna 
   1141       1.1    tsarna 		/* clear interrupts */
   1142      1.27      kent 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1143      1.17      fvdl 		rval = 1;
   1144       1.1    tsarna 	}
   1145      1.74  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
   1146       1.1    tsarna 
   1147      1.17      fvdl 	return rval;
   1148       1.1    tsarna }
   1149      1.43      kent 
   1150      1.61  jmcneill static bool
   1151      1.72    dyoung auvia_resume(device_t dv, const pmf_qual_t *qual)
   1152      1.43      kent {
   1153      1.61  jmcneill 	struct auvia_softc *sc = device_private(dv);
   1154      1.43      kent 
   1155      1.74  jmcneill 	mutex_enter(&sc->sc_lock);
   1156      1.74  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
   1157      1.61  jmcneill 	auvia_reset_codec(sc);
   1158      1.61  jmcneill 	DELAY(1000);
   1159      1.74  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
   1160      1.61  jmcneill 	(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
   1161      1.74  jmcneill 	mutex_exit(&sc->sc_lock);
   1162      1.43      kent 
   1163      1.61  jmcneill 	return true;
   1164      1.43      kent }
   1165