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