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