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