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