Home | History | Annotate | Line # | Download | only in pci
eap.c revision 1.81.8.3
      1 /*	$NetBSD: eap.c,v 1.81.8.3 2006/09/03 15:24:21 yamt 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.81.8.3 2006/09/03 15:24:21 yamt 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 <machine/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(struct device *, struct cfdata *, void *);
    104 static void	eap_attach(struct device *, struct device *, void *);
    105 static int	eap_detach(struct device *, int);
    106 static int	eap_intr(void *);
    107 
    108 struct eap_dma {
    109 	bus_dmamap_t map;
    110 	caddr_t 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 	struct device *parent;
    130 	int index;
    131 
    132 	void	(*ei_pintr)(void *);	/* DMA completion intr handler */
    133 	void	*ei_parg;		/* arg for ei_intr() */
    134 	struct device *ei_audiodev;		/* audio device, for detach */
    135 #ifdef DIAGNOSTIC
    136 	char	ei_prun;
    137 #endif
    138 };
    139 
    140 struct eap_softc {
    141 	struct device 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 	struct device *sc_mididev;
    161 #endif
    162 #if NJOY_EAP > 0
    163 	struct device *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(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(struct device *parent, struct cfdata *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 		printf("%s: eap1371_ready_codec timeout 1\n",
    392 		       sc->sc_dev.dv_xname);
    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 		printf("%s: eap1371_ready_codec timeout 2\n",
    406 		       sc->sc_dev.dv_xname);
    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 		printf("%s: eap1371_ready_codec timeout 3\n",
    416 		       sc->sc_dev.dv_xname);
    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 		printf("%s: eap1371_read_codec timeout 1\n",
    442 		       sc->sc_dev.dv_xname);
    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 		printf("%s: eap1371_read_codec timeout 2\n",
    451 		       sc->sc_dev.dv_xname);
    452 
    453 	*d = (uint16_t)t;
    454 
    455 	DPRINTFN(10, ("eap1371: reading codec (%x) = %x\n", a, *d));
    456 
    457 	return 0;
    458 }
    459 
    460 static int
    461 eap1371_write_codec(void *sc_, uint8_t a, uint16_t d)
    462 {
    463 	struct eap_softc *sc;
    464 
    465 	sc = sc_;
    466 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, d));
    467 
    468 	DPRINTFN(10, ("eap1371: writing codec %x --> %x\n", d, a));
    469 
    470 	return 0;
    471 }
    472 
    473 static uint32_t
    474 eap1371_src_wait(struct eap_softc *sc)
    475 {
    476 	int to;
    477 	u_int32_t src;
    478 
    479 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    480 		src = EREAD4(sc, E1371_SRC);
    481 		if (!(src & E1371_SRC_RBUSY))
    482 			return src;
    483 		delay(1);
    484 	}
    485 	printf("%s: eap1371_src_wait timeout\n", sc->sc_dev.dv_xname);
    486 	return src;
    487 }
    488 
    489 static int
    490 eap1371_src_read(struct eap_softc *sc, int a)
    491 {
    492 	int to;
    493 	uint32_t src, t;
    494 
    495 	src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    496 	src |= E1371_SRC_ADDR(a);
    497 	EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
    498 
    499 	t = eap1371_src_wait(sc);
    500 	if ((t & E1371_SRC_STATE_MASK) != E1371_SRC_STATE_OK) {
    501 		for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    502 			t = EREAD4(sc, E1371_SRC);
    503 			if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
    504 				break;
    505 			delay(1);
    506 		}
    507 	}
    508 
    509 	EWRITE4(sc, E1371_SRC, src);
    510 
    511 	return t & E1371_SRC_DATAMASK;
    512 }
    513 
    514 static void
    515 eap1371_src_write(struct eap_softc *sc, int a, int d)
    516 {
    517 	uint32_t r;
    518 
    519 	r = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    520 	r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
    521 	EWRITE4(sc, E1371_SRC, r);
    522 }
    523 
    524 static void
    525 eap1371_set_adc_rate(struct eap_softc *sc, int rate)
    526 {
    527 	int freq, n, truncm;
    528 	int out;
    529 	int s;
    530 
    531 	/* Whatever, it works, so I'll leave it :) */
    532 
    533 	if (rate > 48000)
    534 		rate = 48000;
    535 	if (rate < 4000)
    536 		rate = 4000;
    537 	n = rate / 3000;
    538 	if ((1 << n) & SRC_MAGIC)
    539 		n--;
    540 	truncm = ((21 * n) - 1) | 1;
    541 	freq = ((48000 << 15) / rate) * n;
    542 	if (rate >= 24000) {
    543 		if (truncm > 239)
    544 			truncm = 239;
    545 		out = ESRC_SET_TRUNC((239 - truncm) / 2);
    546 	} else {
    547 		if (truncm > 119)
    548 			truncm = 119;
    549 		out = ESRC_SMF | ESRC_SET_TRUNC((119 - truncm) / 2);
    550 	}
    551 	out |= ESRC_SET_N(n);
    552 	s = splaudio();
    553 	eap1371_src_write(sc, ESRC_ADC+ESRC_TRUNC_N, out);
    554 
    555 	out = eap1371_src_read(sc, ESRC_ADC+ESRC_IREGS) & 0xff;
    556 	eap1371_src_write(sc, ESRC_ADC+ESRC_IREGS, out |
    557 			  ESRC_SET_VFI(freq >> 15));
    558 	eap1371_src_write(sc, ESRC_ADC+ESRC_VFF, freq & 0x7fff);
    559 	eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(n));
    560 	eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(n));
    561 	splx(s);
    562 }
    563 
    564 static void
    565 eap1371_set_dac_rate(struct eap_instance *ei, int rate)
    566 {
    567 	struct eap_softc *sc;
    568 	int dac;
    569 	int freq, r;
    570 	int s;
    571 
    572 	DPRINTFN(2, ("eap1371_set_dac_date: set rate for %d\n", ei->index));
    573 	sc = (struct eap_softc *)ei->parent;
    574 	dac = ei->index == EAP_DAC1 ? ESRC_DAC1 : ESRC_DAC2;
    575 
    576 	/* Whatever, it works, so I'll leave it :) */
    577 
    578 	if (rate > 48000)
    579 	    rate = 48000;
    580 	if (rate < 4000)
    581 	    rate = 4000;
    582 	freq = ((rate << 15) + 1500) / 3000;
    583 
    584 	s = splaudio();
    585 	eap1371_src_wait(sc);
    586 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    587 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    588 	r |= ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2;
    589 	EWRITE4(sc, E1371_SRC, r);
    590 	r = eap1371_src_read(sc, dac + ESRC_IREGS) & 0x00ff;
    591 	eap1371_src_write(sc, dac + ESRC_IREGS, r | ((freq >> 5) & 0xfc00));
    592 	eap1371_src_write(sc, dac + ESRC_VFF, freq & 0x7fff);
    593 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    594 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    595 	r &= ~(ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2);
    596 	EWRITE4(sc, E1371_SRC, r);
    597 	splx(s);
    598 }
    599 
    600 static void
    601 eap_attach(struct device *parent, struct device *self, void *aux)
    602 {
    603 	struct eap_softc *sc;
    604 	struct pci_attach_args *pa;
    605 	pci_chipset_tag_t pc;
    606 	const struct audio_hw_if *eap_hw_if;
    607 	char const *intrstr;
    608 	pci_intr_handle_t ih;
    609 	pcireg_t csr;
    610 	char devinfo[256];
    611 	mixer_ctrl_t ctl;
    612 	int i;
    613 	int revision, ct5880;
    614 	const char *revstr;
    615 #if NJOY_EAP > 0
    616 	struct eap_gameport_args gpargs;
    617 #endif
    618 
    619 	sc = (struct eap_softc *)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("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    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("%s: couldn't map interrupt\n",
    678 		    sc->sc_dev.dv_xname);
    679 		return;
    680 	}
    681 	intrstr = pci_intr_string(pc, ih);
    682 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
    683 	if (sc->sc_ih == NULL) {
    684 		aprint_error("%s: couldn't establish interrupt",
    685 		    sc->sc_dev.dv_xname);
    686 		if (intrstr != NULL)
    687 			aprint_normal(" at %s", intrstr);
    688 		aprint_normal("\n");
    689 		return;
    690 	}
    691 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    692 
    693 	sc->sc_ei[EAP_I1].parent = (struct device *)sc;
    694 	sc->sc_ei[EAP_I1].index = EAP_DAC2;
    695 	sc->sc_ei[EAP_I2].parent = (struct device *)sc;
    696 	sc->sc_ei[EAP_I2].index = EAP_DAC1;
    697 
    698 	if (!sc->sc_1371) {
    699 		/* Enable interrupts and looping mode. */
    700 		/* enable the parts we need */
    701 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    702 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
    703 
    704 		/* reset codec */
    705 		/* normal operation */
    706 		/* select codec clocks */
    707 		eap1370_write_codec(sc, AK_RESET, AK_PD);
    708 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
    709 		eap1370_write_codec(sc, AK_CS, 0x0);
    710 
    711 		eap_hw_if = &eap1370_hw_if;
    712 
    713 		/* Enable all relevant mixer switches. */
    714 		ctl.dev = EAP_INPUT_SOURCE;
    715 		ctl.type = AUDIO_MIXER_SET;
    716 		ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
    717 			1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
    718 			1 << EAP_MIC_VOL;
    719 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    720 
    721 		ctl.type = AUDIO_MIXER_VALUE;
    722 		ctl.un.value.num_channels = 1;
    723 		for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
    724 		     ctl.dev++) {
    725 			ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
    726 			eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    727 		}
    728 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
    729 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    730 		ctl.dev = EAP_MIC_PREAMP;
    731 		ctl.type = AUDIO_MIXER_ENUM;
    732 		ctl.un.ord = 0;
    733 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    734 		ctl.dev = EAP_RECORD_SOURCE;
    735 		ctl.type = AUDIO_MIXER_SET;
    736 		ctl.un.mask = 1 << EAP_MIC_VOL;
    737 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    738 	} else {
    739 		/* clean slate */
    740 
    741 		EWRITE4(sc, EAP_SIC, 0);
    742 		EWRITE4(sc, EAP_ICSC, 0);
    743 		EWRITE4(sc, E1371_LEGACY, 0);
    744 
    745 		if (ct5880) {
    746 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
    747 			/* Let codec wake up */
    748 			delay(20000);
    749 		}
    750 
    751 		/* Reset from es1371's perspective */
    752 		EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
    753 		delay(20);
    754 		EWRITE4(sc, EAP_ICSC, 0);
    755 
    756 		/*
    757 		 * Must properly reprogram sample rate converter,
    758 		 * or it locks up.  Set some defaults for the life of the
    759 		 * machine, and set up a sb default sample rate.
    760 		 */
    761 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
    762 		for (i = 0; i < 0x80; i++)
    763 			eap1371_src_write(sc, i, 0);
    764 		eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
    765 		eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
    766 		eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
    767 		eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
    768 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
    769 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
    770 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
    771 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
    772 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
    773 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
    774 		eap1371_set_adc_rate(sc, 22050);
    775 		eap1371_set_dac_rate(&sc->sc_ei[0], 22050);
    776 		eap1371_set_dac_rate(&sc->sc_ei[1], 22050);
    777 
    778 		EWRITE4(sc, E1371_SRC, 0);
    779 
    780 		/* Reset codec */
    781 
    782 		/* Interrupt enable */
    783 		sc->host_if.arg = sc;
    784 		sc->host_if.attach = eap1371_attach_codec;
    785 		sc->host_if.read = eap1371_read_codec;
    786 		sc->host_if.write = eap1371_write_codec;
    787 		sc->host_if.reset = eap1371_reset_codec;
    788 
    789 		if (ac97_attach(&sc->host_if, self) == 0) {
    790 			/* Interrupt enable */
    791 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    792 		} else
    793 			return;
    794 
    795 		eap_hw_if = &eap1371_hw_if;
    796 	}
    797 
    798 	sc->sc_ei[EAP_I1].ei_audiodev =
    799 	    audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I1], &sc->sc_dev);
    800 
    801 #ifdef EAP_USE_BOTH_DACS
    802 	aprint_normal("%s: attaching secondary DAC\n", sc->sc_dev.dv_xname);
    803 	sc->sc_ei[EAP_I2].ei_audiodev =
    804 	    audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I2], &sc->sc_dev);
    805 #endif
    806 
    807 #if NMIDI > 0
    808 	sc->sc_mididev = midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
    809 #endif
    810 
    811 #if NJOY_EAP > 0
    812 	if (sc->sc_1371) {
    813 		gpargs.gpa_iot = sc->iot;
    814 		gpargs.gpa_ioh = sc->ioh;
    815 		sc->sc_gameport = eap_joy_attach(&sc->sc_dev, &gpargs);
    816 	}
    817 #endif
    818 }
    819 
    820 static int
    821 eap_detach(struct device *self, int flags)
    822 {
    823 	struct eap_softc *sc;
    824 	int res;
    825 #if NJOY_EAP > 0
    826 	struct eap_gameport_args gpargs;
    827 
    828 	sc = (struct eap_softc *)self;
    829 	if (sc->sc_gameport) {
    830 		gpargs.gpa_iot = sc->iot;
    831 		gpargs.gpa_ioh = sc->ioh;
    832 		res = eap_joy_detach(sc->sc_gameport, &gpargs);
    833 		if (res)
    834 			return res;
    835 	}
    836 #else
    837 	sc = (struct eap_softc *)self;
    838 #endif
    839 #if NMIDI > 0
    840 	if (sc->sc_mididev != NULL) {
    841 		res = config_detach(sc->sc_mididev, 0);
    842 		if (res)
    843 			return res;
    844 	}
    845 #endif
    846 #ifdef EAP_USE_BOTH_DACS
    847 	if (sc->sc_ei[EAP_I2].ei_audiodev != NULL) {
    848 		res = config_detach(sc->sc_ei[EAP_I2].ei_audiodev, 0);
    849 		if (res)
    850 			return res;
    851 	}
    852 #endif
    853 	if (sc->sc_ei[EAP_I1].ei_audiodev != NULL) {
    854 		res = config_detach(sc->sc_ei[EAP_I1].ei_audiodev, 0);
    855 		if (res)
    856 			return res;
    857 	}
    858 
    859 	bus_space_unmap(sc->iot, sc->ioh, sc->iosz);
    860 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    861 
    862 	return 0;
    863 }
    864 
    865 static int
    866 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    867 {
    868 	struct eap_softc *sc;
    869 
    870 	sc = sc_;
    871 	sc->codec_if = codec_if;
    872 	return 0;
    873 }
    874 
    875 static int
    876 eap1371_reset_codec(void *sc_)
    877 {
    878 	struct eap_softc *sc;
    879 	uint32_t icsc;
    880 	int s;
    881 
    882 	sc = sc_;
    883 	s = splaudio();
    884 	icsc = EREAD4(sc, EAP_ICSC);
    885 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
    886 	delay(20);
    887 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
    888 	delay(1);
    889 	splx(s);
    890 
    891 	return 0;
    892 }
    893 
    894 static int
    895 eap_intr(void *p)
    896 {
    897 	struct eap_softc *sc;
    898 	uint32_t intr, sic;
    899 
    900 	sc = p;
    901 	intr = EREAD4(sc, EAP_ICSS);
    902 	if (!(intr & EAP_INTR))
    903 		return 0;
    904 	sic = EREAD4(sc, EAP_SIC);
    905 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
    906 	if (intr & EAP_I_ADC) {
    907 #if 0
    908 		/*
    909 		 * XXX This is a hack!
    910 		 * The EAP chip sometimes generates the recording interrupt
    911 		 * while it is still transferring the data.  To make sure
    912 		 * it has all arrived we busy wait until the count is right.
    913 		 * The transfer we are waiting for is 8 longwords.
    914 		 */
    915 		int s, nw, n;
    916 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
    917 		s = EREAD4(sc, EAP_ADC_CSR);
    918 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
    919 		n = 0;
    920 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
    921 			delay(10);
    922 			if (++n > 100) {
    923 				printf("eapintr: DMA fix timeout");
    924 				break;
    925 			}
    926 		}
    927 		/* Continue with normal interrupt handling. */
    928 #endif
    929 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
    930 		EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
    931 		if (sc->sc_rintr)
    932 			sc->sc_rintr(sc->sc_rarg);
    933 	}
    934 
    935 	if (intr & EAP_I_DAC2) {
    936 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
    937 		EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
    938 		if (sc->sc_ei[EAP_DAC2].ei_pintr)
    939 			sc->sc_ei[EAP_DAC2].ei_pintr(sc->sc_ei[EAP_DAC2].ei_parg);
    940 	}
    941 
    942 	if (intr & EAP_I_DAC1) {
    943 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P1_INTR_EN);
    944 		EWRITE4(sc, EAP_SIC, sic | EAP_P1_INTR_EN);
    945 		if (sc->sc_ei[EAP_DAC1].ei_pintr)
    946 			sc->sc_ei[EAP_DAC1].ei_pintr(sc->sc_ei[EAP_DAC1].ei_parg);
    947 	}
    948 
    949 	if (intr & EAP_I_MCCB)
    950 		panic("eap_intr: unexpected MCCB interrupt");
    951 #if NMIDI > 0
    952 	if (intr & EAP_I_UART) {
    953 		uint8_t ustat;
    954 		uint32_t data;
    955 
    956 		ustat = EREAD1(sc, EAP_UART_STATUS);
    957 
    958 		if (ustat & EAP_US_RXINT) {
    959 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
    960 				data = EREAD1(sc, EAP_UART_DATA);
    961 				sc->sc_iintr(sc->sc_arg, data);
    962 			}
    963 		}
    964 
    965 		if (ustat & EAP_US_TXINT)
    966 			eap_uart_txrdy(sc);
    967 	}
    968 #endif
    969 	return 1;
    970 }
    971 
    972 static int
    973 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
    974 {
    975 	int error;
    976 
    977 	p->size = size;
    978 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    979 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    980 				 &p->nsegs, BUS_DMA_NOWAIT);
    981 	if (error)
    982 		return error;
    983 
    984 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    985 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    986 	if (error)
    987 		goto free;
    988 
    989 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    990 				  0, BUS_DMA_NOWAIT, &p->map);
    991 	if (error)
    992 		goto unmap;
    993 
    994 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    995 				BUS_DMA_NOWAIT);
    996 	if (error)
    997 		goto destroy;
    998 	return (0);
    999 
   1000 destroy:
   1001 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1002 unmap:
   1003 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1004 free:
   1005 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1006 	return error;
   1007 }
   1008 
   1009 static int
   1010 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
   1011 {
   1012 
   1013 	bus_dmamap_unload(sc->sc_dmatag, p->map);
   1014 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1015 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1016 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1017 	return 0;
   1018 }
   1019 
   1020 static int
   1021 eap_open(void *addr, int flags)
   1022 {
   1023 	struct eap_instance *ei;
   1024 
   1025 	ei = addr;
   1026 	/* there is only one ADC */
   1027 	if (ei->index == EAP_I2 && flags & FREAD)
   1028 		return EOPNOTSUPP;
   1029 
   1030 	return 0;
   1031 }
   1032 
   1033 static int
   1034 eap_query_encoding(void *addr, struct audio_encoding *fp)
   1035 {
   1036 
   1037 	switch (fp->index) {
   1038 	case 0:
   1039 		strcpy(fp->name, AudioEulinear);
   1040 		fp->encoding = AUDIO_ENCODING_ULINEAR;
   1041 		fp->precision = 8;
   1042 		fp->flags = 0;
   1043 		return 0;
   1044 	case 1:
   1045 		strcpy(fp->name, AudioEmulaw);
   1046 		fp->encoding = AUDIO_ENCODING_ULAW;
   1047 		fp->precision = 8;
   1048 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1049 		return 0;
   1050 	case 2:
   1051 		strcpy(fp->name, AudioEalaw);
   1052 		fp->encoding = AUDIO_ENCODING_ALAW;
   1053 		fp->precision = 8;
   1054 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1055 		return 0;
   1056 	case 3:
   1057 		strcpy(fp->name, AudioEslinear);
   1058 		fp->encoding = AUDIO_ENCODING_SLINEAR;
   1059 		fp->precision = 8;
   1060 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1061 		return 0;
   1062 	case 4:
   1063 		strcpy(fp->name, AudioEslinear_le);
   1064 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
   1065 		fp->precision = 16;
   1066 		fp->flags = 0;
   1067 		return 0;
   1068 	case 5:
   1069 		strcpy(fp->name, AudioEulinear_le);
   1070 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
   1071 		fp->precision = 16;
   1072 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1073 		return 0;
   1074 	case 6:
   1075 		strcpy(fp->name, AudioEslinear_be);
   1076 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
   1077 		fp->precision = 16;
   1078 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1079 		return 0;
   1080 	case 7:
   1081 		strcpy(fp->name, AudioEulinear_be);
   1082 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
   1083 		fp->precision = 16;
   1084 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1085 		return 0;
   1086 	default:
   1087 		return EINVAL;
   1088 	}
   1089 }
   1090 
   1091 static int
   1092 eap_set_params(void *addr, int setmode, int usemode,
   1093 	       audio_params_t *play, audio_params_t *rec,
   1094 	       stream_filter_list_t *pfil, stream_filter_list_t *rfil)
   1095 {
   1096 	struct eap_instance *ei;
   1097 	struct eap_softc *sc;
   1098 	struct audio_params *p;
   1099 	stream_filter_list_t *fil;
   1100 	int mode, i;
   1101 	uint32_t div;
   1102 
   1103 	ei = addr;
   1104 	sc = (struct eap_softc *)ei->parent;
   1105 	/*
   1106 	 * The es1370 only has one clock, so make the sample rates match.
   1107 	 * This only applies for ADC/DAC2. The FM DAC is handled below.
   1108 	 */
   1109 	if (!sc->sc_1371 && ei->index == EAP_DAC2) {
   1110 		if (play->sample_rate != rec->sample_rate &&
   1111 		    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1112 			if (setmode == AUMODE_PLAY) {
   1113 				rec->sample_rate = play->sample_rate;
   1114 				setmode |= AUMODE_RECORD;
   1115 			} else if (setmode == AUMODE_RECORD) {
   1116 				play->sample_rate = rec->sample_rate;
   1117 				setmode |= AUMODE_PLAY;
   1118 			} else
   1119 				return EINVAL;
   1120 		}
   1121 	}
   1122 
   1123 	for (mode = AUMODE_RECORD; mode != -1;
   1124 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1125 		if ((setmode & mode) == 0)
   1126 			continue;
   1127 
   1128 		p = mode == AUMODE_PLAY ? play : rec;
   1129 
   1130 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
   1131 		    (p->precision != 8 && p->precision != 16) ||
   1132 		    (p->channels != 1 && p->channels != 2))
   1133 			return EINVAL;
   1134 
   1135 		fil = mode == AUMODE_PLAY ? pfil : rfil;
   1136 		i = auconv_set_converter(eap_formats, EAP_NFORMATS,
   1137 					 mode, p, FALSE, fil);
   1138 		if (i < 0)
   1139 			return EINVAL;
   1140 	}
   1141 
   1142 	if (sc->sc_1371) {
   1143 		eap1371_set_dac_rate(ei, play->sample_rate);
   1144 		eap1371_set_adc_rate(sc, rec->sample_rate);
   1145 	} else if (ei->index == EAP_DAC2) {
   1146 		/* Set the speed */
   1147 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
   1148 			     EREAD4(sc, EAP_ICSC)));
   1149 		div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
   1150 		/*
   1151 		 * XXX
   1152 		 * The -2 isn't documented, but seemed to make the wall
   1153 		 * time match
   1154 		 * what I expect.  - mycroft
   1155 		 */
   1156 		if (usemode == AUMODE_RECORD)
   1157 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1158 				rec->sample_rate - 2);
   1159 		else
   1160 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1161 				play->sample_rate - 2);
   1162 #if 0
   1163 		div |= EAP_CCB_INTRM;
   1164 #else
   1165 		/*
   1166 		 * It is not obvious how to acknowledge MCCB interrupts, so
   1167 		 * we had better not enable them.
   1168 		 */
   1169 #endif
   1170 		EWRITE4(sc, EAP_ICSC, div);
   1171 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1172 	} else {
   1173 		/*
   1174 		 * The FM DAC has only a few fixed-frequency choises, so
   1175 		 * pick out the best candidate.
   1176 		 */
   1177 		div = EREAD4(sc, EAP_ICSC);
   1178 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n", div));
   1179 
   1180 		div &= ~EAP_WTSRSEL;
   1181 		if (play->sample_rate < 8268)
   1182 			div |= EAP_WTSRSEL_5;
   1183 		else if (play->sample_rate < 16537)
   1184 			div |= EAP_WTSRSEL_11;
   1185 		else if (play->sample_rate < 33075)
   1186 			div |= EAP_WTSRSEL_22;
   1187 		else
   1188 			div |= EAP_WTSRSEL_44;
   1189 
   1190 		EWRITE4(sc, EAP_ICSC, div);
   1191 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1192 	}
   1193 
   1194 	return 0;
   1195 }
   1196 
   1197 static int
   1198 eap_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
   1199 {
   1200 
   1201 	return blk & -32;	/* keep good alignment */
   1202 }
   1203 
   1204 static int
   1205 eap_trigger_output(
   1206 	void *addr,
   1207 	void *start,
   1208 	void *end,
   1209 	int blksize,
   1210 	void (*intr)(void *),
   1211 	void *arg,
   1212 	const audio_params_t *param)
   1213 {
   1214 	struct eap_instance *ei;
   1215 	struct eap_softc *sc;
   1216 	struct eap_dma *p;
   1217 	uint32_t icsc, sic;
   1218 	int sampshift;
   1219 
   1220 	ei = addr;
   1221 	sc = (struct eap_softc *)ei->parent;
   1222 #ifdef DIAGNOSTIC
   1223 	if (ei->ei_prun)
   1224 		panic("eap_trigger_output: already running");
   1225 	ei->ei_prun = 1;
   1226 #endif
   1227 
   1228 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
   1229 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1230 	ei->ei_pintr = intr;
   1231 	ei->ei_parg = arg;
   1232 
   1233 	sic = EREAD4(sc, EAP_SIC);
   1234 	sic &= ~(EAP_S_EB(ei->index) | EAP_S_MB(ei->index) | EAP_INC_BITS);
   1235 
   1236 	if (ei->index == EAP_DAC2)
   1237 		sic |= EAP_SET_P2_ST_INC(0)
   1238 		    | EAP_SET_P2_END_INC(param->precision / 8);
   1239 
   1240 	sampshift = 0;
   1241 	if (param->precision == 16) {
   1242 		sic |= EAP_S_EB(ei->index);
   1243 		sampshift++;
   1244 	}
   1245 	if (param->channels == 2) {
   1246 		sic |= EAP_S_MB(ei->index);
   1247 		sampshift++;
   1248 	}
   1249 	EWRITE4(sc, EAP_SIC, sic & ~EAP_P_INTR_EN(ei->index));
   1250 	EWRITE4(sc, EAP_SIC, sic | EAP_P_INTR_EN(ei->index));
   1251 
   1252 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1253 		continue;
   1254 	if (!p) {
   1255 		printf("eap_trigger_output: bad addr %p\n", start);
   1256 		return EINVAL;
   1257 	}
   1258 
   1259 	if (ei->index == EAP_DAC2) {
   1260 		DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
   1261 			 (int)DMAADDR(p),
   1262 			 (int)EAP_SET_SIZE(0,
   1263 			 (((char *)end - (char *)start) >> 2) - 1)));
   1264 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1265 		EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
   1266 		EWRITE4(sc, EAP_DAC2_SIZE,
   1267 			EAP_SET_SIZE(0,
   1268 			((char *)end - (char *)start) >> 2) - 1);
   1269 		EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
   1270 	} else if (ei->index == EAP_DAC1) {
   1271 		DPRINTF(("eap_trigger_output: DAC1_ADDR=0x%x, DAC1_SIZE=0x%x\n",
   1272 			 (int)DMAADDR(p),
   1273 			 (int)EAP_SET_SIZE(0,
   1274 			 (((char *)end - (char *)start) >> 2) - 1)));
   1275 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1276 		EWRITE4(sc, EAP_DAC1_ADDR, DMAADDR(p));
   1277 		EWRITE4(sc, EAP_DAC1_SIZE,
   1278 			EAP_SET_SIZE(0,
   1279 			((char *)end - (char *)start) >> 2) - 1);
   1280 		EWRITE4(sc, EAP_DAC1_CSR, (blksize >> sampshift) - 1);
   1281 	}
   1282 #ifdef DIAGNOSTIC
   1283 	else
   1284 		panic("eap_trigger_output: impossible instance %d", ei->index);
   1285 #endif
   1286 
   1287 	if (sc->sc_1371)
   1288 		EWRITE4(sc, E1371_SRC, 0);
   1289 
   1290 	icsc = EREAD4(sc, EAP_ICSC);
   1291 	icsc |= EAP_DAC_EN(ei->index);
   1292 	EWRITE4(sc, EAP_ICSC, icsc);
   1293 
   1294 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
   1295 
   1296 	return 0;
   1297 }
   1298 
   1299 static int
   1300 eap_trigger_input(
   1301 	void *addr,
   1302 	void *start,
   1303 	void *end,
   1304 	int blksize,
   1305 	void (*intr)(void *),
   1306 	void *arg,
   1307 	const audio_params_t *param)
   1308 {
   1309 	struct eap_instance *ei;
   1310 	struct eap_softc *sc;
   1311 	struct eap_dma *p;
   1312 	uint32_t icsc, sic;
   1313 	int sampshift;
   1314 
   1315 	ei = addr;
   1316 	sc = (struct eap_softc *)ei->parent;
   1317 #ifdef DIAGNOSTIC
   1318 	if (sc->sc_rrun)
   1319 		panic("eap_trigger_input: already running");
   1320 	sc->sc_rrun = 1;
   1321 #endif
   1322 
   1323 	DPRINTFN(1, ("eap_trigger_input: ei=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1324 	    addr, start, end, blksize, intr, arg));
   1325 	sc->sc_rintr = intr;
   1326 	sc->sc_rarg = arg;
   1327 
   1328 	sic = EREAD4(sc, EAP_SIC);
   1329 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
   1330 	sampshift = 0;
   1331 	if (param->precision == 16) {
   1332 		sic |= EAP_R1_S_EB;
   1333 		sampshift++;
   1334 	}
   1335 	if (param->channels == 2) {
   1336 		sic |= EAP_R1_S_MB;
   1337 		sampshift++;
   1338 	}
   1339 	EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
   1340 	EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
   1341 
   1342 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1343 		continue;
   1344 	if (!p) {
   1345 		printf("eap_trigger_input: bad addr %p\n", start);
   1346 		return (EINVAL);
   1347 	}
   1348 
   1349 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
   1350 		 (int)DMAADDR(p),
   1351 		 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
   1352 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
   1353 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
   1354 	EWRITE4(sc, EAP_ADC_SIZE,
   1355 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
   1356 
   1357 	EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
   1358 
   1359 	if (sc->sc_1371)
   1360 		EWRITE4(sc, E1371_SRC, 0);
   1361 
   1362 	icsc = EREAD4(sc, EAP_ICSC);
   1363 	icsc |= EAP_ADC_EN;
   1364 	EWRITE4(sc, EAP_ICSC, icsc);
   1365 
   1366 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
   1367 
   1368 	return 0;
   1369 }
   1370 
   1371 static int
   1372 eap_halt_output(void *addr)
   1373 {
   1374 	struct eap_instance *ei;
   1375 	struct eap_softc *sc;
   1376 	uint32_t icsc;
   1377 
   1378 	DPRINTF(("eap: eap_halt_output\n"));
   1379 	ei = addr;
   1380 	sc = (struct eap_softc *)ei->parent;
   1381 	icsc = EREAD4(sc, EAP_ICSC);
   1382 	EWRITE4(sc, EAP_ICSC, icsc & ~(EAP_DAC_EN(ei->index)));
   1383 	ei->ei_pintr = 0;
   1384 #ifdef DIAGNOSTIC
   1385 	ei->ei_prun = 0;
   1386 #endif
   1387 
   1388 	return 0;
   1389 }
   1390 
   1391 static int
   1392 eap_halt_input(void *addr)
   1393 {
   1394 	struct eap_instance *ei;
   1395 	struct eap_softc *sc;
   1396 	uint32_t icsc;
   1397 
   1398 #define EAP_USE_FMDAC_ALSO
   1399 	DPRINTF(("eap: eap_halt_input\n"));
   1400 	ei = addr;
   1401 	sc = (struct eap_softc *)ei->parent;
   1402 	icsc = EREAD4(sc, EAP_ICSC);
   1403 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
   1404 	sc->sc_rintr = 0;
   1405 #ifdef DIAGNOSTIC
   1406 	sc->sc_rrun = 0;
   1407 #endif
   1408 
   1409 	return 0;
   1410 }
   1411 
   1412 static int
   1413 eap_getdev(void *addr, struct audio_device *retp)
   1414 {
   1415 
   1416 	*retp = eap_device;
   1417 	return 0;
   1418 }
   1419 
   1420 static int
   1421 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1422 {
   1423 	struct eap_instance *ei;
   1424 	struct eap_softc *sc;
   1425 
   1426 	ei = addr;
   1427 	sc = (struct eap_softc *)ei->parent;
   1428 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
   1429 }
   1430 
   1431 static int
   1432 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1433 {
   1434 	struct eap_instance *ei;
   1435 	struct eap_softc *sc;
   1436 
   1437 	ei = addr;
   1438 	sc = (struct eap_softc *)ei->parent;
   1439 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
   1440 }
   1441 
   1442 static int
   1443 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1444 {
   1445 	struct eap_instance *ei;
   1446 	struct eap_softc *sc;
   1447 
   1448 	ei = addr;
   1449 	sc = (struct eap_softc *)ei->parent;
   1450 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
   1451 }
   1452 
   1453 static void
   1454 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
   1455 {
   1456 	eap1370_write_codec(sc, a, d);
   1457 
   1458 	sc->sc_port[a] = d;
   1459 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
   1460 }
   1461 
   1462 static int
   1463 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1464 {
   1465 	struct eap_instance *ei;
   1466 	struct eap_softc *sc;
   1467 	int lval, rval, l, r, la, ra;
   1468 	int l1, r1, l2, r2, m, o1, o2;
   1469 
   1470 	ei = addr;
   1471 	sc = (struct eap_softc *)ei->parent;
   1472 	if (cp->dev == EAP_RECORD_SOURCE) {
   1473 		if (cp->type != AUDIO_MIXER_SET)
   1474 			return EINVAL;
   1475 		m = sc->sc_record_source = cp->un.mask;
   1476 		l1 = l2 = r1 = r2 = 0;
   1477 		if (m & (1 << EAP_VOICE_VOL))
   1478 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
   1479 		if (m & (1 << EAP_FM_VOL))
   1480 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
   1481 		if (m & (1 << EAP_CD_VOL))
   1482 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
   1483 		if (m & (1 << EAP_LINE_VOL))
   1484 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
   1485 		if (m & (1 << EAP_AUX_VOL))
   1486 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
   1487 		if (m & (1 << EAP_MIC_VOL))
   1488 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
   1489 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
   1490 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
   1491 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
   1492 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
   1493 		return 0;
   1494 	}
   1495 	if (cp->dev == EAP_INPUT_SOURCE) {
   1496 		if (cp->type != AUDIO_MIXER_SET)
   1497 			return EINVAL;
   1498 		m = sc->sc_input_source = cp->un.mask;
   1499 		o1 = o2 = 0;
   1500 		if (m & (1 << EAP_VOICE_VOL))
   1501 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
   1502 		if (m & (1 << EAP_FM_VOL))
   1503 			o1 |= AK_M_FM_L | AK_M_FM_R;
   1504 		if (m & (1 << EAP_CD_VOL))
   1505 			o1 |= AK_M_CD_L | AK_M_CD_R;
   1506 		if (m & (1 << EAP_LINE_VOL))
   1507 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
   1508 		if (m & (1 << EAP_AUX_VOL))
   1509 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
   1510 		if (m & (1 << EAP_MIC_VOL))
   1511 			o1 |= AK_M_MIC;
   1512 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
   1513 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
   1514 		return 0;
   1515 	}
   1516 	if (cp->dev == EAP_MIC_PREAMP) {
   1517 		if (cp->type != AUDIO_MIXER_ENUM)
   1518 			return EINVAL;
   1519 		if (cp->un.ord != 0 && cp->un.ord != 1)
   1520 			return EINVAL;
   1521 		sc->sc_mic_preamp = cp->un.ord;
   1522 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
   1523 		return 0;
   1524 	}
   1525 	if (cp->type != AUDIO_MIXER_VALUE)
   1526 		return EINVAL;
   1527 	if (cp->un.value.num_channels == 1)
   1528 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1529 	else if (cp->un.value.num_channels == 2) {
   1530 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1531 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1532 	} else
   1533 		return EINVAL;
   1534 	ra = -1;
   1535 	switch (cp->dev) {
   1536 	case EAP_MASTER_VOL:
   1537 		l = VOL_TO_ATT5(lval);
   1538 		r = VOL_TO_ATT5(rval);
   1539 		la = AK_MASTER_L;
   1540 		ra = AK_MASTER_R;
   1541 		break;
   1542 	case EAP_MIC_VOL:
   1543 		if (cp->un.value.num_channels != 1)
   1544 			return EINVAL;
   1545 		la = AK_MIC;
   1546 		goto lr;
   1547 	case EAP_VOICE_VOL:
   1548 		la = AK_VOICE_L;
   1549 		ra = AK_VOICE_R;
   1550 		goto lr;
   1551 	case EAP_FM_VOL:
   1552 		la = AK_FM_L;
   1553 		ra = AK_FM_R;
   1554 		goto lr;
   1555 	case EAP_CD_VOL:
   1556 		la = AK_CD_L;
   1557 		ra = AK_CD_R;
   1558 		goto lr;
   1559 	case EAP_LINE_VOL:
   1560 		la = AK_LINE_L;
   1561 		ra = AK_LINE_R;
   1562 		goto lr;
   1563 	case EAP_AUX_VOL:
   1564 		la = AK_AUX_L;
   1565 		ra = AK_AUX_R;
   1566 	lr:
   1567 		l = VOL_TO_GAIN5(lval);
   1568 		r = VOL_TO_GAIN5(rval);
   1569 		break;
   1570 	default:
   1571 		return EINVAL;
   1572 	}
   1573 	eap1370_set_mixer(sc, la, l);
   1574 	if (ra >= 0) {
   1575 		eap1370_set_mixer(sc, ra, r);
   1576 	}
   1577 	return 0;
   1578 }
   1579 
   1580 static int
   1581 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1582 {
   1583 	struct eap_instance *ei;
   1584 	struct eap_softc *sc;
   1585 	int la, ra, l, r;
   1586 
   1587 	ei = addr;
   1588 	sc = (struct eap_softc *)ei->parent;
   1589 	switch (cp->dev) {
   1590 	case EAP_RECORD_SOURCE:
   1591 		if (cp->type != AUDIO_MIXER_SET)
   1592 			return EINVAL;
   1593 		cp->un.mask = sc->sc_record_source;
   1594 		return 0;
   1595 	case EAP_INPUT_SOURCE:
   1596 		if (cp->type != AUDIO_MIXER_SET)
   1597 			return EINVAL;
   1598 		cp->un.mask = sc->sc_input_source;
   1599 		return 0;
   1600 	case EAP_MIC_PREAMP:
   1601 		if (cp->type != AUDIO_MIXER_ENUM)
   1602 			return EINVAL;
   1603 		cp->un.ord = sc->sc_mic_preamp;
   1604 		return 0;
   1605 	case EAP_MASTER_VOL:
   1606 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
   1607 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
   1608 		break;
   1609 	case EAP_MIC_VOL:
   1610 		if (cp->un.value.num_channels != 1)
   1611 			return EINVAL;
   1612 		la = ra = AK_MIC;
   1613 		goto lr;
   1614 	case EAP_VOICE_VOL:
   1615 		la = AK_VOICE_L;
   1616 		ra = AK_VOICE_R;
   1617 		goto lr;
   1618 	case EAP_FM_VOL:
   1619 		la = AK_FM_L;
   1620 		ra = AK_FM_R;
   1621 		goto lr;
   1622 	case EAP_CD_VOL:
   1623 		la = AK_CD_L;
   1624 		ra = AK_CD_R;
   1625 		goto lr;
   1626 	case EAP_LINE_VOL:
   1627 		la = AK_LINE_L;
   1628 		ra = AK_LINE_R;
   1629 		goto lr;
   1630 	case EAP_AUX_VOL:
   1631 		la = AK_AUX_L;
   1632 		ra = AK_AUX_R;
   1633 	lr:
   1634 		l = GAIN5_TO_VOL(sc->sc_port[la]);
   1635 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
   1636 		break;
   1637 	default:
   1638 		return EINVAL;
   1639 	}
   1640 	if (cp->un.value.num_channels == 1)
   1641 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
   1642 	else if (cp->un.value.num_channels == 2) {
   1643 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
   1644 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1645 	} else
   1646 		return EINVAL;
   1647 	return 0;
   1648 }
   1649 
   1650 static int
   1651 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1652 {
   1653 
   1654 	switch (dip->index) {
   1655 	case EAP_MASTER_VOL:
   1656 		dip->type = AUDIO_MIXER_VALUE;
   1657 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1658 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1659 		strcpy(dip->label.name, AudioNmaster);
   1660 		dip->un.v.num_channels = 2;
   1661 		dip->un.v.delta = 8;
   1662 		strcpy(dip->un.v.units.name, AudioNvolume);
   1663 		return 0;
   1664 	case EAP_VOICE_VOL:
   1665 		dip->type = AUDIO_MIXER_VALUE;
   1666 		dip->mixer_class = EAP_INPUT_CLASS;
   1667 		dip->prev = AUDIO_MIXER_LAST;
   1668 		dip->next = AUDIO_MIXER_LAST;
   1669 		strcpy(dip->label.name, AudioNdac);
   1670 		dip->un.v.num_channels = 2;
   1671 		dip->un.v.delta = 8;
   1672 		strcpy(dip->un.v.units.name, AudioNvolume);
   1673 		return 0;
   1674 	case EAP_FM_VOL:
   1675 		dip->type = AUDIO_MIXER_VALUE;
   1676 		dip->mixer_class = EAP_INPUT_CLASS;
   1677 		dip->prev = AUDIO_MIXER_LAST;
   1678 		dip->next = AUDIO_MIXER_LAST;
   1679 		strcpy(dip->label.name, AudioNfmsynth);
   1680 		dip->un.v.num_channels = 2;
   1681 		dip->un.v.delta = 8;
   1682 		strcpy(dip->un.v.units.name, AudioNvolume);
   1683 		return 0;
   1684 	case EAP_CD_VOL:
   1685 		dip->type = AUDIO_MIXER_VALUE;
   1686 		dip->mixer_class = EAP_INPUT_CLASS;
   1687 		dip->prev = AUDIO_MIXER_LAST;
   1688 		dip->next = AUDIO_MIXER_LAST;
   1689 		strcpy(dip->label.name, AudioNcd);
   1690 		dip->un.v.num_channels = 2;
   1691 		dip->un.v.delta = 8;
   1692 		strcpy(dip->un.v.units.name, AudioNvolume);
   1693 		return 0;
   1694 	case EAP_LINE_VOL:
   1695 		dip->type = AUDIO_MIXER_VALUE;
   1696 		dip->mixer_class = EAP_INPUT_CLASS;
   1697 		dip->prev = AUDIO_MIXER_LAST;
   1698 		dip->next = AUDIO_MIXER_LAST;
   1699 		strcpy(dip->label.name, AudioNline);
   1700 		dip->un.v.num_channels = 2;
   1701 		dip->un.v.delta = 8;
   1702 		strcpy(dip->un.v.units.name, AudioNvolume);
   1703 		return 0;
   1704 	case EAP_AUX_VOL:
   1705 		dip->type = AUDIO_MIXER_VALUE;
   1706 		dip->mixer_class = EAP_INPUT_CLASS;
   1707 		dip->prev = AUDIO_MIXER_LAST;
   1708 		dip->next = AUDIO_MIXER_LAST;
   1709 		strcpy(dip->label.name, AudioNaux);
   1710 		dip->un.v.num_channels = 2;
   1711 		dip->un.v.delta = 8;
   1712 		strcpy(dip->un.v.units.name, AudioNvolume);
   1713 		return 0;
   1714 	case EAP_MIC_VOL:
   1715 		dip->type = AUDIO_MIXER_VALUE;
   1716 		dip->mixer_class = EAP_INPUT_CLASS;
   1717 		dip->prev = AUDIO_MIXER_LAST;
   1718 		dip->next = EAP_MIC_PREAMP;
   1719 		strcpy(dip->label.name, AudioNmicrophone);
   1720 		dip->un.v.num_channels = 1;
   1721 		dip->un.v.delta = 8;
   1722 		strcpy(dip->un.v.units.name, AudioNvolume);
   1723 		return 0;
   1724 	case EAP_RECORD_SOURCE:
   1725 		dip->mixer_class = EAP_RECORD_CLASS;
   1726 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1727 		strcpy(dip->label.name, AudioNsource);
   1728 		dip->type = AUDIO_MIXER_SET;
   1729 		dip->un.s.num_mem = 6;
   1730 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1731 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1732 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1733 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1734 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1735 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1736 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1737 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1738 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1739 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1740 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1741 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1742 		return 0;
   1743 	case EAP_INPUT_SOURCE:
   1744 		dip->mixer_class = EAP_INPUT_CLASS;
   1745 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1746 		strcpy(dip->label.name, AudioNsource);
   1747 		dip->type = AUDIO_MIXER_SET;
   1748 		dip->un.s.num_mem = 6;
   1749 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1750 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1751 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1752 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1753 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1754 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1755 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1756 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1757 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1758 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1759 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1760 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1761 		return 0;
   1762 	case EAP_MIC_PREAMP:
   1763 		dip->type = AUDIO_MIXER_ENUM;
   1764 		dip->mixer_class = EAP_INPUT_CLASS;
   1765 		dip->prev = EAP_MIC_VOL;
   1766 		dip->next = AUDIO_MIXER_LAST;
   1767 		strcpy(dip->label.name, AudioNpreamp);
   1768 		dip->un.e.num_mem = 2;
   1769 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1770 		dip->un.e.member[0].ord = 0;
   1771 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1772 		dip->un.e.member[1].ord = 1;
   1773 		return 0;
   1774 	case EAP_OUTPUT_CLASS:
   1775 		dip->type = AUDIO_MIXER_CLASS;
   1776 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1777 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1778 		strcpy(dip->label.name, AudioCoutputs);
   1779 		return 0;
   1780 	case EAP_RECORD_CLASS:
   1781 		dip->type = AUDIO_MIXER_CLASS;
   1782 		dip->mixer_class = EAP_RECORD_CLASS;
   1783 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1784 		strcpy(dip->label.name, AudioCrecord);
   1785 		return 0;
   1786 	case EAP_INPUT_CLASS:
   1787 		dip->type = AUDIO_MIXER_CLASS;
   1788 		dip->mixer_class = EAP_INPUT_CLASS;
   1789 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1790 		strcpy(dip->label.name, AudioCinputs);
   1791 		return 0;
   1792 	}
   1793 	return ENXIO;
   1794 }
   1795 
   1796 static void *
   1797 eap_malloc(void *addr, int direction, size_t size,
   1798     struct malloc_type *pool, int flags)
   1799 {
   1800 	struct eap_instance *ei;
   1801 	struct eap_softc *sc;
   1802 	struct eap_dma *p;
   1803 	int error;
   1804 
   1805 	p = malloc(sizeof(*p), pool, flags);
   1806 	if (!p)
   1807 		return NULL;
   1808 	ei = addr;
   1809 	sc = (struct eap_softc *)ei->parent;
   1810 	error = eap_allocmem(sc, size, 16, p);
   1811 	if (error) {
   1812 		free(p, pool);
   1813 		return NULL;
   1814 	}
   1815 	p->next = sc->sc_dmas;
   1816 	sc->sc_dmas = p;
   1817 	return KERNADDR(p);
   1818 }
   1819 
   1820 static void
   1821 eap_free(void *addr, void *ptr, struct malloc_type *pool)
   1822 {
   1823 	struct eap_instance *ei;
   1824 	struct eap_softc *sc;
   1825 	struct eap_dma **pp, *p;
   1826 
   1827 	ei = addr;
   1828 	sc = (struct eap_softc *)ei->parent;
   1829 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1830 		if (KERNADDR(p) == ptr) {
   1831 			eap_freemem(sc, p);
   1832 			*pp = p->next;
   1833 			free(p, pool);
   1834 			return;
   1835 		}
   1836 	}
   1837 }
   1838 
   1839 static size_t
   1840 eap_round_buffersize(void *addr, int direction, size_t size)
   1841 {
   1842 
   1843 	return size;
   1844 }
   1845 
   1846 static paddr_t
   1847 eap_mappage(void *addr, void *mem, off_t off, int prot)
   1848 {
   1849 	struct eap_instance *ei;
   1850 	struct eap_softc *sc;
   1851 	struct eap_dma *p;
   1852 
   1853 	if (off < 0)
   1854 		return -1;
   1855 	ei = addr;
   1856 	sc = (struct eap_softc *)ei->parent;
   1857 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1858 		continue;
   1859 	if (!p)
   1860 		return -1;
   1861 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1862 			       off, prot, BUS_DMA_WAITOK);
   1863 }
   1864 
   1865 static int
   1866 eap_get_props(void *addr)
   1867 {
   1868 
   1869 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1870 	    AUDIO_PROP_FULLDUPLEX;
   1871 }
   1872 
   1873 #if NMIDI > 0
   1874 static int
   1875 eap_midi_open(void *addr, int flags,
   1876 	      void (*iintr)(void *, int),
   1877 	      void (*ointr)(void *),
   1878 	      void *arg)
   1879 {
   1880 	struct eap_softc *sc;
   1881 	uint8_t uctrl;
   1882 
   1883 	sc = addr;
   1884 	sc->sc_arg = arg;
   1885 
   1886 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
   1887 	uctrl = 0;
   1888 	if (flags & FREAD) {
   1889 		uctrl |= EAP_UC_RXINTEN;
   1890 		sc->sc_iintr = iintr;
   1891 	}
   1892 	if (flags & FWRITE)
   1893 		sc->sc_ointr = ointr;
   1894 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1895 
   1896 	return 0;
   1897 }
   1898 
   1899 static void
   1900 eap_midi_close(void *addr)
   1901 {
   1902 	struct eap_softc *sc;
   1903 
   1904 	sc = addr;
   1905 	tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
   1906 	EWRITE1(sc, EAP_UART_CONTROL, 0);
   1907 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
   1908 
   1909 	sc->sc_iintr = 0;
   1910 	sc->sc_ointr = 0;
   1911 }
   1912 
   1913 static int
   1914 eap_midi_output(void *addr, int d)
   1915 {
   1916 	struct eap_softc *sc;
   1917 	uint8_t uctrl;
   1918 
   1919 	sc = addr;
   1920 	EWRITE1(sc, EAP_UART_DATA, d);
   1921 
   1922 	uctrl = EAP_UC_TXINTEN;
   1923 	if (sc->sc_iintr)
   1924 		uctrl |= EAP_UC_RXINTEN;
   1925 	/*
   1926 	 * This is a write-only register, so we have to remember the right
   1927 	 * value of RXINTEN as well as setting TXINTEN. But if we are open
   1928 	 * for reading, it will always be correct to set RXINTEN here; only
   1929 	 * during service of a receive interrupt could it be momentarily
   1930 	 * toggled off, and whether we got here from the top half or from
   1931 	 * an interrupt, that won't be the current state.
   1932 	 */
   1933 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1934 	return 0;
   1935 }
   1936 
   1937 static void
   1938 eap_midi_getinfo(void *addr, struct midi_info *mi)
   1939 {
   1940 	mi->name = "AudioPCI MIDI UART";
   1941 	mi->props = MIDI_PROP_CAN_INPUT | MIDI_PROP_OUT_INTR;
   1942 }
   1943 
   1944 static void
   1945 eap_uart_txrdy(struct eap_softc *sc)
   1946 {
   1947 	uint8_t uctrl;
   1948 	uctrl = 0;
   1949 	if (sc->sc_iintr)
   1950 		uctrl = EAP_UC_RXINTEN;
   1951 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1952 	sc->sc_ointr(sc->sc_arg);
   1953 }
   1954 
   1955 #endif
   1956