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