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