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