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