Home | History | Annotate | Line # | Download | only in pci
auvia.c revision 1.1
      1 /*	$NetBSD: auvia.c,v 1.1 2000/03/31 04:45:28 tsarna 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 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/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/malloc.h>
     52 #include <sys/device.h>
     53 #include <sys/audioio.h>
     54 
     55 #include <dev/pci/pcidevs.h>
     56 #include <dev/pci/pcivar.h>
     57 
     58 #include <dev/audio_if.h>
     59 #include <dev/mulaw.h>
     60 #include <dev/auconv.h>
     61 
     62 #include <dev/ic/ac97.h>
     63 
     64 #include <dev/pci/auviavar.h>
     65 
     66 struct auvia_dma {
     67 	struct auvia_dma *next;
     68 	caddr_t addr;
     69 	size_t size;
     70 	bus_dmamap_t map;
     71 	bus_dma_segment_t seg;
     72 };
     73 
     74 struct auvia_dma_op {
     75 	u_int32_t ptr;
     76 	u_int32_t flags;
     77 #define AUVIA_DMAOP_EOL		0x80000000
     78 #define AUVIA_DMAOP_FLAG	0x40000000
     79 #define AUVIA_DMAOP_STOP	0x20000000
     80 #define AUVIA_DMAOP_COUNT(x)	((x)&0x00FFFFFF)
     81 };
     82 
     83 int	auvia_match(struct device *, struct cfdata *, void *);
     84 void	auvia_attach(struct device *, struct device *, void *);
     85 int	auvia_open(void *, int);
     86 void	auvia_close(void *);
     87 int	auvia_query_encoding(void *addr, struct audio_encoding *fp);
     88 int	auvia_set_params(void *, int, int, struct audio_params *,
     89 	struct audio_params *);
     90 int	auvia_round_blocksize(void *, int);
     91 int	auvia_halt_output(void *);
     92 int	auvia_halt_input(void *);
     93 int	auvia_getdev(void *, struct audio_device *);
     94 int	auvia_set_port(void *, mixer_ctrl_t *);
     95 int	auvia_get_port(void *, mixer_ctrl_t *);
     96 int	auvia_query_devinfo(void *, mixer_devinfo_t *);
     97 void *	auvia_malloc(void *, int, size_t, int, int);
     98 void	auvia_free(void *, void *, int);
     99 size_t	auvia_round_buffersize(void *, int, size_t);
    100 int	auvia_mappage(void *, void *, int, int);
    101 int	auvia_get_props(void *);
    102 int	auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
    103 	struct auvia_dma *, void *, void *, int);
    104 int	auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
    105 	void *, struct audio_params *);
    106 int	auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
    107 	void *, struct audio_params *);
    108 
    109 int	auvia_intr __P((void *));
    110 
    111 struct cfattach auvia_ca = {
    112 	sizeof (struct auvia_softc), auvia_match, auvia_attach
    113 };
    114 
    115 #define AUVIA_PCICONF_JUNK	0x40
    116 #define		AUVIA_PCICONF_ENABLES	 0x00FF0000	/* reg 42 mask */
    117 #define		AUVIA_PCICONF_ACLINKENAB 0x00008000	/* ac link enab */
    118 #define		AUVIA_PCICONF_ACNOTRST	 0x00004000	/* ~(ac reset) */
    119 #define		AUVIA_PCICONF_ACSYNC	 0x00002000	/* ac sync */
    120 #define		AUVIA_PCICONF_ACVSR	 0x00000800	/* var. samp. rate */
    121 #define		AUVIA_PCICONF_ACSGD	 0x00000400	/* SGD enab */
    122 #define		AUVIA_PCICONF_ACFM	 0x00000200	/* FM enab */
    123 #define		AUVIA_PCICONF_ACSB	 0x00000100	/* SB enab */
    124 
    125 #define AUVIA_PLAY_STAT			0x00
    126 #define AUVIA_RECORD_STAT		0x10
    127 #define		AUVIA_RPSTAT_INTR		0x03
    128 #define AUVIA_PLAY_CONTROL		0x01
    129 #define AUVIA_RECORD_CONTROL		0x11
    130 #define		AUVIA_RPCTRL_START		0x80
    131 #define		AUVIA_RPCTRL_TERMINATE		0x40
    132 #define AUVIA_PLAY_MODE			0x02
    133 #define AUVIA_RECORD_MODE		0x12
    134 #define		AUVIA_RPMODE_INTR_FLAG		0x01
    135 #define		AUVIA_RPMODE_INTR_EOL		0x02
    136 #define		AUVIA_RPMODE_STEREO		0x10
    137 #define		AUVIA_RPMODE_16BIT		0x20
    138 #define		AUVIA_RPMODE_AUTOSTART		0x80
    139 #define	AUVIA_PLAY_DMAOPS_BASE		0x04
    140 #define	AUVIA_RECORD_DMAOPS_BASE	0x14
    141 
    142 #define	AUVIA_CODEC_CTL			0x80
    143 #define		AUVIA_CODEC_READ		0x00800000
    144 #define		AUVIA_CODEC_BUSY		0x01000000
    145 #define		AUVIA_CODEC_PRIVALID		0x02000000
    146 #define		AUVIA_CODEC_INDEX(x)		((x)<<16)
    147 
    148 #define TIMEOUT	50
    149 
    150 #define	AC97_REG_EXT_AUDIO_ID		0x28
    151 #define		AC97_CODEC_DOES_VRA		0x0001
    152 #define	AC97_REG_EXT_AUDIO_STAT		0x2A
    153 #define		AC97_ENAB_VRA			0x0001
    154 #define		AC97_ENAB_MICVRA		0x0004
    155 #define	AC97_REG_EXT_DAC_RATE		0x2C
    156 #define	AC97_REG_EXT_ADC_RATE		0x32
    157 
    158 struct audio_hw_if auvia_hw_if = {
    159 	auvia_open,
    160 	auvia_close,
    161 	NULL, /* drain */
    162 	auvia_query_encoding,
    163 	auvia_set_params,
    164 	auvia_round_blocksize,
    165 	NULL, /* commit_settings */
    166 	NULL, /* init_output */
    167 	NULL, /* init_input */
    168 	NULL, /* start_output */
    169 	NULL, /* start_input */
    170 	auvia_halt_output,
    171 	auvia_halt_input,
    172 	NULL, /* speaker_ctl */
    173 	auvia_getdev,
    174 	NULL, /* setfd */
    175 	auvia_set_port,
    176 	auvia_get_port,
    177 	auvia_query_devinfo,
    178 	auvia_malloc,
    179 	auvia_free,
    180 	auvia_round_buffersize,
    181 	auvia_mappage,
    182 	auvia_get_props,
    183 	auvia_trigger_output,
    184 	auvia_trigger_input,
    185 };
    186 
    187 int	auvia_attach_codec(void *, struct ac97_codec_if *);
    188 int	auvia_write_codec(void *, u_int8_t, u_int16_t);
    189 int	auvia_read_codec(void *, u_int8_t, u_int16_t *);
    190 void	auvia_reset_codec(void *);
    191 int	auvia_waitready_codec(struct auvia_softc *sc);
    192 int	auvia_waitvalid_codec(struct auvia_softc *sc);
    193 
    194 
    195 int
    196 auvia_match(struct device *parent, struct cfdata *match, void *aux)
    197 {
    198 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    199 
    200 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
    201 		return 0;
    202 	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_VIATECH_VT82C686A_AC97)
    203 		return 0;
    204 
    205 	return 1;
    206 }
    207 
    208 
    209 void
    210 auvia_attach(struct device *parent, struct device *self, void *aux)
    211 {
    212 	struct pci_attach_args *pa = aux;
    213 	struct auvia_softc *sc = (struct auvia_softc *) self;
    214         const char *intrstr = NULL;
    215 	struct mixer_ctrl ctl;
    216 	pci_chipset_tag_t pc = pa->pa_pc;
    217         pcitag_t pt = pa->pa_tag;
    218         pci_intr_handle_t ih;
    219 	pcireg_t pr;
    220 	u_int16_t v;
    221         int r, i;
    222 
    223 	r = PCI_REVISION(pa->pa_class);
    224 	sc->sc_revision[1] = '\0';
    225 	if (r == 0x20) {
    226 		sc->sc_revision[0] = 'H';
    227 	} else if ((r >= 0x10) && (r <= 0x14)) {
    228 		sc->sc_revision[0] = 'A' + (r - 0x10);
    229 	} else {
    230 		sprintf(sc->sc_revision, "0x%02X", r);
    231 	}
    232 
    233 	printf(": VIA VT82C686A AC'97 Audio (rev %s)\n",
    234 		sc->sc_revision);
    235 
    236 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline,
    237 			&ih)) {
    238 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    239 		return;
    240 	}
    241 	intrstr = pci_intr_string(pc, ih);
    242 
    243 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
    244 	if (sc->sc_ih == NULL) {
    245 		printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
    246 		if (intrstr != NULL)
    247 			printf(" at %s", intrstr);
    248 		printf("\n");
    249 		return;
    250 	}
    251 
    252 	sc->sc_dmat = pa->pa_dmat;
    253 	sc->sc_pc = pc;
    254 	sc->sc_pt = pt;
    255 
    256 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    257 
    258 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
    259                            &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
    260 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    261 		return;
    262 	}
    263 
    264 	/* disable SBPro compat & others */
    265 	pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
    266 
    267 	pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
    268 	/* XXX what to do about MIDI, FM, joystick? */
    269 
    270 	pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
    271 		| AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
    272 
    273 	pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
    274 
    275 	pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
    276 
    277 	sc->host_if.arg = sc;
    278 	sc->host_if.attach = auvia_attach_codec;
    279 	sc->host_if.read = auvia_read_codec;
    280 	sc->host_if.write = auvia_write_codec;
    281 	sc->host_if.reset = auvia_reset_codec;
    282 
    283 	if ((r = ac97_attach(&sc->host_if)) != 0) {
    284 		printf("%s: can't attach codec (error 0x%X)\n",
    285 			sc->sc_dev.dv_xname, r);
    286 		return;
    287 	}
    288 
    289 	if (auvia_read_codec(sc, AC97_REG_EXT_AUDIO_ID, &v)
    290 	|| !(v & AC97_CODEC_DOES_VRA)) {
    291 		/* XXX */
    292 
    293 		printf("%s: codec must support AC'97 2.0 Variable Rate Audio\n",
    294 			sc->sc_dev.dv_xname);
    295 		return;
    296 	} else {
    297 		/* enable VRA */
    298 		auvia_write_codec(sc, AC97_REG_EXT_AUDIO_STAT,
    299 			AC97_ENAB_VRA | AC97_ENAB_MICVRA);
    300 	}
    301 
    302 	/* disable mutes */
    303 	for (i = 0; i < 4; i++) {
    304 		static struct {
    305 			char *class, *device;
    306 		} d[] = {
    307 			{ AudioCoutputs, AudioNmaster},
    308 			{ AudioCinputs, AudioNdac},
    309 			{ AudioCinputs, AudioNcd},
    310 			{ AudioCrecord, AudioNvolume},
    311 		};
    312 
    313 		ctl.type = AUDIO_MIXER_ENUM;
    314 		ctl.un.ord = 0;
    315 
    316 		ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
    317 			d[i].class, d[i].device, AudioNmute);
    318 		auvia_set_port(sc, &ctl);
    319 	}
    320 
    321 	/* set a reasonable default volume */
    322 
    323 	ctl.type = AUDIO_MIXER_VALUE;
    324 	ctl.un.value.num_channels = 2;
    325 	ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \
    326 	ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 199;
    327 
    328 	ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
    329 		AudioCoutputs, AudioNmaster, NULL);
    330 	auvia_set_port(sc, &ctl);
    331 
    332         audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
    333 }
    334 
    335 
    336 int
    337 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
    338 {
    339 	struct auvia_softc *sc = addr;
    340 
    341 	sc->codec_if = cif;
    342 
    343 	return 0;
    344 }
    345 
    346 
    347 void
    348 auvia_reset_codec(void *addr)
    349 {
    350 #ifdef notyet /* XXX seems to make codec become unready... ??? */
    351 	struct auvia_softc *sc = addr;
    352 	pcireg_t r;
    353 
    354 	/* perform a codec cold reset */
    355 
    356 	r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
    357 
    358 	r &= ~AUVIA_PCICONF_ACNOTRST;	/* enable RESET (active low) */
    359 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    360 	delay(2);
    361 
    362 	r |= AUVIA_PCICONF_ACNOTRST;		/* disable RESET (inactive high) */
    363 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    364 	delay(200);
    365 
    366 	auvia_waitready_codec(sc);
    367 #endif
    368 }
    369 
    370 
    371 int
    372 auvia_waitready_codec(struct auvia_softc *sc)
    373 {
    374 	int i;
    375 
    376 	/* poll until codec not busy */
    377 	for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    378 		AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
    379 		delay(1);
    380 	if (i >= TIMEOUT) {
    381 		printf("%s: codec busy\n", sc->sc_dev.dv_xname);
    382 		return 1;
    383 	}
    384 
    385 	return 0;
    386 }
    387 
    388 
    389 int
    390 auvia_waitvalid_codec(struct auvia_softc *sc)
    391 {
    392 	int i;
    393 
    394 	/* poll until codec valid */
    395 	for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    396 		AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
    397 			delay(1);
    398 	if (i >= TIMEOUT) {
    399 		printf("%s: codec invalid\n", sc->sc_dev.dv_xname);
    400 		return 1;
    401 	}
    402 
    403 	return 0;
    404 }
    405 
    406 
    407 int
    408 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
    409 {
    410 	struct auvia_softc *sc = addr;
    411 
    412 	if (auvia_waitready_codec(sc))
    413 		return 1;
    414 
    415 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    416 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
    417 
    418 	return 0;
    419 }
    420 
    421 
    422 int
    423 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
    424 {
    425 	struct auvia_softc *sc = addr;
    426 
    427 	if (auvia_waitready_codec(sc))
    428 		return 1;
    429 
    430 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
    431 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
    432 
    433 	if (auvia_waitready_codec(sc))
    434 		return 1;
    435 
    436 	if (auvia_waitvalid_codec(sc))
    437 		return 1;
    438 
    439 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
    440 
    441 	return 0;
    442 }
    443 
    444 
    445 int
    446 auvia_open(void *addr, int flags)
    447 {
    448 	return 0;
    449 }
    450 
    451 
    452 void
    453 auvia_close(void *addr)
    454 {
    455 	/* NOP */
    456 }
    457 
    458 
    459 int
    460 auvia_query_encoding(void *addr, struct audio_encoding *fp)
    461 {
    462 	switch (fp->index) {
    463 	case 0:
    464 		strcpy(fp->name, AudioEulinear);
    465 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    466 		fp->precision = 8;
    467 		fp->flags = 0;
    468 		return (0);
    469 	case 1:
    470 		strcpy(fp->name, AudioEmulaw);
    471 		fp->encoding = AUDIO_ENCODING_ULAW;
    472 		fp->precision = 8;
    473 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    474 		return (0);
    475 	case 2:
    476 		strcpy(fp->name, AudioEalaw);
    477 		fp->encoding = AUDIO_ENCODING_ALAW;
    478 		fp->precision = 8;
    479 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    480 		return (0);
    481 	case 3:
    482 		strcpy(fp->name, AudioEslinear);
    483 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    484 		fp->precision = 8;
    485 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    486 		return (0);
    487 	case 4:
    488 		strcpy(fp->name, AudioEslinear_le);
    489 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    490 		fp->precision = 16;
    491 		fp->flags = 0;
    492 		return (0);
    493 	case 5:
    494 		strcpy(fp->name, AudioEulinear_le);
    495 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    496 		fp->precision = 16;
    497 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    498 		return (0);
    499 	case 6:
    500 		strcpy(fp->name, AudioEslinear_be);
    501 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    502 		fp->precision = 16;
    503 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    504 		return (0);
    505 	case 7:
    506 		strcpy(fp->name, AudioEulinear_be);
    507 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    508 		fp->precision = 16;
    509 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    510 		return (0);
    511 	default:
    512 		return (EINVAL);
    513 	}
    514 }
    515 
    516 
    517 int
    518 auvia_set_params(void *addr, int setmode, int usemode,
    519 	struct audio_params *play, struct audio_params *rec)
    520 {
    521 	struct auvia_softc *sc = addr;
    522 	struct audio_params *p;
    523 	u_int16_t regval;
    524 	int reg, mode;
    525 
    526 	/* for mode in (RECORD, PLAY) */
    527 	for (mode = AUMODE_RECORD; mode != -1;
    528 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    529 		if ((setmode & mode) == 0)
    530 			continue;
    531 
    532 		p = mode == AUMODE_PLAY ? play : rec;
    533 
    534 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
    535 		    (p->precision != 8 && p->precision != 16) ||
    536 		    (p->channels != 1 && p->channels != 2))
    537 			return (EINVAL);
    538 
    539 		reg = mode == AUMODE_PLAY ?
    540 			AC97_REG_EXT_DAC_RATE : AC97_REG_EXT_ADC_RATE;
    541 
    542 		auvia_write_codec(sc, reg, (u_int16_t) p->sample_rate);
    543 		auvia_read_codec(sc, reg, &regval);
    544 		p->sample_rate = regval;
    545 
    546 		p->factor = 1;
    547 		p->sw_code = 0;
    548 		switch (p->encoding) {
    549 		case AUDIO_ENCODING_SLINEAR_BE:
    550 			if (p->precision == 16)
    551 				p->sw_code = swap_bytes;
    552 			else
    553 				p->sw_code = change_sign8;
    554 			break;
    555 		case AUDIO_ENCODING_SLINEAR_LE:
    556 			if (p->precision != 16)
    557 				p->sw_code = change_sign8;
    558 			break;
    559 		case AUDIO_ENCODING_ULINEAR_BE:
    560 			if (p->precision == 16) {
    561 				if (mode == AUMODE_PLAY)
    562 					p->sw_code = swap_bytes_change_sign16_le;
    563 				else
    564 					p->sw_code = change_sign16_swap_bytes_le;
    565 			}
    566 			break;
    567 		case AUDIO_ENCODING_ULINEAR_LE:
    568 			if (p->precision == 16)
    569 				p->sw_code = change_sign16_le;
    570 			break;
    571 		case AUDIO_ENCODING_ULAW:
    572 			if (mode == AUMODE_PLAY) {
    573 				p->factor = 2;
    574 				p->sw_code = mulaw_to_slinear16_le;
    575 			} else
    576 				p->sw_code = ulinear8_to_mulaw;
    577 			break;
    578 		case AUDIO_ENCODING_ALAW:
    579 			if (mode == AUMODE_PLAY) {
    580 				p->factor = 2;
    581 				p->sw_code = alaw_to_slinear16_le;
    582 			} else
    583 				p->sw_code = ulinear8_to_alaw;
    584 			break;
    585 		default:
    586 			return (EINVAL);
    587 		}
    588 
    589 		regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
    590 			| (p->precision * p->factor == 16 ?
    591 				AUVIA_RPMODE_16BIT : 0)
    592 			| AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
    593 			| AUVIA_RPMODE_AUTOSTART;
    594 
    595 		if (mode == AUMODE_PLAY) {
    596 			sc->sc_play.sc_reg = regval;
    597 		} else {
    598 			sc->sc_record.sc_reg = regval;
    599 		}
    600 	}
    601 
    602 	return 0;
    603 }
    604 
    605 
    606 int
    607 auvia_round_blocksize(void *addr, int blk)
    608 {
    609 	return (blk & -32);
    610 }
    611 
    612 
    613 int
    614 auvia_halt_output(void *addr)
    615 {
    616         struct auvia_softc *sc = addr;
    617 
    618 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL,
    619 		AUVIA_RPCTRL_TERMINATE);
    620 
    621 	return 0;
    622 }
    623 
    624 
    625 int
    626 auvia_halt_input(void *addr)
    627 {
    628         struct auvia_softc *sc = addr;
    629 
    630 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL,
    631 		AUVIA_RPCTRL_TERMINATE);
    632 
    633 	return 0;
    634 }
    635 
    636 
    637 int
    638 auvia_getdev(void *addr, struct audio_device *retp)
    639 {
    640         struct auvia_softc *sc = addr;
    641 
    642 	if (retp) {
    643 		strncpy(retp->name, "VIA VT82C686A", sizeof(retp->name));
    644 		strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
    645 		strncpy(retp->config, "auvia", sizeof(retp->config));
    646 	}
    647 
    648 	return 0;
    649 }
    650 
    651 
    652 int
    653 auvia_set_port(void *addr, mixer_ctrl_t *cp)
    654 {
    655 	struct auvia_softc *sc = addr;
    656 
    657 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
    658 }
    659 
    660 
    661 int
    662 auvia_get_port(void *addr, mixer_ctrl_t *cp)
    663 {
    664 	struct auvia_softc *sc = addr;
    665 
    666 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
    667 }
    668 
    669 
    670 int
    671 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
    672 {
    673 	struct auvia_softc *sc = addr;
    674 
    675 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
    676 }
    677 
    678 
    679 void *
    680 auvia_malloc(void *addr, int direction, size_t size, int pool, int flags)
    681 {
    682 	struct auvia_softc *sc = addr;
    683 	struct auvia_dma *p;
    684 	int error;
    685 	int rseg;
    686 
    687 	p = malloc(sizeof(*p), pool, flags);
    688 	if (!p)
    689 		return 0;
    690 
    691 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &p->seg, 1,
    692 				      &rseg, BUS_DMA_NOWAIT)) != 0) {
    693 		printf("%s: unable to allocate dma, error = %d\n",
    694 		       sc->sc_dev.dv_xname, error);
    695 		goto fail_alloc;
    696 	}
    697 
    698 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
    699 				    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    700 		printf("%s: unable to map dma, error = %d\n",
    701 		       sc->sc_dev.dv_xname, error);
    702 		goto fail_map;
    703 	}
    704 
    705 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    706 				       BUS_DMA_NOWAIT, &p->map)) != 0) {
    707 		printf("%s: unable to create dma map, error = %d\n",
    708 		       sc->sc_dev.dv_xname, error);
    709 		goto fail_create;
    710 	}
    711 
    712 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
    713 				     BUS_DMA_NOWAIT)) != 0) {
    714 		printf("%s: unable to load dma map, error = %d\n",
    715 		       sc->sc_dev.dv_xname, error);
    716 		goto fail_load;
    717 	}
    718 
    719 	p->next = sc->sc_dmas;
    720 	sc->sc_dmas = p;
    721 
    722 	return p->addr;
    723 
    724 
    725 fail_load:
    726 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    727 fail_create:
    728 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
    729 fail_map:
    730 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    731 fail_alloc:
    732 	free(p, pool);
    733 	return 0;
    734 }
    735 
    736 
    737 void
    738 auvia_free(void *addr, void *ptr, int pool)
    739 {
    740 	struct auvia_softc *sc = addr;
    741 	struct auvia_dma *p;
    742 
    743 	for (p = sc->sc_dmas; p->addr != ptr; p = p->next)
    744 		if (p->next == NULL)
    745 			panic("auvia_free: trying to free unallocated memory");
    746 
    747 	bus_dmamap_unload(sc->sc_dmat, p->map);
    748 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    749 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    750 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    751 }
    752 
    753 
    754 size_t
    755 auvia_round_buffersize(void *addr, int direction, size_t size)
    756 {
    757 	return size;
    758 }
    759 
    760 
    761 int
    762 auvia_mappage(void *addr, void *mem, int off, int prot)
    763 {
    764 	struct auvia_softc *sc = addr;
    765 	struct auvia_dma *p;
    766 
    767 	if (off < 0)
    768 		return -1;
    769 
    770 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
    771 		;
    772 
    773 	if (!p)
    774 		return -1;
    775 
    776 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
    777 	       BUS_DMA_WAITOK);
    778 }
    779 
    780 
    781 int
    782 auvia_get_props(void *addr)
    783 {
    784 	return AUDIO_PROP_MMAP |  AUDIO_PROP_INDEPENDENT
    785 		| AUDIO_PROP_FULLDUPLEX;
    786 }
    787 
    788 
    789 int
    790 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    791 	struct auvia_dma *p, void *start, void *end, int blksize)
    792 {
    793 	struct auvia_dma_op *op;
    794 	struct auvia_dma *dp;
    795 	bus_addr_t s, e;
    796 	size_t l;
    797 	int segs;
    798 
    799 	s = p->map->dm_segs[0].ds_addr;
    800 	l = ((char *)end - (char *)start);
    801 	e = s + l;
    802 	segs = (l + blksize - 1) / blksize;
    803 
    804 	if (segs > (ch->sc_dma_op_count)) {
    805 		/* if old list was too small, free it */
    806 		if (ch->sc_dma_ops) {
    807 			auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
    808 		}
    809 
    810 		ch->sc_dma_ops = auvia_malloc(sc, 0,
    811 			sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
    812 
    813 		if (ch->sc_dma_ops == NULL) {
    814 			printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
    815 			return 1;
    816 		}
    817 
    818 		for (dp = sc->sc_dmas;
    819 			dp && dp->addr != (void *)(ch->sc_dma_ops);
    820 			dp = dp->next)
    821 				;
    822 
    823 		if (!dp)
    824 			panic("%s: build_dma_ops: where'd my memory go??? "
    825 				"address (%p)\n", sc->sc_dev.dv_xname,
    826 				ch->sc_dma_ops);
    827 
    828 		ch->sc_dma_op_count = segs;
    829 		ch->sc_dma_ops_dma = dp;
    830 	}
    831 
    832 	dp = ch->sc_dma_ops_dma;
    833 	op = ch->sc_dma_ops;
    834 
    835 	while (l) {
    836 		op->ptr = s;
    837 		l = l - blksize;
    838 		if (!l) {
    839 			/* if last block */
    840 			op->flags = AUVIA_DMAOP_EOL | blksize;
    841 		} else {
    842 			op->flags = AUVIA_DMAOP_FLAG | blksize;
    843 		}
    844 		s += blksize;
    845 		op++;
    846 	}
    847 
    848 	return 0;
    849 }
    850 
    851 
    852 int
    853 auvia_trigger_output(void *addr, void *start, void *end,
    854 	int blksize, void (*intr)(void *), void *arg,
    855 	struct audio_params *param)
    856 {
    857 	struct auvia_softc *sc = addr;
    858 	struct auvia_softc_chan *ch = &(sc->sc_play);
    859 	struct auvia_dma *p;
    860 
    861 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
    862 		;
    863 
    864 	if (!p)
    865 		panic("auvia_trigger_output: request with bad start "
    866 			"address (%p)\n", start);
    867 
    868 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
    869 		return 1;
    870 	}
    871 
    872 	ch->sc_intr = intr;
    873 	ch->sc_arg = arg;
    874 
    875 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_DMAOPS_BASE,
    876 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
    877 
    878 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_MODE,
    879 		ch->sc_reg);
    880 
    881 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL,
    882 		AUVIA_RPCTRL_START);
    883 
    884 	return 0;
    885 }
    886 
    887 
    888 int
    889 auvia_trigger_input(void *addr, void *start, void *end,
    890 	int blksize, void (*intr)(void *), void *arg,
    891 	struct audio_params *param)
    892 {
    893 	struct auvia_softc *sc = addr;
    894 	struct auvia_softc_chan *ch = &(sc->sc_record);
    895 	struct auvia_dma *p;
    896 
    897 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
    898 		;
    899 
    900 	if (!p)
    901 		panic("auvia_trigger_input: request with bad start "
    902 			"address (%p)\n", start);
    903 
    904 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
    905 		return 1;
    906 	}
    907 
    908 	ch->sc_intr = intr;
    909 	ch->sc_arg = arg;
    910 
    911 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_DMAOPS_BASE,
    912 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
    913 
    914 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_MODE,
    915 		ch->sc_reg);
    916 
    917 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL,
    918 		AUVIA_RPCTRL_START);
    919 
    920 	return 0;
    921 }
    922 
    923 
    924 int
    925 auvia_intr(void *arg)
    926 {
    927 	struct auvia_softc *sc = arg;
    928 	u_int8_t r;
    929 
    930 	r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT);
    931 	if (r & AUVIA_RPSTAT_INTR) {
    932 		sc->sc_record.sc_intr(sc->sc_record.sc_arg);
    933 
    934 		/* clear interrupts */
    935 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT,
    936 			AUVIA_RPSTAT_INTR);
    937 	}
    938 	r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT);
    939 	if (r & AUVIA_RPSTAT_INTR) {
    940 		sc->sc_play.sc_intr(sc->sc_play.sc_arg);
    941 
    942 		/* clear interrupts */
    943 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT,
    944 			AUVIA_RPSTAT_INTR);
    945 	}
    946 
    947 	return 1;
    948 }
    949