Home | History | Annotate | Line # | Download | only in pci
auvia.c revision 1.32
      1 /*	$NetBSD: auvia.c,v 1.32 2003/09/14 14:48:17 jmmv 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.32 2003/09/14 14:48:17 jmmv 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 *, struct audio_encoding *);
     94 void	auvia_set_params_sub(struct auvia_softc *, struct auvia_softc_chan *,
     95 	struct audio_params *);
     96 int	auvia_set_params(void *, int, int, struct audio_params *,
     97 	struct audio_params *);
     98 int	auvia_round_blocksize(void *, int);
     99 int	auvia_halt_output(void *);
    100 int	auvia_halt_input(void *);
    101 int	auvia_getdev(void *, struct audio_device *);
    102 int	auvia_set_port(void *, mixer_ctrl_t *);
    103 int	auvia_get_port(void *, mixer_ctrl_t *);
    104 int	auvia_query_devinfo(void *, mixer_devinfo_t *);
    105 void *	auvia_malloc(void *, int, size_t, struct malloc_type *, int);
    106 void	auvia_free(void *, void *, struct malloc_type *);
    107 size_t	auvia_round_buffersize(void *, int, size_t);
    108 paddr_t	auvia_mappage(void *, void *, off_t, int);
    109 int	auvia_get_props(void *);
    110 int	auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
    111 	struct auvia_dma *, void *, void *, int);
    112 int	auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
    113 	void *, struct audio_params *);
    114 int	auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
    115 	void *, struct audio_params *);
    116 
    117 int	auvia_intr __P((void *));
    118 
    119 CFATTACH_DECL(auvia, sizeof (struct auvia_softc),
    120     auvia_match, auvia_attach, NULL, NULL);
    121 
    122 #define AUVIA_PCICONF_JUNK	0x40
    123 #define		AUVIA_PCICONF_ENABLES	 0x00FF0000	/* reg 42 mask */
    124 #define		AUVIA_PCICONF_ACLINKENAB 0x00008000	/* ac link enab */
    125 #define		AUVIA_PCICONF_ACNOTRST	 0x00004000	/* ~(ac reset) */
    126 #define		AUVIA_PCICONF_ACSYNC	 0x00002000	/* ac sync */
    127 #define		AUVIA_PCICONF_ACVSR	 0x00000800	/* var. samp. rate */
    128 #define		AUVIA_PCICONF_ACSGD	 0x00000400	/* SGD enab */
    129 #define		AUVIA_PCICONF_ACFM	 0x00000200	/* FM enab */
    130 #define		AUVIA_PCICONF_ACSB	 0x00000100	/* SB enab */
    131 #define		AUVIA_PCICONF_PRIVALID	 0x00000001	/* primary codec rdy */
    132 
    133 #define	AUVIA_PLAY_BASE			0x00
    134 #define	AUVIA_RECORD_BASE		0x10
    135 
    136 /* *_RP_* are offsets from AUVIA_PLAY_BASE or AUVIA_RECORD_BASE */
    137 #define	AUVIA_RP_STAT			0x00
    138 #define		AUVIA_RPSTAT_INTR		0x03
    139 #define	AUVIA_RP_CONTROL		0x01
    140 #define		AUVIA_RPCTRL_START		0x80
    141 #define		AUVIA_RPCTRL_TERMINATE		0x40
    142 #define		AUVIA_RPCTRL_AUTOSTART		0x20
    143 /* The following are 8233 specific */
    144 #define		AUVIA_RPCTRL_STOP		0x04
    145 #define		AUVIA_RPCTRL_EOL		0x02
    146 #define		AUVIA_RPCTRL_FLAG		0x01
    147 #define	AUVIA_RP_MODE			0x02		/* 82c686 specific */
    148 #define		AUVIA_RPMODE_INTR_FLAG		0x01
    149 #define		AUVIA_RPMODE_INTR_EOL		0x02
    150 #define		AUVIA_RPMODE_STEREO		0x10
    151 #define		AUVIA_RPMODE_16BIT		0x20
    152 #define		AUVIA_RPMODE_AUTOSTART		0x80
    153 #define	AUVIA_RP_DMAOPS_BASE		0x04
    154 
    155 #define	VIA8233_RP_DXS_LVOL		0x02
    156 #define	VIA8233_RP_DXS_RVOL		0x03
    157 #define	VIA8233_RP_RATEFMT		0x08
    158 #define		VIA8233_RATEFMT_48K		0xfffff
    159 #define		VIA8233_RATEFMT_STEREO		0x00100000
    160 #define		VIA8233_RATEFMT_16BIT		0x00200000
    161 
    162 #define	VIA_RP_DMAOPS_COUNT		0x0c
    163 
    164 #define VIA8233_MP_BASE			0x40
    165 	/* STAT, CONTROL, DMAOPS_BASE, DMAOPS_COUNT are valid */
    166 #define VIA8233_OFF_MP_FORMAT		0x02
    167 #define		VIA8233_MP_FORMAT_8BIT		0x00
    168 #define		VIA8233_MP_FORMAT_16BIT		0x80
    169 #define		VIA8233_MP_FORMAT_CHANNLE_MASK	0x70 /* 1, 2, 4, 6 */
    170 #define VIA8233_OFF_MP_SCRATCH		0x03
    171 #define VIA8233_OFF_MP_STOP		0x08
    172 
    173 #define	AUVIA_CODEC_CTL			0x80
    174 #define		AUVIA_CODEC_READ		0x00800000
    175 #define		AUVIA_CODEC_BUSY		0x01000000
    176 #define		AUVIA_CODEC_PRIVALID		0x02000000
    177 #define		AUVIA_CODEC_INDEX(x)		((x)<<16)
    178 
    179 #define CH_WRITE1(sc, ch, off, v)	\
    180 	bus_space_write_1((sc)->sc_iot,	(sc)->sc_ioh, (ch)->sc_base + (off), v)
    181 #define CH_WRITE4(sc, ch, off, v)	\
    182 	bus_space_write_4((sc)->sc_iot,	(sc)->sc_ioh, (ch)->sc_base + (off), v)
    183 #define CH_READ1(sc, ch, off)		\
    184 	bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
    185 #define CH_READ4(sc, ch, off)		\
    186 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
    187 
    188 #define TIMEOUT	50
    189 
    190 struct audio_hw_if auvia_hw_if = {
    191 	auvia_open,
    192 	auvia_close,
    193 	NULL, /* drain */
    194 	auvia_query_encoding,
    195 	auvia_set_params,
    196 	auvia_round_blocksize,
    197 	NULL, /* commit_settings */
    198 	NULL, /* init_output */
    199 	NULL, /* init_input */
    200 	NULL, /* start_output */
    201 	NULL, /* start_input */
    202 	auvia_halt_output,
    203 	auvia_halt_input,
    204 	NULL, /* speaker_ctl */
    205 	auvia_getdev,
    206 	NULL, /* setfd */
    207 	auvia_set_port,
    208 	auvia_get_port,
    209 	auvia_query_devinfo,
    210 	auvia_malloc,
    211 	auvia_free,
    212 	auvia_round_buffersize,
    213 	auvia_mappage,
    214 	auvia_get_props,
    215 	auvia_trigger_output,
    216 	auvia_trigger_input,
    217 	NULL, /* dev_ioctl */
    218 };
    219 
    220 int	auvia_attach_codec(void *, struct ac97_codec_if *);
    221 int	auvia_write_codec(void *, u_int8_t, u_int16_t);
    222 int	auvia_read_codec(void *, u_int8_t, u_int16_t *);
    223 void	auvia_reset_codec(void *);
    224 int	auvia_waitready_codec(struct auvia_softc *sc);
    225 int	auvia_waitvalid_codec(struct auvia_softc *sc);
    226 
    227 
    228 int
    229 auvia_match(struct device *parent, struct cfdata *match, void *aux)
    230 {
    231 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    232 
    233 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
    234 		return 0;
    235 	switch (PCI_PRODUCT(pa->pa_id)) {
    236 	case PCI_PRODUCT_VIATECH_VT82C686A_AC97:
    237 	case PCI_PRODUCT_VIATECH_VT8233_AC97:
    238 		break;
    239 	default:
    240 		return 0;
    241 	}
    242 
    243 	return 1;
    244 }
    245 
    246 
    247 void
    248 auvia_attach(struct device *parent, struct device *self, void *aux)
    249 {
    250 	struct pci_attach_args *pa = aux;
    251 	struct auvia_softc *sc = (struct auvia_softc *) self;
    252 	const char *intrstr = NULL;
    253 	pci_chipset_tag_t pc = pa->pa_pc;
    254 	pcitag_t pt = pa->pa_tag;
    255 	pci_intr_handle_t ih;
    256 	bus_size_t iosize;
    257 	pcireg_t pr;
    258 	int r;
    259 
    260 	aprint_naive(": Audio controller\n");
    261 
    262 	sc->sc_play.sc_base = AUVIA_PLAY_BASE;
    263 	sc->sc_record.sc_base = AUVIA_RECORD_BASE;
    264 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT8233_AC97) {
    265 		sc->sc_flags |= AUVIA_FLAGS_VT8233;
    266 		sc->sc_play.sc_base = VIA8233_MP_BASE;
    267 	}
    268 
    269 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
    270 		&sc->sc_ioh, NULL, &iosize)) {
    271 		aprint_error(": can't map i/o space\n");
    272 		return;
    273 	}
    274 
    275 	sc->sc_dmat = pa->pa_dmat;
    276 	sc->sc_pc = pc;
    277 	sc->sc_pt = pt;
    278 
    279 	r = PCI_REVISION(pa->pa_class);
    280 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    281 		sprintf(sc->sc_revision, "0x%02X", r);
    282 		if (r < 0x50) {
    283 			aprint_normal(": VIA VT8233 AC'97 Audio (rev %s)\n",
    284 			       sc->sc_revision);
    285 		} else {
    286 			aprint_normal(": VIA VT8235 AC'97 Audio (rev %s)\n",
    287 			       sc->sc_revision);
    288 		}
    289 	} else {
    290 		sc->sc_revision[1] = '\0';
    291 		if (r == 0x20) {
    292 			sc->sc_revision[0] = 'H';
    293 		} else if ((r >= 0x10) && (r <= 0x14)) {
    294 			sc->sc_revision[0] = 'A' + (r - 0x10);
    295 		} else {
    296 			sprintf(sc->sc_revision, "0x%02X", r);
    297 		}
    298 
    299 		aprint_normal(": VIA VT82C686A AC'97 Audio (rev %s)\n",
    300 		       sc->sc_revision);
    301 	}
    302 
    303 	if (pci_intr_map(pa, &ih)) {
    304 		aprint_error(": couldn't map interrupt\n");
    305 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
    306 		return;
    307 	}
    308 	intrstr = pci_intr_string(pc, ih);
    309 
    310 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
    311 	if (sc->sc_ih == NULL) {
    312 		aprint_error("%s: couldn't establish interrupt",
    313 		    sc->sc_dev.dv_xname);
    314 		if (intrstr != NULL)
    315 			aprint_normal(" at %s", intrstr);
    316 		aprint_normal("\n");
    317 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
    318 		return;
    319 	}
    320 
    321 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    322 
    323 	/* disable SBPro compat & others */
    324 	pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
    325 
    326 	pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
    327 	/* XXX what to do about MIDI, FM, joystick? */
    328 
    329 	pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
    330 		| AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
    331 
    332 	pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
    333 
    334 	pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
    335 
    336 	sc->host_if.arg = sc;
    337 	sc->host_if.attach = auvia_attach_codec;
    338 	sc->host_if.read = auvia_read_codec;
    339 	sc->host_if.write = auvia_write_codec;
    340 	sc->host_if.reset = auvia_reset_codec;
    341 
    342 	if ((r = ac97_attach(&sc->host_if)) != 0) {
    343 		aprint_error("%s: can't attach codec (error 0x%X)\n",
    344 			sc->sc_dev.dv_xname, r);
    345 		pci_intr_disestablish(pc, sc->sc_ih);
    346 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
    347 		return;
    348 	}
    349 
    350 	audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
    351 }
    352 
    353 
    354 int
    355 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
    356 {
    357 	struct auvia_softc *sc = addr;
    358 
    359 	sc->codec_if = cif;
    360 
    361 	return 0;
    362 }
    363 
    364 
    365 void
    366 auvia_reset_codec(void *addr)
    367 {
    368 	int i;
    369 	struct auvia_softc *sc = addr;
    370 	pcireg_t r;
    371 
    372 	/* perform a codec cold reset */
    373 
    374 	r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
    375 
    376 	r &= ~AUVIA_PCICONF_ACNOTRST;	/* enable RESET (active low) */
    377 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    378 	delay(2);
    379 
    380 	r |= AUVIA_PCICONF_ACNOTRST;	/* disable RESET (inactive high) */
    381 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
    382 	delay(200);
    383 
    384 	for (i = 500000; i != 0 && !(pci_conf_read(sc->sc_pc, sc->sc_pt,
    385 		AUVIA_PCICONF_JUNK) & AUVIA_PCICONF_PRIVALID); i--)
    386 		DELAY(1);
    387 	if (i == 0)
    388 		printf("%s: codec reset timed out\n", sc->sc_dev.dv_xname);
    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 void
    544 auvia_set_params_sub(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    545 		     struct audio_params *p)
    546 {
    547 	u_int32_t v;
    548 	u_int16_t regval;
    549 
    550 	if (!(sc->sc_flags & AUVIA_FLAGS_VT8233)) {
    551 		regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
    552 			| (p->precision * p->factor == 16 ?
    553 				AUVIA_RPMODE_16BIT : 0)
    554 			| AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
    555 			| AUVIA_RPMODE_AUTOSTART;
    556 		ch->sc_reg = regval;
    557 	} else if (ch->sc_base != VIA8233_MP_BASE) {
    558 		v = CH_READ4(sc, ch, VIA8233_RP_RATEFMT);
    559 		v &= ~(VIA8233_RATEFMT_48K | VIA8233_RATEFMT_STEREO
    560 			| VIA8233_RATEFMT_16BIT);
    561 
    562 		v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20)
    563 			/ (48000 / 20);
    564 		if (p->channels == 2)
    565 			v |= VIA8233_RATEFMT_STEREO;
    566 		if (p->precision == 16)
    567 			v |= VIA8233_RATEFMT_16BIT;
    568 
    569 		CH_WRITE4(sc, ch, VIA8233_RP_RATEFMT, v);
    570 	} else {
    571 		static const u_int32_t slottab[7] =
    572 			{ 0, 0xff000011, 0xff000021, 0,
    573 			  0xff004321, 0, 0xff436521};
    574 
    575 		regval = (p->hw_precision == 16
    576 			? VIA8233_MP_FORMAT_16BIT : VIA8233_MP_FORMAT_8BIT)
    577 			| (p->hw_channels << 4);
    578 		CH_WRITE1(sc, ch, VIA8233_OFF_MP_FORMAT, regval);
    579 		CH_WRITE4(sc, ch, VIA8233_OFF_MP_STOP, slottab[p->hw_channels]);
    580 	}
    581 }
    582 
    583 int
    584 auvia_set_params(void *addr, int setmode, int usemode,
    585 	struct audio_params *play, struct audio_params *rec)
    586 {
    587 	struct auvia_softc *sc = addr;
    588 	struct auvia_softc_chan *ch;
    589 	struct audio_params *p;
    590 	struct ac97_codec_if* codec;
    591 	int reg, mode;
    592 	u_int16_t ext_id;
    593 
    594 	codec = sc->codec_if;
    595 	/* for mode in (RECORD, PLAY) */
    596 	for (mode = AUMODE_RECORD; mode != -1;
    597 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    598 		if ((setmode & mode) == 0)
    599 			continue;
    600 
    601 		if (mode == AUMODE_PLAY ) {
    602 			p = play;
    603 			ch = &sc->sc_play;
    604 			reg = AC97_REG_PCM_FRONT_DAC_RATE;
    605 		} else {
    606 			p = rec;
    607 			ch = &sc->sc_record;
    608 			reg = AC97_REG_PCM_LR_ADC_RATE;
    609 		}
    610 
    611 		if (ch->sc_base == VIA8233_MP_BASE && mode == AUMODE_PLAY) {
    612 			ext_id = codec->vtbl->get_extcaps(codec);
    613 			if (p->channels == 1) {
    614 				/* ok */
    615 			} else if (p->channels == 2) {
    616 				/* ok */
    617 			} else if (p->channels == 4
    618 				&& ext_id & AC97_EXT_AUDIO_SDAC) {
    619 				/* ok */
    620 #define BITS_6CH	(AC97_EXT_AUDIO_SDAC | AC97_EXT_AUDIO_CDAC | AC97_EXT_AUDIO_LDAC)
    621 			} else if (p->channels == 6
    622 				&& (ext_id & BITS_6CH) == BITS_6CH) {
    623 				/* ok */
    624 			} else {
    625 				return (EINVAL);
    626 			}
    627 		} else {
    628 			if (p->channels != 1 && p->channels != 2)
    629 				return (EINVAL);
    630 		}
    631 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
    632 		    (p->precision != 8 && p->precision != 16))
    633 			return (EINVAL);
    634 
    635 		if (IS_FIXED_RATE(codec)) {
    636 			/* Enable aurateconv */
    637 			p->hw_sample_rate = AC97_SINGLE_RATE;
    638 		} else {
    639 			if (codec->vtbl->set_rate(codec, reg, &p->sample_rate))
    640 				return (EINVAL);
    641 			reg = AC97_REG_PCM_SURR_DAC_RATE;
    642 			if (p->channels >= 4
    643 			    && codec->vtbl->set_rate(codec, reg,
    644 						     &p->sample_rate))
    645 				return (EINVAL);
    646 			reg = AC97_REG_PCM_LFE_DAC_RATE;
    647 			if (p->channels == 6
    648 			    && codec->vtbl->set_rate(codec, reg,
    649 						     &p->sample_rate))
    650 				return (EINVAL);
    651 		}
    652 
    653 		p->factor = 1;
    654 		p->sw_code = 0;
    655 		switch (p->encoding) {
    656 		case AUDIO_ENCODING_SLINEAR_BE:
    657 			if (p->precision == 16) {
    658 				p->sw_code = swap_bytes;
    659 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    660 			} else {
    661 				p->sw_code = change_sign8;
    662 				p->hw_encoding = AUDIO_ENCODING_ULINEAR;
    663 			}
    664 			break;
    665 		case AUDIO_ENCODING_SLINEAR_LE:
    666 			if (p->precision != 16) {
    667 				p->sw_code = change_sign8;
    668 				p->hw_encoding = AUDIO_ENCODING_ULINEAR;
    669 			}
    670 			break;
    671 		case AUDIO_ENCODING_ULINEAR_BE:
    672 			if (p->precision == 16) {
    673 				if (mode == AUMODE_PLAY)
    674 					p->sw_code = swap_bytes_change_sign16_le;
    675 				else
    676 					p->sw_code = change_sign16_swap_bytes_le;
    677 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    678 			}
    679 			break;
    680 		case AUDIO_ENCODING_ULINEAR_LE:
    681 			if (p->precision == 16) {
    682 				p->sw_code = change_sign16_le;
    683 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    684 			}
    685 			break;
    686 		case AUDIO_ENCODING_ULAW:
    687 			if (p->precision != 8)
    688 				return (EINVAL);
    689 			if (mode == AUMODE_PLAY) {
    690 				p->factor = 2;
    691 				p->sw_code = mulaw_to_slinear16_le;
    692 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    693 				p->hw_precision = 16;
    694 			} else if (!IS_FIXED_RATE(codec)) {
    695 				p->sw_code = ulinear8_to_mulaw;
    696 				p->hw_encoding = AUDIO_ENCODING_ULINEAR;
    697 			} else {
    698 				/* aurateconv supports no 8 bit PCM */
    699 				p->factor = 2;
    700 				p->sw_code = slinear16_to_mulaw_le;
    701 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    702 				p->hw_precision = 16;
    703 			}
    704 			break;
    705 		case AUDIO_ENCODING_ALAW:
    706 			if (p->precision != 8)
    707 				return (EINVAL);
    708 			if (mode == AUMODE_PLAY) {
    709 				p->factor = 2;
    710 				p->sw_code = alaw_to_slinear16_le;
    711 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    712 				p->hw_precision = 16;
    713 			} else if (!IS_FIXED_RATE(codec)) {
    714 				p->sw_code = ulinear8_to_alaw;
    715 				p->hw_encoding = AUDIO_ENCODING_ULINEAR;
    716 			} else {
    717 				/* aurateconv supports no 8 bit PCM */
    718 				p->factor = 2;
    719 				p->sw_code = slinear16_to_alaw_le;
    720 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    721 				p->hw_precision = 16;
    722 			}
    723 			break;
    724 		default:
    725 			return (EINVAL);
    726 		}
    727 		auvia_set_params_sub(sc, ch, p);
    728 	}
    729 
    730 	return 0;
    731 }
    732 
    733 
    734 int
    735 auvia_round_blocksize(void *addr, int blk)
    736 {
    737 	return (blk & -32);
    738 }
    739 
    740 
    741 int
    742 auvia_halt_output(void *addr)
    743 {
    744 	struct auvia_softc *sc = addr;
    745 	struct auvia_softc_chan *ch = &(sc->sc_play);
    746 
    747 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    748 	return 0;
    749 }
    750 
    751 
    752 int
    753 auvia_halt_input(void *addr)
    754 {
    755 	struct auvia_softc *sc = addr;
    756 	struct auvia_softc_chan *ch = &(sc->sc_record);
    757 
    758 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
    759 	return 0;
    760 }
    761 
    762 
    763 int
    764 auvia_getdev(void *addr, struct audio_device *retp)
    765 {
    766 	struct auvia_softc *sc = addr;
    767 
    768 	if (retp) {
    769 		if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
    770 			strncpy(retp->name, "VIA VT8233/8235",
    771 				sizeof(retp->name));
    772 		} else {
    773 			strncpy(retp->name, "VIA VT82C686A",
    774 				sizeof(retp->name));
    775 		}
    776 		strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
    777 		strncpy(retp->config, "auvia", sizeof(retp->config));
    778 	}
    779 
    780 	return 0;
    781 }
    782 
    783 
    784 int
    785 auvia_set_port(void *addr, mixer_ctrl_t *cp)
    786 {
    787 	struct auvia_softc *sc = addr;
    788 
    789 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
    790 }
    791 
    792 
    793 int
    794 auvia_get_port(void *addr, mixer_ctrl_t *cp)
    795 {
    796 	struct auvia_softc *sc = addr;
    797 
    798 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
    799 }
    800 
    801 
    802 int
    803 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
    804 {
    805 	struct auvia_softc *sc = addr;
    806 
    807 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
    808 }
    809 
    810 
    811 void *
    812 auvia_malloc(void *addr, int direction, size_t size, struct malloc_type * pool,
    813     int flags)
    814 {
    815 	struct auvia_softc *sc = addr;
    816 	struct auvia_dma *p;
    817 	int error;
    818 	int rseg;
    819 
    820 	p = malloc(sizeof(*p), pool, flags);
    821 	if (!p)
    822 		return 0;
    823 
    824 	p->size = size;
    825 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
    826 				      1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    827 		printf("%s: unable to allocate DMA, error = %d\n",
    828 		       sc->sc_dev.dv_xname, error);
    829 		goto fail_alloc;
    830 	}
    831 
    832 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
    833 				    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    834 		printf("%s: unable to map DMA, error = %d\n",
    835 		       sc->sc_dev.dv_xname, error);
    836 		goto fail_map;
    837 	}
    838 
    839 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    840 				       BUS_DMA_NOWAIT, &p->map)) != 0) {
    841 		printf("%s: unable to create DMA map, error = %d\n",
    842 		       sc->sc_dev.dv_xname, error);
    843 		goto fail_create;
    844 	}
    845 
    846 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
    847 				     BUS_DMA_NOWAIT)) != 0) {
    848 		printf("%s: unable to load DMA map, error = %d\n",
    849 		       sc->sc_dev.dv_xname, error);
    850 		goto fail_load;
    851 	}
    852 
    853 	p->next = sc->sc_dmas;
    854 	sc->sc_dmas = p;
    855 
    856 	return p->addr;
    857 
    858 
    859 fail_load:
    860 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    861 fail_create:
    862 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
    863 fail_map:
    864 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    865 fail_alloc:
    866 	free(p, pool);
    867 	return 0;
    868 }
    869 
    870 
    871 void
    872 auvia_free(void *addr, void *ptr, struct malloc_type *pool)
    873 {
    874 	struct auvia_softc *sc = addr;
    875 	struct auvia_dma **pp, *p;
    876 
    877 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
    878 		if (p->addr == ptr) {
    879 			bus_dmamap_unload(sc->sc_dmat, p->map);
    880 			bus_dmamap_destroy(sc->sc_dmat, p->map);
    881 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    882 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
    883 
    884 			*pp = p->next;
    885 			free(p, pool);
    886 			return;
    887 		}
    888 
    889 	panic("auvia_free: trying to free unallocated memory");
    890 }
    891 
    892 
    893 size_t
    894 auvia_round_buffersize(void *addr, int direction, size_t size)
    895 {
    896 	return size;
    897 }
    898 
    899 
    900 paddr_t
    901 auvia_mappage(void *addr, void *mem, off_t off, int prot)
    902 {
    903 	struct auvia_softc *sc = addr;
    904 	struct auvia_dma *p;
    905 
    906 	if (off < 0)
    907 		return -1;
    908 
    909 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
    910 		;
    911 
    912 	if (!p)
    913 		return -1;
    914 
    915 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
    916 	       BUS_DMA_WAITOK);
    917 }
    918 
    919 
    920 int
    921 auvia_get_props(void *addr)
    922 {
    923 	struct auvia_softc *sc = addr;
    924 	int props;
    925 
    926 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    927 	/*
    928 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
    929 	 * rate because of aurateconv.  Applications can't know what rate the
    930 	 * device can process in the case of mmap().
    931 	 */
    932 	if (!IS_FIXED_RATE(sc->codec_if))
    933 		props |= AUDIO_PROP_MMAP;
    934 	return props;
    935 }
    936 
    937 
    938 int
    939 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
    940 	struct auvia_dma *p, void *start, void *end, int blksize)
    941 {
    942 	struct auvia_dma_op *op;
    943 	struct auvia_dma *dp;
    944 	bus_addr_t s, e;
    945 	size_t l;
    946 	int segs;
    947 
    948 	s = p->map->dm_segs[0].ds_addr;
    949 	l = ((char *)end - (char *)start);
    950 	e = s + l;
    951 	segs = (l + blksize - 1) / blksize;
    952 
    953 	if (segs > (ch->sc_dma_op_count)) {
    954 		/* if old list was too small, free it */
    955 		if (ch->sc_dma_ops) {
    956 			auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
    957 		}
    958 
    959 		ch->sc_dma_ops = auvia_malloc(sc, 0,
    960 			sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
    961 
    962 		if (ch->sc_dma_ops == NULL) {
    963 			printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
    964 			return 1;
    965 		}
    966 
    967 		for (dp = sc->sc_dmas;
    968 			dp && dp->addr != (void *)(ch->sc_dma_ops);
    969 			dp = dp->next)
    970 				;
    971 
    972 		if (!dp)
    973 			panic("%s: build_dma_ops: where'd my memory go??? "
    974 				"address (%p)\n", sc->sc_dev.dv_xname,
    975 				ch->sc_dma_ops);
    976 
    977 		ch->sc_dma_op_count = segs;
    978 		ch->sc_dma_ops_dma = dp;
    979 	}
    980 
    981 	dp = ch->sc_dma_ops_dma;
    982 	op = ch->sc_dma_ops;
    983 
    984 	while (l) {
    985 		op->ptr = s;
    986 		l = l - blksize;
    987 		if (!l) {
    988 			/* if last block */
    989 			op->flags = AUVIA_DMAOP_EOL | blksize;
    990 		} else {
    991 			op->flags = AUVIA_DMAOP_FLAG | blksize;
    992 		}
    993 		s += blksize;
    994 		op++;
    995 	}
    996 
    997 	return 0;
    998 }
    999 
   1000 
   1001 int
   1002 auvia_trigger_output(void *addr, void *start, void *end,
   1003 	int blksize, void (*intr)(void *), void *arg,
   1004 	struct audio_params *param)
   1005 {
   1006 	struct auvia_softc *sc = addr;
   1007 	struct auvia_softc_chan *ch = &(sc->sc_play);
   1008 	struct auvia_dma *p;
   1009 
   1010 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
   1011 		;
   1012 
   1013 	if (!p)
   1014 		panic("auvia_trigger_output: request with bad start "
   1015 			"address (%p)", start);
   1016 
   1017 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
   1018 		return 1;
   1019 	}
   1020 
   1021 	ch->sc_intr = intr;
   1022 	ch->sc_arg = arg;
   1023 
   1024 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
   1025 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1026 
   1027 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1028 		if (ch->sc_base != VIA8233_MP_BASE) {
   1029 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
   1030 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
   1031 		}
   1032 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
   1033 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1034 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1035 	} else {
   1036 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
   1037 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1038 	}
   1039 
   1040 	return 0;
   1041 }
   1042 
   1043 
   1044 int
   1045 auvia_trigger_input(void *addr, void *start, void *end,
   1046 	int blksize, void (*intr)(void *), void *arg,
   1047 	struct audio_params *param)
   1048 {
   1049 	struct auvia_softc *sc = addr;
   1050 	struct auvia_softc_chan *ch = &(sc->sc_record);
   1051 	struct auvia_dma *p;
   1052 
   1053 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
   1054 		;
   1055 
   1056 	if (!p)
   1057 		panic("auvia_trigger_input: request with bad start "
   1058 			"address (%p)", start);
   1059 
   1060 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
   1061 		return 1;
   1062 	}
   1063 
   1064 	ch->sc_intr = intr;
   1065 	ch->sc_arg = arg;
   1066 
   1067 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
   1068 		  ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
   1069 
   1070 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
   1071 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
   1072 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
   1073 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
   1074 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
   1075 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
   1076 	} else {
   1077 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
   1078 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
   1079 	}
   1080 
   1081 	return 0;
   1082 }
   1083 
   1084 
   1085 int
   1086 auvia_intr(void *arg)
   1087 {
   1088 	struct auvia_softc *sc = arg;
   1089 	struct auvia_softc_chan *ch;
   1090 	u_int8_t r;
   1091 	int rval;
   1092 
   1093 	rval = 0;
   1094 
   1095 	ch = &sc->sc_record;
   1096 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
   1097 	if (r & AUVIA_RPSTAT_INTR) {
   1098 		if (sc->sc_record.sc_intr)
   1099 			sc->sc_record.sc_intr(sc->sc_record.sc_arg);
   1100 
   1101 		/* clear interrupts */
   1102 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1103 		rval = 1;
   1104 	}
   1105 
   1106 	ch = &sc->sc_play;
   1107 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
   1108 	if (r & AUVIA_RPSTAT_INTR) {
   1109 		if (sc->sc_play.sc_intr)
   1110 			sc->sc_play.sc_intr(sc->sc_play.sc_arg);
   1111 
   1112 		/* clear interrupts */
   1113 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
   1114 		rval = 1;
   1115 	}
   1116 
   1117 	return rval;
   1118 }
   1119