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