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