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