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