Home | History | Annotate | Line # | Download | only in pci
eap.c revision 1.54
      1 /*	$NetBSD: eap.c,v 1.54 2002/11/04 16:34:25 pooka 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.54 2002/11/04 16:34:25 pooka 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,
   1183 			 DAC2_SIZE=0x%x\n",
   1184 			 (int)DMAADDR(p),
   1185 			 (int)EAP_SET_SIZE(0,
   1186 			 (((char *)end - (char *)start) >> 2) - 1)));
   1187 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1188 		EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
   1189 		EWRITE4(sc, EAP_DAC2_SIZE,
   1190 			EAP_SET_SIZE(0,
   1191 			((char *)end - (char *)start) >> 2) - 1);
   1192 		EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
   1193 	} else if (ei->index == EAP_DAC1) {
   1194 		DPRINTF(("eap_trigger_output: DAC1_ADDR=0x%x,
   1195 			 DAC1_SIZE=0x%x\n",
   1196 			 (int)DMAADDR(p),
   1197 			 (int)EAP_SET_SIZE(0,
   1198 			 (((char *)end - (char *)start) >> 2) - 1)));
   1199 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1200 		EWRITE4(sc, EAP_DAC1_ADDR, DMAADDR(p));
   1201 		EWRITE4(sc, EAP_DAC1_SIZE,
   1202 			EAP_SET_SIZE(0,
   1203 			((char *)end - (char *)start) >> 2) - 1);
   1204 		EWRITE4(sc, EAP_DAC1_CSR, (blksize >> sampshift) - 1);
   1205 	}
   1206 #ifdef DIAGNOSTIC
   1207 	else
   1208 		panic("eap_trigger_output: impossible instance %d", ei->index);
   1209 #endif
   1210 
   1211 	if (sc->sc_1371)
   1212 		EWRITE4(sc, E1371_SRC, 0);
   1213 
   1214 	icsc = EREAD4(sc, EAP_ICSC);
   1215 	icsc |= EAP_EN(ei->index);
   1216 	EWRITE4(sc, EAP_ICSC, icsc);
   1217 
   1218 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
   1219 
   1220 	return (0);
   1221 }
   1222 
   1223 int
   1224 eap_trigger_input(
   1225 	void *addr,
   1226 	void *start,
   1227 	void *end,
   1228 	int blksize,
   1229 	void (*intr)(void *),
   1230 	void *arg,
   1231 	struct audio_params *param)
   1232 {
   1233 	struct eap_instance *ei = addr;
   1234 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1235 	struct eap_dma *p;
   1236 	u_int32_t icsc, sic;
   1237 	int sampshift;
   1238 
   1239 #ifdef DIAGNOSTIC
   1240 	if (sc->sc_rrun)
   1241 		panic("eap_trigger_input: already running");
   1242 	sc->sc_rrun = 1;
   1243 #endif
   1244 
   1245 	DPRINTFN(1, ("eap_trigger_input: ei=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1246 	    addr, start, end, blksize, intr, arg));
   1247 	sc->sc_rintr = intr;
   1248 	sc->sc_rarg = arg;
   1249 
   1250 	sic = EREAD4(sc, EAP_SIC);
   1251 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
   1252 	sampshift = 0;
   1253 	if (param->precision * param->factor == 16) {
   1254 		sic |= EAP_R1_S_EB;
   1255 		sampshift++;
   1256 	}
   1257 	if (param->channels == 2) {
   1258 		sic |= EAP_R1_S_MB;
   1259 		sampshift++;
   1260 	}
   1261 	EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
   1262 	EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
   1263 
   1264 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1265 		;
   1266 	if (!p) {
   1267 		printf("eap_trigger_input: bad addr %p\n", start);
   1268 		return (EINVAL);
   1269 	}
   1270 
   1271 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
   1272 		 (int)DMAADDR(p),
   1273 		 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
   1274 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
   1275 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
   1276 	EWRITE4(sc, EAP_ADC_SIZE,
   1277 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
   1278 
   1279 	EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
   1280 
   1281 	if (sc->sc_1371)
   1282 		EWRITE4(sc, E1371_SRC, 0);
   1283 
   1284 	icsc = EREAD4(sc, EAP_ICSC);
   1285 	icsc |= EAP_ADC_EN;
   1286 	EWRITE4(sc, EAP_ICSC, icsc);
   1287 
   1288 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
   1289 
   1290 	return (0);
   1291 }
   1292 
   1293 int
   1294 eap_halt_output(void *addr)
   1295 {
   1296 	struct eap_instance *ei = addr;
   1297 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1298 	u_int32_t icsc;
   1299 
   1300 	DPRINTF(("eap: eap_halt_output\n"));
   1301 	icsc = EREAD4(sc, EAP_ICSC);
   1302 	EWRITE4(sc, EAP_ICSC, icsc & ~(EAP_DAC2_EN << ei->index));
   1303 #ifdef DIAGNOSTIC
   1304 	ei->ei_prun = 0;
   1305 #endif
   1306 
   1307 	return (0);
   1308 }
   1309 
   1310 int
   1311 eap_halt_input(void *addr)
   1312 {
   1313 	struct eap_instance *ei = addr;
   1314 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1315 	u_int32_t icsc;
   1316 
   1317 #define EAP_USE_FMDAC_ALSO
   1318 	DPRINTF(("eap: eap_halt_input\n"));
   1319 	icsc = EREAD4(sc, EAP_ICSC);
   1320 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
   1321 #ifdef DIAGNOSTIC
   1322 	sc->sc_rrun = 0;
   1323 #endif
   1324 	return (0);
   1325 }
   1326 
   1327 int
   1328 eap_getdev(void *addr, struct audio_device *retp)
   1329 {
   1330 	*retp = eap_device;
   1331 	return (0);
   1332 }
   1333 
   1334 int
   1335 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1336 {
   1337 	struct eap_instance *ei = addr;
   1338 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1339 
   1340 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
   1341 }
   1342 
   1343 int
   1344 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1345 {
   1346 	struct eap_instance *ei = addr;
   1347 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1348 
   1349 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
   1350 }
   1351 
   1352 int
   1353 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1354 {
   1355 	struct eap_instance *ei = addr;
   1356 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1357 
   1358 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
   1359 }
   1360 
   1361 int
   1362 eap1371_get_portnum_by_name(struct eap_softc *sc,
   1363 			    char *class, char *device, char *qualifier)
   1364 {
   1365 	return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
   1366 	     device, qualifier));
   1367 }
   1368 
   1369 void
   1370 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
   1371 {
   1372 	eap1370_write_codec(sc, a, d);
   1373 
   1374 	sc->sc_port[a] = d;
   1375 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
   1376 }
   1377 
   1378 int
   1379 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1380 {
   1381 	struct eap_instance *ei = addr;
   1382 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1383 	int lval, rval, l, r, la, ra;
   1384 	int l1, r1, l2, r2, m, o1, o2;
   1385 
   1386 	if (cp->dev == EAP_RECORD_SOURCE) {
   1387 		if (cp->type != AUDIO_MIXER_SET)
   1388 			return (EINVAL);
   1389 		m = sc->sc_record_source = cp->un.mask;
   1390 		l1 = l2 = r1 = r2 = 0;
   1391 		if (m & (1 << EAP_VOICE_VOL))
   1392 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
   1393 		if (m & (1 << EAP_FM_VOL))
   1394 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
   1395 		if (m & (1 << EAP_CD_VOL))
   1396 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
   1397 		if (m & (1 << EAP_LINE_VOL))
   1398 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
   1399 		if (m & (1 << EAP_AUX_VOL))
   1400 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
   1401 		if (m & (1 << EAP_MIC_VOL))
   1402 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
   1403 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
   1404 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
   1405 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
   1406 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
   1407 		return (0);
   1408 	}
   1409 	if (cp->dev == EAP_OUTPUT_SELECT) {
   1410 		if (cp->type != AUDIO_MIXER_SET)
   1411 			return (EINVAL);
   1412 		m = sc->sc_output_source = cp->un.mask;
   1413 		o1 = o2 = 0;
   1414 		if (m & (1 << EAP_VOICE_VOL))
   1415 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
   1416 		if (m & (1 << EAP_FM_VOL))
   1417 			o1 |= AK_M_FM_L | AK_M_FM_R;
   1418 		if (m & (1 << EAP_CD_VOL))
   1419 			o1 |= AK_M_CD_L | AK_M_CD_R;
   1420 		if (m & (1 << EAP_LINE_VOL))
   1421 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
   1422 		if (m & (1 << EAP_AUX_VOL))
   1423 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
   1424 		if (m & (1 << EAP_MIC_VOL))
   1425 			o1 |= AK_M_MIC;
   1426 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
   1427 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
   1428 		return (0);
   1429 	}
   1430 	if (cp->dev == EAP_MIC_PREAMP) {
   1431 		if (cp->type != AUDIO_MIXER_ENUM)
   1432 			return (EINVAL);
   1433 		if (cp->un.ord != 0 && cp->un.ord != 1)
   1434 			return (EINVAL);
   1435 		sc->sc_mic_preamp = cp->un.ord;
   1436 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
   1437 		return (0);
   1438 	}
   1439 	if (cp->type != AUDIO_MIXER_VALUE)
   1440 		return (EINVAL);
   1441 	if (cp->un.value.num_channels == 1)
   1442 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1443 	else if (cp->un.value.num_channels == 2) {
   1444 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1445 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1446 	} else
   1447 		return (EINVAL);
   1448 	ra = -1;
   1449 	switch (cp->dev) {
   1450 	case EAP_MASTER_VOL:
   1451 		l = VOL_TO_ATT5(lval);
   1452 		r = VOL_TO_ATT5(rval);
   1453 		la = AK_MASTER_L;
   1454 		ra = AK_MASTER_R;
   1455 		break;
   1456 	case EAP_MIC_VOL:
   1457 		if (cp->un.value.num_channels != 1)
   1458 			return (EINVAL);
   1459 		la = AK_MIC;
   1460 		goto lr;
   1461 	case EAP_VOICE_VOL:
   1462 		la = AK_VOICE_L;
   1463 		ra = AK_VOICE_R;
   1464 		goto lr;
   1465 	case EAP_FM_VOL:
   1466 		la = AK_FM_L;
   1467 		ra = AK_FM_R;
   1468 		goto lr;
   1469 	case EAP_CD_VOL:
   1470 		la = AK_CD_L;
   1471 		ra = AK_CD_R;
   1472 		goto lr;
   1473 	case EAP_LINE_VOL:
   1474 		la = AK_LINE_L;
   1475 		ra = AK_LINE_R;
   1476 		goto lr;
   1477 	case EAP_AUX_VOL:
   1478 		la = AK_AUX_L;
   1479 		ra = AK_AUX_R;
   1480 	lr:
   1481 		l = VOL_TO_GAIN5(lval);
   1482 		r = VOL_TO_GAIN5(rval);
   1483 		break;
   1484 	default:
   1485 		return (EINVAL);
   1486 	}
   1487 	eap1370_set_mixer(sc, la, l);
   1488 	if (ra >= 0) {
   1489 		eap1370_set_mixer(sc, ra, r);
   1490 	}
   1491 	return (0);
   1492 }
   1493 
   1494 int
   1495 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1496 {
   1497 	struct eap_instance *ei = addr;
   1498 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1499 	int la, ra, l, r;
   1500 
   1501 	switch (cp->dev) {
   1502 	case EAP_RECORD_SOURCE:
   1503 		if (cp->type != AUDIO_MIXER_SET)
   1504 			return (EINVAL);
   1505 		cp->un.mask = sc->sc_record_source;
   1506 		return (0);
   1507 	case EAP_OUTPUT_SELECT:
   1508 		if (cp->type != AUDIO_MIXER_SET)
   1509 			return (EINVAL);
   1510 		cp->un.mask = sc->sc_output_source;
   1511 		return (0);
   1512 	case EAP_MIC_PREAMP:
   1513 		if (cp->type != AUDIO_MIXER_ENUM)
   1514 			return (EINVAL);
   1515 		cp->un.ord = sc->sc_mic_preamp;
   1516 		return (0);
   1517 	case EAP_MASTER_VOL:
   1518 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
   1519 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
   1520 		break;
   1521 	case EAP_MIC_VOL:
   1522 		if (cp->un.value.num_channels != 1)
   1523 			return (EINVAL);
   1524 		la = ra = AK_MIC;
   1525 		goto lr;
   1526 	case EAP_VOICE_VOL:
   1527 		la = AK_VOICE_L;
   1528 		ra = AK_VOICE_R;
   1529 		goto lr;
   1530 	case EAP_FM_VOL:
   1531 		la = AK_FM_L;
   1532 		ra = AK_FM_R;
   1533 		goto lr;
   1534 	case EAP_CD_VOL:
   1535 		la = AK_CD_L;
   1536 		ra = AK_CD_R;
   1537 		goto lr;
   1538 	case EAP_LINE_VOL:
   1539 		la = AK_LINE_L;
   1540 		ra = AK_LINE_R;
   1541 		goto lr;
   1542 	case EAP_AUX_VOL:
   1543 		la = AK_AUX_L;
   1544 		ra = AK_AUX_R;
   1545 	lr:
   1546 		l = GAIN5_TO_VOL(sc->sc_port[la]);
   1547 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
   1548 		break;
   1549 	default:
   1550 		return (EINVAL);
   1551 	}
   1552 	if (cp->un.value.num_channels == 1)
   1553 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
   1554 	else if (cp->un.value.num_channels == 2) {
   1555 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
   1556 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1557 	} else
   1558 		return (EINVAL);
   1559 	return (0);
   1560 }
   1561 
   1562 int
   1563 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1564 {
   1565 
   1566 	switch (dip->index) {
   1567 	case EAP_MASTER_VOL:
   1568 		dip->type = AUDIO_MIXER_VALUE;
   1569 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1570 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1571 		strcpy(dip->label.name, AudioNmaster);
   1572 		dip->un.v.num_channels = 2;
   1573 		strcpy(dip->un.v.units.name, AudioNvolume);
   1574 		return (0);
   1575 	case EAP_VOICE_VOL:
   1576 		dip->type = AUDIO_MIXER_VALUE;
   1577 		dip->mixer_class = EAP_INPUT_CLASS;
   1578 		dip->prev = AUDIO_MIXER_LAST;
   1579 		dip->next = AUDIO_MIXER_LAST;
   1580 		strcpy(dip->label.name, AudioNdac);
   1581 		dip->un.v.num_channels = 2;
   1582 		strcpy(dip->un.v.units.name, AudioNvolume);
   1583 		return (0);
   1584 	case EAP_FM_VOL:
   1585 		dip->type = AUDIO_MIXER_VALUE;
   1586 		dip->mixer_class = EAP_INPUT_CLASS;
   1587 		dip->prev = AUDIO_MIXER_LAST;
   1588 		dip->next = AUDIO_MIXER_LAST;
   1589 		strcpy(dip->label.name, AudioNfmsynth);
   1590 		dip->un.v.num_channels = 2;
   1591 		strcpy(dip->un.v.units.name, AudioNvolume);
   1592 		return (0);
   1593 	case EAP_CD_VOL:
   1594 		dip->type = AUDIO_MIXER_VALUE;
   1595 		dip->mixer_class = EAP_INPUT_CLASS;
   1596 		dip->prev = AUDIO_MIXER_LAST;
   1597 		dip->next = AUDIO_MIXER_LAST;
   1598 		strcpy(dip->label.name, AudioNcd);
   1599 		dip->un.v.num_channels = 2;
   1600 		strcpy(dip->un.v.units.name, AudioNvolume);
   1601 		return (0);
   1602 	case EAP_LINE_VOL:
   1603 		dip->type = AUDIO_MIXER_VALUE;
   1604 		dip->mixer_class = EAP_INPUT_CLASS;
   1605 		dip->prev = AUDIO_MIXER_LAST;
   1606 		dip->next = AUDIO_MIXER_LAST;
   1607 		strcpy(dip->label.name, AudioNline);
   1608 		dip->un.v.num_channels = 2;
   1609 		strcpy(dip->un.v.units.name, AudioNvolume);
   1610 		return (0);
   1611 	case EAP_AUX_VOL:
   1612 		dip->type = AUDIO_MIXER_VALUE;
   1613 		dip->mixer_class = EAP_INPUT_CLASS;
   1614 		dip->prev = AUDIO_MIXER_LAST;
   1615 		dip->next = AUDIO_MIXER_LAST;
   1616 		strcpy(dip->label.name, AudioNaux);
   1617 		dip->un.v.num_channels = 2;
   1618 		strcpy(dip->un.v.units.name, AudioNvolume);
   1619 		return (0);
   1620 	case EAP_MIC_VOL:
   1621 		dip->type = AUDIO_MIXER_VALUE;
   1622 		dip->mixer_class = EAP_INPUT_CLASS;
   1623 		dip->prev = AUDIO_MIXER_LAST;
   1624 		dip->next = EAP_MIC_PREAMP;
   1625 		strcpy(dip->label.name, AudioNmicrophone);
   1626 		dip->un.v.num_channels = 1;
   1627 		strcpy(dip->un.v.units.name, AudioNvolume);
   1628 		return (0);
   1629 	case EAP_RECORD_SOURCE:
   1630 		dip->mixer_class = EAP_RECORD_CLASS;
   1631 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1632 		strcpy(dip->label.name, AudioNsource);
   1633 		dip->type = AUDIO_MIXER_SET;
   1634 		dip->un.s.num_mem = 6;
   1635 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1636 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1637 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1638 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1639 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1640 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1641 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1642 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1643 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1644 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1645 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1646 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1647 		return (0);
   1648 	case EAP_OUTPUT_SELECT:
   1649 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1650 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1651 		strcpy(dip->label.name, AudioNselect);
   1652 		dip->type = AUDIO_MIXER_SET;
   1653 		dip->un.s.num_mem = 6;
   1654 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1655 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1656 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1657 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1658 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1659 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1660 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1661 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1662 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1663 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1664 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1665 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1666 		return (0);
   1667 	case EAP_MIC_PREAMP:
   1668 		dip->type = AUDIO_MIXER_ENUM;
   1669 		dip->mixer_class = EAP_INPUT_CLASS;
   1670 		dip->prev = EAP_MIC_VOL;
   1671 		dip->next = AUDIO_MIXER_LAST;
   1672 		strcpy(dip->label.name, AudioNpreamp);
   1673 		dip->un.e.num_mem = 2;
   1674 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1675 		dip->un.e.member[0].ord = 0;
   1676 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1677 		dip->un.e.member[1].ord = 1;
   1678 		return (0);
   1679 	case EAP_OUTPUT_CLASS:
   1680 		dip->type = AUDIO_MIXER_CLASS;
   1681 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1682 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1683 		strcpy(dip->label.name, AudioCoutputs);
   1684 		return (0);
   1685 	case EAP_RECORD_CLASS:
   1686 		dip->type = AUDIO_MIXER_CLASS;
   1687 		dip->mixer_class = EAP_RECORD_CLASS;
   1688 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1689 		strcpy(dip->label.name, AudioCrecord);
   1690 		return (0);
   1691 	case EAP_INPUT_CLASS:
   1692 		dip->type = AUDIO_MIXER_CLASS;
   1693 		dip->mixer_class = EAP_INPUT_CLASS;
   1694 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1695 		strcpy(dip->label.name, AudioCinputs);
   1696 		return (0);
   1697 	}
   1698 	return (ENXIO);
   1699 }
   1700 
   1701 void *
   1702 eap_malloc(void *addr, int direction, size_t size, int pool, int flags)
   1703 {
   1704 	struct eap_instance *ei = addr;
   1705 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1706 	struct eap_dma *p;
   1707 	int error;
   1708 
   1709 	p = malloc(sizeof(*p), pool, flags);
   1710 	if (!p)
   1711 		return (0);
   1712 	error = eap_allocmem(sc, size, 16, p);
   1713 	if (error) {
   1714 		free(p, pool);
   1715 		return (0);
   1716 	}
   1717 	p->next = sc->sc_dmas;
   1718 	sc->sc_dmas = p;
   1719 	return (KERNADDR(p));
   1720 }
   1721 
   1722 void
   1723 eap_free(void *addr, void *ptr, int pool)
   1724 {
   1725 	struct eap_instance *ei = addr;
   1726 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1727 	struct eap_dma **pp, *p;
   1728 
   1729 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1730 		if (KERNADDR(p) == ptr) {
   1731 			eap_freemem(sc, p);
   1732 			*pp = p->next;
   1733 			free(p, pool);
   1734 			return;
   1735 		}
   1736 	}
   1737 }
   1738 
   1739 size_t
   1740 eap_round_buffersize(void *addr, int direction, size_t size)
   1741 {
   1742 
   1743 	return (size);
   1744 }
   1745 
   1746 paddr_t
   1747 eap_mappage(void *addr, void *mem, off_t off, int prot)
   1748 {
   1749 	struct eap_instance *ei = addr;
   1750 	struct eap_softc *sc = (struct eap_softc *)ei->parent;
   1751 	struct eap_dma *p;
   1752 
   1753 	if (off < 0)
   1754 		return (-1);
   1755 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1756 		;
   1757 	if (!p)
   1758 		return (-1);
   1759 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1760 				off, prot, BUS_DMA_WAITOK));
   1761 }
   1762 
   1763 int
   1764 eap_get_props(void *addr)
   1765 {
   1766 
   1767 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1768 		AUDIO_PROP_FULLDUPLEX);
   1769 }
   1770 
   1771 #if NMIDI > 0
   1772 int
   1773 eap_midi_open(void *addr, int flags,
   1774 	      void (*iintr)(void *, int),
   1775 	      void (*ointr)(void *),
   1776 	      void *arg)
   1777 {
   1778 	struct eap_softc *sc = addr;
   1779 	u_int32_t uctrl;
   1780 
   1781 	sc->sc_iintr = iintr;
   1782 	sc->sc_ointr = ointr;
   1783 	sc->sc_arg = arg;
   1784 
   1785 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
   1786 	uctrl = 0;
   1787 	if (flags & FREAD)
   1788 		uctrl |= EAP_UC_RXINTEN;
   1789 #if 0
   1790 	/* I don't understand ../midi.c well enough to use output interrupts */
   1791 	if (flags & FWRITE)
   1792 		uctrl |= EAP_UC_TXINTEN; */
   1793 #endif
   1794 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1795 
   1796 	return (0);
   1797 }
   1798 
   1799 void
   1800 eap_midi_close(void *addr)
   1801 {
   1802 	struct eap_softc *sc = addr;
   1803 
   1804 	tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
   1805 	EWRITE1(sc, EAP_UART_CONTROL, 0);
   1806 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
   1807 
   1808 	sc->sc_iintr = 0;
   1809 	sc->sc_ointr = 0;
   1810 }
   1811 
   1812 int
   1813 eap_midi_output(void *addr, int d)
   1814 {
   1815 	struct eap_softc *sc = addr;
   1816 	int x;
   1817 
   1818 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
   1819 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
   1820 			EWRITE1(sc, EAP_UART_DATA, d);
   1821 			return (0);
   1822 		}
   1823 		delay(MIDI_BUSY_DELAY);
   1824 	}
   1825 	return (EIO);
   1826 }
   1827 
   1828 void
   1829 eap_midi_getinfo(void *addr, struct midi_info *mi)
   1830 {
   1831 	mi->name = "AudioPCI MIDI UART";
   1832 	mi->props = MIDI_PROP_CAN_INPUT;
   1833 }
   1834 
   1835 #endif
   1836