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