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