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