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