Home | History | Annotate | Line # | Download | only in pci
auvia.c revision 1.81
      1 /*	$NetBSD: auvia.c,v 1.81 2019/03/16 04:41:09 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.81 2019/03/16 04:41:09 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 	auvia_open,
    214 	auvia_close,
    215 	NULL, /* drain */
    216 	auvia_query_encoding,
    217 	auvia_set_params,
    218 	auvia_round_blocksize,
    219 	NULL, /* commit_settings */
    220 	NULL, /* init_output */
    221 	NULL, /* init_input */
    222 	NULL, /* start_output */
    223 	NULL, /* start_input */
    224 	auvia_halt_output,
    225 	auvia_halt_input,
    226 	NULL, /* speaker_ctl */
    227 	auvia_getdev,
    228 	NULL, /* setfd */
    229 	auvia_set_port,
    230 	auvia_get_port,
    231 	auvia_query_devinfo,
    232 	auvia_malloc,
    233 	auvia_free,
    234 	auvia_round_buffersize,
    235 	auvia_mappage,
    236 	auvia_get_props,
    237 	auvia_trigger_output,
    238 	auvia_trigger_input,
    239 	NULL, /* dev_ioctl */
    240 	auvia_get_locks,
    241 };
    242 
    243 #define AUVIA_FORMATS_4CH_16	2
    244 #define AUVIA_FORMATS_6CH_16	3
    245 #define AUVIA_FORMATS_4CH_8	6
    246 #define AUVIA_FORMATS_6CH_8	7
    247 static const struct audio_format auvia_formats[AUVIA_NFORMATS] = {
    248 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    249 	 1, AUFMT_MONAURAL, 0, {8000, 48000}},
    250 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    251 	 2, AUFMT_STEREO, 0, {8000, 48000}},
    252 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    253 	 4, AUFMT_SURROUND4, 0, {8000, 48000}},
    254 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    255 	 6, AUFMT_DOLBY_5_1, 0, {8000, 48000}},
    256 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    257 	 1, AUFMT_MONAURAL, 0, {8000, 48000}},
    258 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    259 	 2, AUFMT_STEREO, 0, {8000, 48000}},
    260 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    261 	 4, AUFMT_SURROUND4, 0, {8000, 48000}},
    262 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    263 	 6, AUFMT_DOLBY_5_1, 0, {8000, 48000}},
    264 };
    265 
    266 #define	AUVIA_SPDIF_NFORMATS	1
    267 static const struct audio_format auvia_spdif_formats[AUVIA_SPDIF_NFORMATS] = {
    268 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    269 	 2, AUFMT_STEREO, 1, {48000}},
    270 };
    271 
    272 
    273 static int
    274 auvia_match(device_t parent, cfdata_t match, void *aux)
    275 {
    276 	struct pci_attach_args *pa;
    277 
    278 	pa = (struct pci_attach_args *) aux;
    279 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
    280 		return 0;
    281 	switch (PCI_PRODUCT(pa->pa_id)) {
    282 	case PCI_PRODUCT_VIATECH_VT82C686A_AC97:
    283 	case PCI_PRODUCT_VIATECH_VT8233_AC97:
    284 		break;
    285 	default:
    286 		return 0;
    287 	}
    288 
    289 	return 1;
    290 }
    291 
    292 static void
    293 auvia_childdet(device_t self, device_t child)
    294 {
    295 	/* we hold no child references, so do nothing */
    296 }
    297 
    298 static int
    299 auvia_detach(device_t self, int flags)
    300 {
    301 	int rc;
    302 	struct auvia_softc *sc = device_private(self);
    303 
    304 	if ((rc = config_detach_children(self, flags)) != 0)
    305 		return rc;
    306 	pmf_device_deregister(self);
    307 
    308 	mutex_enter(&sc->sc_lock);
    309 	auconv_delete_encodings(sc->sc_encodings);
    310 	auconv_delete_encodings(sc->sc_spdif_encodings);
    311 	if (sc->codec_if != NULL)
    312 		sc->codec_if->vtbl->detach(sc->codec_if);
    313 	mutex_exit(&sc->sc_lock);
    314 
    315 	/* XXX restore compatibility? */
    316 	if (sc->sc_ih != NULL)
    317 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    318 
    319 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    320 	if (sc->sc_play.sc_dma_ops != NULL) {
    321 		auvia_free(sc, sc->sc_play.sc_dma_ops,
    322 		    sc->sc_play.sc_dma_op_count *
    323 		    sizeof(struct auvia_dma_op));
    324 	}
    325 	if (sc->sc_record.sc_dma_ops != NULL) {
    326 		auvia_free(sc, sc->sc_record.sc_dma_ops,
    327 		    sc->sc_play.sc_dma_op_count *
    328 		    sizeof(struct auvia_dma_op));
    329 	}
    330 	mutex_destroy(&sc->sc_lock);
    331 	mutex_destroy(&sc->sc_intr_lock);
    332 
    333 	return 0;
    334 }
    335 
    336 static void
    337 auvia_attach(device_t parent, device_t self, void *aux)
    338 {
    339 	struct pci_attach_args *pa;
    340 	struct auvia_softc *sc;
    341 	const char *intrstr;
    342 	pci_chipset_tag_t pc;
    343 	pcitag_t pt;
    344 	pci_intr_handle_t ih;
    345 	pcireg_t pr;
    346 	int r;
    347 	const char *revnum;	/* VT823xx revision number */
    348 	char intrbuf[PCI_INTRSTR_LEN];
    349 
    350 	pa = aux;
    351 	sc = device_private(self);
    352 	sc->sc_dev = self;
    353 	intrstr = NULL;
    354 	pc = pa->pa_pc;
    355 	pt = pa->pa_tag;
    356 	revnum = NULL;
    357 
    358 	aprint_naive(": Audio controller\n");
    359 
    360 	sc->sc_play.sc_base = AUVIA_PLAY_BASE;
    361 	sc->sc_record.sc_base = AUVIA_RECORD_BASE;
    362 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT8233_AC97) {
    363 		sc->sc_flags |= AUVIA_FLAGS_VT8233;
    364 		sc->sc_play.sc_base = VIA8233_MP_BASE;
    365 		sc->sc_record.sc_base = VIA8233_WR_BASE;
    366 	}
    367 
    368 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
    369 		&sc->sc_ioh, NULL, &sc->sc_iosize)) {
    370 		aprint_error(": can't map i/o space\n");
    371 		return;
    372 	}
    373 
    374 	sc->sc_dmat = pa->pa_dmat;
    375 	sc->sc_pc = pc;
    376 	sc->sc_pt = pt;
    377 
    378 	r = PCI_REVISION(pa->pa_class);
    379 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    380 		snprintf(sc->sc_revision, sizeof(sc->sc_revision), "0x%02X", r);
    381 		switch(r) {
    382 		case VIA_REV_8233PRE:
    383 			/* same as 8233, but should not be in the market */
    384 			revnum = "3-Pre";
    385 			break;
    386 		case VIA_REV_8233C:
    387 			/* 2 rec, 4 pb, 1 multi-pb */
    388 			revnum = "3C";
    389 			break;
    390 		case VIA_REV_8233:
    391 			/* 2 rec, 4 pb, 1 multi-pb, spdif */
    392 			revnum = "3";
    393 			break;
    394 		case VIA_REV_8233A:
    395 			/* 1 rec, 1 multi-pb, spdif */
    396 			revnum = "3A";
    397 			break;
    398 		default:
    399 			break;
    400 		}
    401 		if (r >= VIA_REV_8237)
    402 			revnum = "7";
    403 		else if (r >= VIA_REV_8235) /* 2 rec, 4 pb, 1 multi-pb, spdif */
    404 			revnum = "5";
    405 		aprint_normal(": VIA Technologies VT823%s AC'97 Audio "
    406 		    "(rev %s)\n", revnum, sc->sc_revision);
    407 	} else {
    408 		sc->sc_revision[1] = '\0';
    409 		if (r == 0x20) {
    410 			sc->sc_revision[0] = 'H';
    411 		} else if ((r >= 0x10) && (r <= 0x14)) {
    412 			sc->sc_revision[0] = 'A' + (r - 0x10);
    413 		} else {
    414 			snprintf(sc->sc_revision, sizeof(sc->sc_revision),
    415 			    "0x%02X", r);
    416 		}
    417 
    418 		aprint_normal(": VIA Technologies VT82C686A AC'97 Audio "
    419 		    "(rev %s)\n", sc->sc_revision);
    420 	}
    421 
    422 	if (pci_intr_map(pa, &ih)) {
    423 		aprint_error(": couldn't map interrupt\n");
    424 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    425 		return;
    426 	}
    427 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    428 
    429 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    430 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    431 
    432 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, auvia_intr, sc,
    433 	    device_xname(self));
    434 	if (sc->sc_ih == NULL) {
    435 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
    436 		if (intrstr != NULL)
    437 			aprint_error(" at %s", intrstr);
    438 		aprint_error("\n");
    439 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    440 		mutex_destroy(&sc->sc_lock);
    441 		mutex_destroy(&sc->sc_intr_lock);
    442 		return;
    443 	}
    444 
    445 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    446 
    447 	/* disable SBPro compat & others */
    448 	pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
    449 
    450 	pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
    451 	/* XXX what to do about MIDI, FM, joystick? */
    452 
    453 	pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
    454 		| AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
    455 
    456 	pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
    457 
    458 	pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
    459 
    460 	sc->host_if.arg = sc;
    461 	sc->host_if.attach = auvia_attach_codec;
    462 	sc->host_if.read = auvia_read_codec;
    463 	sc->host_if.write = auvia_write_codec;
    464 	sc->host_if.reset = auvia_reset_codec;
    465 	sc->host_if.spdif_event = auvia_spdif_event;
    466 
    467 	if ((r = ac97_attach(&sc->host_if, self, &sc->sc_lock)) != 0) {
    468 		aprint_error_dev(sc->sc_dev, "can't attach codec (error 0x%X)\n", r);
    469 		pci_intr_disestablish(pc, sc->sc_ih);
    470 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    471 		mutex_destroy(&sc->sc_lock);
    472 		mutex_destroy(&sc->sc_intr_lock);
    473 		return;
    474 	}
    475 
    476 	/* setup audio_format */
    477 	memcpy(sc->sc_formats, auvia_formats, sizeof(auvia_formats));
    478 	mutex_enter(&sc->sc_lock);
    479 	if (sc->sc_play.sc_base != VIA8233_MP_BASE || !AC97_IS_4CH(sc->codec_if)) {
    480 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_4CH_8]);
    481 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_4CH_16]);
    482 	}
    483 	if (sc->sc_play.sc_base != VIA8233_MP_BASE || !AC97_IS_6CH(sc->codec_if)) {
    484 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_6CH_8]);
    485 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_6CH_16]);
    486 	}
    487 	if (AC97_IS_FIXED_RATE(sc->codec_if)) {
    488 		for (r = 0; r < AUVIA_NFORMATS; r++) {
    489 			sc->sc_formats[r].frequency_type = 1;
    490 			sc->sc_formats[r].frequency[0] = 48000;
    491 		}
    492 	}
    493 	mutex_exit(&sc->sc_lock);
    494 
    495 	if (0 != auconv_create_encodings(sc->sc_formats, AUVIA_NFORMATS,
    496 					 &sc->sc_encodings)) {
    497 		mutex_enter(&sc->sc_lock);
    498 		sc->codec_if->vtbl->detach(sc->codec_if);
    499 		mutex_exit(&sc->sc_lock);
    500 		pci_intr_disestablish(pc, sc->sc_ih);
    501 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    502 		mutex_destroy(&sc->sc_lock);
    503 		mutex_destroy(&sc->sc_intr_lock);
    504 		aprint_error_dev(sc->sc_dev, "can't create encodings\n");
    505 		return;
    506 	}
    507 	if (0 != auconv_create_encodings(auvia_spdif_formats,
    508 	    AUVIA_SPDIF_NFORMATS, &sc->sc_spdif_encodings)) {
    509 		mutex_enter(&sc->sc_lock);
    510 		sc->codec_if->vtbl->detach(sc->codec_if);
    511 		mutex_exit(&sc->sc_lock);
    512 		pci_intr_disestablish(pc, sc->sc_ih);
    513 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    514 		mutex_destroy(&sc->sc_lock);
    515 		mutex_destroy(&sc->sc_intr_lock);
    516 		aprint_error_dev(sc->sc_dev, "can't create spdif encodings\n");
    517 		return;
    518 	}
    519 
    520 	if (!pmf_device_register(self, NULL, auvia_resume))
    521 		aprint_error_dev(self, "couldn't establish power handler\n");
    522 
    523 	audio_attach_mi(&auvia_hw_if, sc, sc->sc_dev);
    524 	mutex_enter(&sc->sc_lock);
    525 	sc->codec_if->vtbl->unlock(sc->codec_if);
    526 	mutex_exit(&sc->sc_lock);
    527 	return;
    528 }
    529 
    530 static int
    531 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
    532 {
    533 	struct auvia_softc *sc;
    534 
    535 	sc = addr;
    536 	sc->codec_if = cif;
    537 	return 0;
    538 }
    539 
    540 static int
    541 auvia_reset_codec(void *addr)
    542 {
    543 	struct auvia_softc *sc;
    544 	pcireg_t r;
    545 	int i;
    546 
    547 	/* perform a codec cold reset */
    548 	sc = addr;
    549 	r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
    550 
    551 	r &= ~AUVIA_PCICONF_ACNOTRST;	/* enable RESET (active low) */
    552 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    553 	delay(2);
    554 
    555 	r |= AUVIA_PCICONF_ACNOTRST;	/* disable RESET (inactive high) */
    556 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    557 	delay(200);
    558 
    559 	for (i = 500000; i != 0 && !(pci_conf_read(sc->sc_pc, sc->sc_pt,
    560 		AUVIA_PCICONF_JUNK) & AUVIA_PCICONF_PRIVALID); i--)
    561 		DELAY(1);
    562 	if (i == 0) {
    563 		printf("%s: codec reset timed out\n", device_xname(sc->sc_dev));
    564 		return ETIMEDOUT;
    565 	}
    566 	return 0;
    567 }
    568 
    569 static int
    570 auvia_waitready_codec(struct auvia_softc *sc)
    571 {
    572 	int i;
    573 
    574 	/* poll until codec not busy */
    575 	for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    576 		AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
    577 		delay(1);
    578 	if (i >= TIMEOUT) {
    579 		printf("%s: codec busy\n", device_xname(sc->sc_dev));
    580 		return 1;
    581 	}
    582 
    583 	return 0;
    584 }
    585 
    586 static int
    587 auvia_waitvalid_codec(struct auvia_softc *sc)
    588 {
    589 	int i;
    590 
    591 	/* poll until codec valid */
    592 	for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    593 		AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
    594 			delay(1);
    595 	if (i >= TIMEOUT) {
    596 		printf("%s: codec invalid\n", device_xname(sc->sc_dev));
    597 		return 1;
    598 	}
    599 
    600 	return 0;
    601 }
    602 
    603 static int
    604 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
    605 {
    606 	struct auvia_softc *sc;
    607 
    608 	sc = addr;
    609 	if (auvia_waitready_codec(sc))
    610 		return 1;
    611 
    612 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    613 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
    614 
    615 	return 0;
    616 }
    617 
    618 static int
    619 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
    620 {
    621 	struct auvia_softc *sc;
    622 
    623 	sc = addr;
    624 	if (auvia_waitready_codec(sc))
    625 		return 1;
    626 
    627 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    628 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
    629 
    630 	if (auvia_waitready_codec(sc))
    631 		return 1;
    632 
    633 	if (auvia_waitvalid_codec(sc))
    634 		return 1;
    635 
    636 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
    637 
    638 	return 0;
    639 }
    640 
    641 static void
    642 auvia_spdif_event(void *addr, bool flag)
    643 {
    644 	struct auvia_softc *sc;
    645 
    646 	sc = addr;
    647 	sc->sc_spdif = flag;
    648 }
    649 
    650 static int
    651 auvia_open(void *addr, int flags)
    652 {
    653 	struct auvia_softc *sc;
    654 
    655 	sc = (struct auvia_softc *)addr;
    656 	mutex_spin_exit(&sc->sc_intr_lock);
    657 	sc->codec_if->vtbl->lock(sc->codec_if);
    658 	mutex_spin_enter(&sc->sc_intr_lock);
    659 	return 0;
    660 }
    661 
    662 static void
    663 auvia_close(void *addr)
    664 {
    665 	struct auvia_softc *sc;
    666 
    667 	sc = (struct auvia_softc *)addr;
    668 	mutex_spin_exit(&sc->sc_intr_lock);
    669 	sc->codec_if->vtbl->unlock(sc->codec_if);
    670 	mutex_spin_enter(&sc->sc_intr_lock);
    671 }
    672 
    673 static int
    674 auvia_query_encoding(void *addr, struct audio_encoding *fp)
    675 {
    676 	struct auvia_softc *sc;
    677 
    678 	sc = (struct auvia_softc *)addr;
    679 	return auconv_query_encoding(
    680 	    sc->sc_spdif ? sc->sc_spdif_encodings : sc->sc_encodings, fp);
    681 }
    682 
    683 static void
    684 auvia_set_params_sub(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    685 		     const audio_params_t *p)
    686 {
    687 	uint32_t v;
    688 	uint16_t regval;
    689 
    690 	if (!(sc->sc_flags & AUVIA_FLAGS_VT8233)) {
    691 		regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
    692 			| (p->precision  == 16 ?
    693 				AUVIA_RPMODE_16BIT : 0)
    694 			| AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
    695 			| AUVIA_RPMODE_AUTOSTART;
    696 		ch->sc_reg = regval;
    697 	} else if (ch->sc_base != VIA8233_MP_BASE) {
    698 		v = CH_READ4(sc, ch, VIA8233_RP_RATEFMT);
    699 		v &= ~(VIA8233_RATEFMT_48K | VIA8233_RATEFMT_STEREO
    700 			| VIA8233_RATEFMT_16BIT);
    701 
    702 		v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20)
    703 			/ (48000 / 20);
    704 		if (p->channels == 2)
    705 			v |= VIA8233_RATEFMT_STEREO;
    706 		if (p->precision == 16)
    707 			v |= VIA8233_RATEFMT_16BIT;
    708 
    709 		CH_WRITE4(sc, ch, VIA8233_RP_RATEFMT, v);
    710 	} else {
    711 		static const u_int32_t slottab[7] =
    712 			{ 0, 0xff000011, 0xff000021, 0,
    713 			  0xff004321, 0, 0xff436521};
    714 
    715 		regval = (p->precision == 16
    716 			? VIA8233_MP_FORMAT_16BIT : VIA8233_MP_FORMAT_8BIT)
    717 			| (p->channels << 4);
    718 		CH_WRITE1(sc, ch, VIA8233_OFF_MP_FORMAT, regval);
    719 		CH_WRITE4(sc, ch, VIA8233_OFF_MP_STOP, slottab[p->channels]);
    720 	}
    721 }
    722 
    723 static int
    724 auvia_set_params(void *addr, int setmode, int usemode,
    725     audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
    726     stream_filter_list_t *rfil)
    727 {
    728 	struct auvia_softc *sc;
    729 	struct auvia_softc_chan *ch;
    730 	struct audio_params *p;
    731 	struct ac97_codec_if* codec;
    732 	stream_filter_list_t *fil;
    733 	int reg, mode;
    734 	int index;
    735 
    736 	sc = addr;
    737 	codec = sc->codec_if;
    738 	/* for mode in (RECORD, PLAY) */
    739 	for (mode = AUMODE_RECORD; mode != -1;
    740 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    741 		if ((setmode & mode) == 0)
    742 			continue;
    743 
    744 		if (mode == AUMODE_PLAY ) {
    745 			p = play;
    746 			ch = &sc->sc_play;
    747 			reg = AC97_REG_PCM_FRONT_DAC_RATE;
    748 			fil = pfil;
    749 		} else {
    750 			p = rec;
    751 			ch = &sc->sc_record;
    752 			reg = AC97_REG_PCM_LR_ADC_RATE;
    753 			fil = rfil;
    754 		}
    755 
    756 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
    757 		    (p->precision != 8 && p->precision != 16))
    758 			return (EINVAL);
    759 		if (sc->sc_spdif)
    760 			index = auconv_set_converter(auvia_spdif_formats,
    761 			    AUVIA_SPDIF_NFORMATS, mode, p, TRUE, fil);
    762 		else
    763 			index = auconv_set_converter(sc->sc_formats,
    764 			    AUVIA_NFORMATS, mode, p, TRUE, fil);
    765 		if (index < 0)
    766 			return EINVAL;
    767 		if (fil->req_size > 0)
    768 			p = &fil->filters[0].param;
    769 		if (!AC97_IS_FIXED_RATE(codec)) {
    770 			if (codec->vtbl->set_rate(codec, reg, &p->sample_rate))
    771 				return EINVAL;
    772 			reg = AC97_REG_PCM_SURR_DAC_RATE;
    773 			if (p->channels >= 4
    774 			    && codec->vtbl->set_rate(codec, reg,
    775 						     &p->sample_rate))
    776 				return EINVAL;
    777 			reg = AC97_REG_PCM_LFE_DAC_RATE;
    778 			if (p->channels == 6
    779 			    && codec->vtbl->set_rate(codec, reg,
    780 						     &p->sample_rate))
    781 				return EINVAL;
    782 		}
    783 		auvia_set_params_sub(sc, ch, p);
    784 	}
    785 
    786 	return 0;
    787 }
    788 
    789 static int
    790 auvia_round_blocksize(void *addr, int blk,
    791     int mode, const audio_params_t *param)
    792 {
    793 	struct auvia_softc *sc;
    794 
    795 	sc = addr;
    796 
    797 	/* XXX VT823x might have the limitation of dma_ops size */
    798 	if (sc->sc_flags & AUVIA_FLAGS_VT8233 && blk < 288)
    799 		blk = 288;
    800 
    801 	/* Avoid too many dma_ops. */
    802 	return uimin((blk & -32), AUVIA_MINBLKSZ);
    803 }
    804 
    805 static int
    806 auvia_halt_output(void *addr)
    807 {
    808 	struct auvia_softc *sc;
    809 	struct auvia_softc_chan *ch;
    810 
    811 	sc = addr;
    812 	ch = &(sc->sc_play);
    813 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    814 	ch->sc_intr = NULL;
    815 	return 0;
    816 }
    817 
    818 static int
    819 auvia_halt_input(void *addr)
    820 {
    821 	struct auvia_softc *sc;
    822 	struct auvia_softc_chan *ch;
    823 
    824 	sc = addr;
    825 	ch = &(sc->sc_record);
    826 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    827 	ch->sc_intr = NULL;
    828 	return 0;
    829 }
    830 
    831 static int
    832 auvia_getdev(void *addr, struct audio_device *retp)
    833 {
    834 	struct auvia_softc *sc;
    835 
    836 	if (retp) {
    837 		sc = addr;
    838 		if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    839 			strncpy(retp->name, "VIA VT823x",
    840 				sizeof(retp->name));
    841 		} else {
    842 			strncpy(retp->name, "VIA VT82C686A",
    843 				sizeof(retp->name));
    844 		}
    845 		strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
    846 		strncpy(retp->config, "auvia", sizeof(retp->config));
    847 	}
    848 
    849 	return 0;
    850 }
    851 
    852 static int
    853 auvia_set_port(void *addr, mixer_ctrl_t *cp)
    854 {
    855 	struct auvia_softc *sc;
    856 
    857 	sc = addr;
    858 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    859 }
    860 
    861 static int
    862 auvia_get_port(void *addr, mixer_ctrl_t *cp)
    863 {
    864 	struct auvia_softc *sc;
    865 
    866 	sc = addr;
    867 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
    868 }
    869 
    870 static int
    871 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
    872 {
    873 	struct auvia_softc *sc;
    874 
    875 	sc = addr;
    876 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
    877 }
    878 
    879 static int
    880 auvia_malloc_channel(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    881 		     size_t size)
    882 {
    883 	struct auvia_dma *dp;
    884 	int segs;
    885 
    886 	/* if old list is large enough, nothing to do */
    887 	segs = (size + AUVIA_MINBLKSZ - 1) / AUVIA_MINBLKSZ;
    888 	if (segs <= ch->sc_dma_op_count) {
    889 		return 0;
    890 	}
    891 
    892 	if (ch->sc_dma_ops) {
    893 		auvia_free(sc, ch->sc_dma_ops,
    894 		    ch->sc_dma_op_count * sizeof(*dp));
    895 	}
    896 
    897 	ch->sc_dma_ops = auvia_malloc_dmamem(sc, 0,
    898 	    sizeof(struct auvia_dma_op) * segs);
    899 
    900 	if (ch->sc_dma_ops == NULL) {
    901 		aprint_error_dev(sc->sc_dev, "couldn't build dmaops\n");
    902 		return ENOMEM;
    903 	}
    904 
    905 	for (dp = sc->sc_dmas;
    906 	    dp && dp->addr != (void *)(ch->sc_dma_ops);
    907 	    dp = dp->next)
    908 		continue;
    909 
    910 	if (!dp)
    911 		panic("%s: build_dma_ops: where'd my memory go??? "
    912 			"address (%p)\n", device_xname(sc->sc_dev),
    913 			ch->sc_dma_ops);
    914 
    915 	ch->sc_dma_op_count = segs;
    916 	ch->sc_dma_ops_dma = dp;
    917 
    918 	return 0;
    919 }
    920 
    921 static void *
    922 auvia_malloc_dmamem(void *addr, int direction, size_t size)
    923 {
    924 	struct auvia_softc *sc;
    925 	struct auvia_dma *p;
    926 	int error;
    927 	int rseg;
    928 
    929 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
    930 	sc = addr;
    931 	p->size = size;
    932 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
    933 				      1, &rseg, BUS_DMA_WAITOK)) != 0) {
    934 		aprint_error_dev(sc->sc_dev, "unable to allocate DMA, error = %d\n", error);
    935 		goto fail_alloc;
    936 	}
    937 
    938 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
    939 				    BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
    940 		aprint_error_dev(sc->sc_dev, "unable to map DMA, error = %d\n",
    941 		       error);
    942 		goto fail_map;
    943 	}
    944 
    945 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    946 				       BUS_DMA_WAITOK, &p->map)) != 0) {
    947 		aprint_error_dev(sc->sc_dev, "unable to create DMA map, error = %d\n",
    948 		       error);
    949 		goto fail_create;
    950 	}
    951 
    952 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
    953 				     BUS_DMA_WAITOK)) != 0) {
    954 		aprint_error_dev(sc->sc_dev, "unable to load DMA map, error = %d\n",
    955 		       error);
    956 		goto fail_load;
    957 	}
    958 
    959 	p->next = sc->sc_dmas;
    960 	sc->sc_dmas = p;
    961 
    962 	return p->addr;
    963 
    964 fail_load:
    965 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    966 fail_create:
    967 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
    968 fail_map:
    969 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    970 fail_alloc:
    971 	kmem_free(p, sizeof(*p));
    972 	return NULL;
    973 }
    974 
    975 static void *
    976 auvia_malloc(void *addr, int direction, size_t size)
    977 {
    978 	struct auvia_softc *sc;
    979 	void *p;
    980 
    981 	sc = addr;
    982 
    983 	p = auvia_malloc_dmamem(addr, direction, size);
    984 	if (p == NULL) {
    985 		return NULL;
    986 	}
    987 	if (auvia_malloc_channel(sc, &sc->sc_play, size) != 0) {
    988 		auvia_free(addr, p, size);
    989 		return NULL;
    990 	}
    991 	if (auvia_malloc_channel(sc, &sc->sc_record, size) != 0) {
    992 		auvia_free(addr, p, size);
    993 		return NULL;
    994 	}
    995 	return p;
    996 }
    997 
    998 static void
    999 auvia_free(void *addr, void *ptr, size_t size)
   1000 {
   1001 	struct auvia_softc *sc;
   1002 	struct auvia_dma **pp, *p;
   1003 
   1004 	sc = addr;
   1005 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
   1006 		if (p->addr == ptr) {
   1007 			bus_dmamap_unload(sc->sc_dmat, p->map);
   1008 			bus_dmamap_destroy(sc->sc_dmat, p->map);
   1009 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
   1010 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
   1011 
   1012 			*pp = p->next;
   1013 			kmem_free(p, sizeof(*p));
   1014 			return;
   1015 		}
   1016 
   1017 	panic("auvia_free: trying to free unallocated memory");
   1018 }
   1019 
   1020 static size_t
   1021 auvia_round_buffersize(void *addr, int direction, size_t size)
   1022 {
   1023 
   1024 	return size;
   1025 }
   1026 
   1027 static paddr_t
   1028 auvia_mappage(void *addr, void *mem, off_t off, int prot)
   1029 {
   1030 	struct auvia_softc *sc;
   1031 	struct auvia_dma *p;
   1032 
   1033 	if (off < 0)
   1034 		return -1;
   1035 	sc = addr;
   1036 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
   1037 		continue;
   1038 
   1039 	if (!p)
   1040 		return -1;
   1041 
   1042 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
   1043 	    BUS_DMA_WAITOK);
   1044 }
   1045 
   1046 static int
   1047 auvia_get_props(void *addr)
   1048 {
   1049 	struct auvia_softc *sc;
   1050 	int props;
   1051 
   1052 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
   1053 	sc = addr;
   1054 	/*
   1055 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
   1056 	 * rate because of aurateconv.  Applications can't know what rate the
   1057 	 * device can process in the case of mmap().
   1058 	 */
   1059 	if (!AC97_IS_FIXED_RATE(sc->codec_if))
   1060 		props |= AUDIO_PROP_MMAP;
   1061 	return props;
   1062 }
   1063 
   1064 static void
   1065 auvia_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
   1066 {
   1067 	struct auvia_softc *sc;
   1068 
   1069 	sc = addr;
   1070 	*intr = &sc->sc_intr_lock;
   1071 	*thread = &sc->sc_lock;
   1072 }
   1073 
   1074 static int
   1075 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
   1076 	struct auvia_dma *p, void *start, void *end, int blksize)
   1077 {
   1078 	struct auvia_dma_op *op;
   1079 	bus_addr_t s;
   1080 	size_t l;
   1081 
   1082 	op = ch->sc_dma_ops;
   1083 	s = p->map->dm_segs[0].ds_addr;
   1084 	l = ((char *)end - (char *)start);
   1085 
   1086 	while (l) {
   1087 		op->ptr = htole32(s);
   1088 		l = l - blksize;
   1089 		if (!l) {
   1090 			/* if last block */
   1091 			op->flags = htole32(AUVIA_DMAOP_EOL | blksize);
   1092 		} else {
   1093 			op->flags = htole32(AUVIA_DMAOP_FLAG | blksize);
   1094 		}
   1095 		s += blksize;
   1096 		op++;
   1097 	}
   1098 
   1099 	return 0;
   1100 }
   1101 
   1102 
   1103 static int
   1104 auvia_trigger_output(void *addr, void *start, void *end, int blksize,
   1105     void (*intr)(void *), void *arg, const audio_params_t *param)
   1106 {
   1107 	struct auvia_softc *sc;
   1108 	struct auvia_softc_chan *ch;
   1109 	struct auvia_dma *p;
   1110 
   1111 	sc = addr;
   1112 	ch = &(sc->sc_play);
   1113 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
   1114 		continue;
   1115 
   1116 	if (!p)
   1117 		panic("auvia_trigger_output: request with bad start "
   1118 			"address (%p)", start);
   1119 
   1120 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
   1121 		return 1;
   1122 	}
   1123 
   1124 	ch->sc_intr = intr;
   1125 	ch->sc_arg = arg;
   1126 
   1127 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
   1128 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1129 
   1130 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1131 		if (ch->sc_base != VIA8233_MP_BASE) {
   1132 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
   1133 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
   1134 		}
   1135 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
   1136 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1137 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1138 	} else {
   1139 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
   1140 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1141 	}
   1142 
   1143 	return 0;
   1144 }
   1145 
   1146 static int
   1147 auvia_trigger_input(void *addr, void *start, void *end, int blksize,
   1148     void (*intr)(void *), void *arg, const audio_params_t *param)
   1149 {
   1150 	struct auvia_softc *sc;
   1151 	struct auvia_softc_chan *ch;
   1152 	struct auvia_dma *p;
   1153 
   1154 	sc = addr;
   1155 	ch = &(sc->sc_record);
   1156 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
   1157 		continue;
   1158 
   1159 	if (!p)
   1160 		panic("auvia_trigger_input: request with bad start "
   1161 			"address (%p)", start);
   1162 
   1163 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
   1164 		return 1;
   1165 	}
   1166 
   1167 	ch->sc_intr = intr;
   1168 	ch->sc_arg = arg;
   1169 
   1170 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
   1171 		  ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1172 
   1173 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1174 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
   1175 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
   1176 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
   1177 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1178 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1179 	} else {
   1180 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
   1181 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1182 	}
   1183 
   1184 	return 0;
   1185 }
   1186 
   1187 static int
   1188 auvia_intr(void *arg)
   1189 {
   1190 	struct auvia_softc *sc;
   1191 	struct auvia_softc_chan *ch;
   1192 	u_int8_t r;
   1193 	int rval;
   1194 
   1195 	sc = arg;
   1196 	rval = 0;
   1197 	ch = &sc->sc_record;
   1198 
   1199 	mutex_spin_enter(&sc->sc_intr_lock);
   1200 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
   1201 	if (r & AUVIA_RPSTAT_INTR) {
   1202 		if (sc->sc_record.sc_intr)
   1203 			sc->sc_record.sc_intr(sc->sc_record.sc_arg);
   1204 
   1205 		/* clear interrupts */
   1206 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1207 		rval = 1;
   1208 	}
   1209 
   1210 	ch = &sc->sc_play;
   1211 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
   1212 	if (r & AUVIA_RPSTAT_INTR) {
   1213 		if (sc->sc_play.sc_intr)
   1214 			sc->sc_play.sc_intr(sc->sc_play.sc_arg);
   1215 
   1216 		/* clear interrupts */
   1217 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1218 		rval = 1;
   1219 	}
   1220 	mutex_spin_exit(&sc->sc_intr_lock);
   1221 
   1222 	return rval;
   1223 }
   1224 
   1225 static bool
   1226 auvia_resume(device_t dv, const pmf_qual_t *qual)
   1227 {
   1228 	struct auvia_softc *sc = device_private(dv);
   1229 
   1230 	mutex_enter(&sc->sc_lock);
   1231 	mutex_spin_enter(&sc->sc_intr_lock);
   1232 	auvia_reset_codec(sc);
   1233 	DELAY(1000);
   1234 	mutex_spin_exit(&sc->sc_intr_lock);
   1235 	(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
   1236 	mutex_exit(&sc->sc_lock);
   1237 
   1238 	return true;
   1239 }
   1240