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