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