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