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