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