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