Home | History | Annotate | Line # | Download | only in pci
eap.c revision 1.19
      1 /*	$NetBSD: eap.c,v 1.19 1998/11/25 22:17:07 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) netbsd.org).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Further work: Charles M. Hannum  <mycroft (at) netbsd.org>
     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 
     46 /*
     47  * Ensoniq AudoiPCI ES1370 + AK4531 driver.
     48  * Data sheets can be found at
     49  * http://www.ensoniq.com/multimedia/semi_html/html/es1370.zip
     50  * and
     51  * http://206.214.38.151/pdf/4531.pdf
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/malloc.h>
     58 #include <sys/device.h>
     59 
     60 #include <dev/pci/pcidevs.h>
     61 #include <dev/pci/pcivar.h>
     62 
     63 #include <sys/audioio.h>
     64 #include <dev/audio_if.h>
     65 #include <dev/mulaw.h>
     66 #include <dev/auconv.h>
     67 
     68 #include <machine/bus.h>
     69 
     70 #define	PCI_CBIO		0x10
     71 
     72 #define EAP_ICSC		0x00    /* interrupt / chip select control */
     73 #define  EAP_SERR_DISABLE	0x00000001
     74 #define  EAP_CDC_EN		0x00000002
     75 #define  EAP_JYSTK_EN		0x00000004
     76 #define  EAP_UART_EN		0x00000008
     77 #define  EAP_ADC_EN		0x00000010
     78 #define  EAP_DAC2_EN		0x00000020
     79 #define  EAP_DAC1_EN		0x00000040
     80 #define  EAP_BREQ		0x00000080
     81 #define  EAP_XTCL0		0x00000100
     82 #define  EAP_M_CB		0x00000200
     83 #define  EAP_CCB_INTRM		0x00000400
     84 #define  EAP_DAC_SYNC		0x00000800
     85 #define  EAP_WTSRSEL		0x00003000
     86 #define   EAP_WTSRSEL_5		0x00000000
     87 #define   EAP_WTSRSEL_11	0x00001000
     88 #define   EAP_WTSRSEL_22	0x00002000
     89 #define   EAP_WTSRSEL_44	0x00003000
     90 #define  EAP_M_SBB		0x00004000
     91 #define  EAP_MSFMTSEL		0x00008000
     92 #define  EAP_SET_PCLKDIV(n)	(((n)&0x1fff)<<16)
     93 #define  EAP_GET_PCLKDIV(n)	(((n)>>16)&0x1fff)
     94 #define  EAP_PCLKBITS		0x1fff0000
     95 #define  EAP_XTCL1		0x40000000
     96 #define  EAP_ADC_STOP		0x80000000
     97 
     98 #define EAP_ICSS		0x04	/* interrupt / chip select status */
     99 #define  EAP_I_ADC		0x00000001
    100 #define  EAP_I_DAC2		0x00000002
    101 #define  EAP_I_DAC1		0x00000004
    102 #define  EAP_I_UART		0x00000008
    103 #define  EAP_I_MCCB		0x00000010
    104 #define  EAP_VC			0x00000060
    105 #define  EAP_CWRIP		0x00000100
    106 #define  EAP_CBUSY		0x00000200
    107 #define  EAP_CSTAT		0x00000400
    108 #define  EAP_INTR		0x80000000
    109 
    110 #define EAP_UART_DATA		0x08
    111 #define EAP_UART_STATUS		0x09
    112 #define EAP_UART_CONTROL	0x09
    113 #define EAP_MEMPAGE		0x0c
    114 #define EAP_CODEC		0x10
    115 #define  EAP_SET_CODEC(a,d)	(((a)<<8) | (d))
    116 
    117 #define EAP_SIC			0x20
    118 #define  EAP_P1_S_MB		0x00000001
    119 #define  EAP_P1_S_EB		0x00000002
    120 #define  EAP_P2_S_MB		0x00000004
    121 #define  EAP_P2_S_EB		0x00000008
    122 #define  EAP_R1_S_MB		0x00000010
    123 #define  EAP_R1_S_EB		0x00000020
    124 #define  EAP_P2_DAC_SEN		0x00000040
    125 #define  EAP_P1_SCT_RLD		0x00000080
    126 #define  EAP_P1_INTR_EN		0x00000100
    127 #define  EAP_P2_INTR_EN		0x00000200
    128 #define  EAP_R1_INTR_EN		0x00000400
    129 #define  EAP_P1_PAUSE		0x00000800
    130 #define  EAP_P2_PAUSE		0x00001000
    131 #define  EAP_P1_LOOP_SEL	0x00002000
    132 #define  EAP_P2_LOOP_SEL	0x00004000
    133 #define  EAP_R1_LOOP_SEL	0x00008000
    134 #define  EAP_SET_P2_ST_INC(i)	((i) << 16)
    135 #define  EAP_SET_P2_END_INC(i)	((i) << 19)
    136 #define  EAP_INC_BITS		0x003f0000
    137 
    138 #define EAP_DAC1_CSR		0x24
    139 #define EAP_DAC2_CSR		0x28
    140 #define EAP_ADC_CSR		0x2c
    141 #define  EAP_GET_CURRSAMP(r)	((r) >> 16)
    142 
    143 #define EAP_DAC_PAGE		0xc
    144 #define EAP_ADC_PAGE		0xd
    145 #define EAP_UART_PAGE1		0xe
    146 #define EAP_UART_PAGE2		0xf
    147 
    148 #define EAP_DAC1_ADDR		0x30
    149 #define EAP_DAC1_SIZE		0x34
    150 #define EAP_DAC2_ADDR		0x38
    151 #define EAP_DAC2_SIZE		0x3c
    152 #define EAP_ADC_ADDR		0x30
    153 #define EAP_ADC_SIZE		0x34
    154 #define  EAP_SET_SIZE(c,s)	(((c)<<16) | (s))
    155 
    156 #define EAP_XTAL_FREQ 1411200 /* 22.5792 / 16 MHz */
    157 
    158 /* AK4531 registers */
    159 #define AK_MASTER_L		0x00
    160 #define AK_MASTER_R		0x01
    161 #define AK_VOICE_L		0x02
    162 #define AK_VOICE_R		0x03
    163 #define AK_FM_L			0x04
    164 #define AK_FM_R			0x05
    165 #define AK_CD_L			0x06
    166 #define AK_CD_R			0x07
    167 #define AK_LINE_L		0x08
    168 #define AK_LINE_R		0x09
    169 #define AK_AUX_L		0x0a
    170 #define AK_AUX_R		0x0b
    171 #define AK_MONO1		0x0c
    172 #define AK_MONO2		0x0d
    173 #define AK_MIC			0x0e
    174 #define AK_MONO			0x0f
    175 #define AK_OUT_MIXER1		0x10
    176 #define  AK_M_FM_L		0x40
    177 #define  AK_M_FM_R		0x20
    178 #define  AK_M_LINE_L		0x10
    179 #define  AK_M_LINE_R		0x08
    180 #define  AK_M_CD_L		0x04
    181 #define  AK_M_CD_R		0x02
    182 #define  AK_M_MIC		0x01
    183 #define AK_OUT_MIXER2		0x11
    184 #define  AK_M_AUX_L		0x20
    185 #define  AK_M_AUX_R		0x10
    186 #define  AK_M_VOICE_L		0x08
    187 #define  AK_M_VOICE_R		0x04
    188 #define  AK_M_MONO2		0x02
    189 #define  AK_M_MONO1		0x01
    190 #define AK_IN_MIXER1_L		0x12
    191 #define AK_IN_MIXER1_R		0x13
    192 #define AK_IN_MIXER2_L		0x14
    193 #define AK_IN_MIXER2_R		0x15
    194 #define  AK_M_TMIC		0x80
    195 #define  AK_M_TMONO1		0x40
    196 #define  AK_M_TMONO2		0x20
    197 #define  AK_M2_AUX_L		0x10
    198 #define  AK_M2_AUX_R		0x08
    199 #define  AK_M_VOICE		0x04
    200 #define  AK_M2_MONO2		0x02
    201 #define  AK_M2_MONO1		0x01
    202 #define AK_RESET		0x16
    203 #define  AK_PD			0x02
    204 #define  AK_NRST		0x01
    205 #define AK_CS			0x17
    206 #define AK_ADSEL		0x18
    207 #define AK_MGAIN		0x19
    208 
    209 #define AK_NPORTS 16
    210 
    211 #define VOL_TO_ATT5(v) (0x1f - ((v) >> 3))
    212 #define VOL_TO_GAIN5(v) VOL_TO_ATT5(v)
    213 #define ATT5_TO_VOL(v) ((0x1f - (v)) << 3)
    214 #define GAIN5_TO_VOL(v) ATT5_TO_VOL(v)
    215 #define VOL_0DB 200
    216 
    217 #define EAP_MASTER_VOL		0
    218 #define EAP_VOICE_VOL		1
    219 #define EAP_FM_VOL		2
    220 #define EAP_CD_VOL		3
    221 #define EAP_LINE_VOL		4
    222 #define EAP_AUX_VOL		5
    223 #define EAP_MIC_VOL		6
    224 #define	EAP_RECORD_SOURCE 	7
    225 #define EAP_OUTPUT_SELECT	8
    226 #define	EAP_MIC_PREAMP		9
    227 #define EAP_OUTPUT_CLASS	10
    228 #define EAP_RECORD_CLASS	11
    229 #define EAP_INPUT_CLASS		12
    230 
    231 #ifdef AUDIO_DEBUG
    232 #define DPRINTF(x)	if (eapdebug) printf x
    233 #define DPRINTFN(n,x)	if (eapdebug>(n)) printf x
    234 int	eapdebug = 0;
    235 #else
    236 #define DPRINTF(x)
    237 #define DPRINTFN(n,x)
    238 #endif
    239 
    240 int	eap_match __P((struct device *, struct cfdata *, void *));
    241 void	eap_attach __P((struct device *, struct device *, void *));
    242 int	eap_intr __P((void *));
    243 
    244 struct eap_dma {
    245 	bus_dmamap_t map;
    246 	caddr_t addr;
    247 	bus_dma_segment_t segs[1];
    248 	int nsegs;
    249 	size_t size;
    250 	struct eap_dma *next;
    251 };
    252 #define DMAADDR(map) ((map)->segs[0].ds_addr)
    253 #define KERNADDR(map) ((void *)((map)->addr))
    254 
    255 struct eap_softc {
    256 	struct device sc_dev;		/* base device */
    257 	void *sc_ih;			/* interrupt vectoring */
    258 	bus_space_tag_t iot;
    259 	bus_space_handle_t ioh;
    260 	bus_dma_tag_t sc_dmatag;	/* DMA tag */
    261 
    262 	struct eap_dma *sc_dmas;
    263 
    264 	void	(*sc_pintr)(void *);	/* dma completion intr handler */
    265 	void	*sc_parg;		/* arg for sc_intr() */
    266 #ifdef DIAGNOSTIC
    267 	char	sc_prun;
    268 #endif
    269 
    270 	void	(*sc_rintr)(void *);	/* dma completion intr handler */
    271 	void	*sc_rarg;		/* arg for sc_intr() */
    272 #ifdef DIAGNOSTIC
    273 	char	sc_rrun;
    274 #endif
    275 
    276 	u_char	sc_port[AK_NPORTS];	/* mirror of the hardware setting */
    277 	u_int	sc_record_source;	/* recording source mask */
    278 	u_int	sc_output_source;	/* output source mask */
    279 	u_int	sc_mic_preamp;
    280 };
    281 
    282 int	eap_allocmem __P((struct eap_softc *, size_t, size_t, struct eap_dma *));
    283 int	eap_freemem __P((struct eap_softc *, struct eap_dma *));
    284 
    285 #define EWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
    286 #define EWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
    287 #define EREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
    288 #define EREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
    289 
    290 struct cfattach eap_ca = {
    291 	sizeof(struct eap_softc), eap_match, eap_attach
    292 };
    293 
    294 int	eap_open __P((void *, int));
    295 void	eap_close __P((void *));
    296 int	eap_query_encoding __P((void *, struct audio_encoding *));
    297 int	eap_set_params __P((void *, int, int, struct audio_params *, struct audio_params *));
    298 int	eap_round_blocksize __P((void *, int));
    299 int	eap_trigger_output __P((void *, void *, void *, int, void (*)(void *),
    300 	    void *, struct audio_params *));
    301 int	eap_trigger_input __P((void *, void *, void *, int, void (*)(void *),
    302 	    void *, struct audio_params *));
    303 int	eap_halt_output __P((void *));
    304 int	eap_halt_input __P((void *));
    305 int	eap_getdev __P((void *, struct audio_device *));
    306 int	eap_mixer_set_port __P((void *, mixer_ctrl_t *));
    307 int	eap_mixer_get_port __P((void *, mixer_ctrl_t *));
    308 int	eap_query_devinfo __P((void *, mixer_devinfo_t *));
    309 void   *eap_malloc __P((void *, u_long, int, int));
    310 void	eap_free __P((void *, void *, int));
    311 u_long	eap_round __P((void *, u_long));
    312 int	eap_mappage __P((void *, void *, int, int));
    313 int	eap_get_props __P((void *));
    314 void	eap_write_codec __P((struct eap_softc *sc, int a, int d));
    315 void	eap_set_mixer __P((struct eap_softc *sc, int a, int d));
    316 
    317 struct audio_hw_if eap_hw_if = {
    318 	eap_open,
    319 	eap_close,
    320 	NULL,
    321 	eap_query_encoding,
    322 	eap_set_params,
    323 	eap_round_blocksize,
    324 	NULL,
    325 	NULL,
    326 	NULL,
    327 	NULL,
    328 	NULL,
    329 	eap_halt_output,
    330 	eap_halt_input,
    331 	NULL,
    332 	eap_getdev,
    333 	NULL,
    334 	eap_mixer_set_port,
    335 	eap_mixer_get_port,
    336 	eap_query_devinfo,
    337 	eap_malloc,
    338 	eap_free,
    339 	eap_round,
    340 	eap_mappage,
    341 	eap_get_props,
    342 	eap_trigger_output,
    343 	eap_trigger_input,
    344 };
    345 
    346 struct audio_device eap_device = {
    347 	"Ensoniq AudioPCI",
    348 	"",
    349 	"eap"
    350 };
    351 
    352 int
    353 eap_match(parent, match, aux)
    354 	struct device *parent;
    355 	struct cfdata *match;
    356 	void *aux;
    357 {
    358 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    359 
    360 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ENSONIQ)
    361 		return (0);
    362 	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_ENSONIQ_AUDIOPCI)
    363 		return (0);
    364 
    365 	return (1);
    366 }
    367 
    368 void
    369 eap_write_codec(sc, a, d)
    370 	struct eap_softc *sc;
    371 	int a, d;
    372 {
    373 	int icss;
    374 
    375 	do {
    376 		icss = EREAD4(sc, EAP_ICSS);
    377 		DPRINTFN(5,("eap: codec %d prog: icss=0x%08x\n", a, icss));
    378 	} while(icss & EAP_CWRIP);
    379 	EWRITE4(sc, EAP_CODEC, EAP_SET_CODEC(a, d));
    380 }
    381 
    382 void
    383 eap_attach(parent, self, aux)
    384 	struct device *parent;
    385 	struct device *self;
    386 	void *aux;
    387 {
    388 	struct eap_softc *sc = (struct eap_softc *)self;
    389 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    390 	pci_chipset_tag_t pc = pa->pa_pc;
    391 	char const *intrstr;
    392 	pci_intr_handle_t ih;
    393 	pcireg_t csr;
    394 	char devinfo[256];
    395 	mixer_ctrl_t ctl;
    396 
    397 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    398 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    399 
    400 	/* Map I/O register */
    401 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    402 	      &sc->iot, &sc->ioh, NULL, NULL)) {
    403 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    404 		return;
    405 	}
    406 
    407 	sc->sc_dmatag = pa->pa_dmat;
    408 
    409 	/* Enable the device. */
    410 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    411 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    412 		       csr | PCI_COMMAND_MASTER_ENABLE);
    413 
    414 	/* Map and establish the interrupt. */
    415 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    416 	    pa->pa_intrline, &ih)) {
    417 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    418 		return;
    419 	}
    420 	intrstr = pci_intr_string(pc, ih);
    421 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
    422 	if (sc->sc_ih == NULL) {
    423 		printf("%s: couldn't establish interrupt",
    424 		    sc->sc_dev.dv_xname);
    425 		if (intrstr != NULL)
    426 			printf(" at %s", intrstr);
    427 		printf("\n");
    428 		return;
    429 	}
    430 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    431 
    432 	/* Enable interrupts and looping mode. */
    433 	EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    434 	EWRITE4(sc, EAP_ICSC, EAP_CDC_EN); /* enable the parts we need */
    435 
    436 	eap_write_codec(sc, AK_RESET, AK_PD); /* reset codec */
    437 	eap_write_codec(sc, AK_RESET, AK_PD | AK_NRST);	/* normal operation */
    438 	eap_write_codec(sc, AK_CS, 0x0); /* select codec clocks */
    439 
    440 	/* Enable all relevant mixer switches. */
    441 	ctl.dev = EAP_OUTPUT_SELECT;
    442 	ctl.type = AUDIO_MIXER_SET;
    443 	ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL | 1 << EAP_CD_VOL |
    444 	    1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL | 1 << EAP_MIC_VOL;
    445 	eap_mixer_set_port(sc, &ctl);
    446 
    447 	ctl.type = AUDIO_MIXER_VALUE;
    448 	ctl.un.value.num_channels = 1;
    449 	for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL; ctl.dev++) {
    450 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
    451 		eap_mixer_set_port(sc, &ctl);
    452 	}
    453 	ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
    454 	eap_mixer_set_port(sc, &ctl); /* set the mic to 0 */
    455 	ctl.dev = EAP_MIC_PREAMP;
    456 	ctl.type = AUDIO_MIXER_ENUM;
    457 	ctl.un.ord = 0;
    458 	eap_mixer_set_port(sc, &ctl);
    459 	ctl.dev = EAP_RECORD_SOURCE;
    460 	ctl.type = AUDIO_MIXER_SET;
    461 	ctl.un.mask = 1 << EAP_MIC_VOL;
    462 	eap_mixer_set_port(sc, &ctl);
    463 
    464 	audio_attach_mi(&eap_hw_if, sc, &sc->sc_dev);
    465 }
    466 
    467 int
    468 eap_intr(p)
    469 	void *p;
    470 {
    471 	struct eap_softc *sc = p;
    472 	u_int32_t intr, sic;
    473 
    474 	intr = EREAD4(sc, EAP_ICSS);
    475 	if (!(intr & EAP_INTR))
    476 		return (0);
    477 	sic = EREAD4(sc, EAP_SIC);
    478 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
    479 	if (intr & EAP_I_ADC) {
    480 		/*
    481 		 * XXX This is a hack!
    482 		 * The EAP chip sometimes generates the recording interrupt
    483 		 * while it is still transferring the data.  To make sure
    484 		 * it has all arrived we busy wait until the count is right.
    485 		 * The transfer we are waiting for is 8 longwords.
    486 		 */
    487 		int s, nw, n;
    488 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
    489 		s = EREAD4(sc, EAP_ADC_CSR);
    490 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
    491 		n = 0;
    492 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
    493 			delay(10);
    494 			if (++n > 100) {
    495 				printf("eapintr: dma fix timeout");
    496 				break;
    497 			}
    498 		}
    499 		/* Continue with normal interrupt handling. */
    500 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
    501 		EWRITE4(sc, EAP_SIC, sic);
    502 		if (sc->sc_rintr)
    503 			sc->sc_rintr(sc->sc_rarg);
    504 	}
    505 	if (intr & EAP_I_DAC2) {
    506 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
    507 		EWRITE4(sc, EAP_SIC, sic);
    508 		if (sc->sc_pintr)
    509 			sc->sc_pintr(sc->sc_parg);
    510 	}
    511 	return (1);
    512 }
    513 
    514 int
    515 eap_allocmem(sc, size, align, p)
    516 	struct eap_softc *sc;
    517 	size_t size;
    518 	size_t align;
    519 	struct eap_dma *p;
    520 {
    521 	int error;
    522 
    523 	p->size = size;
    524 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    525 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    526 				 &p->nsegs, BUS_DMA_NOWAIT);
    527 	if (error)
    528 		return (error);
    529 
    530 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    531 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    532 	if (error)
    533 		goto free;
    534 
    535 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    536 				  0, BUS_DMA_NOWAIT, &p->map);
    537 	if (error)
    538 		goto unmap;
    539 
    540 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    541 				BUS_DMA_NOWAIT);
    542 	if (error)
    543 		goto destroy;
    544 	return (0);
    545 
    546 destroy:
    547 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    548 unmap:
    549 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    550 free:
    551 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    552 	return (error);
    553 }
    554 
    555 int
    556 eap_freemem(sc, p)
    557 	struct eap_softc *sc;
    558 	struct eap_dma *p;
    559 {
    560 	bus_dmamap_unload(sc->sc_dmatag, p->map);
    561 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    562 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    563 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    564 	return (0);
    565 }
    566 
    567 int
    568 eap_open(addr, flags)
    569 	void *addr;
    570 	int flags;
    571 {
    572 
    573 	return (0);
    574 }
    575 
    576 /*
    577  * Close function is called at splaudio().
    578  */
    579 void
    580 eap_close(addr)
    581 	void *addr;
    582 {
    583 	struct eap_softc *sc = addr;
    584 
    585 	eap_halt_output(sc);
    586 	eap_halt_input(sc);
    587 
    588 	sc->sc_pintr = 0;
    589 	sc->sc_rintr = 0;
    590 }
    591 
    592 int
    593 eap_query_encoding(addr, fp)
    594 	void *addr;
    595 	struct audio_encoding *fp;
    596 {
    597 	switch (fp->index) {
    598 	case 0:
    599 		strcpy(fp->name, AudioEulinear);
    600 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    601 		fp->precision = 8;
    602 		fp->flags = 0;
    603 		return (0);
    604 	case 1:
    605 		strcpy(fp->name, AudioEmulaw);
    606 		fp->encoding = AUDIO_ENCODING_ULAW;
    607 		fp->precision = 8;
    608 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    609 		return (0);
    610 	case 2:
    611 		strcpy(fp->name, AudioEalaw);
    612 		fp->encoding = AUDIO_ENCODING_ALAW;
    613 		fp->precision = 8;
    614 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    615 		return (0);
    616 	case 3:
    617 		strcpy(fp->name, AudioEslinear);
    618 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    619 		fp->precision = 8;
    620 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    621 		return (0);
    622 	case 4:
    623 		strcpy(fp->name, AudioEslinear_le);
    624 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    625 		fp->precision = 16;
    626 		fp->flags = 0;
    627 		return (0);
    628 	case 5:
    629 		strcpy(fp->name, AudioEulinear_le);
    630 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    631 		fp->precision = 16;
    632 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    633 		return (0);
    634 	case 6:
    635 		strcpy(fp->name, AudioEslinear_be);
    636 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    637 		fp->precision = 16;
    638 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    639 		return (0);
    640 	case 7:
    641 		strcpy(fp->name, AudioEulinear_be);
    642 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    643 		fp->precision = 16;
    644 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    645 		return (0);
    646 	default:
    647 		return (EINVAL);
    648 	}
    649 }
    650 
    651 int
    652 eap_set_params(addr, setmode, usemode, play, rec)
    653 	void *addr;
    654 	int setmode, usemode;
    655 	struct audio_params *play, *rec;
    656 {
    657 	struct eap_softc *sc = addr;
    658 	struct audio_params *p;
    659 	u_int32_t mode, div;
    660 
    661 	/*
    662 	 * This device only has one clock, so make the sample rates match.
    663 	 */
    664 	if (play->sample_rate != rec->sample_rate &&
    665 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    666 		if (setmode == AUMODE_PLAY) {
    667 			rec->sample_rate = play->sample_rate;
    668 			setmode |= AUMODE_RECORD;
    669 		} else if (setmode == AUMODE_RECORD) {
    670 			play->sample_rate = rec->sample_rate;
    671 			setmode |= AUMODE_PLAY;
    672 		} else
    673 			return (EINVAL);
    674 	}
    675 
    676 	for (mode = AUMODE_RECORD; mode != -1;
    677 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    678 		if ((setmode & mode) == 0)
    679 			continue;
    680 
    681 		p = mode == AUMODE_PLAY ? play : rec;
    682 
    683 		if (p->sample_rate < 4000 || p->sample_rate > 50000 ||
    684 		    (p->precision != 8 && p->precision != 16) ||
    685 		    (p->channels != 1 && p->channels != 2))
    686 			return (EINVAL);
    687 
    688 		p->factor = 1;
    689 		p->sw_code = 0;
    690 		switch (p->encoding) {
    691 		case AUDIO_ENCODING_SLINEAR_BE:
    692 			if (p->precision == 16)
    693 				p->sw_code = swap_bytes;
    694 			else
    695 				p->sw_code = change_sign8;
    696 			break;
    697 		case AUDIO_ENCODING_SLINEAR_LE:
    698 			if (p->precision != 16)
    699 				p->sw_code = change_sign8;
    700 			break;
    701 		case AUDIO_ENCODING_ULINEAR_BE:
    702 			if (p->precision == 16) {
    703 				if (mode == AUMODE_PLAY)
    704 					p->sw_code = swap_bytes_change_sign16;
    705 				else
    706 					p->sw_code = change_sign16_swap_bytes;
    707 			}
    708 			break;
    709 		case AUDIO_ENCODING_ULINEAR_LE:
    710 			if (p->precision == 16)
    711 				p->sw_code = change_sign16;
    712 			break;
    713 		case AUDIO_ENCODING_ULAW:
    714 			if (mode == AUMODE_PLAY) {
    715 				p->factor = 2;
    716 				p->sw_code = mulaw_to_slinear16;
    717 			} else
    718 				p->sw_code = ulinear8_to_mulaw;
    719 			break;
    720 		case AUDIO_ENCODING_ALAW:
    721 			if (mode == AUMODE_PLAY) {
    722 				p->factor = 2;
    723 				p->sw_code = alaw_to_slinear16;
    724 			} else
    725 				p->sw_code = ulinear8_to_alaw;
    726 			break;
    727 		default:
    728 			return (EINVAL);
    729 		}
    730 	}
    731 
    732 	/* Set the speed */
    733 	DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
    734 		     EREAD4(sc, EAP_ICSC)));
    735 	div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
    736 	/*
    737 	 * XXX
    738 	 * The -2 isn't documented, but seemed to make the wall time match
    739 	 * what I expect.  - mycroft
    740 	 */
    741 	if (usemode == AUMODE_RECORD)
    742 		div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ / rec->sample_rate - 2);
    743 	else
    744 		div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ / play->sample_rate - 2);
    745 	div |= EAP_CCB_INTRM;
    746 	EWRITE4(sc, EAP_ICSC, div);
    747 	DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
    748 
    749 	return (0);
    750 }
    751 
    752 int
    753 eap_round_blocksize(addr, blk)
    754 	void *addr;
    755 	int blk;
    756 {
    757 	return (blk & -32);	/* keep good alignment */
    758 }
    759 
    760 int
    761 eap_trigger_output(addr, start, end, blksize, intr, arg, param)
    762 	void *addr;
    763 	void *start, *end;
    764 	int blksize;
    765 	void (*intr) __P((void *));
    766 	void *arg;
    767 	struct audio_params *param;
    768 {
    769 	struct eap_softc *sc = addr;
    770 	struct eap_dma *p;
    771 	u_int32_t mode;
    772 	int sampshift;
    773 
    774 #ifdef DIAGNOSTIC
    775 	if (sc->sc_prun)
    776 		panic("eap_trigger_output: already running");
    777 	sc->sc_prun = 1;
    778 #endif
    779 
    780 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
    781 	    addr, start, end, blksize, intr, arg));
    782 	sc->sc_pintr = intr;
    783 	sc->sc_parg = arg;
    784 
    785 	mode = EREAD4(sc, EAP_SIC) & ~(EAP_P2_S_EB | EAP_P2_S_MB | EAP_INC_BITS);
    786 	mode |= EAP_SET_P2_ST_INC(0) | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
    787 	sampshift = 0;
    788 	if (param->precision * param->factor == 16) {
    789 		mode |= EAP_P2_S_EB;
    790 		sampshift++;
    791 	}
    792 	if (param->channels == 2) {
    793 		mode |= EAP_P2_S_MB;
    794 		sampshift++;
    795 	}
    796 	EWRITE4(sc, EAP_SIC, mode);
    797 
    798 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
    799 		;
    800 	if (!p) {
    801 		printf("eap_trigger_output: bad addr %p\n", start);
    802 		return (EINVAL);
    803 	}
    804 
    805 	DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
    806 		 (int)DMAADDR(p), EAP_SET_SIZE(0, ((end - start) >> 2) - 1)));
    807 	EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
    808 	EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
    809 	EWRITE4(sc, EAP_DAC2_SIZE, EAP_SET_SIZE(0, ((end - start) >> 2) - 1));
    810 
    811 	EWRITE2(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
    812 	mode = EREAD4(sc, EAP_ICSC) & ~EAP_DAC2_EN;
    813 	EWRITE4(sc, EAP_ICSC, mode);
    814 	mode |= EAP_DAC2_EN;
    815 	EWRITE4(sc, EAP_ICSC, mode);
    816 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", mode));
    817 
    818 	return (0);
    819 }
    820 
    821 int
    822 eap_trigger_input(addr, start, end, blksize, intr, arg, param)
    823 	void *addr;
    824 	void *start, *end;
    825 	int blksize;
    826 	void (*intr) __P((void *));
    827 	void *arg;
    828 	struct audio_params *param;
    829 {
    830 	struct eap_softc *sc = addr;
    831 	struct eap_dma *p;
    832 	u_int32_t mode;
    833 	int sampshift;
    834 
    835 #ifdef DIAGNOSTIC
    836 	if (sc->sc_rrun)
    837 		panic("eap_trigger_input: already running");
    838 	sc->sc_rrun = 1;
    839 #endif
    840 
    841 	DPRINTFN(1, ("eap_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
    842 	    addr, start, end, blksize, intr, arg));
    843 	sc->sc_rintr = intr;
    844 	sc->sc_rarg = arg;
    845 
    846 	mode = EREAD4(sc, EAP_SIC) & ~(EAP_R1_S_EB | EAP_R1_S_MB);
    847 	sampshift = 0;
    848 	if (param->precision * param->factor == 16) {
    849 		mode |= EAP_R1_S_EB;
    850 		sampshift++;
    851 	}
    852 	if (param->channels == 2) {
    853 		mode |= EAP_R1_S_MB;
    854 		sampshift++;
    855 	}
    856 	EWRITE4(sc, EAP_SIC, mode);
    857 
    858 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
    859 		;
    860 	if (!p) {
    861 		printf("eap_trigger_input: bad addr %p\n", start);
    862 		return (EINVAL);
    863 	}
    864 
    865 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
    866 		 (int)DMAADDR(p), EAP_SET_SIZE(0, ((end - start) >> 2) - 1)));
    867 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
    868 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
    869 	EWRITE4(sc, EAP_ADC_SIZE, EAP_SET_SIZE(0, ((end - start) >> 2) - 1));
    870 
    871 	EWRITE2(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
    872 	mode = EREAD4(sc, EAP_ICSC) & ~EAP_ADC_EN;
    873 	EWRITE4(sc, EAP_ICSC, mode);
    874 	mode |= EAP_ADC_EN;
    875 	EWRITE4(sc, EAP_ICSC, mode);
    876 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", mode));
    877 
    878 	return (0);
    879 }
    880 
    881 int
    882 eap_halt_output(addr)
    883 	void *addr;
    884 {
    885 	struct eap_softc *sc = addr;
    886 	u_int32_t mode;
    887 
    888 	DPRINTF(("eap: eap_halt_output\n"));
    889 	mode = EREAD4(sc, EAP_ICSC) & ~EAP_DAC2_EN;
    890 	EWRITE4(sc, EAP_ICSC, mode);
    891 #ifdef DIAGNOSTIC
    892 	sc->sc_prun = 0;
    893 #endif
    894 	return (0);
    895 }
    896 
    897 int
    898 eap_halt_input(addr)
    899 	void *addr;
    900 {
    901 	struct eap_softc *sc = addr;
    902 	u_int32_t mode;
    903 
    904 	DPRINTF(("eap: eap_halt_input\n"));
    905 	mode = EREAD4(sc, EAP_ICSC) & ~EAP_ADC_EN;
    906 	EWRITE4(sc, EAP_ICSC, mode);
    907 #ifdef DIAGNOSTIC
    908 	sc->sc_rrun = 0;
    909 #endif
    910 	return (0);
    911 }
    912 
    913 int
    914 eap_getdev(addr, retp)
    915 	void *addr;
    916 	struct audio_device *retp;
    917 {
    918 	*retp = eap_device;
    919 	return (0);
    920 }
    921 
    922 void
    923 eap_set_mixer(sc, a, d)
    924 	struct eap_softc *sc;
    925 	int a, d;
    926 {
    927 	eap_write_codec(sc, a, d);
    928 	DPRINTFN(1, ("eap_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
    929 }
    930 
    931 
    932 int
    933 eap_mixer_set_port(addr, cp)
    934 	void *addr;
    935 	mixer_ctrl_t *cp;
    936 {
    937 	struct eap_softc *sc = addr;
    938 	int lval, rval, l, r, la, ra;
    939 	int l1, r1, l2, r2, m, o1, o2;
    940 
    941 	if (cp->dev == EAP_RECORD_SOURCE) {
    942 		if (cp->type != AUDIO_MIXER_SET)
    943 			return (EINVAL);
    944 		m = sc->sc_record_source = cp->un.mask;
    945 		l1 = l2 = r1 = r2 = 0;
    946 		if (m & (1 << EAP_VOICE_VOL))
    947 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
    948 		if (m & (1 << EAP_FM_VOL))
    949 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
    950 		if (m & (1 << EAP_CD_VOL))
    951 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
    952 		if (m & (1 << EAP_LINE_VOL))
    953 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
    954 		if (m & (1 << EAP_AUX_VOL))
    955 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
    956 		if (m & (1 << EAP_MIC_VOL))
    957 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
    958 		eap_set_mixer(sc, AK_IN_MIXER1_L, l1);
    959 		eap_set_mixer(sc, AK_IN_MIXER1_R, r1);
    960 		eap_set_mixer(sc, AK_IN_MIXER2_L, l2);
    961 		eap_set_mixer(sc, AK_IN_MIXER2_R, r2);
    962 		return (0);
    963 	}
    964 	if (cp->dev == EAP_OUTPUT_SELECT) {
    965 		if (cp->type != AUDIO_MIXER_SET)
    966 			return (EINVAL);
    967 		m = sc->sc_output_source = cp->un.mask;
    968 		o1 = o2 = 0;
    969 		if (m & (1 << EAP_VOICE_VOL))
    970 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
    971 		if (m & (1 << EAP_FM_VOL))
    972 			o1 |= AK_M_FM_L | AK_M_FM_R;
    973 		if (m & (1 << EAP_CD_VOL))
    974 			o1 |= AK_M_CD_L | AK_M_CD_R;
    975 		if (m & (1 << EAP_LINE_VOL))
    976 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
    977 		if (m & (1 << EAP_AUX_VOL))
    978 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
    979 		if (m & (1 << EAP_MIC_VOL))
    980 			o1 |= AK_M_MIC;
    981 		eap_set_mixer(sc, AK_OUT_MIXER1, o1);
    982 		eap_set_mixer(sc, AK_OUT_MIXER2, o2);
    983 		return (0);
    984 	}
    985 	if (cp->dev == EAP_MIC_PREAMP) {
    986 		if (cp->type != AUDIO_MIXER_ENUM)
    987 			return (EINVAL);
    988 		if (cp->un.ord != 0 && cp->un.ord != 1)
    989 			return (EINVAL);
    990 		sc->sc_mic_preamp = cp->un.ord;
    991 		eap_set_mixer(sc, AK_MGAIN, cp->un.ord);
    992 		return (0);
    993 	}
    994 	if (cp->type != AUDIO_MIXER_VALUE)
    995 		return (EINVAL);
    996 	if (cp->un.value.num_channels == 1)
    997 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    998 	else if (cp->un.value.num_channels == 2) {
    999 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1000 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1001 	} else
   1002 		return (EINVAL);
   1003 	ra = -1;
   1004 	switch (cp->dev) {
   1005 	case EAP_MASTER_VOL:
   1006 		l = VOL_TO_ATT5(lval);
   1007 		r = VOL_TO_ATT5(rval);
   1008 		la = AK_MASTER_L;
   1009 		ra = AK_MASTER_R;
   1010 		break;
   1011 	case EAP_MIC_VOL:
   1012 		if (cp->un.value.num_channels != 1)
   1013 			return (EINVAL);
   1014 		la = AK_MIC;
   1015 		goto lr;
   1016 	case EAP_VOICE_VOL:
   1017 		la = AK_VOICE_L;
   1018 		ra = AK_VOICE_R;
   1019 		goto lr;
   1020 	case EAP_FM_VOL:
   1021 		la = AK_FM_L;
   1022 		ra = AK_FM_R;
   1023 		goto lr;
   1024 	case EAP_CD_VOL:
   1025 		la = AK_CD_L;
   1026 		ra = AK_CD_R;
   1027 		goto lr;
   1028 	case EAP_LINE_VOL:
   1029 		la = AK_LINE_L;
   1030 		ra = AK_LINE_R;
   1031 		goto lr;
   1032 	case EAP_AUX_VOL:
   1033 		la = AK_AUX_L;
   1034 		ra = AK_AUX_R;
   1035 	lr:
   1036 		l = VOL_TO_GAIN5(lval);
   1037 		r = VOL_TO_GAIN5(rval);
   1038 		break;
   1039 	default:
   1040 		return (EINVAL);
   1041 	}
   1042 	eap_set_mixer(sc, la, l);
   1043 	sc->sc_port[la] = l;
   1044 	if (ra >= 0) {
   1045 		eap_set_mixer(sc, ra, r);
   1046 		sc->sc_port[ra] = r;
   1047 	}
   1048 	return (0);
   1049 }
   1050 
   1051 int
   1052 eap_mixer_get_port(addr, cp)
   1053 	void *addr;
   1054 	mixer_ctrl_t *cp;
   1055 {
   1056 	struct eap_softc *sc = addr;
   1057 	int la, ra, l, r;
   1058 
   1059 	switch (cp->dev) {
   1060 	case EAP_RECORD_SOURCE:
   1061 		if (cp->type != AUDIO_MIXER_SET)
   1062 			return (EINVAL);
   1063 		cp->un.mask = sc->sc_record_source;
   1064 		return (0);
   1065 	case EAP_OUTPUT_SELECT:
   1066 		if (cp->type != AUDIO_MIXER_SET)
   1067 			return (EINVAL);
   1068 		cp->un.mask = sc->sc_output_source;
   1069 		return (0);
   1070 	case EAP_MIC_PREAMP:
   1071 		if (cp->type != AUDIO_MIXER_ENUM)
   1072 			return (EINVAL);
   1073 		cp->un.ord = sc->sc_mic_preamp;
   1074 		return (0);
   1075 	case EAP_MASTER_VOL:
   1076 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
   1077 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
   1078 		break;
   1079 	case EAP_MIC_VOL:
   1080 		if (cp->un.value.num_channels != 1)
   1081 			return (EINVAL);
   1082 		la = ra = AK_MIC;
   1083 		goto lr;
   1084 	case EAP_VOICE_VOL:
   1085 		la = AK_VOICE_L;
   1086 		ra = AK_VOICE_R;
   1087 		goto lr;
   1088 	case EAP_FM_VOL:
   1089 		la = AK_FM_L;
   1090 		ra = AK_FM_R;
   1091 		goto lr;
   1092 	case EAP_CD_VOL:
   1093 		la = AK_CD_L;
   1094 		ra = AK_CD_R;
   1095 		goto lr;
   1096 	case EAP_LINE_VOL:
   1097 		la = AK_LINE_L;
   1098 		ra = AK_LINE_R;
   1099 		goto lr;
   1100 	case EAP_AUX_VOL:
   1101 		la = AK_AUX_L;
   1102 		ra = AK_AUX_R;
   1103 	lr:
   1104 		l = GAIN5_TO_VOL(sc->sc_port[la]);
   1105 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
   1106 		break;
   1107 	default:
   1108 		return (EINVAL);
   1109 	}
   1110 	if (cp->un.value.num_channels == 1)
   1111 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
   1112 	else if (cp->un.value.num_channels == 2) {
   1113 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
   1114 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1115 	} else
   1116 		return (EINVAL);
   1117 	return (0);
   1118 }
   1119 
   1120 int
   1121 eap_query_devinfo(addr, dip)
   1122 	void *addr;
   1123 	mixer_devinfo_t *dip;
   1124 {
   1125 	switch (dip->index) {
   1126 	case EAP_MASTER_VOL:
   1127 		dip->type = AUDIO_MIXER_VALUE;
   1128 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1129 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1130 		strcpy(dip->label.name, AudioNmaster);
   1131 		dip->un.v.num_channels = 2;
   1132 		strcpy(dip->un.v.units.name, AudioNvolume);
   1133 		return (0);
   1134 	case EAP_VOICE_VOL:
   1135 		dip->type = AUDIO_MIXER_VALUE;
   1136 		dip->mixer_class = EAP_INPUT_CLASS;
   1137 		dip->prev = AUDIO_MIXER_LAST;
   1138 		dip->next = AUDIO_MIXER_LAST;
   1139 		strcpy(dip->label.name, AudioNdac);
   1140 		dip->un.v.num_channels = 2;
   1141 		strcpy(dip->un.v.units.name, AudioNvolume);
   1142 		return (0);
   1143 	case EAP_FM_VOL:
   1144 		dip->type = AUDIO_MIXER_VALUE;
   1145 		dip->mixer_class = EAP_INPUT_CLASS;
   1146 		dip->prev = AUDIO_MIXER_LAST;
   1147 		dip->next = AUDIO_MIXER_LAST;
   1148 		strcpy(dip->label.name, AudioNfmsynth);
   1149 		dip->un.v.num_channels = 2;
   1150 		strcpy(dip->un.v.units.name, AudioNvolume);
   1151 		return (0);
   1152 	case EAP_CD_VOL:
   1153 		dip->type = AUDIO_MIXER_VALUE;
   1154 		dip->mixer_class = EAP_INPUT_CLASS;
   1155 		dip->prev = AUDIO_MIXER_LAST;
   1156 		dip->next = AUDIO_MIXER_LAST;
   1157 		strcpy(dip->label.name, AudioNcd);
   1158 		dip->un.v.num_channels = 2;
   1159 		strcpy(dip->un.v.units.name, AudioNvolume);
   1160 		return (0);
   1161 	case EAP_LINE_VOL:
   1162 		dip->type = AUDIO_MIXER_VALUE;
   1163 		dip->mixer_class = EAP_INPUT_CLASS;
   1164 		dip->prev = AUDIO_MIXER_LAST;
   1165 		dip->next = AUDIO_MIXER_LAST;
   1166 		strcpy(dip->label.name, AudioNline);
   1167 		dip->un.v.num_channels = 2;
   1168 		strcpy(dip->un.v.units.name, AudioNvolume);
   1169 		return (0);
   1170 	case EAP_AUX_VOL:
   1171 		dip->type = AUDIO_MIXER_VALUE;
   1172 		dip->mixer_class = EAP_INPUT_CLASS;
   1173 		dip->prev = AUDIO_MIXER_LAST;
   1174 		dip->next = AUDIO_MIXER_LAST;
   1175 		strcpy(dip->label.name, AudioNaux);
   1176 		dip->un.v.num_channels = 2;
   1177 		strcpy(dip->un.v.units.name, AudioNvolume);
   1178 		return (0);
   1179 	case EAP_MIC_VOL:
   1180 		dip->type = AUDIO_MIXER_VALUE;
   1181 		dip->mixer_class = EAP_INPUT_CLASS;
   1182 		dip->prev = AUDIO_MIXER_LAST;
   1183 		dip->next = EAP_MIC_PREAMP;
   1184 		strcpy(dip->label.name, AudioNmicrophone);
   1185 		dip->un.v.num_channels = 1;
   1186 		strcpy(dip->un.v.units.name, AudioNvolume);
   1187 		return (0);
   1188 	case EAP_RECORD_SOURCE:
   1189 		dip->mixer_class = EAP_RECORD_CLASS;
   1190 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1191 		strcpy(dip->label.name, AudioNsource);
   1192 		dip->type = AUDIO_MIXER_SET;
   1193 		dip->un.s.num_mem = 6;
   1194 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1195 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1196 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1197 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1198 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1199 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1200 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1201 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1202 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1203 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1204 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1205 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1206 		return (0);
   1207 	case EAP_OUTPUT_SELECT:
   1208 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1209 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1210 		strcpy(dip->label.name, AudioNselect);
   1211 		dip->type = AUDIO_MIXER_SET;
   1212 		dip->un.s.num_mem = 6;
   1213 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1214 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1215 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1216 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1217 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1218 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1219 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1220 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1221 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1222 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1223 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1224 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1225 		return (0);
   1226 	case EAP_MIC_PREAMP:
   1227 		dip->type = AUDIO_MIXER_ENUM;
   1228 		dip->mixer_class = EAP_INPUT_CLASS;
   1229 		dip->prev = EAP_MIC_VOL;
   1230 		dip->next = AUDIO_MIXER_LAST;
   1231 		strcpy(dip->label.name, AudioNpreamp);
   1232 		dip->un.e.num_mem = 2;
   1233 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1234 		dip->un.e.member[0].ord = 0;
   1235 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1236 		dip->un.e.member[1].ord = 1;
   1237 		return (0);
   1238 	case EAP_OUTPUT_CLASS:
   1239 		dip->type = AUDIO_MIXER_CLASS;
   1240 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1241 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1242 		strcpy(dip->label.name, AudioCoutputs);
   1243 		return (0);
   1244 	case EAP_RECORD_CLASS:
   1245 		dip->type = AUDIO_MIXER_CLASS;
   1246 		dip->mixer_class = EAP_RECORD_CLASS;
   1247 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1248 		strcpy(dip->label.name, AudioCrecord);
   1249 		return (0);
   1250 	case EAP_INPUT_CLASS:
   1251 		dip->type = AUDIO_MIXER_CLASS;
   1252 		dip->mixer_class = EAP_INPUT_CLASS;
   1253 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1254 		strcpy(dip->label.name, AudioCinputs);
   1255 		return (0);
   1256 	}
   1257 	return (ENXIO);
   1258 }
   1259 
   1260 void *
   1261 eap_malloc(addr, size, pool, flags)
   1262 	void *addr;
   1263 	u_long size;
   1264 	int pool;
   1265 	int flags;
   1266 {
   1267 	struct eap_softc *sc = addr;
   1268 	struct eap_dma *p;
   1269 	int error;
   1270 
   1271 	p = malloc(sizeof(*p), pool, flags);
   1272 	if (!p)
   1273 		return (0);
   1274 	error = eap_allocmem(sc, size, 16, p);
   1275 	if (error) {
   1276 		free(p, pool);
   1277 		return (0);
   1278 	}
   1279 	p->next = sc->sc_dmas;
   1280 	sc->sc_dmas = p;
   1281 	return (KERNADDR(p));
   1282 }
   1283 
   1284 void
   1285 eap_free(addr, ptr, pool)
   1286 	void *addr;
   1287 	void *ptr;
   1288 	int pool;
   1289 {
   1290 	struct eap_softc *sc = addr;
   1291 	struct eap_dma **p;
   1292 
   1293 	for (p = &sc->sc_dmas; *p; p = &(*p)->next) {
   1294 		if (KERNADDR(*p) == ptr) {
   1295 			eap_freemem(sc, *p);
   1296 			*p = (*p)->next;
   1297 			free(*p, pool);
   1298 			return;
   1299 		}
   1300 	}
   1301 }
   1302 
   1303 u_long
   1304 eap_round(addr, size)
   1305 	void *addr;
   1306 	u_long size;
   1307 {
   1308 	return (size);
   1309 }
   1310 
   1311 int
   1312 eap_mappage(addr, mem, off, prot)
   1313 	void *addr;
   1314 	void *mem;
   1315 	int off;
   1316 	int prot;
   1317 {
   1318 	struct eap_softc *sc = addr;
   1319 	struct eap_dma *p;
   1320 
   1321 	if (off < 0)
   1322 		return (-1);
   1323 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1324 		;
   1325 	if (!p)
   1326 		return (-1);
   1327 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1328 				off, prot, BUS_DMA_WAITOK));
   1329 }
   1330 
   1331 int
   1332 eap_get_props(addr)
   1333 	void *addr;
   1334 {
   1335 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
   1336 }
   1337