Home | History | Annotate | Line # | Download | only in pci
eap.c revision 1.58
      1 /*	$NetBSD: eap.c,v 1.58 2002/11/24 12:06:12 scw Exp $	*/
      2 /*      $OpenBSD: eap.c,v 1.6 1999/10/05 19:24:42 csapuntz Exp $ */
      3 
      4 /*
      5  * Copyright (c) 1998, 1999, 2002 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson <augustss (at) netbsd.org>, Charles M. Hannum, and
     10  * Antti Kantee <pooka (at) netbsd.org>.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * Debugging:   Andreas Gustafsson <gson (at) araneus.fi>
     43  * Testing:     Chuck Cranor       <chuck (at) maria.wustl.edu>
     44  *              Phil Nelson        <phil (at) cs.wwu.edu>
     45  *
     46  * ES1371/AC97:	Ezra Story         <ezy (at) panix.com>
     47  */
     48 
     49 /*
     50  * Ensoniq ES1370 + AK4531 and ES1371/ES1373 + AC97
     51  *
     52  * Documentation links:
     53  *
     54  * ftp://ftp.alsa-project.org/pub/manuals/ensoniq/
     55  * ftp://ftp.alsa-project.org/pub/manuals/asahi_kasei/4531.pdf
     56  * ftp://download.intel.com/ial/scalableplatforms/audio/ac97r21.pdf
     57  */
     58 
     59 #include <sys/cdefs.h>
     60 __KERNEL_RCSID(0, "$NetBSD: eap.c,v 1.58 2002/11/24 12:06:12 scw Exp $");
     61 
     62 #include "midi.h"
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/kernel.h>
     67 #include <sys/fcntl.h>
     68 #include <sys/malloc.h>
     69 #include <sys/device.h>
     70 #include <sys/proc.h>
     71 #include <sys/select.h>
     72 
     73 #include <dev/pci/pcidevs.h>
     74 #include <dev/pci/pcivar.h>
     75 
     76 #include <sys/audioio.h>
     77 #include <dev/audio_if.h>
     78 #include <dev/midi_if.h>
     79 #include <dev/audiovar.h>
     80 #include <dev/mulaw.h>
     81 #include <dev/auconv.h>
     82 #include <dev/ic/ac97var.h>
     83 
     84 #include <machine/bus.h>
     85 
     86 #include <dev/pci/eapreg.h>
     87 
     88 #define	PCI_CBIO		0x10
     89 
     90 /* Debug */
     91 #ifdef AUDIO_DEBUG
     92 #define DPRINTF(x)	if (eapdebug) printf x
     93 #define DPRINTFN(n,x)	if (eapdebug>(n)) printf x
     94 int	eapdebug = 0;
     95 #else
     96 #define DPRINTF(x)
     97 #define DPRINTFN(n,x)
     98 #endif
     99 
    100 int	eap_match(struct device *, struct cfdata *, void *);
    101 void	eap_attach(struct device *, struct device *, void *);
    102 int	eap_intr(void *);
    103 
    104 struct eap_dma {
    105 	bus_dmamap_t map;
    106 	caddr_t addr;
    107 	bus_dma_segment_t segs[1];
    108 	int nsegs;
    109 	size_t size;
    110 	struct eap_dma *next;
    111 };
    112 
    113 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
    114 #define KERNADDR(p) ((void *)((p)->addr))
    115 
    116 /*
    117  * The card has two DACs. Using them is a bit twisted: we use DAC2
    118  * as default and DAC1 as the optional secondary DAC.
    119  */
    120 #define EAP_DAC1 1
    121 #define EAP_DAC2 0
    122 #define EAP_I1 EAP_DAC2
    123 #define EAP_I2 EAP_DAC1
    124 struct eap_instance {
    125 	struct device *parent;
    126 	int index;
    127 
    128 	void	(*ei_pintr)(void *);	/* dma completion intr handler */
    129 	void	*ei_parg;		/* arg for ei_intr() */
    130 #ifdef DIAGNOSTIC
    131 	char	ei_prun;
    132 #endif
    133 };
    134 
    135 struct eap_softc {
    136 	struct device sc_dev;		/* base device */
    137 	void *sc_ih;			/* interrupt vectoring */
    138 	bus_space_tag_t iot;
    139 	bus_space_handle_t ioh;
    140 	bus_dma_tag_t sc_dmatag;	/* DMA tag */
    141 
    142 	struct eap_dma *sc_dmas;
    143 
    144 	void	(*sc_rintr)(void *);	/* dma completion intr handler */
    145 	void	*sc_rarg;		/* arg for sc_intr() */
    146 #ifdef DIAGNOSTIC
    147 	char	sc_rrun;
    148 #endif
    149 
    150 #if NMIDI > 0
    151 	void	(*sc_iintr)(void *, int); /* midi input ready handler */
    152 	void	(*sc_ointr)(void *);	/* midi output ready handler */
    153 	void	*sc_arg;
    154 #endif
    155 
    156 	u_short	sc_port[AK_NPORTS];	/* mirror of the hardware setting */
    157 	u_int	sc_record_source;	/* recording source mask */
    158 	u_int	sc_output_source;	/* output source mask */
    159 	u_int	sc_mic_preamp;
    160 	char    sc_1371;		/* Using ES1371/AC97 codec */
    161 
    162 	struct ac97_codec_if *codec_if;
    163 	struct ac97_host_if host_if;
    164 
    165 	struct eap_instance sc_ei[2];
    166 };
    167 
    168 int	eap_allocmem(struct eap_softc *, size_t, size_t, struct eap_dma *);
    169 int	eap_freemem(struct eap_softc *, struct eap_dma *);
    170 
    171 #define EWRITE1(sc, r, x) bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x))
    172 #define EWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
    173 #define EWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
    174 #define EREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
    175 #define EREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
    176 #define EREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
    177 
    178 CFATTACH_DECL(eap, sizeof(struct eap_softc),
    179     eap_match, eap_attach, NULL, NULL);
    180 
    181 int	eap_open(void *, int);
    182 void	eap_close(void *);
    183 int	eap_query_encoding(void *, struct audio_encoding *);
    184 int	eap_set_params(void *, int, int, struct audio_params *, struct audio_params *);
    185 int	eap_round_blocksize(void *, int);
    186 int	eap_trigger_output(void *, void *, void *, int, void (*)(void *),
    187 	    void *, struct audio_params *);
    188 int	eap_trigger_input(void *, void *, void *, int, void (*)(void *),
    189 	    void *, struct audio_params *);
    190 int	eap_halt_output(void *);
    191 int	eap_halt_input(void *);
    192 void    eap1370_write_codec(struct eap_softc *, int, int);
    193 int	eap_getdev(void *, struct audio_device *);
    194 int	eap1370_mixer_set_port(void *, mixer_ctrl_t *);
    195 int	eap1370_mixer_get_port(void *, mixer_ctrl_t *);
    196 int	eap1371_mixer_set_port(void *, mixer_ctrl_t *);
    197 int	eap1371_mixer_get_port(void *, mixer_ctrl_t *);
    198 int	eap1370_query_devinfo(void *, mixer_devinfo_t *);
    199 void   *eap_malloc(void *, int, size_t, int, int);
    200 void	eap_free(void *, void *, int);
    201 size_t	eap_round_buffersize(void *, int, size_t);
    202 paddr_t	eap_mappage(void *, void *, off_t, int);
    203 int	eap_get_props(void *);
    204 void	eap1370_set_mixer(struct eap_softc *sc, int a, int d);
    205 u_int32_t eap1371_src_wait(struct eap_softc *sc);
    206 void 	eap1371_set_adc_rate(struct eap_softc *sc, int rate);
    207 void 	eap1371_set_dac_rate(struct eap_instance *ei, int rate);
    208 int	eap1371_src_read(struct eap_softc *sc, int a);
    209 void	eap1371_src_write(struct eap_softc *sc, int a, int d);
    210 int	eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip);
    211 
    212 int     eap1371_attach_codec(void *sc, struct ac97_codec_if *);
    213 int	eap1371_read_codec(void *sc, u_int8_t a, u_int16_t *d);
    214 int	eap1371_write_codec(void *sc, u_int8_t a, u_int16_t d);
    215 void    eap1371_reset_codec(void *sc);
    216 int     eap1371_get_portnum_by_name(struct eap_softc *, char *, char *,
    217 					 char *);
    218 #if NMIDI > 0
    219 void	eap_midi_close(void *);
    220 void	eap_midi_getinfo(void *, struct midi_info *);
    221 int	eap_midi_open(void *, int, void (*)(void *, int),
    222 			   void (*)(void *), void *);
    223 int	eap_midi_output(void *, int);
    224 #endif
    225 
    226 struct audio_hw_if eap1370_hw_if = {
    227 	eap_open,
    228 	eap_close,
    229 	NULL,
    230 	eap_query_encoding,
    231 	eap_set_params,
    232 	eap_round_blocksize,
    233 	NULL,
    234 	NULL,
    235 	NULL,
    236 	NULL,
    237 	NULL,
    238 	eap_halt_output,
    239 	eap_halt_input,
    240 	NULL,
    241 	eap_getdev,
    242 	NULL,
    243 	eap1370_mixer_set_port,
    244 	eap1370_mixer_get_port,
    245 	eap1370_query_devinfo,
    246 	eap_malloc,
    247 	eap_free,
    248 	eap_round_buffersize,
    249 	eap_mappage,
    250 	eap_get_props,
    251 	eap_trigger_output,
    252 	eap_trigger_input,
    253 	NULL,
    254 };
    255 
    256 struct audio_hw_if eap1371_hw_if = {
    257 	eap_open,
    258 	eap_close,
    259 	NULL,
    260 	eap_query_encoding,
    261 	eap_set_params,
    262 	eap_round_blocksize,
    263 	NULL,
    264 	NULL,
    265 	NULL,
    266 	NULL,
    267 	NULL,
    268 	eap_halt_output,
    269 	eap_halt_input,
    270 	NULL,
    271 	eap_getdev,
    272 	NULL,
    273 	eap1371_mixer_set_port,
    274 	eap1371_mixer_get_port,
    275 	eap1371_query_devinfo,
    276 	eap_malloc,
    277 	eap_free,
    278 	eap_round_buffersize,
    279 	eap_mappage,
    280 	eap_get_props,
    281 	eap_trigger_output,
    282 	eap_trigger_input,
    283 	NULL,
    284 };
    285 
    286 #if NMIDI > 0
    287 struct midi_hw_if eap_midi_hw_if = {
    288 	eap_midi_open,
    289 	eap_midi_close,
    290 	eap_midi_output,
    291 	eap_midi_getinfo,
    292 	0,				/* ioctl */
    293 };
    294 #endif
    295 
    296 struct audio_device eap_device = {
    297 	"Ensoniq AudioPCI",
    298 	"",
    299 	"eap"
    300 };
    301 
    302 int
    303 eap_match(struct device *parent, struct cfdata *match, void *aux)
    304 {
    305 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    306 
    307 	switch (PCI_VENDOR(pa->pa_id)) {
    308 	case PCI_VENDOR_CREATIVELABS:
    309 		switch (PCI_PRODUCT(pa->pa_id)) {
    310 		case PCI_PRODUCT_CREATIVELABS_EV1938:
    311 			return (1);
    312 		}
    313 		break;
    314 	case PCI_VENDOR_ENSONIQ:
    315 		switch (PCI_PRODUCT(pa->pa_id)) {
    316 		case PCI_PRODUCT_ENSONIQ_AUDIOPCI:
    317 		case PCI_PRODUCT_ENSONIQ_AUDIOPCI97:
    318 		case PCI_PRODUCT_ENSONIQ_CT5880:
    319 			return (1);
    320 		}
    321 		break;
    322 	}
    323 
    324 	return (0);
    325 }
    326 
    327 void
    328 eap1370_write_codec(struct eap_softc *sc, int a, int d)
    329 {
    330 	int icss, to;
    331 
    332 	to = EAP_WRITE_TIMEOUT;
    333 	do {
    334 		icss = EREAD4(sc, EAP_ICSS);
    335 		DPRINTFN(5,("eap: codec %d prog: icss=0x%08x\n", a, icss));
    336 		if (!to--) {
    337 			printf("eap: timeout writing to codec\n");
    338 			return;
    339 		}
    340 	} while(icss & EAP_CWRIP);  /* XXX could use CSTAT here */
    341 	EWRITE4(sc, EAP_CODEC, EAP_SET_CODEC(a, d));
    342 }
    343 
    344 /*
    345  * Reading and writing the CODEC is very convoluted.  This mimics the
    346  * FreeBSD and Linux drivers.
    347  */
    348 
    349 static __inline void
    350 eap1371_ready_codec(struct eap_softc *sc, u_int8_t a, u_int32_t wd)
    351 {
    352 	int to, s;
    353 	u_int32_t src, t;
    354 
    355 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    356 		if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
    357 			break;
    358 		delay(1);
    359 	}
    360 	if (to >= EAP_WRITE_TIMEOUT)
    361 		printf("%s: eap1371_ready_codec timeout 1\n",
    362 		       sc->sc_dev.dv_xname);
    363 
    364 	s = splaudio();
    365 	src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    366 	EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
    367 
    368 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    369 		t = EREAD4(sc, E1371_SRC);
    370 		if ((t & E1371_SRC_STATE_MASK) == 0)
    371 			break;
    372 		delay(1);
    373 	}
    374 	if (to >= EAP_READ_TIMEOUT)
    375 		printf("%s: eap1371_ready_codec timeout 2\n",
    376 		       sc->sc_dev.dv_xname);
    377 
    378 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    379 		t = EREAD4(sc, E1371_SRC);
    380 		if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
    381 			break;
    382 		delay(1);
    383 	}
    384 	if (to >= EAP_READ_TIMEOUT)
    385 		printf("%s: eap1371_ready_codec timeout 3\n",
    386 		       sc->sc_dev.dv_xname);
    387 
    388 	EWRITE4(sc, E1371_CODEC, wd);
    389 
    390 	eap1371_src_wait(sc);
    391 	EWRITE4(sc, E1371_SRC, src);
    392 
    393 	splx(s);
    394 }
    395 
    396 int
    397 eap1371_read_codec(void *sc_, u_int8_t a, u_int16_t *d)
    398 {
    399 	struct eap_softc *sc = sc_;
    400 	int to;
    401 	u_int32_t t;
    402 
    403 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, 0) | E1371_CODEC_READ);
    404 
    405 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    406 		if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
    407 			break;
    408 	}
    409 	if (to > EAP_WRITE_TIMEOUT)
    410 		printf("%s: eap1371_read_codec timeout 1\n",
    411 		       sc->sc_dev.dv_xname);
    412 
    413 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    414 		t = EREAD4(sc, E1371_CODEC);
    415 		if (t & E1371_CODEC_VALID)
    416 			break;
    417 	}
    418 	if (to > EAP_WRITE_TIMEOUT)
    419 		printf("%s: eap1371_read_codec timeout 2\n",
    420 		       sc->sc_dev.dv_xname);
    421 
    422 	*d = (u_int16_t)t;
    423 
    424 	DPRINTFN(10, ("eap1371: reading codec (%x) = %x\n", a, *d));
    425 
    426 	return (0);
    427 }
    428 
    429 int
    430 eap1371_write_codec(void *sc_, u_int8_t a, u_int16_t d)
    431 {
    432 	struct eap_softc *sc = sc_;
    433 
    434 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, d));
    435 
    436         DPRINTFN(10, ("eap1371: writing codec %x --> %x\n", d, a));
    437 
    438 	return (0);
    439 }
    440 
    441 u_int32_t
    442 eap1371_src_wait(struct eap_softc *sc)
    443 {
    444 	int to;
    445 	u_int32_t src;
    446 
    447 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    448 		src = EREAD4(sc, E1371_SRC);
    449 		if (!(src & E1371_SRC_RBUSY))
    450 			return (src);
    451 		delay(1);
    452 	}
    453 	printf("%s: eap1371_src_wait timeout\n", sc->sc_dev.dv_xname);
    454 	return (src);
    455 }
    456 
    457 int
    458 eap1371_src_read(struct eap_softc *sc, int a)
    459 {
    460 	int to;
    461 	u_int32_t src, t;
    462 
    463 	src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    464 	src |= E1371_SRC_ADDR(a);
    465 	EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
    466 
    467 	t = eap1371_src_wait(sc);
    468 	if ((t & E1371_SRC_STATE_MASK) != E1371_SRC_STATE_OK) {
    469 		for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    470 			t = EREAD4(sc, E1371_SRC);
    471 			if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
    472 				break;
    473 			delay(1);
    474 		}
    475 	}
    476 
    477 	EWRITE4(sc, E1371_SRC, src);
    478 
    479 	return t & E1371_SRC_DATAMASK;
    480 }
    481 
    482 void
    483 eap1371_src_write(struct eap_softc *sc, int a, int d)
    484 {
    485 	u_int32_t r;
    486 
    487 	r = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    488 	r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
    489 	EWRITE4(sc, E1371_SRC, r);
    490 }
    491 
    492 void
    493 eap1371_set_adc_rate(struct eap_softc *sc, int rate)
    494 {
    495 	int freq, n, truncm;
    496 	int out;
    497 	int s;
    498 
    499 	/* Whatever, it works, so I'll leave it :) */
    500 
    501 	if (rate > 48000)
    502 		rate = 48000;
    503 	if (rate < 4000)
    504 		rate = 4000;
    505 	n = rate / 3000;
    506 	if ((1 << n) & SRC_MAGIC)
    507 		n--;
    508 	truncm = ((21 * n) - 1) | 1;
    509 	freq = ((48000 << 15) / rate) * n;
    510 	if (rate >= 24000) {
    511 		if (truncm > 239)
    512 			truncm = 239;
    513 		out = ESRC_SET_TRUNC((239 - truncm) / 2);
    514 	} else {
    515 		if (truncm > 119)
    516 			truncm = 119;
    517 		out = ESRC_SMF | ESRC_SET_TRUNC((119 - truncm) / 2);
    518 	}
    519  	out |= ESRC_SET_N(n);
    520 	s = splaudio();
    521 	eap1371_src_write(sc, ESRC_ADC+ESRC_TRUNC_N, out);
    522 
    523 
    524 	out = eap1371_src_read(sc, ESRC_ADC+ESRC_IREGS) & 0xff;
    525 	eap1371_src_write(sc, ESRC_ADC+ESRC_IREGS, out |
    526 			  ESRC_SET_VFI(freq >> 15));
    527 	eap1371_src_write(sc, ESRC_ADC+ESRC_VFF, freq & 0x7fff);
    528 	eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(n));
    529 	eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(n));
    530 	splx(s);
    531 }
    532 
    533 void
    534 eap1371_set_dac_rate(struct eap_instance *ei, int rate)
    535 {
    536 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
    537 	int dac = ei->index == EAP_DAC1 ? ESRC_DAC1 : ESRC_DAC2;
    538 	int freq, r;
    539 	int s;
    540 
    541 	DPRINTFN(2, ("eap1371_set_dac_date: set rate for %d\n", ei->index));
    542 
    543 	/* Whatever, it works, so I'll leave it :) */
    544 
    545 	if (rate > 48000)
    546 	    rate = 48000;
    547 	if (rate < 4000)
    548 	    rate = 4000;
    549 	freq = ((rate << 15) + 1500) / 3000;
    550 
    551 	s = splaudio();
    552 	eap1371_src_wait(sc);
    553 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    554 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    555 	r |= ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2;
    556 	EWRITE4(sc, E1371_SRC, r);
    557 	r = eap1371_src_read(sc, dac + ESRC_IREGS) & 0x00ff;
    558 	eap1371_src_write(sc, dac + ESRC_IREGS, r | ((freq >> 5) & 0xfc00));
    559 	eap1371_src_write(sc, dac + ESRC_VFF, freq & 0x7fff);
    560 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    561 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    562 	r &= ~(ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2);
    563 	EWRITE4(sc, E1371_SRC, r);
    564 	splx(s);
    565 }
    566 
    567 void
    568 eap_attach(struct device *parent, struct device *self, void *aux)
    569 {
    570 	struct eap_softc *sc = (struct eap_softc *)self;
    571 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    572 	pci_chipset_tag_t pc = pa->pa_pc;
    573 	struct audio_hw_if *eap_hw_if;
    574 	char const *intrstr;
    575 	pci_intr_handle_t ih;
    576 	pcireg_t csr;
    577 	char devinfo[256];
    578 	mixer_ctrl_t ctl;
    579 	int i;
    580 	int revision, ct5880;
    581 	const char *revstr = "";
    582 
    583 	/* Flag if we're "creative" */
    584 	sc->sc_1371 = !(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
    585 			PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI);
    586 
    587 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    588 	revision = PCI_REVISION(pa->pa_class);
    589 	ct5880 = 0;
    590 	if (sc->sc_1371) {
    591 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
    592 		    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_CT5880)
    593 			ct5880 = 1;
    594 		switch (revision) {
    595 		case EAP_EV1938_A: revstr = "EV1938A "; break;
    596 		case EAP_CT5880_C: revstr = "CT5880C "; ct5880 = 1; break;
    597 		case EAP_ES1373_A: revstr = "ES1373A "; break;
    598 		case EAP_ES1373_B: revstr = "ES1373B "; break;
    599 		case EAP_CT5880_A: revstr = "CT5880A "; ct5880 = 1; break;
    600 		case EAP_ES1371_B: revstr = "ES1371B "; break;
    601 		}
    602 	}
    603 	printf(": %s %s(rev. 0x%02x)\n", devinfo, revstr, revision);
    604 
    605 	/* Map I/O register */
    606 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    607 	      &sc->iot, &sc->ioh, NULL, NULL)) {
    608 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    609 		return;
    610 	}
    611 
    612 	sc->sc_dmatag = pa->pa_dmat;
    613 
    614 	/* Enable the device. */
    615 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    616 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    617 		       csr | PCI_COMMAND_MASTER_ENABLE);
    618 
    619 	/* Map and establish the interrupt. */
    620 	if (pci_intr_map(pa, &ih)) {
    621 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    622 		return;
    623 	}
    624 	intrstr = pci_intr_string(pc, ih);
    625 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
    626 	if (sc->sc_ih == NULL) {
    627 		printf("%s: couldn't establish interrupt",
    628 		    sc->sc_dev.dv_xname);
    629 		if (intrstr != NULL)
    630 			printf(" at %s", intrstr);
    631 		printf("\n");
    632 		return;
    633 	}
    634 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    635 
    636 	sc->sc_ei[EAP_I1].parent = (struct device *)sc;
    637 	sc->sc_ei[EAP_I1].index = EAP_DAC2;
    638 	sc->sc_ei[EAP_I2].parent = (struct device *)sc;
    639 	sc->sc_ei[EAP_I2].index = EAP_DAC1;
    640 
    641 	if (!sc->sc_1371) {
    642 		/* Enable interrupts and looping mode. */
    643 		/* enable the parts we need */
    644 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    645 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
    646 
    647 		/* reset codec */
    648 		/* normal operation */
    649 		/* select codec clocks */
    650 		eap1370_write_codec(sc, AK_RESET, AK_PD);
    651 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
    652 		eap1370_write_codec(sc, AK_CS, 0x0);
    653 
    654 		eap_hw_if = &eap1370_hw_if;
    655 
    656 		/* Enable all relevant mixer switches. */
    657 		ctl.dev = EAP_OUTPUT_SELECT;
    658 		ctl.type = AUDIO_MIXER_SET;
    659 		ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
    660 			1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
    661 			1 << EAP_MIC_VOL;
    662 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    663 
    664 		ctl.type = AUDIO_MIXER_VALUE;
    665 		ctl.un.value.num_channels = 1;
    666 		for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
    667 		     ctl.dev++) {
    668 			ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
    669 			eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    670 		}
    671 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
    672 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    673 		ctl.dev = EAP_MIC_PREAMP;
    674 		ctl.type = AUDIO_MIXER_ENUM;
    675 		ctl.un.ord = 0;
    676 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    677 		ctl.dev = EAP_RECORD_SOURCE;
    678 		ctl.type = AUDIO_MIXER_SET;
    679 		ctl.un.mask = 1 << EAP_MIC_VOL;
    680 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    681 	} else {
    682 		/* clean slate */
    683 
    684                 EWRITE4(sc, EAP_SIC, 0);
    685 		EWRITE4(sc, EAP_ICSC, 0);
    686 		EWRITE4(sc, E1371_LEGACY, 0);
    687 
    688 		if (ct5880) {
    689 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
    690 			/* Let codec wake up */
    691 			tsleep(sc, PRIBIO, "eapcdc", hz / 20);
    692 		}
    693 
    694                 /* Reset from es1371's perspective */
    695                 EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
    696                 delay(20);
    697                 EWRITE4(sc, EAP_ICSC, 0);
    698 
    699 		/*
    700 		 * Must properly reprogram sample rate converter,
    701 		 * or it locks up.  Set some defaults for the life of the
    702 		 * machine, and set up a sb default sample rate.
    703 		 */
    704 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
    705 		for (i = 0; i < 0x80; i++)
    706 			eap1371_src_write(sc, i, 0);
    707 		eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
    708 		eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
    709 		eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
    710 		eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
    711 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
    712 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
    713 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
    714 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
    715 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
    716 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
    717 		eap1371_set_adc_rate(sc, 22050);
    718 		eap1371_set_dac_rate(&sc->sc_ei[0], 22050);
    719 		eap1371_set_dac_rate(&sc->sc_ei[1], 22050);
    720 
    721 		EWRITE4(sc, E1371_SRC, 0);
    722 
    723 		/* Reset codec */
    724 
    725 		/* Interrupt enable */
    726 		sc->host_if.arg = sc;
    727 		sc->host_if.attach = eap1371_attach_codec;
    728 		sc->host_if.read = eap1371_read_codec;
    729 		sc->host_if.write = eap1371_write_codec;
    730 		sc->host_if.reset = eap1371_reset_codec;
    731 
    732 		if (ac97_attach(&sc->host_if) == 0) {
    733 			/* Interrupt enable */
    734 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    735 		} else
    736 			return;
    737 
    738 		eap_hw_if = &eap1371_hw_if;
    739 
    740 		/* Just enable the DAC and master volumes by default */
    741 		ctl.type = AUDIO_MIXER_ENUM;
    742 		ctl.un.ord = 0;  /* off */
    743 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCoutputs,
    744 		       AudioNmaster, AudioNmute);
    745 		eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
    746 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCinputs,
    747 		       AudioNdac, AudioNmute);
    748 		eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
    749 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
    750 		       AudioNvolume, AudioNmute);
    751 		eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
    752 
    753 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
    754 		       AudioNsource, NULL);
    755 		ctl.type = AUDIO_MIXER_ENUM;
    756 		ctl.un.ord = 0;
    757 		eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
    758 
    759 	}
    760 
    761 	audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I1], &sc->sc_dev);
    762 
    763 #ifdef EAP_USE_BOTH_DACS
    764 	printf("%s: attaching secondary DAC\n", sc->sc_dev.dv_xname);
    765 	audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I2], &sc->sc_dev);
    766 #endif
    767 
    768 #if NMIDI > 0
    769 	midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
    770 #endif
    771 }
    772 
    773 int
    774 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    775 {
    776 	struct eap_softc *sc = sc_;
    777 
    778 	sc->codec_if = codec_if;
    779 	return (0);
    780 }
    781 
    782 void
    783 eap1371_reset_codec(void *sc_)
    784 {
    785 	struct eap_softc *sc = sc_;
    786 	u_int32_t icsc;
    787 	int s;
    788 
    789 	s = splaudio();
    790 	icsc = EREAD4(sc, EAP_ICSC);
    791 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
    792 	delay(20);
    793 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
    794 	delay(1);
    795 	splx(s);
    796 
    797 	return;
    798 }
    799 
    800 int
    801 eap_intr(void *p)
    802 {
    803 	struct eap_softc *sc = p;
    804 	u_int32_t intr, sic;
    805 
    806 	intr = EREAD4(sc, EAP_ICSS);
    807 	if (!(intr & EAP_INTR))
    808 		return (0);
    809 	sic = EREAD4(sc, EAP_SIC);
    810 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
    811 	if (intr & EAP_I_ADC) {
    812 #if 0
    813 		/*
    814 		 * XXX This is a hack!
    815 		 * The EAP chip sometimes generates the recording interrupt
    816 		 * while it is still transferring the data.  To make sure
    817 		 * it has all arrived we busy wait until the count is right.
    818 		 * The transfer we are waiting for is 8 longwords.
    819 		 */
    820 		int s, nw, n;
    821 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
    822 		s = EREAD4(sc, EAP_ADC_CSR);
    823 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
    824 		n = 0;
    825 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
    826 			delay(10);
    827 			if (++n > 100) {
    828 				printf("eapintr: dma fix timeout");
    829 				break;
    830 			}
    831 		}
    832 		/* Continue with normal interrupt handling. */
    833 #endif
    834 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
    835 		EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
    836 		if (sc->sc_rintr)
    837 			sc->sc_rintr(sc->sc_rarg);
    838 	}
    839 
    840 	if (intr & EAP_I_DAC2) {
    841 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
    842 		EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
    843 		if (sc->sc_ei[EAP_DAC2].ei_pintr)
    844 			sc->sc_ei[EAP_DAC2].ei_pintr(sc->sc_ei[EAP_DAC2].ei_parg);
    845 	}
    846 
    847 	if (intr & EAP_I_DAC1) {
    848 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P1_INTR_EN);
    849 		EWRITE4(sc, EAP_SIC, sic | EAP_P1_INTR_EN);
    850 		if (sc->sc_ei[EAP_DAC1].ei_pintr)
    851 			sc->sc_ei[EAP_DAC1].ei_pintr(sc->sc_ei[EAP_DAC1].ei_parg);
    852 	}
    853 
    854 	if (intr & EAP_I_MCCB)
    855 		panic("eap_intr: unexpected MCCB interrupt");
    856 #if NMIDI > 0
    857 	if ((intr & EAP_I_UART) && sc->sc_iintr != NULL) {
    858 		u_int32_t data;
    859 
    860 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXINT) {
    861 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
    862 				data = EREAD1(sc, EAP_UART_DATA);
    863 				sc->sc_iintr(sc->sc_arg, data);
    864 			}
    865 		}
    866 	}
    867 #endif
    868 	return (1);
    869 }
    870 
    871 int
    872 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
    873 {
    874 	int error;
    875 
    876 	p->size = size;
    877 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    878 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    879 				 &p->nsegs, BUS_DMA_NOWAIT);
    880 	if (error)
    881 		return (error);
    882 
    883 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    884 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    885 	if (error)
    886 		goto free;
    887 
    888 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    889 				  0, BUS_DMA_NOWAIT, &p->map);
    890 	if (error)
    891 		goto unmap;
    892 
    893 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    894 				BUS_DMA_NOWAIT);
    895 	if (error)
    896 		goto destroy;
    897 	return (0);
    898 
    899 destroy:
    900 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    901 unmap:
    902 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    903 free:
    904 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    905 	return (error);
    906 }
    907 
    908 int
    909 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
    910 {
    911 	bus_dmamap_unload(sc->sc_dmatag, p->map);
    912 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    913 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    914 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    915 	return (0);
    916 }
    917 
    918 int
    919 eap_open(void *addr, int flags)
    920 {
    921 	struct eap_instance *ei = addr;
    922 
    923 	/* there is only one ADC */
    924 	if (ei->index == EAP_I2 && flags & AUOPEN_READ)
    925 		return (EOPNOTSUPP);
    926 
    927 	return (0);
    928 }
    929 
    930 /*
    931  * Close function is called at splaudio().
    932  */
    933 void
    934 eap_close(void *addr)
    935 {
    936 	struct eap_instance *ei = addr;
    937 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
    938 
    939 	eap_halt_output(ei);
    940 	if (ei->index == EAP_I1) {
    941 		eap_halt_input(ei);
    942 		sc->sc_rintr = 0;
    943 	}
    944 
    945 	ei->ei_pintr = 0;
    946 }
    947 
    948 int
    949 eap_query_encoding(void *addr, struct audio_encoding *fp)
    950 {
    951 	switch (fp->index) {
    952 	case 0:
    953 		strcpy(fp->name, AudioEulinear);
    954 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    955 		fp->precision = 8;
    956 		fp->flags = 0;
    957 		return (0);
    958 	case 1:
    959 		strcpy(fp->name, AudioEmulaw);
    960 		fp->encoding = AUDIO_ENCODING_ULAW;
    961 		fp->precision = 8;
    962 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    963 		return (0);
    964 	case 2:
    965 		strcpy(fp->name, AudioEalaw);
    966 		fp->encoding = AUDIO_ENCODING_ALAW;
    967 		fp->precision = 8;
    968 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    969 		return (0);
    970 	case 3:
    971 		strcpy(fp->name, AudioEslinear);
    972 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    973 		fp->precision = 8;
    974 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    975 		return (0);
    976 	case 4:
    977 		strcpy(fp->name, AudioEslinear_le);
    978 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    979 		fp->precision = 16;
    980 		fp->flags = 0;
    981 		return (0);
    982 	case 5:
    983 		strcpy(fp->name, AudioEulinear_le);
    984 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    985 		fp->precision = 16;
    986 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    987 		return (0);
    988 	case 6:
    989 		strcpy(fp->name, AudioEslinear_be);
    990 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    991 		fp->precision = 16;
    992 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    993 		return (0);
    994 	case 7:
    995 		strcpy(fp->name, AudioEulinear_be);
    996 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    997 		fp->precision = 16;
    998 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    999 		return (0);
   1000 	default:
   1001 		return (EINVAL);
   1002 	}
   1003 }
   1004 
   1005 int
   1006 eap_set_params(void *addr, int setmode, int usemode,
   1007 	       struct audio_params *play, struct audio_params *rec)
   1008 {
   1009 	struct eap_instance *ei = addr;
   1010 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1011 	struct audio_params *p;
   1012 	int mode;
   1013 	u_int32_t div;
   1014 
   1015 	/*
   1016 	 * The es1370 only has one clock, so make the sample rates match.
   1017 	 */
   1018 	if (!sc->sc_1371) {
   1019 	    if (play->sample_rate != rec->sample_rate &&
   1020 		usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1021 	    	if (setmode == AUMODE_PLAY) {
   1022 		    rec->sample_rate = play->sample_rate;
   1023 		    setmode |= AUMODE_RECORD;
   1024 		} else if (setmode == AUMODE_RECORD) {
   1025 		    play->sample_rate = rec->sample_rate;
   1026 		    setmode |= AUMODE_PLAY;
   1027 		} else
   1028 		    return (EINVAL);
   1029 	    }
   1030 	}
   1031 
   1032 	for (mode = AUMODE_RECORD; mode != -1;
   1033 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1034 		if ((setmode & mode) == 0)
   1035 			continue;
   1036 
   1037 		p = mode == AUMODE_PLAY ? play : rec;
   1038 
   1039 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
   1040 		    (p->precision != 8 && p->precision != 16) ||
   1041 		    (p->channels != 1 && p->channels != 2))
   1042 			return (EINVAL);
   1043 
   1044 		p->factor = 1;
   1045 		p->sw_code = 0;
   1046 		switch (p->encoding) {
   1047 		case AUDIO_ENCODING_SLINEAR_BE:
   1048 			if (p->precision == 16)
   1049 				p->sw_code = swap_bytes;
   1050 			else
   1051 				p->sw_code = change_sign8;
   1052 			break;
   1053 		case AUDIO_ENCODING_SLINEAR_LE:
   1054 			if (p->precision != 16)
   1055 				p->sw_code = change_sign8;
   1056 			break;
   1057 		case AUDIO_ENCODING_ULINEAR_BE:
   1058 			if (p->precision == 16) {
   1059 				if (mode == AUMODE_PLAY)
   1060 					p->sw_code = swap_bytes_change_sign16_le;
   1061 				else
   1062 					p->sw_code = change_sign16_swap_bytes_le;
   1063 			}
   1064 			break;
   1065 		case AUDIO_ENCODING_ULINEAR_LE:
   1066 			if (p->precision == 16)
   1067 				p->sw_code = change_sign16_le;
   1068 			break;
   1069 		case AUDIO_ENCODING_ULAW:
   1070 			if (mode == AUMODE_PLAY) {
   1071 				p->factor = 2;
   1072 				p->sw_code = mulaw_to_slinear16_le;
   1073 			} else
   1074 				p->sw_code = ulinear8_to_mulaw;
   1075 			break;
   1076 		case AUDIO_ENCODING_ALAW:
   1077 			if (mode == AUMODE_PLAY) {
   1078 				p->factor = 2;
   1079 				p->sw_code = alaw_to_slinear16_le;
   1080 			} else
   1081 				p->sw_code = ulinear8_to_alaw;
   1082 			break;
   1083 		default:
   1084 			return (EINVAL);
   1085 		}
   1086 	}
   1087 
   1088 	if (sc->sc_1371) {
   1089 		eap1371_set_dac_rate(ei, play->sample_rate);
   1090 		eap1371_set_adc_rate(sc, rec->sample_rate);
   1091 	} else {
   1092 		/* Set the speed */
   1093 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
   1094 			     EREAD4(sc, EAP_ICSC)));
   1095 		div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
   1096 		/*
   1097 		 * XXX
   1098 		 * The -2 isn't documented, but seemed to make the wall
   1099 		 * time match
   1100 		 * what I expect.  - mycroft
   1101 		 */
   1102 		if (usemode == AUMODE_RECORD)
   1103 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1104 				rec->sample_rate - 2);
   1105 		else
   1106 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1107 				play->sample_rate - 2);
   1108 #if 0
   1109 		div |= EAP_CCB_INTRM;
   1110 #else
   1111 		/*
   1112 		 * It is not obvious how to acknowledge MCCB interrupts, so
   1113 		 * we had better not enable them.
   1114 		 */
   1115 #endif
   1116 		EWRITE4(sc, EAP_ICSC, div);
   1117 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1118 	}
   1119 
   1120 	return (0);
   1121 }
   1122 
   1123 int
   1124 eap_round_blocksize(void *addr, int blk)
   1125 {
   1126 	return (blk & -32);	/* keep good alignment */
   1127 }
   1128 
   1129 int
   1130 eap_trigger_output(
   1131 	void *addr,
   1132 	void *start,
   1133 	void *end,
   1134 	int blksize,
   1135 	void (*intr)(void *),
   1136 	void *arg,
   1137 	struct audio_params *param)
   1138 {
   1139 	struct eap_instance *ei = addr;
   1140 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1141 	struct eap_dma *p;
   1142 	u_int32_t icsc, sic;
   1143 	int sampshift;
   1144 
   1145 #ifdef DIAGNOSTIC
   1146 	if (ei->ei_prun)
   1147 		panic("eap_trigger_output: already running");
   1148 	ei->ei_prun = 1;
   1149 #endif
   1150 
   1151 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
   1152 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1153 	ei->ei_pintr = intr;
   1154 	ei->ei_parg = arg;
   1155 
   1156 	sic = EREAD4(sc, EAP_SIC);
   1157 	sic &= ~(EAP_S_EB(ei->index) | EAP_S_MB(ei->index) | EAP_INC_BITS);
   1158 
   1159 	if (ei->index == EAP_DAC2)
   1160 		sic |= EAP_SET_P2_ST_INC(0)
   1161 		    | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
   1162 
   1163 	sampshift = 0;
   1164 	if (param->precision * param->factor == 16) {
   1165 		sic |= EAP_S_EB(ei->index);
   1166 		sampshift++;
   1167 	}
   1168 	if (param->channels == 2) {
   1169 		sic |= EAP_S_MB(ei->index);
   1170 		sampshift++;
   1171 	}
   1172 	EWRITE4(sc, EAP_SIC, sic & ~EAP_P_INTR_EN(ei->index));
   1173 	EWRITE4(sc, EAP_SIC, sic | EAP_P_INTR_EN(ei->index));
   1174 
   1175 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1176 		;
   1177 	if (!p) {
   1178 		printf("eap_trigger_output: bad addr %p\n", start);
   1179 		return (EINVAL);
   1180 	}
   1181 
   1182 	if (ei->index == EAP_DAC2) {
   1183 		DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
   1184 			 (int)DMAADDR(p),
   1185 			 (int)EAP_SET_SIZE(0,
   1186 			 (((char *)end - (char *)start) >> 2) - 1)));
   1187 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1188 		EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
   1189 		EWRITE4(sc, EAP_DAC2_SIZE,
   1190 			EAP_SET_SIZE(0,
   1191 			((char *)end - (char *)start) >> 2) - 1);
   1192 		EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
   1193 	} else if (ei->index == EAP_DAC1) {
   1194 		DPRINTF(("eap_trigger_output: DAC1_ADDR=0x%x, DAC1_SIZE=0x%x\n",
   1195 			 (int)DMAADDR(p),
   1196 			 (int)EAP_SET_SIZE(0,
   1197 			 (((char *)end - (char *)start) >> 2) - 1)));
   1198 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1199 		EWRITE4(sc, EAP_DAC1_ADDR, DMAADDR(p));
   1200 		EWRITE4(sc, EAP_DAC1_SIZE,
   1201 			EAP_SET_SIZE(0,
   1202 			((char *)end - (char *)start) >> 2) - 1);
   1203 		EWRITE4(sc, EAP_DAC1_CSR, (blksize >> sampshift) - 1);
   1204 	}
   1205 #ifdef DIAGNOSTIC
   1206 	else
   1207 		panic("eap_trigger_output: impossible instance %d", ei->index);
   1208 #endif
   1209 
   1210 	if (sc->sc_1371)
   1211 		EWRITE4(sc, E1371_SRC, 0);
   1212 
   1213 	icsc = EREAD4(sc, EAP_ICSC);
   1214 	icsc |= EAP_DAC_EN(ei->index);
   1215 	EWRITE4(sc, EAP_ICSC, icsc);
   1216 
   1217 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
   1218 
   1219 	return (0);
   1220 }
   1221 
   1222 int
   1223 eap_trigger_input(
   1224 	void *addr,
   1225 	void *start,
   1226 	void *end,
   1227 	int blksize,
   1228 	void (*intr)(void *),
   1229 	void *arg,
   1230 	struct audio_params *param)
   1231 {
   1232 	struct eap_instance *ei = addr;
   1233 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1234 	struct eap_dma *p;
   1235 	u_int32_t icsc, sic;
   1236 	int sampshift;
   1237 
   1238 #ifdef DIAGNOSTIC
   1239 	if (sc->sc_rrun)
   1240 		panic("eap_trigger_input: already running");
   1241 	sc->sc_rrun = 1;
   1242 #endif
   1243 
   1244 	DPRINTFN(1, ("eap_trigger_input: ei=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1245 	    addr, start, end, blksize, intr, arg));
   1246 	sc->sc_rintr = intr;
   1247 	sc->sc_rarg = arg;
   1248 
   1249 	sic = EREAD4(sc, EAP_SIC);
   1250 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
   1251 	sampshift = 0;
   1252 	if (param->precision * param->factor == 16) {
   1253 		sic |= EAP_R1_S_EB;
   1254 		sampshift++;
   1255 	}
   1256 	if (param->channels == 2) {
   1257 		sic |= EAP_R1_S_MB;
   1258 		sampshift++;
   1259 	}
   1260 	EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
   1261 	EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
   1262 
   1263 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1264 		;
   1265 	if (!p) {
   1266 		printf("eap_trigger_input: bad addr %p\n", start);
   1267 		return (EINVAL);
   1268 	}
   1269 
   1270 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
   1271 		 (int)DMAADDR(p),
   1272 		 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
   1273 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
   1274 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
   1275 	EWRITE4(sc, EAP_ADC_SIZE,
   1276 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
   1277 
   1278 	EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
   1279 
   1280 	if (sc->sc_1371)
   1281 		EWRITE4(sc, E1371_SRC, 0);
   1282 
   1283 	icsc = EREAD4(sc, EAP_ICSC);
   1284 	icsc |= EAP_ADC_EN;
   1285 	EWRITE4(sc, EAP_ICSC, icsc);
   1286 
   1287 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
   1288 
   1289 	return (0);
   1290 }
   1291 
   1292 int
   1293 eap_halt_output(void *addr)
   1294 {
   1295 	struct eap_instance *ei = addr;
   1296 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1297 	u_int32_t icsc;
   1298 
   1299 	DPRINTF(("eap: eap_halt_output\n"));
   1300 	icsc = EREAD4(sc, EAP_ICSC);
   1301 	EWRITE4(sc, EAP_ICSC, icsc & ~(EAP_DAC_EN(ei->index)));
   1302 #ifdef DIAGNOSTIC
   1303 	ei->ei_prun = 0;
   1304 #endif
   1305 
   1306 	return (0);
   1307 }
   1308 
   1309 int
   1310 eap_halt_input(void *addr)
   1311 {
   1312 	struct eap_instance *ei = addr;
   1313 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1314 	u_int32_t icsc;
   1315 
   1316 #define EAP_USE_FMDAC_ALSO
   1317 	DPRINTF(("eap: eap_halt_input\n"));
   1318 	icsc = EREAD4(sc, EAP_ICSC);
   1319 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
   1320 #ifdef DIAGNOSTIC
   1321 	sc->sc_rrun = 0;
   1322 #endif
   1323 	return (0);
   1324 }
   1325 
   1326 int
   1327 eap_getdev(void *addr, struct audio_device *retp)
   1328 {
   1329 	*retp = eap_device;
   1330 	return (0);
   1331 }
   1332 
   1333 int
   1334 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1335 {
   1336 	struct eap_instance *ei = addr;
   1337 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1338 
   1339 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
   1340 }
   1341 
   1342 int
   1343 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1344 {
   1345 	struct eap_instance *ei = addr;
   1346 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1347 
   1348 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
   1349 }
   1350 
   1351 int
   1352 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1353 {
   1354 	struct eap_instance *ei = addr;
   1355 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1356 
   1357 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
   1358 }
   1359 
   1360 int
   1361 eap1371_get_portnum_by_name(struct eap_softc *sc,
   1362 			    char *class, char *device, char *qualifier)
   1363 {
   1364 	return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
   1365 	     device, qualifier));
   1366 }
   1367 
   1368 void
   1369 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
   1370 {
   1371 	eap1370_write_codec(sc, a, d);
   1372 
   1373 	sc->sc_port[a] = d;
   1374 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
   1375 }
   1376 
   1377 int
   1378 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1379 {
   1380 	struct eap_instance *ei = addr;
   1381 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1382 	int lval, rval, l, r, la, ra;
   1383 	int l1, r1, l2, r2, m, o1, o2;
   1384 
   1385 	if (cp->dev == EAP_RECORD_SOURCE) {
   1386 		if (cp->type != AUDIO_MIXER_SET)
   1387 			return (EINVAL);
   1388 		m = sc->sc_record_source = cp->un.mask;
   1389 		l1 = l2 = r1 = r2 = 0;
   1390 		if (m & (1 << EAP_VOICE_VOL))
   1391 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
   1392 		if (m & (1 << EAP_FM_VOL))
   1393 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
   1394 		if (m & (1 << EAP_CD_VOL))
   1395 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
   1396 		if (m & (1 << EAP_LINE_VOL))
   1397 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
   1398 		if (m & (1 << EAP_AUX_VOL))
   1399 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
   1400 		if (m & (1 << EAP_MIC_VOL))
   1401 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
   1402 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
   1403 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
   1404 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
   1405 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
   1406 		return (0);
   1407 	}
   1408 	if (cp->dev == EAP_OUTPUT_SELECT) {
   1409 		if (cp->type != AUDIO_MIXER_SET)
   1410 			return (EINVAL);
   1411 		m = sc->sc_output_source = cp->un.mask;
   1412 		o1 = o2 = 0;
   1413 		if (m & (1 << EAP_VOICE_VOL))
   1414 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
   1415 		if (m & (1 << EAP_FM_VOL))
   1416 			o1 |= AK_M_FM_L | AK_M_FM_R;
   1417 		if (m & (1 << EAP_CD_VOL))
   1418 			o1 |= AK_M_CD_L | AK_M_CD_R;
   1419 		if (m & (1 << EAP_LINE_VOL))
   1420 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
   1421 		if (m & (1 << EAP_AUX_VOL))
   1422 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
   1423 		if (m & (1 << EAP_MIC_VOL))
   1424 			o1 |= AK_M_MIC;
   1425 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
   1426 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
   1427 		return (0);
   1428 	}
   1429 	if (cp->dev == EAP_MIC_PREAMP) {
   1430 		if (cp->type != AUDIO_MIXER_ENUM)
   1431 			return (EINVAL);
   1432 		if (cp->un.ord != 0 && cp->un.ord != 1)
   1433 			return (EINVAL);
   1434 		sc->sc_mic_preamp = cp->un.ord;
   1435 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
   1436 		return (0);
   1437 	}
   1438 	if (cp->type != AUDIO_MIXER_VALUE)
   1439 		return (EINVAL);
   1440 	if (cp->un.value.num_channels == 1)
   1441 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1442 	else if (cp->un.value.num_channels == 2) {
   1443 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1444 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1445 	} else
   1446 		return (EINVAL);
   1447 	ra = -1;
   1448 	switch (cp->dev) {
   1449 	case EAP_MASTER_VOL:
   1450 		l = VOL_TO_ATT5(lval);
   1451 		r = VOL_TO_ATT5(rval);
   1452 		la = AK_MASTER_L;
   1453 		ra = AK_MASTER_R;
   1454 		break;
   1455 	case EAP_MIC_VOL:
   1456 		if (cp->un.value.num_channels != 1)
   1457 			return (EINVAL);
   1458 		la = AK_MIC;
   1459 		goto lr;
   1460 	case EAP_VOICE_VOL:
   1461 		la = AK_VOICE_L;
   1462 		ra = AK_VOICE_R;
   1463 		goto lr;
   1464 	case EAP_FM_VOL:
   1465 		la = AK_FM_L;
   1466 		ra = AK_FM_R;
   1467 		goto lr;
   1468 	case EAP_CD_VOL:
   1469 		la = AK_CD_L;
   1470 		ra = AK_CD_R;
   1471 		goto lr;
   1472 	case EAP_LINE_VOL:
   1473 		la = AK_LINE_L;
   1474 		ra = AK_LINE_R;
   1475 		goto lr;
   1476 	case EAP_AUX_VOL:
   1477 		la = AK_AUX_L;
   1478 		ra = AK_AUX_R;
   1479 	lr:
   1480 		l = VOL_TO_GAIN5(lval);
   1481 		r = VOL_TO_GAIN5(rval);
   1482 		break;
   1483 	default:
   1484 		return (EINVAL);
   1485 	}
   1486 	eap1370_set_mixer(sc, la, l);
   1487 	if (ra >= 0) {
   1488 		eap1370_set_mixer(sc, ra, r);
   1489 	}
   1490 	return (0);
   1491 }
   1492 
   1493 int
   1494 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1495 {
   1496 	struct eap_instance *ei = addr;
   1497 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1498 	int la, ra, l, r;
   1499 
   1500 	switch (cp->dev) {
   1501 	case EAP_RECORD_SOURCE:
   1502 		if (cp->type != AUDIO_MIXER_SET)
   1503 			return (EINVAL);
   1504 		cp->un.mask = sc->sc_record_source;
   1505 		return (0);
   1506 	case EAP_OUTPUT_SELECT:
   1507 		if (cp->type != AUDIO_MIXER_SET)
   1508 			return (EINVAL);
   1509 		cp->un.mask = sc->sc_output_source;
   1510 		return (0);
   1511 	case EAP_MIC_PREAMP:
   1512 		if (cp->type != AUDIO_MIXER_ENUM)
   1513 			return (EINVAL);
   1514 		cp->un.ord = sc->sc_mic_preamp;
   1515 		return (0);
   1516 	case EAP_MASTER_VOL:
   1517 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
   1518 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
   1519 		break;
   1520 	case EAP_MIC_VOL:
   1521 		if (cp->un.value.num_channels != 1)
   1522 			return (EINVAL);
   1523 		la = ra = AK_MIC;
   1524 		goto lr;
   1525 	case EAP_VOICE_VOL:
   1526 		la = AK_VOICE_L;
   1527 		ra = AK_VOICE_R;
   1528 		goto lr;
   1529 	case EAP_FM_VOL:
   1530 		la = AK_FM_L;
   1531 		ra = AK_FM_R;
   1532 		goto lr;
   1533 	case EAP_CD_VOL:
   1534 		la = AK_CD_L;
   1535 		ra = AK_CD_R;
   1536 		goto lr;
   1537 	case EAP_LINE_VOL:
   1538 		la = AK_LINE_L;
   1539 		ra = AK_LINE_R;
   1540 		goto lr;
   1541 	case EAP_AUX_VOL:
   1542 		la = AK_AUX_L;
   1543 		ra = AK_AUX_R;
   1544 	lr:
   1545 		l = GAIN5_TO_VOL(sc->sc_port[la]);
   1546 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
   1547 		break;
   1548 	default:
   1549 		return (EINVAL);
   1550 	}
   1551 	if (cp->un.value.num_channels == 1)
   1552 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
   1553 	else if (cp->un.value.num_channels == 2) {
   1554 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
   1555 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1556 	} else
   1557 		return (EINVAL);
   1558 	return (0);
   1559 }
   1560 
   1561 int
   1562 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1563 {
   1564 
   1565 	switch (dip->index) {
   1566 	case EAP_MASTER_VOL:
   1567 		dip->type = AUDIO_MIXER_VALUE;
   1568 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1569 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1570 		strcpy(dip->label.name, AudioNmaster);
   1571 		dip->un.v.num_channels = 2;
   1572 		strcpy(dip->un.v.units.name, AudioNvolume);
   1573 		return (0);
   1574 	case EAP_VOICE_VOL:
   1575 		dip->type = AUDIO_MIXER_VALUE;
   1576 		dip->mixer_class = EAP_INPUT_CLASS;
   1577 		dip->prev = AUDIO_MIXER_LAST;
   1578 		dip->next = AUDIO_MIXER_LAST;
   1579 		strcpy(dip->label.name, AudioNdac);
   1580 		dip->un.v.num_channels = 2;
   1581 		strcpy(dip->un.v.units.name, AudioNvolume);
   1582 		return (0);
   1583 	case EAP_FM_VOL:
   1584 		dip->type = AUDIO_MIXER_VALUE;
   1585 		dip->mixer_class = EAP_INPUT_CLASS;
   1586 		dip->prev = AUDIO_MIXER_LAST;
   1587 		dip->next = AUDIO_MIXER_LAST;
   1588 		strcpy(dip->label.name, AudioNfmsynth);
   1589 		dip->un.v.num_channels = 2;
   1590 		strcpy(dip->un.v.units.name, AudioNvolume);
   1591 		return (0);
   1592 	case EAP_CD_VOL:
   1593 		dip->type = AUDIO_MIXER_VALUE;
   1594 		dip->mixer_class = EAP_INPUT_CLASS;
   1595 		dip->prev = AUDIO_MIXER_LAST;
   1596 		dip->next = AUDIO_MIXER_LAST;
   1597 		strcpy(dip->label.name, AudioNcd);
   1598 		dip->un.v.num_channels = 2;
   1599 		strcpy(dip->un.v.units.name, AudioNvolume);
   1600 		return (0);
   1601 	case EAP_LINE_VOL:
   1602 		dip->type = AUDIO_MIXER_VALUE;
   1603 		dip->mixer_class = EAP_INPUT_CLASS;
   1604 		dip->prev = AUDIO_MIXER_LAST;
   1605 		dip->next = AUDIO_MIXER_LAST;
   1606 		strcpy(dip->label.name, AudioNline);
   1607 		dip->un.v.num_channels = 2;
   1608 		strcpy(dip->un.v.units.name, AudioNvolume);
   1609 		return (0);
   1610 	case EAP_AUX_VOL:
   1611 		dip->type = AUDIO_MIXER_VALUE;
   1612 		dip->mixer_class = EAP_INPUT_CLASS;
   1613 		dip->prev = AUDIO_MIXER_LAST;
   1614 		dip->next = AUDIO_MIXER_LAST;
   1615 		strcpy(dip->label.name, AudioNaux);
   1616 		dip->un.v.num_channels = 2;
   1617 		strcpy(dip->un.v.units.name, AudioNvolume);
   1618 		return (0);
   1619 	case EAP_MIC_VOL:
   1620 		dip->type = AUDIO_MIXER_VALUE;
   1621 		dip->mixer_class = EAP_INPUT_CLASS;
   1622 		dip->prev = AUDIO_MIXER_LAST;
   1623 		dip->next = EAP_MIC_PREAMP;
   1624 		strcpy(dip->label.name, AudioNmicrophone);
   1625 		dip->un.v.num_channels = 1;
   1626 		strcpy(dip->un.v.units.name, AudioNvolume);
   1627 		return (0);
   1628 	case EAP_RECORD_SOURCE:
   1629 		dip->mixer_class = EAP_RECORD_CLASS;
   1630 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1631 		strcpy(dip->label.name, AudioNsource);
   1632 		dip->type = AUDIO_MIXER_SET;
   1633 		dip->un.s.num_mem = 6;
   1634 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1635 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1636 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1637 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1638 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1639 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1640 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1641 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1642 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1643 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1644 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1645 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1646 		return (0);
   1647 	case EAP_OUTPUT_SELECT:
   1648 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1649 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1650 		strcpy(dip->label.name, AudioNselect);
   1651 		dip->type = AUDIO_MIXER_SET;
   1652 		dip->un.s.num_mem = 6;
   1653 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1654 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1655 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1656 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1657 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1658 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1659 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1660 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1661 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1662 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1663 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1664 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1665 		return (0);
   1666 	case EAP_MIC_PREAMP:
   1667 		dip->type = AUDIO_MIXER_ENUM;
   1668 		dip->mixer_class = EAP_INPUT_CLASS;
   1669 		dip->prev = EAP_MIC_VOL;
   1670 		dip->next = AUDIO_MIXER_LAST;
   1671 		strcpy(dip->label.name, AudioNpreamp);
   1672 		dip->un.e.num_mem = 2;
   1673 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1674 		dip->un.e.member[0].ord = 0;
   1675 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1676 		dip->un.e.member[1].ord = 1;
   1677 		return (0);
   1678 	case EAP_OUTPUT_CLASS:
   1679 		dip->type = AUDIO_MIXER_CLASS;
   1680 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1681 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1682 		strcpy(dip->label.name, AudioCoutputs);
   1683 		return (0);
   1684 	case EAP_RECORD_CLASS:
   1685 		dip->type = AUDIO_MIXER_CLASS;
   1686 		dip->mixer_class = EAP_RECORD_CLASS;
   1687 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1688 		strcpy(dip->label.name, AudioCrecord);
   1689 		return (0);
   1690 	case EAP_INPUT_CLASS:
   1691 		dip->type = AUDIO_MIXER_CLASS;
   1692 		dip->mixer_class = EAP_INPUT_CLASS;
   1693 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1694 		strcpy(dip->label.name, AudioCinputs);
   1695 		return (0);
   1696 	}
   1697 	return (ENXIO);
   1698 }
   1699 
   1700 void *
   1701 eap_malloc(void *addr, int direction, size_t size, int pool, int flags)
   1702 {
   1703 	struct eap_instance *ei = addr;
   1704 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1705 	struct eap_dma *p;
   1706 	int error;
   1707 
   1708 	p = malloc(sizeof(*p), pool, flags);
   1709 	if (!p)
   1710 		return (0);
   1711 	error = eap_allocmem(sc, size, 16, p);
   1712 	if (error) {
   1713 		free(p, pool);
   1714 		return (0);
   1715 	}
   1716 	p->next = sc->sc_dmas;
   1717 	sc->sc_dmas = p;
   1718 	return (KERNADDR(p));
   1719 }
   1720 
   1721 void
   1722 eap_free(void *addr, void *ptr, int pool)
   1723 {
   1724 	struct eap_instance *ei = addr;
   1725 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1726 	struct eap_dma **pp, *p;
   1727 
   1728 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1729 		if (KERNADDR(p) == ptr) {
   1730 			eap_freemem(sc, p);
   1731 			*pp = p->next;
   1732 			free(p, pool);
   1733 			return;
   1734 		}
   1735 	}
   1736 }
   1737 
   1738 size_t
   1739 eap_round_buffersize(void *addr, int direction, size_t size)
   1740 {
   1741 
   1742 	return (size);
   1743 }
   1744 
   1745 paddr_t
   1746 eap_mappage(void *addr, void *mem, off_t off, int prot)
   1747 {
   1748 	struct eap_instance *ei = addr;
   1749 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1750 	struct eap_dma *p;
   1751 
   1752 	if (off < 0)
   1753 		return (-1);
   1754 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1755 		;
   1756 	if (!p)
   1757 		return (-1);
   1758 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1759 				off, prot, BUS_DMA_WAITOK));
   1760 }
   1761 
   1762 int
   1763 eap_get_props(void *addr)
   1764 {
   1765 
   1766 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1767 		AUDIO_PROP_FULLDUPLEX);
   1768 }
   1769 
   1770 #if NMIDI > 0
   1771 int
   1772 eap_midi_open(void *addr, int flags,
   1773 	      void (*iintr)(void *, int),
   1774 	      void (*ointr)(void *),
   1775 	      void *arg)
   1776 {
   1777 	struct eap_softc *sc = addr;
   1778 	u_int32_t uctrl;
   1779 
   1780 	sc->sc_iintr = iintr;
   1781 	sc->sc_ointr = ointr;
   1782 	sc->sc_arg = arg;
   1783 
   1784 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
   1785 	uctrl = 0;
   1786 	if (flags & FREAD)
   1787 		uctrl |= EAP_UC_RXINTEN;
   1788 #if 0
   1789 	/* I don't understand ../midi.c well enough to use output interrupts */
   1790 	if (flags & FWRITE)
   1791 		uctrl |= EAP_UC_TXINTEN; */
   1792 #endif
   1793 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1794 
   1795 	return (0);
   1796 }
   1797 
   1798 void
   1799 eap_midi_close(void *addr)
   1800 {
   1801 	struct eap_softc *sc = addr;
   1802 
   1803 	tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
   1804 	EWRITE1(sc, EAP_UART_CONTROL, 0);
   1805 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
   1806 
   1807 	sc->sc_iintr = 0;
   1808 	sc->sc_ointr = 0;
   1809 }
   1810 
   1811 int
   1812 eap_midi_output(void *addr, int d)
   1813 {
   1814 	struct eap_softc *sc = addr;
   1815 	int x;
   1816 
   1817 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
   1818 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
   1819 			EWRITE1(sc, EAP_UART_DATA, d);
   1820 			return (0);
   1821 		}
   1822 		delay(MIDI_BUSY_DELAY);
   1823 	}
   1824 	return (EIO);
   1825 }
   1826 
   1827 void
   1828 eap_midi_getinfo(void *addr, struct midi_info *mi)
   1829 {
   1830 	mi->name = "AudioPCI MIDI UART";
   1831 	mi->props = MIDI_PROP_CAN_INPUT;
   1832 }
   1833 
   1834 #endif
   1835