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