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