Home | History | Annotate | Line # | Download | only in pci
auvia.c revision 1.77
      1 /*	$NetBSD: auvia.c,v 1.77 2014/03/29 19:28:24 christos 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.77 2014/03/29 19:28:24 christos 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_SLINEAR_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(pc, ih, IPL_AUDIO, auvia_intr, sc);
    433 	if (sc->sc_ih == NULL) {
    434 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
    435 		if (intrstr != NULL)
    436 			aprint_error(" at %s", intrstr);
    437 		aprint_error("\n");
    438 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    439 		mutex_destroy(&sc->sc_lock);
    440 		mutex_destroy(&sc->sc_intr_lock);
    441 		return;
    442 	}
    443 
    444 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    445 
    446 	/* disable SBPro compat & others */
    447 	pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
    448 
    449 	pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
    450 	/* XXX what to do about MIDI, FM, joystick? */
    451 
    452 	pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
    453 		| AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
    454 
    455 	pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
    456 
    457 	pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
    458 
    459 	sc->host_if.arg = sc;
    460 	sc->host_if.attach = auvia_attach_codec;
    461 	sc->host_if.read = auvia_read_codec;
    462 	sc->host_if.write = auvia_write_codec;
    463 	sc->host_if.reset = auvia_reset_codec;
    464 	sc->host_if.spdif_event = auvia_spdif_event;
    465 
    466 	if ((r = ac97_attach(&sc->host_if, self, &sc->sc_lock)) != 0) {
    467 		aprint_error_dev(sc->sc_dev, "can't attach codec (error 0x%X)\n", r);
    468 		pci_intr_disestablish(pc, sc->sc_ih);
    469 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    470 		mutex_destroy(&sc->sc_lock);
    471 		mutex_destroy(&sc->sc_intr_lock);
    472 		return;
    473 	}
    474 
    475 	/* setup audio_format */
    476 	memcpy(sc->sc_formats, auvia_formats, sizeof(auvia_formats));
    477 	mutex_enter(&sc->sc_lock);
    478 	if (sc->sc_play.sc_base != VIA8233_MP_BASE || !AC97_IS_4CH(sc->codec_if)) {
    479 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_4CH_8]);
    480 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_4CH_16]);
    481 	}
    482 	if (sc->sc_play.sc_base != VIA8233_MP_BASE || !AC97_IS_6CH(sc->codec_if)) {
    483 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_6CH_8]);
    484 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_6CH_16]);
    485 	}
    486 	if (AC97_IS_FIXED_RATE(sc->codec_if)) {
    487 		for (r = 0; r < AUVIA_NFORMATS; r++) {
    488 			sc->sc_formats[r].frequency_type = 1;
    489 			sc->sc_formats[r].frequency[0] = 48000;
    490 		}
    491 	}
    492 	mutex_exit(&sc->sc_lock);
    493 
    494 	if (0 != auconv_create_encodings(sc->sc_formats, AUVIA_NFORMATS,
    495 					 &sc->sc_encodings)) {
    496 		mutex_enter(&sc->sc_lock);
    497 		sc->codec_if->vtbl->detach(sc->codec_if);
    498 		mutex_exit(&sc->sc_lock);
    499 		pci_intr_disestablish(pc, sc->sc_ih);
    500 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    501 		mutex_destroy(&sc->sc_lock);
    502 		mutex_destroy(&sc->sc_intr_lock);
    503 		aprint_error_dev(sc->sc_dev, "can't create encodings\n");
    504 		return;
    505 	}
    506 	if (0 != auconv_create_encodings(auvia_spdif_formats,
    507 	    AUVIA_SPDIF_NFORMATS, &sc->sc_spdif_encodings)) {
    508 		mutex_enter(&sc->sc_lock);
    509 		sc->codec_if->vtbl->detach(sc->codec_if);
    510 		mutex_exit(&sc->sc_lock);
    511 		pci_intr_disestablish(pc, sc->sc_ih);
    512 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    513 		mutex_destroy(&sc->sc_lock);
    514 		mutex_destroy(&sc->sc_intr_lock);
    515 		aprint_error_dev(sc->sc_dev, "can't create spdif encodings\n");
    516 		return;
    517 	}
    518 
    519 	if (!pmf_device_register(self, NULL, auvia_resume))
    520 		aprint_error_dev(self, "couldn't establish power handler\n");
    521 
    522 	audio_attach_mi(&auvia_hw_if, sc, sc->sc_dev);
    523 	mutex_enter(&sc->sc_lock);
    524 	sc->codec_if->vtbl->unlock(sc->codec_if);
    525 	mutex_exit(&sc->sc_lock);
    526 	return;
    527 }
    528 
    529 static int
    530 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
    531 {
    532 	struct auvia_softc *sc;
    533 
    534 	sc = addr;
    535 	sc->codec_if = cif;
    536 	return 0;
    537 }
    538 
    539 static int
    540 auvia_reset_codec(void *addr)
    541 {
    542 	struct auvia_softc *sc;
    543 	pcireg_t r;
    544 	int i;
    545 
    546 	/* perform a codec cold reset */
    547 	sc = addr;
    548 	r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
    549 
    550 	r &= ~AUVIA_PCICONF_ACNOTRST;	/* enable RESET (active low) */
    551 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    552 	delay(2);
    553 
    554 	r |= AUVIA_PCICONF_ACNOTRST;	/* disable RESET (inactive high) */
    555 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    556 	delay(200);
    557 
    558 	for (i = 500000; i != 0 && !(pci_conf_read(sc->sc_pc, sc->sc_pt,
    559 		AUVIA_PCICONF_JUNK) & AUVIA_PCICONF_PRIVALID); i--)
    560 		DELAY(1);
    561 	if (i == 0) {
    562 		printf("%s: codec reset timed out\n", device_xname(sc->sc_dev));
    563 		return ETIMEDOUT;
    564 	}
    565 	return 0;
    566 }
    567 
    568 static int
    569 auvia_waitready_codec(struct auvia_softc *sc)
    570 {
    571 	int i;
    572 
    573 	/* poll until codec not busy */
    574 	for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    575 		AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
    576 		delay(1);
    577 	if (i >= TIMEOUT) {
    578 		printf("%s: codec busy\n", device_xname(sc->sc_dev));
    579 		return 1;
    580 	}
    581 
    582 	return 0;
    583 }
    584 
    585 static int
    586 auvia_waitvalid_codec(struct auvia_softc *sc)
    587 {
    588 	int i;
    589 
    590 	/* poll until codec valid */
    591 	for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    592 		AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
    593 			delay(1);
    594 	if (i >= TIMEOUT) {
    595 		printf("%s: codec invalid\n", device_xname(sc->sc_dev));
    596 		return 1;
    597 	}
    598 
    599 	return 0;
    600 }
    601 
    602 static int
    603 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
    604 {
    605 	struct auvia_softc *sc;
    606 
    607 	sc = addr;
    608 	if (auvia_waitready_codec(sc))
    609 		return 1;
    610 
    611 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    612 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
    613 
    614 	return 0;
    615 }
    616 
    617 static int
    618 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
    619 {
    620 	struct auvia_softc *sc;
    621 
    622 	sc = addr;
    623 	if (auvia_waitready_codec(sc))
    624 		return 1;
    625 
    626 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    627 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
    628 
    629 	if (auvia_waitready_codec(sc))
    630 		return 1;
    631 
    632 	if (auvia_waitvalid_codec(sc))
    633 		return 1;
    634 
    635 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
    636 
    637 	return 0;
    638 }
    639 
    640 static void
    641 auvia_spdif_event(void *addr, bool flag)
    642 {
    643 	struct auvia_softc *sc;
    644 
    645 	sc = addr;
    646 	sc->sc_spdif = flag;
    647 }
    648 
    649 static int
    650 auvia_open(void *addr, int flags)
    651 {
    652 	struct auvia_softc *sc;
    653 
    654 	sc = (struct auvia_softc *)addr;
    655 	mutex_spin_exit(&sc->sc_intr_lock);
    656 	sc->codec_if->vtbl->lock(sc->codec_if);
    657 	mutex_spin_enter(&sc->sc_intr_lock);
    658 	return 0;
    659 }
    660 
    661 static void
    662 auvia_close(void *addr)
    663 {
    664 	struct auvia_softc *sc;
    665 
    666 	sc = (struct auvia_softc *)addr;
    667 	mutex_spin_exit(&sc->sc_intr_lock);
    668 	sc->codec_if->vtbl->unlock(sc->codec_if);
    669 	mutex_spin_enter(&sc->sc_intr_lock);
    670 }
    671 
    672 static int
    673 auvia_query_encoding(void *addr, struct audio_encoding *fp)
    674 {
    675 	struct auvia_softc *sc;
    676 
    677 	sc = (struct auvia_softc *)addr;
    678 	return auconv_query_encoding(
    679 	    sc->sc_spdif ? sc->sc_spdif_encodings : sc->sc_encodings, fp);
    680 }
    681 
    682 static void
    683 auvia_set_params_sub(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    684 		     const audio_params_t *p)
    685 {
    686 	uint32_t v;
    687 	uint16_t regval;
    688 
    689 	if (!(sc->sc_flags & AUVIA_FLAGS_VT8233)) {
    690 		regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
    691 			| (p->precision  == 16 ?
    692 				AUVIA_RPMODE_16BIT : 0)
    693 			| AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
    694 			| AUVIA_RPMODE_AUTOSTART;
    695 		ch->sc_reg = regval;
    696 	} else if (ch->sc_base != VIA8233_MP_BASE) {
    697 		v = CH_READ4(sc, ch, VIA8233_RP_RATEFMT);
    698 		v &= ~(VIA8233_RATEFMT_48K | VIA8233_RATEFMT_STEREO
    699 			| VIA8233_RATEFMT_16BIT);
    700 
    701 		v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20)
    702 			/ (48000 / 20);
    703 		if (p->channels == 2)
    704 			v |= VIA8233_RATEFMT_STEREO;
    705 		if (p->precision == 16)
    706 			v |= VIA8233_RATEFMT_16BIT;
    707 
    708 		CH_WRITE4(sc, ch, VIA8233_RP_RATEFMT, v);
    709 	} else {
    710 		static const u_int32_t slottab[7] =
    711 			{ 0, 0xff000011, 0xff000021, 0,
    712 			  0xff004321, 0, 0xff436521};
    713 
    714 		regval = (p->precision == 16
    715 			? VIA8233_MP_FORMAT_16BIT : VIA8233_MP_FORMAT_8BIT)
    716 			| (p->channels << 4);
    717 		CH_WRITE1(sc, ch, VIA8233_OFF_MP_FORMAT, regval);
    718 		CH_WRITE4(sc, ch, VIA8233_OFF_MP_STOP, slottab[p->channels]);
    719 	}
    720 }
    721 
    722 static int
    723 auvia_set_params(void *addr, int setmode, int usemode,
    724     audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
    725     stream_filter_list_t *rfil)
    726 {
    727 	struct auvia_softc *sc;
    728 	struct auvia_softc_chan *ch;
    729 	struct audio_params *p;
    730 	struct ac97_codec_if* codec;
    731 	stream_filter_list_t *fil;
    732 	int reg, mode;
    733 	int index;
    734 
    735 	sc = addr;
    736 	codec = sc->codec_if;
    737 	/* for mode in (RECORD, PLAY) */
    738 	for (mode = AUMODE_RECORD; mode != -1;
    739 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    740 		if ((setmode & mode) == 0)
    741 			continue;
    742 
    743 		if (mode == AUMODE_PLAY ) {
    744 			p = play;
    745 			ch = &sc->sc_play;
    746 			reg = AC97_REG_PCM_FRONT_DAC_RATE;
    747 			fil = pfil;
    748 		} else {
    749 			p = rec;
    750 			ch = &sc->sc_record;
    751 			reg = AC97_REG_PCM_LR_ADC_RATE;
    752 			fil = rfil;
    753 		}
    754 
    755 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
    756 		    (p->precision != 8 && p->precision != 16))
    757 			return (EINVAL);
    758 		if (sc->sc_spdif)
    759 			index = auconv_set_converter(auvia_spdif_formats,
    760 			    AUVIA_SPDIF_NFORMATS, mode, p, TRUE, fil);
    761 		else
    762 			index = auconv_set_converter(sc->sc_formats,
    763 			    AUVIA_NFORMATS, mode, p, TRUE, fil);
    764 		if (index < 0)
    765 			return EINVAL;
    766 		if (fil->req_size > 0)
    767 			p = &fil->filters[0].param;
    768 		if (!AC97_IS_FIXED_RATE(codec)) {
    769 			if (codec->vtbl->set_rate(codec, reg, &p->sample_rate))
    770 				return EINVAL;
    771 			reg = AC97_REG_PCM_SURR_DAC_RATE;
    772 			if (p->channels >= 4
    773 			    && codec->vtbl->set_rate(codec, reg,
    774 						     &p->sample_rate))
    775 				return EINVAL;
    776 			reg = AC97_REG_PCM_LFE_DAC_RATE;
    777 			if (p->channels == 6
    778 			    && codec->vtbl->set_rate(codec, reg,
    779 						     &p->sample_rate))
    780 				return EINVAL;
    781 		}
    782 		auvia_set_params_sub(sc, ch, p);
    783 	}
    784 
    785 	return 0;
    786 }
    787 
    788 static int
    789 auvia_round_blocksize(void *addr, int blk,
    790     int mode, const audio_params_t *param)
    791 {
    792 	struct auvia_softc *sc;
    793 
    794 	sc = addr;
    795 
    796 	/* XXX VT823x might have the limitation of dma_ops size */
    797 	if (sc->sc_flags & AUVIA_FLAGS_VT8233 && blk < 288)
    798 		blk = 288;
    799 
    800 	/* Avoid too many dma_ops. */
    801 	return min((blk & -32), AUVIA_MINBLKSZ);
    802 }
    803 
    804 static int
    805 auvia_halt_output(void *addr)
    806 {
    807 	struct auvia_softc *sc;
    808 	struct auvia_softc_chan *ch;
    809 
    810 	sc = addr;
    811 	ch = &(sc->sc_play);
    812 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    813 	ch->sc_intr = NULL;
    814 	return 0;
    815 }
    816 
    817 static int
    818 auvia_halt_input(void *addr)
    819 {
    820 	struct auvia_softc *sc;
    821 	struct auvia_softc_chan *ch;
    822 
    823 	sc = addr;
    824 	ch = &(sc->sc_record);
    825 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    826 	ch->sc_intr = NULL;
    827 	return 0;
    828 }
    829 
    830 static int
    831 auvia_getdev(void *addr, struct audio_device *retp)
    832 {
    833 	struct auvia_softc *sc;
    834 
    835 	if (retp) {
    836 		sc = addr;
    837 		if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    838 			strncpy(retp->name, "VIA VT823x",
    839 				sizeof(retp->name));
    840 		} else {
    841 			strncpy(retp->name, "VIA VT82C686A",
    842 				sizeof(retp->name));
    843 		}
    844 		strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
    845 		strncpy(retp->config, "auvia", sizeof(retp->config));
    846 	}
    847 
    848 	return 0;
    849 }
    850 
    851 static int
    852 auvia_set_port(void *addr, mixer_ctrl_t *cp)
    853 {
    854 	struct auvia_softc *sc;
    855 
    856 	sc = addr;
    857 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    858 }
    859 
    860 static int
    861 auvia_get_port(void *addr, mixer_ctrl_t *cp)
    862 {
    863 	struct auvia_softc *sc;
    864 
    865 	sc = addr;
    866 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
    867 }
    868 
    869 static int
    870 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
    871 {
    872 	struct auvia_softc *sc;
    873 
    874 	sc = addr;
    875 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
    876 }
    877 
    878 static int
    879 auvia_malloc_channel(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    880 		     size_t size)
    881 {
    882 	struct auvia_dma *dp;
    883 	int segs;
    884 
    885 	/* if old list is large enough, nothing to do */
    886 	segs = (size + AUVIA_MINBLKSZ - 1) / AUVIA_MINBLKSZ;
    887 	if (segs <= ch->sc_dma_op_count) {
    888 		return 0;
    889 	}
    890 
    891 	if (ch->sc_dma_ops) {
    892 		auvia_free(sc, ch->sc_dma_ops,
    893 		    ch->sc_dma_op_count * sizeof(*dp));
    894 	}
    895 
    896 	ch->sc_dma_ops = auvia_malloc_dmamem(sc, 0,
    897 	    sizeof(struct auvia_dma_op) * segs);
    898 
    899 	if (ch->sc_dma_ops == NULL) {
    900 		aprint_error_dev(sc->sc_dev, "couldn't build dmaops\n");
    901 		return ENOMEM;
    902 	}
    903 
    904 	for (dp = sc->sc_dmas;
    905 	    dp && dp->addr != (void *)(ch->sc_dma_ops);
    906 	    dp = dp->next)
    907 		continue;
    908 
    909 	if (!dp)
    910 		panic("%s: build_dma_ops: where'd my memory go??? "
    911 			"address (%p)\n", device_xname(sc->sc_dev),
    912 			ch->sc_dma_ops);
    913 
    914 	ch->sc_dma_op_count = segs;
    915 	ch->sc_dma_ops_dma = dp;
    916 
    917 	return 0;
    918 }
    919 
    920 static void *
    921 auvia_malloc_dmamem(void *addr, int direction, size_t size)
    922 {
    923 	struct auvia_softc *sc;
    924 	struct auvia_dma *p;
    925 	int error;
    926 	int rseg;
    927 
    928 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
    929 	if (p == NULL)
    930 		return NULL;
    931 	sc = addr;
    932 	p->size = size;
    933 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
    934 				      1, &rseg, BUS_DMA_WAITOK)) != 0) {
    935 		aprint_error_dev(sc->sc_dev, "unable to allocate DMA, error = %d\n", error);
    936 		goto fail_alloc;
    937 	}
    938 
    939 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
    940 				    BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
    941 		aprint_error_dev(sc->sc_dev, "unable to map DMA, error = %d\n",
    942 		       error);
    943 		goto fail_map;
    944 	}
    945 
    946 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    947 				       BUS_DMA_WAITOK, &p->map)) != 0) {
    948 		aprint_error_dev(sc->sc_dev, "unable to create DMA map, error = %d\n",
    949 		       error);
    950 		goto fail_create;
    951 	}
    952 
    953 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
    954 				     BUS_DMA_WAITOK)) != 0) {
    955 		aprint_error_dev(sc->sc_dev, "unable to load DMA map, error = %d\n",
    956 		       error);
    957 		goto fail_load;
    958 	}
    959 
    960 	p->next = sc->sc_dmas;
    961 	sc->sc_dmas = p;
    962 
    963 	return p->addr;
    964 
    965 fail_load:
    966 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    967 fail_create:
    968 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
    969 fail_map:
    970 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    971 fail_alloc:
    972 	kmem_free(p, sizeof(*p));
    973 	return NULL;
    974 }
    975 
    976 static void *
    977 auvia_malloc(void *addr, int direction, size_t size)
    978 {
    979 	struct auvia_softc *sc;
    980 	void *p;
    981 
    982 	sc = addr;
    983 
    984 	p = auvia_malloc_dmamem(addr, direction, size);
    985 	if (p == NULL) {
    986 		return NULL;
    987 	}
    988 	if (auvia_malloc_channel(sc, &sc->sc_play, size) != 0) {
    989 		auvia_free(addr, p, size);
    990 		return NULL;
    991 	}
    992 	if (auvia_malloc_channel(sc, &sc->sc_record, size) != 0) {
    993 		auvia_free(addr, p, size);
    994 		return NULL;
    995 	}
    996 	return p;
    997 }
    998 
    999 static void
   1000 auvia_free(void *addr, void *ptr, size_t size)
   1001 {
   1002 	struct auvia_softc *sc;
   1003 	struct auvia_dma **pp, *p;
   1004 
   1005 	sc = addr;
   1006 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
   1007 		if (p->addr == ptr) {
   1008 			bus_dmamap_unload(sc->sc_dmat, p->map);
   1009 			bus_dmamap_destroy(sc->sc_dmat, p->map);
   1010 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
   1011 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
   1012 
   1013 			*pp = p->next;
   1014 			kmem_free(p, sizeof(*p));
   1015 			return;
   1016 		}
   1017 
   1018 	panic("auvia_free: trying to free unallocated memory");
   1019 }
   1020 
   1021 static size_t
   1022 auvia_round_buffersize(void *addr, int direction, size_t size)
   1023 {
   1024 
   1025 	return size;
   1026 }
   1027 
   1028 static paddr_t
   1029 auvia_mappage(void *addr, void *mem, off_t off, int prot)
   1030 {
   1031 	struct auvia_softc *sc;
   1032 	struct auvia_dma *p;
   1033 
   1034 	if (off < 0)
   1035 		return -1;
   1036 	sc = addr;
   1037 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
   1038 		continue;
   1039 
   1040 	if (!p)
   1041 		return -1;
   1042 
   1043 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
   1044 	    BUS_DMA_WAITOK);
   1045 }
   1046 
   1047 static int
   1048 auvia_get_props(void *addr)
   1049 {
   1050 	struct auvia_softc *sc;
   1051 	int props;
   1052 
   1053 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
   1054 	sc = addr;
   1055 	/*
   1056 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
   1057 	 * rate because of aurateconv.  Applications can't know what rate the
   1058 	 * device can process in the case of mmap().
   1059 	 */
   1060 	if (!AC97_IS_FIXED_RATE(sc->codec_if))
   1061 		props |= AUDIO_PROP_MMAP;
   1062 	return props;
   1063 }
   1064 
   1065 static void
   1066 auvia_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
   1067 {
   1068 	struct auvia_softc *sc;
   1069 
   1070 	sc = addr;
   1071 	*intr = &sc->sc_intr_lock;
   1072 	*thread = &sc->sc_lock;
   1073 }
   1074 
   1075 static int
   1076 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
   1077 	struct auvia_dma *p, void *start, void *end, int blksize)
   1078 {
   1079 	struct auvia_dma_op *op;
   1080 	bus_addr_t s;
   1081 	size_t l;
   1082 
   1083 	op = ch->sc_dma_ops;
   1084 	s = p->map->dm_segs[0].ds_addr;
   1085 	l = ((char *)end - (char *)start);
   1086 
   1087 	while (l) {
   1088 		op->ptr = htole32(s);
   1089 		l = l - blksize;
   1090 		if (!l) {
   1091 			/* if last block */
   1092 			op->flags = htole32(AUVIA_DMAOP_EOL | blksize);
   1093 		} else {
   1094 			op->flags = htole32(AUVIA_DMAOP_FLAG | blksize);
   1095 		}
   1096 		s += blksize;
   1097 		op++;
   1098 	}
   1099 
   1100 	return 0;
   1101 }
   1102 
   1103 
   1104 static int
   1105 auvia_trigger_output(void *addr, void *start, void *end, int blksize,
   1106     void (*intr)(void *), void *arg, const audio_params_t *param)
   1107 {
   1108 	struct auvia_softc *sc;
   1109 	struct auvia_softc_chan *ch;
   1110 	struct auvia_dma *p;
   1111 
   1112 	sc = addr;
   1113 	ch = &(sc->sc_play);
   1114 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
   1115 		continue;
   1116 
   1117 	if (!p)
   1118 		panic("auvia_trigger_output: request with bad start "
   1119 			"address (%p)", start);
   1120 
   1121 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
   1122 		return 1;
   1123 	}
   1124 
   1125 	ch->sc_intr = intr;
   1126 	ch->sc_arg = arg;
   1127 
   1128 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
   1129 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1130 
   1131 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1132 		if (ch->sc_base != VIA8233_MP_BASE) {
   1133 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
   1134 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
   1135 		}
   1136 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
   1137 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1138 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1139 	} else {
   1140 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
   1141 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1142 	}
   1143 
   1144 	return 0;
   1145 }
   1146 
   1147 static int
   1148 auvia_trigger_input(void *addr, void *start, void *end, int blksize,
   1149     void (*intr)(void *), void *arg, const audio_params_t *param)
   1150 {
   1151 	struct auvia_softc *sc;
   1152 	struct auvia_softc_chan *ch;
   1153 	struct auvia_dma *p;
   1154 
   1155 	sc = addr;
   1156 	ch = &(sc->sc_record);
   1157 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
   1158 		continue;
   1159 
   1160 	if (!p)
   1161 		panic("auvia_trigger_input: request with bad start "
   1162 			"address (%p)", start);
   1163 
   1164 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
   1165 		return 1;
   1166 	}
   1167 
   1168 	ch->sc_intr = intr;
   1169 	ch->sc_arg = arg;
   1170 
   1171 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
   1172 		  ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1173 
   1174 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1175 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
   1176 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
   1177 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
   1178 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1179 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1180 	} else {
   1181 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
   1182 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1183 	}
   1184 
   1185 	return 0;
   1186 }
   1187 
   1188 static int
   1189 auvia_intr(void *arg)
   1190 {
   1191 	struct auvia_softc *sc;
   1192 	struct auvia_softc_chan *ch;
   1193 	u_int8_t r;
   1194 	int rval;
   1195 
   1196 	sc = arg;
   1197 	rval = 0;
   1198 	ch = &sc->sc_record;
   1199 
   1200 	mutex_spin_enter(&sc->sc_intr_lock);
   1201 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
   1202 	if (r & AUVIA_RPSTAT_INTR) {
   1203 		if (sc->sc_record.sc_intr)
   1204 			sc->sc_record.sc_intr(sc->sc_record.sc_arg);
   1205 
   1206 		/* clear interrupts */
   1207 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1208 		rval = 1;
   1209 	}
   1210 
   1211 	ch = &sc->sc_play;
   1212 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
   1213 	if (r & AUVIA_RPSTAT_INTR) {
   1214 		if (sc->sc_play.sc_intr)
   1215 			sc->sc_play.sc_intr(sc->sc_play.sc_arg);
   1216 
   1217 		/* clear interrupts */
   1218 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1219 		rval = 1;
   1220 	}
   1221 	mutex_spin_exit(&sc->sc_intr_lock);
   1222 
   1223 	return rval;
   1224 }
   1225 
   1226 static bool
   1227 auvia_resume(device_t dv, const pmf_qual_t *qual)
   1228 {
   1229 	struct auvia_softc *sc = device_private(dv);
   1230 
   1231 	mutex_enter(&sc->sc_lock);
   1232 	mutex_spin_enter(&sc->sc_intr_lock);
   1233 	auvia_reset_codec(sc);
   1234 	DELAY(1000);
   1235 	mutex_spin_exit(&sc->sc_intr_lock);
   1236 	(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
   1237 	mutex_exit(&sc->sc_lock);
   1238 
   1239 	return true;
   1240 }
   1241