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