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