Home | History | Annotate | Line # | Download | only in pci
eap.c revision 1.44.2.7
      1 /*	$NetBSD: eap.c,v 1.44.2.7 2002/11/11 22:11:03 nathanw 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.44.2.7 2002/11/11 22:11:03 nathanw 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 	if ((eap1371_src_wait(sc) & E1371_SRC_STATE_MASK) != E1371_SRC_STATE_OK) {
    468 		for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    469 			t = EREAD4(sc, E1371_SRC);
    470 			if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
    471 				break;
    472 			delay(1);
    473 		}
    474 	}
    475 
    476 	EWRITE4(sc, E1371_SRC, src);
    477 
    478 	return t & E1371_SRC_DATAMASK;
    479 }
    480 
    481 void
    482 eap1371_src_write(struct eap_softc *sc, int a, int d)
    483 {
    484 	u_int32_t r;
    485 
    486 	r = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    487 	r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
    488 	EWRITE4(sc, E1371_SRC, r);
    489 }
    490 
    491 void
    492 eap1371_set_adc_rate(struct eap_softc *sc, int rate)
    493 {
    494 	int freq, n, truncm;
    495 	int out;
    496 	int s;
    497 
    498 	/* Whatever, it works, so I'll leave it :) */
    499 
    500 	if (rate > 48000)
    501 		rate = 48000;
    502 	if (rate < 4000)
    503 		rate = 4000;
    504 	n = rate / 3000;
    505 	if ((1 << n) & SRC_MAGIC)
    506 		n--;
    507 	truncm = ((21 * n) - 1) | 1;
    508 	freq = ((48000 << 15) / rate) * n;
    509 	if (rate >= 24000) {
    510 		if (truncm > 239)
    511 			truncm = 239;
    512 		out = ESRC_SET_TRUNC((239 - truncm) / 2);
    513 	} else {
    514 		if (truncm > 119)
    515 			truncm = 119;
    516 		out = ESRC_SMF | ESRC_SET_TRUNC((119 - truncm) / 2);
    517 	}
    518  	out |= ESRC_SET_N(n);
    519 	s = splaudio();
    520 	eap1371_src_write(sc, ESRC_ADC+ESRC_TRUNC_N, out);
    521 
    522 
    523 	out = eap1371_src_read(sc, ESRC_ADC+ESRC_IREGS) & 0xff;
    524 	eap1371_src_write(sc, ESRC_ADC+ESRC_IREGS, out |
    525 			  ESRC_SET_VFI(freq >> 15));
    526 	eap1371_src_write(sc, ESRC_ADC+ESRC_VFF, freq & 0x7fff);
    527 	eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(n));
    528 	eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(n));
    529 	splx(s);
    530 }
    531 
    532 void
    533 eap1371_set_dac_rate(struct eap_instance *ei, int rate)
    534 {
    535 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
    536 	int dac = ei->index == EAP_DAC1 ? ESRC_DAC1 : ESRC_DAC2;
    537 	int freq, r;
    538 	int s;
    539 
    540 	DPRINTFN(2, ("eap1371_set_dac_date: set rate for %d\n", ei->index));
    541 
    542 	/* Whatever, it works, so I'll leave it :) */
    543 
    544 	if (rate > 48000)
    545 	    rate = 48000;
    546 	if (rate < 4000)
    547 	    rate = 4000;
    548 	freq = ((rate << 15) + 1500) / 3000;
    549 
    550 	s = splaudio();
    551 	eap1371_src_wait(sc);
    552 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    553 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    554 	r |= ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2;
    555 	EWRITE4(sc, E1371_SRC, r);
    556 	r = eap1371_src_read(sc, dac + ESRC_IREGS) & 0x00ff;
    557 	eap1371_src_write(sc, dac + ESRC_IREGS, r | ((freq >> 5) & 0xfc00));
    558 	eap1371_src_write(sc, dac + ESRC_VFF, freq & 0x7fff);
    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 	splx(s);
    564 }
    565 
    566 void
    567 eap_attach(struct device *parent, struct device *self, void *aux)
    568 {
    569 	struct eap_softc *sc = (struct eap_softc *)self;
    570 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    571 	pci_chipset_tag_t pc = pa->pa_pc;
    572 	struct audio_hw_if *eap_hw_if;
    573 	char const *intrstr;
    574 	pci_intr_handle_t ih;
    575 	pcireg_t csr;
    576 	char devinfo[256];
    577 	mixer_ctrl_t ctl;
    578 	int i;
    579 	int revision, ct5880;
    580 	const char *revstr = "";
    581 
    582 	/* Flag if we're "creative" */
    583 	sc->sc_1371 = !(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
    584 			PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI);
    585 
    586 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    587 	revision = PCI_REVISION(pa->pa_class);
    588 	if (sc->sc_1371) {
    589 		ct5880 = 0;
    590 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
    591 		    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_CT5880)
    592 			ct5880 = 1;
    593 		switch (revision) {
    594 		case EAP_EV1938_A: revstr = "EV1938A "; break;
    595 		case EAP_CT5880_C: revstr = "CT5880C "; ct5880 = 1; break;
    596 		case EAP_ES1373_A: revstr = "ES1373A "; break;
    597 		case EAP_ES1373_B: revstr = "ES1373B "; break;
    598 		case EAP_CT5880_A: revstr = "CT5880A "; ct5880 = 1; break;
    599 		case EAP_ES1371_B: revstr = "ES1371B "; break;
    600 		}
    601 	}
    602 	printf(": %s %s(rev. 0x%02x)\n", devinfo, revstr, revision);
    603 
    604 	/* Map I/O register */
    605 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    606 	      &sc->iot, &sc->ioh, NULL, NULL)) {
    607 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    608 		return;
    609 	}
    610 
    611 	sc->sc_dmatag = pa->pa_dmat;
    612 
    613 	/* Enable the device. */
    614 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    615 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    616 		       csr | PCI_COMMAND_MASTER_ENABLE);
    617 
    618 	/* Map and establish the interrupt. */
    619 	if (pci_intr_map(pa, &ih)) {
    620 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    621 		return;
    622 	}
    623 	intrstr = pci_intr_string(pc, ih);
    624 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
    625 	if (sc->sc_ih == NULL) {
    626 		printf("%s: couldn't establish interrupt",
    627 		    sc->sc_dev.dv_xname);
    628 		if (intrstr != NULL)
    629 			printf(" at %s", intrstr);
    630 		printf("\n");
    631 		return;
    632 	}
    633 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    634 
    635 	sc->sc_ei[EAP_I1].parent = (struct device *)sc;
    636 	sc->sc_ei[EAP_I1].index = EAP_DAC2;
    637 	sc->sc_ei[EAP_I2].parent = (struct device *)sc;
    638 	sc->sc_ei[EAP_I2].index = EAP_DAC1;
    639 
    640 	if (!sc->sc_1371) {
    641 		/* Enable interrupts and looping mode. */
    642 		/* enable the parts we need */
    643 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    644 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
    645 
    646 		/* reset codec */
    647 		/* normal operation */
    648 		/* select codec clocks */
    649 		eap1370_write_codec(sc, AK_RESET, AK_PD);
    650 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
    651 		eap1370_write_codec(sc, AK_CS, 0x0);
    652 
    653 		eap_hw_if = &eap1370_hw_if;
    654 
    655 		/* Enable all relevant mixer switches. */
    656 		ctl.dev = EAP_OUTPUT_SELECT;
    657 		ctl.type = AUDIO_MIXER_SET;
    658 		ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
    659 			1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
    660 			1 << EAP_MIC_VOL;
    661 		eap_hw_if->set_port(sc, &ctl);
    662 
    663 		ctl.type = AUDIO_MIXER_VALUE;
    664 		ctl.un.value.num_channels = 1;
    665 		for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
    666 		     ctl.dev++) {
    667 			ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
    668 			eap_hw_if->set_port(sc, &ctl);
    669 		}
    670 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
    671 		eap_hw_if->set_port(sc, &ctl);
    672 		ctl.dev = EAP_MIC_PREAMP;
    673 		ctl.type = AUDIO_MIXER_ENUM;
    674 		ctl.un.ord = 0;
    675 		eap_hw_if->set_port(sc, &ctl);
    676 		ctl.dev = EAP_RECORD_SOURCE;
    677 		ctl.type = AUDIO_MIXER_SET;
    678 		ctl.un.mask = 1 << EAP_MIC_VOL;
    679 		eap_hw_if->set_port(sc, &ctl);
    680 	} else {
    681 		/* clean slate */
    682 
    683                 EWRITE4(sc, EAP_SIC, 0);
    684 		EWRITE4(sc, EAP_ICSC, 0);
    685 		EWRITE4(sc, E1371_LEGACY, 0);
    686 
    687 		if (ct5880) {
    688 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
    689 			/* Let codec wake up */
    690 			tsleep(sc, PRIBIO, "eapcdc", hz / 20);
    691 		}
    692 
    693                 /* Reset from es1371's perspective */
    694                 EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
    695                 delay(20);
    696                 EWRITE4(sc, EAP_ICSC, 0);
    697 
    698 		/*
    699 		 * Must properly reprogram sample rate converter,
    700 		 * or it locks up.  Set some defaults for the life of the
    701 		 * machine, and set up a sb default sample rate.
    702 		 */
    703 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
    704 		for (i = 0; i < 0x80; i++)
    705 			eap1371_src_write(sc, i, 0);
    706 		eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
    707 		eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
    708 		eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
    709 		eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
    710 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
    711 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
    712 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
    713 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
    714 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
    715 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
    716 		eap1371_set_adc_rate(sc, 22050);
    717 		eap1371_set_dac_rate(&sc->sc_ei[0], 22050);
    718 		eap1371_set_dac_rate(&sc->sc_ei[1], 22050);
    719 
    720 		EWRITE4(sc, E1371_SRC, 0);
    721 
    722 		/* Reset codec */
    723 
    724 		/* Interrupt enable */
    725 		sc->host_if.arg = sc;
    726 		sc->host_if.attach = eap1371_attach_codec;
    727 		sc->host_if.read = eap1371_read_codec;
    728 		sc->host_if.write = eap1371_write_codec;
    729 		sc->host_if.reset = eap1371_reset_codec;
    730 
    731 		if (ac97_attach(&sc->host_if) == 0) {
    732 			/* Interrupt enable */
    733 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    734 		} else
    735 			return;
    736 
    737 		eap_hw_if = &eap1371_hw_if;
    738 
    739 		/* Just enable the DAC and master volumes by default */
    740 		ctl.type = AUDIO_MIXER_ENUM;
    741 		ctl.un.ord = 0;  /* off */
    742 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCoutputs,
    743 		       AudioNmaster, AudioNmute);
    744 		eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
    745 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCinputs,
    746 		       AudioNdac, AudioNmute);
    747 		eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
    748 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
    749 		       AudioNvolume, AudioNmute);
    750 		eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
    751 
    752 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
    753 		       AudioNsource, NULL);
    754 		ctl.type = AUDIO_MIXER_ENUM;
    755 		ctl.un.ord = 0;
    756 		eap1371_mixer_set_port(&sc->sc_ei[EAP_I1], &ctl);
    757 
    758 	}
    759 
    760 	audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I1], &sc->sc_dev);
    761 
    762 #ifdef EAP_USE_BOTH_DACS
    763 	printf("%s: attaching secondary DAC\n", sc->sc_dev.dv_xname);
    764 	audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I2], &sc->sc_dev);
    765 #endif
    766 
    767 #if NMIDI > 0
    768 	midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
    769 #endif
    770 }
    771 
    772 int
    773 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    774 {
    775 	struct eap_softc *sc = sc_;
    776 
    777 	sc->codec_if = codec_if;
    778 	return (0);
    779 }
    780 
    781 void
    782 eap1371_reset_codec(void *sc_)
    783 {
    784 	struct eap_softc *sc = sc_;
    785 	u_int32_t icsc;
    786 	int s;
    787 
    788 	s = splaudio();
    789 	icsc = EREAD4(sc, EAP_ICSC);
    790 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
    791 	delay(20);
    792 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
    793 	delay(1);
    794 	splx(s);
    795 
    796 	return;
    797 }
    798 
    799 int
    800 eap_intr(void *p)
    801 {
    802 	struct eap_softc *sc = p;
    803 	u_int32_t intr, sic;
    804 
    805 	intr = EREAD4(sc, EAP_ICSS);
    806 	if (!(intr & EAP_INTR))
    807 		return (0);
    808 	sic = EREAD4(sc, EAP_SIC);
    809 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
    810 	if (intr & EAP_I_ADC) {
    811 #if 0
    812 		/*
    813 		 * XXX This is a hack!
    814 		 * The EAP chip sometimes generates the recording interrupt
    815 		 * while it is still transferring the data.  To make sure
    816 		 * it has all arrived we busy wait until the count is right.
    817 		 * The transfer we are waiting for is 8 longwords.
    818 		 */
    819 		int s, nw, n;
    820 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
    821 		s = EREAD4(sc, EAP_ADC_CSR);
    822 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
    823 		n = 0;
    824 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
    825 			delay(10);
    826 			if (++n > 100) {
    827 				printf("eapintr: dma fix timeout");
    828 				break;
    829 			}
    830 		}
    831 		/* Continue with normal interrupt handling. */
    832 #endif
    833 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
    834 		EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
    835 		if (sc->sc_rintr)
    836 			sc->sc_rintr(sc->sc_rarg);
    837 	}
    838 
    839 	if (intr & EAP_I_DAC2) {
    840 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
    841 		EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
    842 		if (sc->sc_ei[EAP_DAC2].ei_pintr)
    843 			sc->sc_ei[EAP_DAC2].ei_pintr(sc->sc_ei[EAP_DAC2].ei_parg);
    844 	}
    845 
    846 	if (intr & EAP_I_DAC1) {
    847 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P1_INTR_EN);
    848 		EWRITE4(sc, EAP_SIC, sic | EAP_P1_INTR_EN);
    849 		if (sc->sc_ei[EAP_DAC1].ei_pintr)
    850 			sc->sc_ei[EAP_DAC1].ei_pintr(sc->sc_ei[EAP_DAC1].ei_parg);
    851 	}
    852 
    853 	if (intr & EAP_I_MCCB)
    854 		panic("eap_intr: unexpected MCCB interrupt");
    855 #if NMIDI > 0
    856 	if ((intr & EAP_I_UART) && sc->sc_iintr != NULL) {
    857 		u_int32_t data;
    858 
    859 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXINT) {
    860 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
    861 				data = EREAD1(sc, EAP_UART_DATA);
    862 				sc->sc_iintr(sc->sc_arg, data);
    863 			}
    864 		}
    865 	}
    866 #endif
    867 	return (1);
    868 }
    869 
    870 int
    871 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
    872 {
    873 	int error;
    874 
    875 	p->size = size;
    876 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    877 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    878 				 &p->nsegs, BUS_DMA_NOWAIT);
    879 	if (error)
    880 		return (error);
    881 
    882 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    883 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    884 	if (error)
    885 		goto free;
    886 
    887 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    888 				  0, BUS_DMA_NOWAIT, &p->map);
    889 	if (error)
    890 		goto unmap;
    891 
    892 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    893 				BUS_DMA_NOWAIT);
    894 	if (error)
    895 		goto destroy;
    896 	return (0);
    897 
    898 destroy:
    899 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    900 unmap:
    901 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    902 free:
    903 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    904 	return (error);
    905 }
    906 
    907 int
    908 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
    909 {
    910 	bus_dmamap_unload(sc->sc_dmatag, p->map);
    911 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    912 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    913 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    914 	return (0);
    915 }
    916 
    917 int
    918 eap_open(void *addr, int flags)
    919 {
    920 	struct eap_instance *ei = addr;
    921 
    922 	/* there is only one ADC */
    923 	if (ei->index == EAP_I2 && flags & AUOPEN_READ)
    924 		return (EOPNOTSUPP);
    925 
    926 	return (0);
    927 }
    928 
    929 /*
    930  * Close function is called at splaudio().
    931  */
    932 void
    933 eap_close(void *addr)
    934 {
    935 	struct eap_instance *ei = addr;
    936 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
    937 
    938 	eap_halt_output(ei);
    939 	if (ei->index == EAP_I1) {
    940 		eap_halt_input(ei);
    941 		sc->sc_rintr = 0;
    942 	}
    943 
    944 	ei->ei_pintr = 0;
    945 }
    946 
    947 int
    948 eap_query_encoding(void *addr, struct audio_encoding *fp)
    949 {
    950 	switch (fp->index) {
    951 	case 0:
    952 		strcpy(fp->name, AudioEulinear);
    953 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    954 		fp->precision = 8;
    955 		fp->flags = 0;
    956 		return (0);
    957 	case 1:
    958 		strcpy(fp->name, AudioEmulaw);
    959 		fp->encoding = AUDIO_ENCODING_ULAW;
    960 		fp->precision = 8;
    961 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    962 		return (0);
    963 	case 2:
    964 		strcpy(fp->name, AudioEalaw);
    965 		fp->encoding = AUDIO_ENCODING_ALAW;
    966 		fp->precision = 8;
    967 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    968 		return (0);
    969 	case 3:
    970 		strcpy(fp->name, AudioEslinear);
    971 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    972 		fp->precision = 8;
    973 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    974 		return (0);
    975 	case 4:
    976 		strcpy(fp->name, AudioEslinear_le);
    977 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    978 		fp->precision = 16;
    979 		fp->flags = 0;
    980 		return (0);
    981 	case 5:
    982 		strcpy(fp->name, AudioEulinear_le);
    983 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    984 		fp->precision = 16;
    985 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    986 		return (0);
    987 	case 6:
    988 		strcpy(fp->name, AudioEslinear_be);
    989 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    990 		fp->precision = 16;
    991 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    992 		return (0);
    993 	case 7:
    994 		strcpy(fp->name, AudioEulinear_be);
    995 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    996 		fp->precision = 16;
    997 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    998 		return (0);
    999 	default:
   1000 		return (EINVAL);
   1001 	}
   1002 }
   1003 
   1004 int
   1005 eap_set_params(void *addr, int setmode, int usemode,
   1006 	       struct audio_params *play, struct audio_params *rec)
   1007 {
   1008 	struct eap_instance *ei = addr;
   1009 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1010 	struct audio_params *p;
   1011 	int mode;
   1012 	u_int32_t div;
   1013 
   1014 	/*
   1015 	 * The es1370 only has one clock, so make the sample rates match.
   1016 	 */
   1017 	if (!sc->sc_1371) {
   1018 	    if (play->sample_rate != rec->sample_rate &&
   1019 		usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1020 	    	if (setmode == AUMODE_PLAY) {
   1021 		    rec->sample_rate = play->sample_rate;
   1022 		    setmode |= AUMODE_RECORD;
   1023 		} else if (setmode == AUMODE_RECORD) {
   1024 		    play->sample_rate = rec->sample_rate;
   1025 		    setmode |= AUMODE_PLAY;
   1026 		} else
   1027 		    return (EINVAL);
   1028 	    }
   1029 	}
   1030 
   1031 	for (mode = AUMODE_RECORD; mode != -1;
   1032 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1033 		if ((setmode & mode) == 0)
   1034 			continue;
   1035 
   1036 		p = mode == AUMODE_PLAY ? play : rec;
   1037 
   1038 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
   1039 		    (p->precision != 8 && p->precision != 16) ||
   1040 		    (p->channels != 1 && p->channels != 2))
   1041 			return (EINVAL);
   1042 
   1043 		p->factor = 1;
   1044 		p->sw_code = 0;
   1045 		switch (p->encoding) {
   1046 		case AUDIO_ENCODING_SLINEAR_BE:
   1047 			if (p->precision == 16)
   1048 				p->sw_code = swap_bytes;
   1049 			else
   1050 				p->sw_code = change_sign8;
   1051 			break;
   1052 		case AUDIO_ENCODING_SLINEAR_LE:
   1053 			if (p->precision != 16)
   1054 				p->sw_code = change_sign8;
   1055 			break;
   1056 		case AUDIO_ENCODING_ULINEAR_BE:
   1057 			if (p->precision == 16) {
   1058 				if (mode == AUMODE_PLAY)
   1059 					p->sw_code = swap_bytes_change_sign16_le;
   1060 				else
   1061 					p->sw_code = change_sign16_swap_bytes_le;
   1062 			}
   1063 			break;
   1064 		case AUDIO_ENCODING_ULINEAR_LE:
   1065 			if (p->precision == 16)
   1066 				p->sw_code = change_sign16_le;
   1067 			break;
   1068 		case AUDIO_ENCODING_ULAW:
   1069 			if (mode == AUMODE_PLAY) {
   1070 				p->factor = 2;
   1071 				p->sw_code = mulaw_to_slinear16_le;
   1072 			} else
   1073 				p->sw_code = ulinear8_to_mulaw;
   1074 			break;
   1075 		case AUDIO_ENCODING_ALAW:
   1076 			if (mode == AUMODE_PLAY) {
   1077 				p->factor = 2;
   1078 				p->sw_code = alaw_to_slinear16_le;
   1079 			} else
   1080 				p->sw_code = ulinear8_to_alaw;
   1081 			break;
   1082 		default:
   1083 			return (EINVAL);
   1084 		}
   1085 	}
   1086 
   1087 	if (sc->sc_1371) {
   1088 		eap1371_set_dac_rate(ei, play->sample_rate);
   1089 		eap1371_set_adc_rate(sc, rec->sample_rate);
   1090 	} else {
   1091 		/* Set the speed */
   1092 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
   1093 			     EREAD4(sc, EAP_ICSC)));
   1094 		div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
   1095 		/*
   1096 		 * XXX
   1097 		 * The -2 isn't documented, but seemed to make the wall
   1098 		 * time match
   1099 		 * what I expect.  - mycroft
   1100 		 */
   1101 		if (usemode == AUMODE_RECORD)
   1102 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1103 				rec->sample_rate - 2);
   1104 		else
   1105 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1106 				play->sample_rate - 2);
   1107 #if 0
   1108 		div |= EAP_CCB_INTRM;
   1109 #else
   1110 		/*
   1111 		 * It is not obvious how to acknowledge MCCB interrupts, so
   1112 		 * we had better not enable them.
   1113 		 */
   1114 #endif
   1115 		EWRITE4(sc, EAP_ICSC, div);
   1116 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1117 	}
   1118 
   1119 	return (0);
   1120 }
   1121 
   1122 int
   1123 eap_round_blocksize(void *addr, int blk)
   1124 {
   1125 	return (blk & -32);	/* keep good alignment */
   1126 }
   1127 
   1128 int
   1129 eap_trigger_output(
   1130 	void *addr,
   1131 	void *start,
   1132 	void *end,
   1133 	int blksize,
   1134 	void (*intr)(void *),
   1135 	void *arg,
   1136 	struct audio_params *param)
   1137 {
   1138 	struct eap_instance *ei = addr;
   1139 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1140 	struct eap_dma *p;
   1141 	u_int32_t icsc, sic;
   1142 	int sampshift;
   1143 
   1144 #ifdef DIAGNOSTIC
   1145 	if (ei->ei_prun)
   1146 		panic("eap_trigger_output: already running");
   1147 	ei->ei_prun = 1;
   1148 #endif
   1149 
   1150 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
   1151 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1152 	ei->ei_pintr = intr;
   1153 	ei->ei_parg = arg;
   1154 
   1155 	sic = EREAD4(sc, EAP_SIC);
   1156 	sic &= ~(EAP_S_EB(ei->index) | EAP_S_MB(ei->index) | EAP_INC_BITS);
   1157 
   1158 	if (ei->index == EAP_DAC2)
   1159 		sic |= EAP_SET_P2_ST_INC(0)
   1160 		    | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
   1161 
   1162 	sampshift = 0;
   1163 	if (param->precision * param->factor == 16) {
   1164 		sic |= EAP_S_EB(ei->index);
   1165 		sampshift++;
   1166 	}
   1167 	if (param->channels == 2) {
   1168 		sic |= EAP_S_MB(ei->index);
   1169 		sampshift++;
   1170 	}
   1171 	EWRITE4(sc, EAP_SIC, sic & ~EAP_P_INTR_EN(ei->index));
   1172 	EWRITE4(sc, EAP_SIC, sic | EAP_P_INTR_EN(ei->index));
   1173 
   1174 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1175 		;
   1176 	if (!p) {
   1177 		printf("eap_trigger_output: bad addr %p\n", start);
   1178 		return (EINVAL);
   1179 	}
   1180 
   1181 	if (ei->index == EAP_DAC2) {
   1182 		DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
   1183 			 (int)DMAADDR(p),
   1184 			 (int)EAP_SET_SIZE(0,
   1185 			 (((char *)end - (char *)start) >> 2) - 1)));
   1186 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1187 		EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
   1188 		EWRITE4(sc, EAP_DAC2_SIZE,
   1189 			EAP_SET_SIZE(0,
   1190 			((char *)end - (char *)start) >> 2) - 1);
   1191 		EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
   1192 	} else if (ei->index == EAP_DAC1) {
   1193 		DPRINTF(("eap_trigger_output: DAC1_ADDR=0x%x, DAC1_SIZE=0x%x\n",
   1194 			 (int)DMAADDR(p),
   1195 			 (int)EAP_SET_SIZE(0,
   1196 			 (((char *)end - (char *)start) >> 2) - 1)));
   1197 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1198 		EWRITE4(sc, EAP_DAC1_ADDR, DMAADDR(p));
   1199 		EWRITE4(sc, EAP_DAC1_SIZE,
   1200 			EAP_SET_SIZE(0,
   1201 			((char *)end - (char *)start) >> 2) - 1);
   1202 		EWRITE4(sc, EAP_DAC1_CSR, (blksize >> sampshift) - 1);
   1203 	}
   1204 #ifdef DIAGNOSTIC
   1205 	else
   1206 		panic("eap_trigger_output: impossible instance %d", ei->index);
   1207 #endif
   1208 
   1209 	if (sc->sc_1371)
   1210 		EWRITE4(sc, E1371_SRC, 0);
   1211 
   1212 	icsc = EREAD4(sc, EAP_ICSC);
   1213 	icsc |= EAP_EN(ei->index);
   1214 	EWRITE4(sc, EAP_ICSC, icsc);
   1215 
   1216 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
   1217 
   1218 	return (0);
   1219 }
   1220 
   1221 int
   1222 eap_trigger_input(
   1223 	void *addr,
   1224 	void *start,
   1225 	void *end,
   1226 	int blksize,
   1227 	void (*intr)(void *),
   1228 	void *arg,
   1229 	struct audio_params *param)
   1230 {
   1231 	struct eap_instance *ei = addr;
   1232 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1233 	struct eap_dma *p;
   1234 	u_int32_t icsc, sic;
   1235 	int sampshift;
   1236 
   1237 #ifdef DIAGNOSTIC
   1238 	if (sc->sc_rrun)
   1239 		panic("eap_trigger_input: already running");
   1240 	sc->sc_rrun = 1;
   1241 #endif
   1242 
   1243 	DPRINTFN(1, ("eap_trigger_input: ei=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1244 	    addr, start, end, blksize, intr, arg));
   1245 	sc->sc_rintr = intr;
   1246 	sc->sc_rarg = arg;
   1247 
   1248 	sic = EREAD4(sc, EAP_SIC);
   1249 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
   1250 	sampshift = 0;
   1251 	if (param->precision * param->factor == 16) {
   1252 		sic |= EAP_R1_S_EB;
   1253 		sampshift++;
   1254 	}
   1255 	if (param->channels == 2) {
   1256 		sic |= EAP_R1_S_MB;
   1257 		sampshift++;
   1258 	}
   1259 	EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
   1260 	EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
   1261 
   1262 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1263 		;
   1264 	if (!p) {
   1265 		printf("eap_trigger_input: bad addr %p\n", start);
   1266 		return (EINVAL);
   1267 	}
   1268 
   1269 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
   1270 		 (int)DMAADDR(p),
   1271 		 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
   1272 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
   1273 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
   1274 	EWRITE4(sc, EAP_ADC_SIZE,
   1275 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
   1276 
   1277 	EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
   1278 
   1279 	if (sc->sc_1371)
   1280 		EWRITE4(sc, E1371_SRC, 0);
   1281 
   1282 	icsc = EREAD4(sc, EAP_ICSC);
   1283 	icsc |= EAP_ADC_EN;
   1284 	EWRITE4(sc, EAP_ICSC, icsc);
   1285 
   1286 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
   1287 
   1288 	return (0);
   1289 }
   1290 
   1291 int
   1292 eap_halt_output(void *addr)
   1293 {
   1294 	struct eap_instance *ei = addr;
   1295 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1296 	u_int32_t icsc;
   1297 
   1298 	DPRINTF(("eap: eap_halt_output\n"));
   1299 	icsc = EREAD4(sc, EAP_ICSC);
   1300 	EWRITE4(sc, EAP_ICSC, icsc & ~(EAP_DAC2_EN << ei->index));
   1301 #ifdef DIAGNOSTIC
   1302 	ei->ei_prun = 0;
   1303 #endif
   1304 
   1305 	return (0);
   1306 }
   1307 
   1308 int
   1309 eap_halt_input(void *addr)
   1310 {
   1311 	struct eap_instance *ei = addr;
   1312 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1313 	u_int32_t icsc;
   1314 
   1315 #define EAP_USE_FMDAC_ALSO
   1316 	DPRINTF(("eap: eap_halt_input\n"));
   1317 	icsc = EREAD4(sc, EAP_ICSC);
   1318 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
   1319 #ifdef DIAGNOSTIC
   1320 	sc->sc_rrun = 0;
   1321 #endif
   1322 	return (0);
   1323 }
   1324 
   1325 int
   1326 eap_getdev(void *addr, struct audio_device *retp)
   1327 {
   1328 	*retp = eap_device;
   1329 	return (0);
   1330 }
   1331 
   1332 int
   1333 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1334 {
   1335 	struct eap_instance *ei = addr;
   1336 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1337 
   1338 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
   1339 }
   1340 
   1341 int
   1342 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1343 {
   1344 	struct eap_instance *ei = addr;
   1345 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1346 
   1347 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
   1348 }
   1349 
   1350 int
   1351 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1352 {
   1353 	struct eap_instance *ei = addr;
   1354 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1355 
   1356 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
   1357 }
   1358 
   1359 int
   1360 eap1371_get_portnum_by_name(struct eap_softc *sc,
   1361 			    char *class, char *device, char *qualifier)
   1362 {
   1363 	return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
   1364 	     device, qualifier));
   1365 }
   1366 
   1367 void
   1368 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
   1369 {
   1370 	eap1370_write_codec(sc, a, d);
   1371 
   1372 	sc->sc_port[a] = d;
   1373 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
   1374 }
   1375 
   1376 int
   1377 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1378 {
   1379 	struct eap_instance *ei = addr;
   1380 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1381 	int lval, rval, l, r, la, ra;
   1382 	int l1, r1, l2, r2, m, o1, o2;
   1383 
   1384 	if (cp->dev == EAP_RECORD_SOURCE) {
   1385 		if (cp->type != AUDIO_MIXER_SET)
   1386 			return (EINVAL);
   1387 		m = sc->sc_record_source = cp->un.mask;
   1388 		l1 = l2 = r1 = r2 = 0;
   1389 		if (m & (1 << EAP_VOICE_VOL))
   1390 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
   1391 		if (m & (1 << EAP_FM_VOL))
   1392 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
   1393 		if (m & (1 << EAP_CD_VOL))
   1394 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
   1395 		if (m & (1 << EAP_LINE_VOL))
   1396 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
   1397 		if (m & (1 << EAP_AUX_VOL))
   1398 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
   1399 		if (m & (1 << EAP_MIC_VOL))
   1400 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
   1401 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
   1402 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
   1403 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
   1404 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
   1405 		return (0);
   1406 	}
   1407 	if (cp->dev == EAP_OUTPUT_SELECT) {
   1408 		if (cp->type != AUDIO_MIXER_SET)
   1409 			return (EINVAL);
   1410 		m = sc->sc_output_source = cp->un.mask;
   1411 		o1 = o2 = 0;
   1412 		if (m & (1 << EAP_VOICE_VOL))
   1413 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
   1414 		if (m & (1 << EAP_FM_VOL))
   1415 			o1 |= AK_M_FM_L | AK_M_FM_R;
   1416 		if (m & (1 << EAP_CD_VOL))
   1417 			o1 |= AK_M_CD_L | AK_M_CD_R;
   1418 		if (m & (1 << EAP_LINE_VOL))
   1419 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
   1420 		if (m & (1 << EAP_AUX_VOL))
   1421 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
   1422 		if (m & (1 << EAP_MIC_VOL))
   1423 			o1 |= AK_M_MIC;
   1424 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
   1425 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
   1426 		return (0);
   1427 	}
   1428 	if (cp->dev == EAP_MIC_PREAMP) {
   1429 		if (cp->type != AUDIO_MIXER_ENUM)
   1430 			return (EINVAL);
   1431 		if (cp->un.ord != 0 && cp->un.ord != 1)
   1432 			return (EINVAL);
   1433 		sc->sc_mic_preamp = cp->un.ord;
   1434 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
   1435 		return (0);
   1436 	}
   1437 	if (cp->type != AUDIO_MIXER_VALUE)
   1438 		return (EINVAL);
   1439 	if (cp->un.value.num_channels == 1)
   1440 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1441 	else if (cp->un.value.num_channels == 2) {
   1442 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1443 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1444 	} else
   1445 		return (EINVAL);
   1446 	ra = -1;
   1447 	switch (cp->dev) {
   1448 	case EAP_MASTER_VOL:
   1449 		l = VOL_TO_ATT5(lval);
   1450 		r = VOL_TO_ATT5(rval);
   1451 		la = AK_MASTER_L;
   1452 		ra = AK_MASTER_R;
   1453 		break;
   1454 	case EAP_MIC_VOL:
   1455 		if (cp->un.value.num_channels != 1)
   1456 			return (EINVAL);
   1457 		la = AK_MIC;
   1458 		goto lr;
   1459 	case EAP_VOICE_VOL:
   1460 		la = AK_VOICE_L;
   1461 		ra = AK_VOICE_R;
   1462 		goto lr;
   1463 	case EAP_FM_VOL:
   1464 		la = AK_FM_L;
   1465 		ra = AK_FM_R;
   1466 		goto lr;
   1467 	case EAP_CD_VOL:
   1468 		la = AK_CD_L;
   1469 		ra = AK_CD_R;
   1470 		goto lr;
   1471 	case EAP_LINE_VOL:
   1472 		la = AK_LINE_L;
   1473 		ra = AK_LINE_R;
   1474 		goto lr;
   1475 	case EAP_AUX_VOL:
   1476 		la = AK_AUX_L;
   1477 		ra = AK_AUX_R;
   1478 	lr:
   1479 		l = VOL_TO_GAIN5(lval);
   1480 		r = VOL_TO_GAIN5(rval);
   1481 		break;
   1482 	default:
   1483 		return (EINVAL);
   1484 	}
   1485 	eap1370_set_mixer(sc, la, l);
   1486 	if (ra >= 0) {
   1487 		eap1370_set_mixer(sc, ra, r);
   1488 	}
   1489 	return (0);
   1490 }
   1491 
   1492 int
   1493 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1494 {
   1495 	struct eap_instance *ei = addr;
   1496 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1497 	int la, ra, l, r;
   1498 
   1499 	switch (cp->dev) {
   1500 	case EAP_RECORD_SOURCE:
   1501 		if (cp->type != AUDIO_MIXER_SET)
   1502 			return (EINVAL);
   1503 		cp->un.mask = sc->sc_record_source;
   1504 		return (0);
   1505 	case EAP_OUTPUT_SELECT:
   1506 		if (cp->type != AUDIO_MIXER_SET)
   1507 			return (EINVAL);
   1508 		cp->un.mask = sc->sc_output_source;
   1509 		return (0);
   1510 	case EAP_MIC_PREAMP:
   1511 		if (cp->type != AUDIO_MIXER_ENUM)
   1512 			return (EINVAL);
   1513 		cp->un.ord = sc->sc_mic_preamp;
   1514 		return (0);
   1515 	case EAP_MASTER_VOL:
   1516 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
   1517 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
   1518 		break;
   1519 	case EAP_MIC_VOL:
   1520 		if (cp->un.value.num_channels != 1)
   1521 			return (EINVAL);
   1522 		la = ra = AK_MIC;
   1523 		goto lr;
   1524 	case EAP_VOICE_VOL:
   1525 		la = AK_VOICE_L;
   1526 		ra = AK_VOICE_R;
   1527 		goto lr;
   1528 	case EAP_FM_VOL:
   1529 		la = AK_FM_L;
   1530 		ra = AK_FM_R;
   1531 		goto lr;
   1532 	case EAP_CD_VOL:
   1533 		la = AK_CD_L;
   1534 		ra = AK_CD_R;
   1535 		goto lr;
   1536 	case EAP_LINE_VOL:
   1537 		la = AK_LINE_L;
   1538 		ra = AK_LINE_R;
   1539 		goto lr;
   1540 	case EAP_AUX_VOL:
   1541 		la = AK_AUX_L;
   1542 		ra = AK_AUX_R;
   1543 	lr:
   1544 		l = GAIN5_TO_VOL(sc->sc_port[la]);
   1545 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
   1546 		break;
   1547 	default:
   1548 		return (EINVAL);
   1549 	}
   1550 	if (cp->un.value.num_channels == 1)
   1551 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
   1552 	else if (cp->un.value.num_channels == 2) {
   1553 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
   1554 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1555 	} else
   1556 		return (EINVAL);
   1557 	return (0);
   1558 }
   1559 
   1560 int
   1561 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1562 {
   1563 
   1564 	switch (dip->index) {
   1565 	case EAP_MASTER_VOL:
   1566 		dip->type = AUDIO_MIXER_VALUE;
   1567 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1568 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1569 		strcpy(dip->label.name, AudioNmaster);
   1570 		dip->un.v.num_channels = 2;
   1571 		strcpy(dip->un.v.units.name, AudioNvolume);
   1572 		return (0);
   1573 	case EAP_VOICE_VOL:
   1574 		dip->type = AUDIO_MIXER_VALUE;
   1575 		dip->mixer_class = EAP_INPUT_CLASS;
   1576 		dip->prev = AUDIO_MIXER_LAST;
   1577 		dip->next = AUDIO_MIXER_LAST;
   1578 		strcpy(dip->label.name, AudioNdac);
   1579 		dip->un.v.num_channels = 2;
   1580 		strcpy(dip->un.v.units.name, AudioNvolume);
   1581 		return (0);
   1582 	case EAP_FM_VOL:
   1583 		dip->type = AUDIO_MIXER_VALUE;
   1584 		dip->mixer_class = EAP_INPUT_CLASS;
   1585 		dip->prev = AUDIO_MIXER_LAST;
   1586 		dip->next = AUDIO_MIXER_LAST;
   1587 		strcpy(dip->label.name, AudioNfmsynth);
   1588 		dip->un.v.num_channels = 2;
   1589 		strcpy(dip->un.v.units.name, AudioNvolume);
   1590 		return (0);
   1591 	case EAP_CD_VOL:
   1592 		dip->type = AUDIO_MIXER_VALUE;
   1593 		dip->mixer_class = EAP_INPUT_CLASS;
   1594 		dip->prev = AUDIO_MIXER_LAST;
   1595 		dip->next = AUDIO_MIXER_LAST;
   1596 		strcpy(dip->label.name, AudioNcd);
   1597 		dip->un.v.num_channels = 2;
   1598 		strcpy(dip->un.v.units.name, AudioNvolume);
   1599 		return (0);
   1600 	case EAP_LINE_VOL:
   1601 		dip->type = AUDIO_MIXER_VALUE;
   1602 		dip->mixer_class = EAP_INPUT_CLASS;
   1603 		dip->prev = AUDIO_MIXER_LAST;
   1604 		dip->next = AUDIO_MIXER_LAST;
   1605 		strcpy(dip->label.name, AudioNline);
   1606 		dip->un.v.num_channels = 2;
   1607 		strcpy(dip->un.v.units.name, AudioNvolume);
   1608 		return (0);
   1609 	case EAP_AUX_VOL:
   1610 		dip->type = AUDIO_MIXER_VALUE;
   1611 		dip->mixer_class = EAP_INPUT_CLASS;
   1612 		dip->prev = AUDIO_MIXER_LAST;
   1613 		dip->next = AUDIO_MIXER_LAST;
   1614 		strcpy(dip->label.name, AudioNaux);
   1615 		dip->un.v.num_channels = 2;
   1616 		strcpy(dip->un.v.units.name, AudioNvolume);
   1617 		return (0);
   1618 	case EAP_MIC_VOL:
   1619 		dip->type = AUDIO_MIXER_VALUE;
   1620 		dip->mixer_class = EAP_INPUT_CLASS;
   1621 		dip->prev = AUDIO_MIXER_LAST;
   1622 		dip->next = EAP_MIC_PREAMP;
   1623 		strcpy(dip->label.name, AudioNmicrophone);
   1624 		dip->un.v.num_channels = 1;
   1625 		strcpy(dip->un.v.units.name, AudioNvolume);
   1626 		return (0);
   1627 	case EAP_RECORD_SOURCE:
   1628 		dip->mixer_class = EAP_RECORD_CLASS;
   1629 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1630 		strcpy(dip->label.name, AudioNsource);
   1631 		dip->type = AUDIO_MIXER_SET;
   1632 		dip->un.s.num_mem = 6;
   1633 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1634 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1635 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1636 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1637 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1638 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1639 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1640 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1641 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1642 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1643 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1644 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1645 		return (0);
   1646 	case EAP_OUTPUT_SELECT:
   1647 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1648 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1649 		strcpy(dip->label.name, AudioNselect);
   1650 		dip->type = AUDIO_MIXER_SET;
   1651 		dip->un.s.num_mem = 6;
   1652 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1653 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1654 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1655 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1656 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1657 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1658 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1659 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1660 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1661 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1662 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1663 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1664 		return (0);
   1665 	case EAP_MIC_PREAMP:
   1666 		dip->type = AUDIO_MIXER_ENUM;
   1667 		dip->mixer_class = EAP_INPUT_CLASS;
   1668 		dip->prev = EAP_MIC_VOL;
   1669 		dip->next = AUDIO_MIXER_LAST;
   1670 		strcpy(dip->label.name, AudioNpreamp);
   1671 		dip->un.e.num_mem = 2;
   1672 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1673 		dip->un.e.member[0].ord = 0;
   1674 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1675 		dip->un.e.member[1].ord = 1;
   1676 		return (0);
   1677 	case EAP_OUTPUT_CLASS:
   1678 		dip->type = AUDIO_MIXER_CLASS;
   1679 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1680 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1681 		strcpy(dip->label.name, AudioCoutputs);
   1682 		return (0);
   1683 	case EAP_RECORD_CLASS:
   1684 		dip->type = AUDIO_MIXER_CLASS;
   1685 		dip->mixer_class = EAP_RECORD_CLASS;
   1686 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1687 		strcpy(dip->label.name, AudioCrecord);
   1688 		return (0);
   1689 	case EAP_INPUT_CLASS:
   1690 		dip->type = AUDIO_MIXER_CLASS;
   1691 		dip->mixer_class = EAP_INPUT_CLASS;
   1692 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1693 		strcpy(dip->label.name, AudioCinputs);
   1694 		return (0);
   1695 	}
   1696 	return (ENXIO);
   1697 }
   1698 
   1699 void *
   1700 eap_malloc(void *addr, int direction, size_t size, int pool, int flags)
   1701 {
   1702 	struct eap_instance *ei = addr;
   1703 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1704 	struct eap_dma *p;
   1705 	int error;
   1706 
   1707 	p = malloc(sizeof(*p), pool, flags);
   1708 	if (!p)
   1709 		return (0);
   1710 	error = eap_allocmem(sc, size, 16, p);
   1711 	if (error) {
   1712 		free(p, pool);
   1713 		return (0);
   1714 	}
   1715 	p->next = sc->sc_dmas;
   1716 	sc->sc_dmas = p;
   1717 	return (KERNADDR(p));
   1718 }
   1719 
   1720 void
   1721 eap_free(void *addr, void *ptr, int pool)
   1722 {
   1723 	struct eap_instance *ei = addr;
   1724 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1725 	struct eap_dma **pp, *p;
   1726 
   1727 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1728 		if (KERNADDR(p) == ptr) {
   1729 			eap_freemem(sc, p);
   1730 			*pp = p->next;
   1731 			free(p, pool);
   1732 			return;
   1733 		}
   1734 	}
   1735 }
   1736 
   1737 size_t
   1738 eap_round_buffersize(void *addr, int direction, size_t size)
   1739 {
   1740 
   1741 	return (size);
   1742 }
   1743 
   1744 paddr_t
   1745 eap_mappage(void *addr, void *mem, off_t off, int prot)
   1746 {
   1747 	struct eap_instance *ei = addr;
   1748 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1749 	struct eap_dma *p;
   1750 
   1751 	if (off < 0)
   1752 		return (-1);
   1753 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1754 		;
   1755 	if (!p)
   1756 		return (-1);
   1757 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1758 				off, prot, BUS_DMA_WAITOK));
   1759 }
   1760 
   1761 int
   1762 eap_get_props(void *addr)
   1763 {
   1764 
   1765 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1766 		AUDIO_PROP_FULLDUPLEX);
   1767 }
   1768 
   1769 #if NMIDI > 0
   1770 int
   1771 eap_midi_open(void *addr, int flags,
   1772 	      void (*iintr)(void *, int),
   1773 	      void (*ointr)(void *),
   1774 	      void *arg)
   1775 {
   1776 	struct eap_softc *sc = addr;
   1777 	u_int32_t uctrl;
   1778 
   1779 	sc->sc_iintr = iintr;
   1780 	sc->sc_ointr = ointr;
   1781 	sc->sc_arg = arg;
   1782 
   1783 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
   1784 	uctrl = 0;
   1785 	if (flags & FREAD)
   1786 		uctrl |= EAP_UC_RXINTEN;
   1787 #if 0
   1788 	/* I don't understand ../midi.c well enough to use output interrupts */
   1789 	if (flags & FWRITE)
   1790 		uctrl |= EAP_UC_TXINTEN; */
   1791 #endif
   1792 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1793 
   1794 	return (0);
   1795 }
   1796 
   1797 void
   1798 eap_midi_close(void *addr)
   1799 {
   1800 	struct eap_softc *sc = addr;
   1801 
   1802 	tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
   1803 	EWRITE1(sc, EAP_UART_CONTROL, 0);
   1804 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
   1805 
   1806 	sc->sc_iintr = 0;
   1807 	sc->sc_ointr = 0;
   1808 }
   1809 
   1810 int
   1811 eap_midi_output(void *addr, int d)
   1812 {
   1813 	struct eap_softc *sc = addr;
   1814 	int x;
   1815 
   1816 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
   1817 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
   1818 			EWRITE1(sc, EAP_UART_DATA, d);
   1819 			return (0);
   1820 		}
   1821 		delay(MIDI_BUSY_DELAY);
   1822 	}
   1823 	return (EIO);
   1824 }
   1825 
   1826 void
   1827 eap_midi_getinfo(void *addr, struct midi_info *mi)
   1828 {
   1829 	mi->name = "AudioPCI MIDI UART";
   1830 	mi->props = MIDI_PROP_CAN_INPUT;
   1831 }
   1832 
   1833 #endif
   1834