Home | History | Annotate | Line # | Download | only in pci
eap.c revision 1.29
      1 /*	$NetBSD: eap.c,v 1.29 1999/10/29 23:03:18 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 
    494 	int icss, to = EAP_WRITE_TIMEOUT;
    495 
    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(to && icss & EAP_CWRIP);  /* XXX could use CSTAT here */
    504         EWRITE4(sc, EAP_CODEC, EAP_SET_CODEC(a, d));
    505 }
    506 
    507 
    508 int
    509 eap1371_read_codec(sc_, a, d)
    510         void *sc_;
    511 	u_int8_t a;
    512 	u_int16_t *d;
    513 {
    514 	struct eap_softc *sc = sc_;
    515         int to;
    516         int cdc;
    517 
    518         to = EAP_WRITE_TIMEOUT;
    519         do {
    520                 cdc = EREAD4(sc, E1371_CODEC);
    521                 if (!to--) {
    522                         printf("eap: timeout writing to codec\n");
    523                         return 1;
    524                 }
    525         } while (cdc & E1371_CODEC_WIP);
    526 
    527         /* just do it */
    528 	eap1371_src_wait(sc);
    529         EWRITE4(sc, E1371_CODEC, E1371_SET_CODEC(a, 0) | E1371_CODEC_READ);
    530 
    531 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    532 		if ((cdc = EREAD4(sc, E1371_CODEC)) & E1371_CODEC_VALID)
    533 			break;
    534 	}
    535 
    536 	if (to == EAP_WRITE_TIMEOUT) {
    537 		DPRINTF(("eap1371: read codec timeout\n"));
    538 	}
    539 
    540 	*d = cdc & 0xffff;
    541 
    542         DPRINTFN(10, ("eap1371: reading codec (%x) = %x\n", a, *d));
    543 
    544 	return (0);
    545 }
    546 
    547 int
    548 eap1371_write_codec(sc_, a, d)
    549         void *sc_;
    550 	u_int8_t a;
    551 	u_int16_t d;
    552 {
    553 	struct eap_softc *sc = sc_;
    554         int to;
    555         int cdc;
    556 
    557         to = EAP_WRITE_TIMEOUT;
    558         do {
    559                 cdc = EREAD4(sc, E1371_CODEC);
    560                 if (!to--) {
    561                         printf("eap: timeout writing to codec\n");
    562                         return 1;
    563                 }
    564         } while (cdc & E1371_CODEC_WIP);
    565 
    566         /* just do it */
    567 	eap1371_src_wait(sc);
    568         EWRITE4(sc, E1371_CODEC, E1371_SET_CODEC(a, d));
    569         DPRINTFN(10, ("eap1371: writing codec %x --> %x\n", d, a));
    570 
    571         return (0);
    572 }
    573 
    574 void
    575 eap1371_src_wait(sc)
    576 	struct eap_softc *sc;
    577 {
    578         int to;
    579         int src;
    580 
    581         to = EAP_READ_TIMEOUT;
    582         do {
    583                 src = EREAD4(sc, E1371_SRC);
    584                 if (!to--) {
    585                         printf("eap: timeout waiting for sample rate"
    586                                 "converter\n");
    587                         return;
    588                 }
    589         } while (src & E1371_SRC_RBUSY);
    590 }
    591 
    592 int
    593 eap1371_src_read(sc, a)
    594 	struct eap_softc *sc;
    595 	int a;
    596 {
    597 	int r;
    598 
    599 	eap1371_src_wait(sc);
    600 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE | E1371_SRC_DISP1 |
    601 				     E1371_SRC_DISP2 | E1371_SRC_DISREC);
    602 	r |= E1371_SRC_ADDR(a);
    603 	EWRITE4(sc, E1371_SRC, r);
    604 	r = EREAD4(sc, E1371_SRC) & E1371_SRC_DATAMASK;
    605 	return r;
    606 }
    607 
    608 void
    609 eap1371_src_write(sc, a, d)
    610 	struct eap_softc *sc;
    611 	int a,d;
    612 {
    613 	int r;
    614 
    615 	eap1371_src_wait(sc);
    616 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE | E1371_SRC_DISP1 |
    617 				     E1371_SRC_DISP2 | E1371_SRC_DISREC);
    618 	r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
    619 	EWRITE4(sc, E1371_SRC, r);
    620 }
    621 
    622 void
    623 eap1371_set_adc_rate(sc, rate)
    624 	struct eap_softc *sc;
    625 	int rate;
    626 {
    627 	int freq, n, truncm;
    628 	int out;
    629 
    630         /* Whatever, it works, so I'll leave it :) */
    631 
    632         if (rate > 48000)
    633             rate = 48000;
    634         if (rate < 4000)
    635             rate = 4000;
    636         n = rate / 3000;
    637         if ((1 << n) & SRC_MAGIC)
    638                 n--;
    639         truncm = ((21 * n) - 1) | 1;
    640         freq = ((48000 << 15) / rate) * n;
    641         if (rate >= 24000) {
    642                 if (truncm > 239)
    643                         truncm = 239;
    644 		out = ESRC_SET_TRUNC((239 - truncm) / 2);
    645         } else {
    646                 if (truncm > 119)
    647                         truncm = 119;
    648 		out = ESRC_SMF | ESRC_SET_TRUNC((119 - truncm) / 2);
    649         }
    650  	out |= ESRC_SET_N(n);
    651         eap1371_src_write(sc, ESRC_ADC+ESRC_TRUNC_N, out);
    652 
    653 
    654         out = eap1371_src_read(sc, ESRC_ADC+ESRC_IREGS) & 0xff;
    655         eap1371_src_write(sc, ESRC_ADC+ESRC_IREGS, out |
    656 			  ESRC_SET_VFI(freq >> 15));
    657         eap1371_src_write(sc, ESRC_ADC+ESRC_VFF, freq & 0x7fff);
    658         eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(n));
    659         eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(n));
    660 }
    661 
    662 void
    663 eap1371_set_dac_rate(sc, rate, which)
    664 	struct eap_softc *sc;
    665 	int rate;
    666 	int which;
    667 {
    668         int dac = (which == 1) ? ESRC_DAC1 : ESRC_DAC2;
    669 	int freq, r;
    670 
    671         /* Whatever, it works, so I'll leave it :) */
    672 
    673         if (rate > 48000)
    674             rate = 48000;
    675         if (rate < 4000)
    676             rate = 4000;
    677         freq = (rate << 15) / 3000;
    678 
    679         eap1371_src_wait(sc);
    680         r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    681             E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    682         r |= (which == 1) ? E1371_SRC_DISP1 : E1371_SRC_DISP2;
    683         EWRITE4(sc, E1371_SRC, r);
    684         r = eap1371_src_read(sc, dac + ESRC_IREGS) & 0x00ff;
    685         eap1371_src_write(sc, dac + ESRC_IREGS, r | ((freq >> 5) & 0xfc00));
    686         eap1371_src_write(sc, dac + ESRC_VFF, freq & 0x7fff);
    687         r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    688             E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    689         r &= ~((which == 1) ? E1371_SRC_DISP1 : E1371_SRC_DISP2);
    690         EWRITE4(sc, E1371_SRC, r);
    691 }
    692 
    693 void
    694 eap_attach(parent, self, aux)
    695 	struct device *parent;
    696 	struct device *self;
    697 	void *aux;
    698 {
    699 	struct eap_softc *sc = (struct eap_softc *)self;
    700 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    701 	pci_chipset_tag_t pc = pa->pa_pc;
    702 	char const *intrstr;
    703 	pci_intr_handle_t ih;
    704 	pcireg_t csr;
    705 	mixer_ctrl_t ctl;
    706 	int i;
    707 
    708         /* Flag if we're "creative" */
    709 	sc->sc_1371 = PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI97;
    710 
    711 	printf(": Ensoniq AudioPCI%s (rev. 0x%02x)\n",
    712 	    sc->sc_1371 ? "97" : "", PCI_REVISION(pa->pa_class));
    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 		int error;
    790 
    791                 /* clean slate */
    792                 EWRITE4(sc, EAP_SIC, 0);
    793                 EWRITE4(sc, EAP_ICSC, 0);
    794                 EWRITE4(sc, E1371_LEGACY, 0);
    795 
    796                 /* Reset from es1371's perspective */
    797                 EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
    798                 delay(20);
    799                 EWRITE4(sc, EAP_ICSC, 0);
    800 
    801                 /* must properly reprogram sample rate converter,
    802                  * or it locks up.  Set some defaults for the life of the
    803                  * machine, and set up a sb default sample rate.
    804                  */
    805                 EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
    806                 for (i=0; i<0x80; i++)
    807                         eap1371_src_write(sc, i, 0);
    808 		eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
    809 		eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
    810                 eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
    811                 eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
    812                 eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
    813                 eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
    814 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
    815 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
    816 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
    817 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
    818                 eap1371_set_adc_rate(sc, 22050);
    819                 eap1371_set_dac_rate(sc, 22050, 1);
    820                 eap1371_set_dac_rate(sc, 22050, 2);
    821 
    822                 EWRITE4(sc, E1371_SRC, 0);
    823 
    824                 /* Reset codec */
    825 
    826 		/* Interrupt enable */
    827 		sc->host_if.arg = sc;
    828 		sc->host_if.attach = eap1371_attach_codec;
    829 		sc->host_if.read = eap1371_read_codec;
    830 		sc->host_if.write = eap1371_write_codec;
    831 		sc->host_if.reset = eap1371_reset_codec;
    832 
    833 		if ((error = ac97_attach(&sc->host_if)) == 0) {
    834 			/* Interrupt enable */
    835 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    836 		} else
    837 			return;
    838 
    839 		eap_hw_if.query_devinfo = eap1371_query_devinfo;
    840 		eap_hw_if.set_port = eap1371_mixer_set_port;
    841 		eap_hw_if.get_port = eap1371_mixer_get_port;
    842 
    843 		/* Just enable the DAC and master volumes by default */
    844 		ctl.type = AUDIO_MIXER_ENUM;
    845 		ctl.un.ord = 0;  /* off */
    846 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCoutputs,
    847 		       AudioNmaster, AudioNmute);
    848 		eap1371_mixer_set_port(sc, &ctl);
    849 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCinputs,
    850 		       AudioNdac, AudioNmute);
    851 		eap1371_mixer_set_port(sc, &ctl);
    852 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
    853 		       AudioNvolume, AudioNmute);
    854 		eap1371_mixer_set_port(sc, &ctl);
    855 
    856 
    857 		ctl.dev = eap1371_get_portnum_by_name(sc, AudioCrecord,
    858 		       AudioNsource, NULL);
    859 		ctl.type = AUDIO_MIXER_ENUM;
    860 		ctl.un.ord = 0;
    861 		eap1371_mixer_set_port(sc, &ctl);
    862 
    863         }
    864 
    865 	audio_attach_mi(&eap_hw_if, sc, &sc->sc_dev);
    866 
    867 #if NMIDI > 0
    868 	midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
    869 #endif
    870 }
    871 
    872 int
    873 eap1371_attach_codec(sc_, codec_if)
    874 	void *sc_;
    875 	struct ac97_codec_if  *codec_if;
    876 {
    877 	struct eap_softc *sc = sc_;
    878 
    879 	sc->codec_if = codec_if;
    880 	return (0);
    881 }
    882 
    883 void
    884 eap1371_reset_codec(sc_)
    885 	void *sc_;
    886 {
    887 	struct eap_softc *sc = sc_;
    888 	u_int32_t icsc = EREAD4(sc, EAP_ICSC);
    889 
    890 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
    891 	delay(100);
    892 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
    893 
    894 	return;
    895 }
    896 
    897 int
    898 eap_intr(p)
    899 	void *p;
    900 {
    901 	struct eap_softc *sc = p;
    902 	u_int32_t intr, sic;
    903 
    904 	intr = EREAD4(sc, EAP_ICSS);
    905 	if (!(intr & EAP_INTR))
    906 		return (0);
    907 	sic = EREAD4(sc, EAP_SIC);
    908 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
    909 	if (intr & EAP_I_ADC) {
    910 		/*
    911 		 * XXX This is a hack!
    912 		 * The EAP chip sometimes generates the recording interrupt
    913 		 * while it is still transferring the data.  To make sure
    914 		 * it has all arrived we busy wait until the count is right.
    915 		 * The transfer we are waiting for is 8 longwords.
    916 		 */
    917 		int s, nw, n;
    918 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
    919 		s = EREAD4(sc, EAP_ADC_CSR);
    920 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
    921 		n = 0;
    922 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
    923 			delay(10);
    924 			if (++n > 100) {
    925 				printf("eapintr: dma fix timeout");
    926 				break;
    927 			}
    928 		}
    929 		/* Continue with normal interrupt handling. */
    930 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
    931 		EWRITE4(sc, EAP_SIC, sic);
    932 		if (sc->sc_rintr)
    933 			sc->sc_rintr(sc->sc_rarg);
    934 	}
    935 	if (intr & EAP_I_DAC2) {
    936 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
    937 		EWRITE4(sc, EAP_SIC, sic);
    938 		if (sc->sc_pintr)
    939 			sc->sc_pintr(sc->sc_parg);
    940 	}
    941 #if NMIDI > 0
    942 	if (intr & EAP_I_UART) {
    943 		u_int32_t data;
    944 
    945 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXINT) {
    946 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
    947 				data = EREAD1(sc, EAP_UART_DATA);
    948 				if (sc->sc_iintr)
    949 					sc->sc_iintr(sc->sc_arg, data);
    950 			}
    951 		}
    952 	}
    953 #endif
    954 	return (1);
    955 }
    956 
    957 int
    958 eap_allocmem(sc, size, align, p)
    959 	struct eap_softc *sc;
    960 	size_t size;
    961 	size_t align;
    962 	struct eap_dma *p;
    963 {
    964 	int error;
    965 
    966 	p->size = size;
    967 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    968 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    969 				 &p->nsegs, BUS_DMA_NOWAIT);
    970 	if (error)
    971 		return (error);
    972 
    973 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    974 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    975 	if (error)
    976 		goto free;
    977 
    978 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    979 				  0, BUS_DMA_NOWAIT, &p->map);
    980 	if (error)
    981 		goto unmap;
    982 
    983 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    984 				BUS_DMA_NOWAIT);
    985 	if (error)
    986 		goto destroy;
    987 	return (0);
    988 
    989 destroy:
    990 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    991 unmap:
    992 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    993 free:
    994 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    995 	return (error);
    996 }
    997 
    998 int
    999 eap_freemem(sc, p)
   1000 	struct eap_softc *sc;
   1001 	struct eap_dma *p;
   1002 {
   1003 	bus_dmamap_unload(sc->sc_dmatag, p->map);
   1004 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1005 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1006 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1007 	return (0);
   1008 }
   1009 
   1010 int
   1011 eap_open(addr, flags)
   1012 	void *addr;
   1013 	int flags;
   1014 {
   1015 	return (0);
   1016 }
   1017 
   1018 /*
   1019  * Close function is called at splaudio().
   1020  */
   1021 void
   1022 eap_close(addr)
   1023 	void *addr;
   1024 {
   1025 	struct eap_softc *sc = addr;
   1026 
   1027 	eap_halt_output(sc);
   1028 	eap_halt_input(sc);
   1029 
   1030 	sc->sc_pintr = 0;
   1031 	sc->sc_rintr = 0;
   1032 }
   1033 
   1034 int
   1035 eap_query_encoding(addr, fp)
   1036 	void *addr;
   1037 	struct audio_encoding *fp;
   1038 {
   1039 	switch (fp->index) {
   1040 	case 0:
   1041 		strcpy(fp->name, AudioEulinear);
   1042 		fp->encoding = AUDIO_ENCODING_ULINEAR;
   1043 		fp->precision = 8;
   1044 		fp->flags = 0;
   1045 		return (0);
   1046 	case 1:
   1047 		strcpy(fp->name, AudioEmulaw);
   1048 		fp->encoding = AUDIO_ENCODING_ULAW;
   1049 		fp->precision = 8;
   1050 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1051 		return (0);
   1052 	case 2:
   1053 		strcpy(fp->name, AudioEalaw);
   1054 		fp->encoding = AUDIO_ENCODING_ALAW;
   1055 		fp->precision = 8;
   1056 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1057 		return (0);
   1058 	case 3:
   1059 		strcpy(fp->name, AudioEslinear);
   1060 		fp->encoding = AUDIO_ENCODING_SLINEAR;
   1061 		fp->precision = 8;
   1062 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1063 		return (0);
   1064 	case 4:
   1065 		strcpy(fp->name, AudioEslinear_le);
   1066 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
   1067 		fp->precision = 16;
   1068 		fp->flags = 0;
   1069 		return (0);
   1070 	case 5:
   1071 		strcpy(fp->name, AudioEulinear_le);
   1072 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
   1073 		fp->precision = 16;
   1074 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1075 		return (0);
   1076 	case 6:
   1077 		strcpy(fp->name, AudioEslinear_be);
   1078 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
   1079 		fp->precision = 16;
   1080 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1081 		return (0);
   1082 	case 7:
   1083 		strcpy(fp->name, AudioEulinear_be);
   1084 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
   1085 		fp->precision = 16;
   1086 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1087 		return (0);
   1088 	default:
   1089 		return (EINVAL);
   1090 	}
   1091 }
   1092 
   1093 int
   1094 eap_set_params(addr, setmode, usemode, play, rec)
   1095 	void *addr;
   1096 	int setmode, usemode;
   1097 	struct audio_params *play, *rec;
   1098 {
   1099 	struct eap_softc *sc = addr;
   1100 	struct audio_params *p;
   1101 	int mode;
   1102 	u_int32_t div;
   1103 
   1104 	/*
   1105 	 * The es1370 only has one clock, so make the sample rates match.
   1106 	 */
   1107 	if (!sc->sc_1371) {
   1108 	    if (play->sample_rate != rec->sample_rate &&
   1109 		usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1110 	    	if (setmode == AUMODE_PLAY) {
   1111 		    rec->sample_rate = play->sample_rate;
   1112 		    setmode |= AUMODE_RECORD;
   1113 		} else if (setmode == AUMODE_RECORD) {
   1114 		    play->sample_rate = rec->sample_rate;
   1115 		    setmode |= AUMODE_PLAY;
   1116 		} else
   1117 		    return (EINVAL);
   1118 	    }
   1119 	}
   1120 
   1121 	for (mode = AUMODE_RECORD; mode != -1;
   1122 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1123 		if ((setmode & mode) == 0)
   1124 			continue;
   1125 
   1126 		p = mode == AUMODE_PLAY ? play : rec;
   1127 
   1128 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
   1129 		    (p->precision != 8 && p->precision != 16) ||
   1130 		    (p->channels != 1 && p->channels != 2))
   1131 			return (EINVAL);
   1132 
   1133 		p->factor = 1;
   1134 		p->sw_code = 0;
   1135 		switch (p->encoding) {
   1136 		case AUDIO_ENCODING_SLINEAR_BE:
   1137 			if (p->precision == 16)
   1138 				p->sw_code = swap_bytes;
   1139 			else
   1140 				p->sw_code = change_sign8;
   1141 			break;
   1142 		case AUDIO_ENCODING_SLINEAR_LE:
   1143 			if (p->precision != 16)
   1144 				p->sw_code = change_sign8;
   1145 			break;
   1146 		case AUDIO_ENCODING_ULINEAR_BE:
   1147 			if (p->precision == 16) {
   1148 				if (mode == AUMODE_PLAY)
   1149 					p->sw_code = swap_bytes_change_sign16;
   1150 				else
   1151 					p->sw_code = change_sign16_swap_bytes;
   1152 			}
   1153 			break;
   1154 		case AUDIO_ENCODING_ULINEAR_LE:
   1155 			if (p->precision == 16)
   1156 				p->sw_code = change_sign16;
   1157 			break;
   1158 		case AUDIO_ENCODING_ULAW:
   1159 			if (mode == AUMODE_PLAY) {
   1160 				p->factor = 2;
   1161 				p->sw_code = mulaw_to_slinear16;
   1162 			} else
   1163 				p->sw_code = ulinear8_to_mulaw;
   1164 			break;
   1165 		case AUDIO_ENCODING_ALAW:
   1166 			if (mode == AUMODE_PLAY) {
   1167 				p->factor = 2;
   1168 				p->sw_code = alaw_to_slinear16;
   1169 			} else
   1170 				p->sw_code = ulinear8_to_alaw;
   1171 			break;
   1172 		default:
   1173 			return (EINVAL);
   1174 		}
   1175 	}
   1176 
   1177         if (sc->sc_1371) {
   1178 		eap1371_set_dac_rate(sc, play->sample_rate, 1);
   1179 		eap1371_set_dac_rate(sc, play->sample_rate, 2);
   1180 		eap1371_set_adc_rate(sc, rec->sample_rate);
   1181 	} else {
   1182                 /* Set the speed */
   1183                 DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
   1184                              EREAD4(sc, EAP_ICSC)));
   1185                 div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
   1186                 /*
   1187                  * XXX
   1188                  * The -2 isn't documented, but seemed to make the wall
   1189                  * time match
   1190                  * what I expect.  - mycroft
   1191                  */
   1192                 if (usemode == AUMODE_RECORD)
   1193                         div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1194                                 rec->sample_rate - 2);
   1195                 else
   1196                         div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1197                                 play->sample_rate - 2);
   1198                 div |= EAP_CCB_INTRM;
   1199                 EWRITE4(sc, EAP_ICSC, div);
   1200                 DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1201         }
   1202 
   1203 	return (0);
   1204 }
   1205 
   1206 int
   1207 eap_round_blocksize(addr, blk)
   1208 	void *addr;
   1209 	int blk;
   1210 {
   1211 	return (blk & -32);	/* keep good alignment */
   1212 }
   1213 
   1214 int
   1215 eap_trigger_output(addr, start, end, blksize, intr, arg, param)
   1216 	void *addr;
   1217 	void *start, *end;
   1218 	int blksize;
   1219 	void (*intr) __P((void *));
   1220 	void *arg;
   1221 	struct audio_params *param;
   1222 {
   1223 	struct eap_softc *sc = addr;
   1224 	struct eap_dma *p;
   1225 	u_int32_t icsc, sic;
   1226 	int sampshift;
   1227 
   1228 #ifdef DIAGNOSTIC
   1229 	if (sc->sc_prun)
   1230 		panic("eap_trigger_output: already running");
   1231 	sc->sc_prun = 1;
   1232 #endif
   1233 
   1234 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
   1235             "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1236 	sc->sc_pintr = intr;
   1237 	sc->sc_parg = arg;
   1238 
   1239 	icsc = EREAD4(sc, EAP_ICSC);
   1240 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_DAC2_EN);
   1241 
   1242 	sic = EREAD4(sc, EAP_SIC);
   1243 	sic &= ~(EAP_P2_S_EB | EAP_P2_S_MB | EAP_INC_BITS);
   1244 	sic |= EAP_SET_P2_ST_INC(0) | EAP_SET_P2_END_INC(param->precision * param->factor / 8);
   1245 	sampshift = 0;
   1246 	if (param->precision * param->factor == 16) {
   1247 		sic |= EAP_P2_S_EB;
   1248 		sampshift++;
   1249 	}
   1250 	if (param->channels == 2) {
   1251 		sic |= EAP_P2_S_MB;
   1252 		sampshift++;
   1253 	}
   1254 	EWRITE4(sc, EAP_SIC, sic);
   1255 
   1256 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1257 		;
   1258 	if (!p) {
   1259 		printf("eap_trigger_output: bad addr %p\n", start);
   1260 		return (EINVAL);
   1261 	}
   1262 
   1263 	DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
   1264 		 (int)DMAADDR(p),
   1265 		 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
   1266 	EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1267 	EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
   1268 	EWRITE4(sc, EAP_DAC2_SIZE,
   1269 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
   1270 
   1271 	EWRITE2(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
   1272 
   1273 	EWRITE4(sc, EAP_ICSC, icsc | EAP_DAC2_EN);
   1274 
   1275 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
   1276 
   1277 	return (0);
   1278 }
   1279 
   1280 int
   1281 eap_trigger_input(addr, start, end, blksize, intr, arg, param)
   1282 	void *addr;
   1283 	void *start, *end;
   1284 	int blksize;
   1285 	void (*intr) __P((void *));
   1286 	void *arg;
   1287 	struct audio_params *param;
   1288 {
   1289 	struct eap_softc *sc = addr;
   1290 	struct eap_dma *p;
   1291 	u_int32_t icsc, sic;
   1292 	int sampshift;
   1293 
   1294 #ifdef DIAGNOSTIC
   1295 	if (sc->sc_rrun)
   1296 		panic("eap_trigger_input: already running");
   1297 	sc->sc_rrun = 1;
   1298 #endif
   1299 
   1300 	DPRINTFN(1, ("eap_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1301 	    addr, start, end, blksize, intr, arg));
   1302 	sc->sc_rintr = intr;
   1303 	sc->sc_rarg = arg;
   1304 
   1305 	icsc = EREAD4(sc, EAP_ICSC);
   1306 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
   1307 
   1308 	sic = EREAD4(sc, EAP_SIC);
   1309 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
   1310 	sampshift = 0;
   1311 	if (param->precision * param->factor == 16) {
   1312 		sic |= EAP_R1_S_EB;
   1313 		sampshift++;
   1314 	}
   1315 	if (param->channels == 2) {
   1316 		sic |= EAP_R1_S_MB;
   1317 		sampshift++;
   1318 	}
   1319 	EWRITE4(sc, EAP_SIC, sic);
   1320 
   1321 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1322 		;
   1323 	if (!p) {
   1324 		printf("eap_trigger_input: bad addr %p\n", start);
   1325 		return (EINVAL);
   1326 	}
   1327 
   1328 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
   1329 		 (int)DMAADDR(p),
   1330 		 EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
   1331 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
   1332 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
   1333 	EWRITE4(sc, EAP_ADC_SIZE,
   1334 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
   1335 
   1336 	EWRITE2(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
   1337 
   1338 	EWRITE4(sc, EAP_ICSC, icsc | EAP_ADC_EN);
   1339 
   1340 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
   1341 
   1342 	return (0);
   1343 }
   1344 
   1345 int
   1346 eap_halt_output(addr)
   1347 	void *addr;
   1348 {
   1349 	struct eap_softc *sc = addr;
   1350 	u_int32_t icsc;
   1351 
   1352 	DPRINTF(("eap: eap_halt_output\n"));
   1353 	icsc = EREAD4(sc, EAP_ICSC);
   1354 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_DAC2_EN);
   1355 #ifdef DIAGNOSTIC
   1356 	sc->sc_prun = 0;
   1357 #endif
   1358 	return (0);
   1359 }
   1360 
   1361 int
   1362 eap_halt_input(addr)
   1363 	void *addr;
   1364 {
   1365 	struct eap_softc *sc = addr;
   1366 	u_int32_t icsc;
   1367 
   1368 	DPRINTF(("eap: eap_halt_input\n"));
   1369 	icsc = EREAD4(sc, EAP_ICSC);
   1370 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
   1371 #ifdef DIAGNOSTIC
   1372 	sc->sc_rrun = 0;
   1373 #endif
   1374 	return (0);
   1375 }
   1376 
   1377 int
   1378 eap_getdev(addr, retp)
   1379 	void *addr;
   1380 	struct audio_device *retp;
   1381 {
   1382 	*retp = eap_device;
   1383 	return (0);
   1384 }
   1385 
   1386 int
   1387 eap1371_mixer_set_port(addr, cp)
   1388 	void *addr;
   1389 	mixer_ctrl_t *cp;
   1390 {
   1391 	struct eap_softc *sc = addr;
   1392 
   1393 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
   1394 }
   1395 
   1396 int
   1397 eap1371_mixer_get_port(addr, cp)
   1398 	void *addr;
   1399 	mixer_ctrl_t *cp;
   1400 {
   1401 	struct eap_softc *sc = addr;
   1402 
   1403 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
   1404 }
   1405 
   1406 int
   1407 eap1371_query_devinfo(addr, dip)
   1408 	void *addr;
   1409 	mixer_devinfo_t *dip;
   1410 {
   1411 	struct eap_softc *sc = addr;
   1412 
   1413 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
   1414 }
   1415 
   1416 int
   1417 eap1371_get_portnum_by_name(sc, class, device, qualifier)
   1418 	struct eap_softc *sc;
   1419 	char *class, *device, *qualifier;
   1420 {
   1421 	return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
   1422              device, qualifier));
   1423 }
   1424 
   1425 void
   1426 eap_set_mixer(sc, a, d)
   1427 	struct eap_softc *sc;
   1428 	int a, d;
   1429 {
   1430 	eap_write_codec(sc, a, d);
   1431 
   1432         sc->sc_port[a] = d;
   1433         DPRINTFN(1, ("eap_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
   1434 }
   1435 
   1436 int
   1437 eap_mixer_set_port(addr, cp)
   1438 	void *addr;
   1439 	mixer_ctrl_t *cp;
   1440 {
   1441 	struct eap_softc *sc = addr;
   1442 	int lval, rval, l, r, la, ra;
   1443 	int l1, r1, l2, r2, m, o1, o2;
   1444 
   1445 	if (cp->dev == EAP_RECORD_SOURCE) {
   1446 		if (cp->type != AUDIO_MIXER_SET)
   1447 			return (EINVAL);
   1448 		m = sc->sc_record_source = cp->un.mask;
   1449 		l1 = l2 = r1 = r2 = 0;
   1450 		if (m & (1 << EAP_VOICE_VOL))
   1451 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
   1452 		if (m & (1 << EAP_FM_VOL))
   1453 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
   1454 		if (m & (1 << EAP_CD_VOL))
   1455 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
   1456 		if (m & (1 << EAP_LINE_VOL))
   1457 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
   1458 		if (m & (1 << EAP_AUX_VOL))
   1459 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
   1460 		if (m & (1 << EAP_MIC_VOL))
   1461 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
   1462 		eap_set_mixer(sc, AK_IN_MIXER1_L, l1);
   1463 		eap_set_mixer(sc, AK_IN_MIXER1_R, r1);
   1464 		eap_set_mixer(sc, AK_IN_MIXER2_L, l2);
   1465 		eap_set_mixer(sc, AK_IN_MIXER2_R, r2);
   1466 		return (0);
   1467 	}
   1468 	if (cp->dev == EAP_OUTPUT_SELECT) {
   1469 		if (cp->type != AUDIO_MIXER_SET)
   1470 			return (EINVAL);
   1471 		m = sc->sc_output_source = cp->un.mask;
   1472 		o1 = o2 = 0;
   1473 		if (m & (1 << EAP_VOICE_VOL))
   1474 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
   1475 		if (m & (1 << EAP_FM_VOL))
   1476 			o1 |= AK_M_FM_L | AK_M_FM_R;
   1477 		if (m & (1 << EAP_CD_VOL))
   1478 			o1 |= AK_M_CD_L | AK_M_CD_R;
   1479 		if (m & (1 << EAP_LINE_VOL))
   1480 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
   1481 		if (m & (1 << EAP_AUX_VOL))
   1482 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
   1483 		if (m & (1 << EAP_MIC_VOL))
   1484 			o1 |= AK_M_MIC;
   1485 		eap_set_mixer(sc, AK_OUT_MIXER1, o1);
   1486 		eap_set_mixer(sc, AK_OUT_MIXER2, o2);
   1487 		return (0);
   1488 	}
   1489 	if (cp->dev == EAP_MIC_PREAMP) {
   1490 		if (cp->type != AUDIO_MIXER_ENUM)
   1491 			return (EINVAL);
   1492 		if (cp->un.ord != 0 && cp->un.ord != 1)
   1493 			return (EINVAL);
   1494 		sc->sc_mic_preamp = cp->un.ord;
   1495 		eap_set_mixer(sc, AK_MGAIN, cp->un.ord);
   1496 		return (0);
   1497 	}
   1498 	if (cp->type != AUDIO_MIXER_VALUE)
   1499 		return (EINVAL);
   1500 	if (cp->un.value.num_channels == 1)
   1501 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1502 	else if (cp->un.value.num_channels == 2) {
   1503 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1504 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1505 	} else
   1506 		return (EINVAL);
   1507 	ra = -1;
   1508 	switch (cp->dev) {
   1509 	case EAP_MASTER_VOL:
   1510 		l = VOL_TO_ATT5(lval);
   1511 		r = VOL_TO_ATT5(rval);
   1512 		la = AK_MASTER_L;
   1513 		ra = AK_MASTER_R;
   1514 		break;
   1515 	case EAP_MIC_VOL:
   1516 		if (cp->un.value.num_channels != 1)
   1517 			return (EINVAL);
   1518 		la = AK_MIC;
   1519 		goto lr;
   1520 	case EAP_VOICE_VOL:
   1521 		la = AK_VOICE_L;
   1522 		ra = AK_VOICE_R;
   1523 		goto lr;
   1524 	case EAP_FM_VOL:
   1525 		la = AK_FM_L;
   1526 		ra = AK_FM_R;
   1527 		goto lr;
   1528 	case EAP_CD_VOL:
   1529 		la = AK_CD_L;
   1530 		ra = AK_CD_R;
   1531 		goto lr;
   1532 	case EAP_LINE_VOL:
   1533 		la = AK_LINE_L;
   1534 		ra = AK_LINE_R;
   1535 		goto lr;
   1536 	case EAP_AUX_VOL:
   1537 		la = AK_AUX_L;
   1538 		ra = AK_AUX_R;
   1539 	lr:
   1540 		l = VOL_TO_GAIN5(lval);
   1541 		r = VOL_TO_GAIN5(rval);
   1542 		break;
   1543 	default:
   1544 		return (EINVAL);
   1545 	}
   1546 	eap_set_mixer(sc, la, l);
   1547 	if (ra >= 0) {
   1548 		eap_set_mixer(sc, ra, r);
   1549 	}
   1550 	return (0);
   1551 }
   1552 
   1553 int
   1554 eap_mixer_get_port(addr, cp)
   1555 	void *addr;
   1556 	mixer_ctrl_t *cp;
   1557 {
   1558 	struct eap_softc *sc = addr;
   1559 	int la, ra, l, r;
   1560 
   1561 	switch (cp->dev) {
   1562 	case EAP_RECORD_SOURCE:
   1563 		if (cp->type != AUDIO_MIXER_SET)
   1564 			return (EINVAL);
   1565 		cp->un.mask = sc->sc_record_source;
   1566 		return (0);
   1567 	case EAP_OUTPUT_SELECT:
   1568 		if (cp->type != AUDIO_MIXER_SET)
   1569 			return (EINVAL);
   1570 		cp->un.mask = sc->sc_output_source;
   1571 		return (0);
   1572 	case EAP_MIC_PREAMP:
   1573 		if (cp->type != AUDIO_MIXER_ENUM)
   1574 			return (EINVAL);
   1575 		cp->un.ord = sc->sc_mic_preamp;
   1576 		return (0);
   1577 	case EAP_MASTER_VOL:
   1578 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
   1579 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
   1580 		break;
   1581 	case EAP_MIC_VOL:
   1582 		if (cp->un.value.num_channels != 1)
   1583 			return (EINVAL);
   1584 		la = ra = AK_MIC;
   1585 		goto lr;
   1586 	case EAP_VOICE_VOL:
   1587 		la = AK_VOICE_L;
   1588 		ra = AK_VOICE_R;
   1589 		goto lr;
   1590 	case EAP_FM_VOL:
   1591 		la = AK_FM_L;
   1592 		ra = AK_FM_R;
   1593 		goto lr;
   1594 	case EAP_CD_VOL:
   1595 		la = AK_CD_L;
   1596 		ra = AK_CD_R;
   1597 		goto lr;
   1598 	case EAP_LINE_VOL:
   1599 		la = AK_LINE_L;
   1600 		ra = AK_LINE_R;
   1601 		goto lr;
   1602 	case EAP_AUX_VOL:
   1603 		la = AK_AUX_L;
   1604 		ra = AK_AUX_R;
   1605 	lr:
   1606 		l = GAIN5_TO_VOL(sc->sc_port[la]);
   1607 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
   1608 		break;
   1609 	default:
   1610 		return (EINVAL);
   1611 	}
   1612 	if (cp->un.value.num_channels == 1)
   1613 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
   1614 	else if (cp->un.value.num_channels == 2) {
   1615 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
   1616 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1617 	} else
   1618 		return (EINVAL);
   1619 	return (0);
   1620 }
   1621 
   1622 int
   1623 eap_query_devinfo(addr, dip)
   1624 	void *addr;
   1625 	mixer_devinfo_t *dip;
   1626 {
   1627 	switch (dip->index) {
   1628 	case EAP_MASTER_VOL:
   1629 		dip->type = AUDIO_MIXER_VALUE;
   1630 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1631 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1632 		strcpy(dip->label.name, AudioNmaster);
   1633 		dip->un.v.num_channels = 2;
   1634 		strcpy(dip->un.v.units.name, AudioNvolume);
   1635 		return (0);
   1636 	case EAP_VOICE_VOL:
   1637 		dip->type = AUDIO_MIXER_VALUE;
   1638 		dip->mixer_class = EAP_INPUT_CLASS;
   1639 		dip->prev = AUDIO_MIXER_LAST;
   1640 		dip->next = AUDIO_MIXER_LAST;
   1641 		strcpy(dip->label.name, AudioNdac);
   1642 		dip->un.v.num_channels = 2;
   1643 		strcpy(dip->un.v.units.name, AudioNvolume);
   1644 		return (0);
   1645 	case EAP_FM_VOL:
   1646 		dip->type = AUDIO_MIXER_VALUE;
   1647 		dip->mixer_class = EAP_INPUT_CLASS;
   1648 		dip->prev = AUDIO_MIXER_LAST;
   1649 		dip->next = AUDIO_MIXER_LAST;
   1650 		strcpy(dip->label.name, AudioNfmsynth);
   1651 		dip->un.v.num_channels = 2;
   1652 		strcpy(dip->un.v.units.name, AudioNvolume);
   1653 		return (0);
   1654 	case EAP_CD_VOL:
   1655 		dip->type = AUDIO_MIXER_VALUE;
   1656 		dip->mixer_class = EAP_INPUT_CLASS;
   1657 		dip->prev = AUDIO_MIXER_LAST;
   1658 		dip->next = AUDIO_MIXER_LAST;
   1659 		strcpy(dip->label.name, AudioNcd);
   1660 		dip->un.v.num_channels = 2;
   1661 		strcpy(dip->un.v.units.name, AudioNvolume);
   1662 		return (0);
   1663 	case EAP_LINE_VOL:
   1664 		dip->type = AUDIO_MIXER_VALUE;
   1665 		dip->mixer_class = EAP_INPUT_CLASS;
   1666 		dip->prev = AUDIO_MIXER_LAST;
   1667 		dip->next = AUDIO_MIXER_LAST;
   1668 		strcpy(dip->label.name, AudioNline);
   1669 		dip->un.v.num_channels = 2;
   1670 		strcpy(dip->un.v.units.name, AudioNvolume);
   1671 		return (0);
   1672 	case EAP_AUX_VOL:
   1673 		dip->type = AUDIO_MIXER_VALUE;
   1674 		dip->mixer_class = EAP_INPUT_CLASS;
   1675 		dip->prev = AUDIO_MIXER_LAST;
   1676 		dip->next = AUDIO_MIXER_LAST;
   1677 		strcpy(dip->label.name, AudioNaux);
   1678 		dip->un.v.num_channels = 2;
   1679 		strcpy(dip->un.v.units.name, AudioNvolume);
   1680 		return (0);
   1681 	case EAP_MIC_VOL:
   1682 		dip->type = AUDIO_MIXER_VALUE;
   1683 		dip->mixer_class = EAP_INPUT_CLASS;
   1684 		dip->prev = AUDIO_MIXER_LAST;
   1685 		dip->next = EAP_MIC_PREAMP;
   1686 		strcpy(dip->label.name, AudioNmicrophone);
   1687 		dip->un.v.num_channels = 1;
   1688 		strcpy(dip->un.v.units.name, AudioNvolume);
   1689 		return (0);
   1690 	case EAP_RECORD_SOURCE:
   1691 		dip->mixer_class = EAP_RECORD_CLASS;
   1692 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1693 		strcpy(dip->label.name, AudioNsource);
   1694 		dip->type = AUDIO_MIXER_SET;
   1695 		dip->un.s.num_mem = 6;
   1696 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1697 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1698 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1699 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1700 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1701 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1702 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1703 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1704 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1705 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1706 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1707 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1708 		return (0);
   1709 	case EAP_OUTPUT_SELECT:
   1710 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1711 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1712 		strcpy(dip->label.name, AudioNselect);
   1713 		dip->type = AUDIO_MIXER_SET;
   1714 		dip->un.s.num_mem = 6;
   1715 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1716 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1717 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1718 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1719 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1720 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1721 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1722 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1723 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1724 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1725 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1726 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1727 		return (0);
   1728 	case EAP_MIC_PREAMP:
   1729 		dip->type = AUDIO_MIXER_ENUM;
   1730 		dip->mixer_class = EAP_INPUT_CLASS;
   1731 		dip->prev = EAP_MIC_VOL;
   1732 		dip->next = AUDIO_MIXER_LAST;
   1733 		strcpy(dip->label.name, AudioNpreamp);
   1734 		dip->un.e.num_mem = 2;
   1735 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1736 		dip->un.e.member[0].ord = 0;
   1737 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1738 		dip->un.e.member[1].ord = 1;
   1739 		return (0);
   1740 	case EAP_OUTPUT_CLASS:
   1741 		dip->type = AUDIO_MIXER_CLASS;
   1742 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1743 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1744 		strcpy(dip->label.name, AudioCoutputs);
   1745 		return (0);
   1746 	case EAP_RECORD_CLASS:
   1747 		dip->type = AUDIO_MIXER_CLASS;
   1748 		dip->mixer_class = EAP_RECORD_CLASS;
   1749 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1750 		strcpy(dip->label.name, AudioCrecord);
   1751 		return (0);
   1752 	case EAP_INPUT_CLASS:
   1753 		dip->type = AUDIO_MIXER_CLASS;
   1754 		dip->mixer_class = EAP_INPUT_CLASS;
   1755 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1756 		strcpy(dip->label.name, AudioCinputs);
   1757 		return (0);
   1758 	}
   1759 	return (ENXIO);
   1760 }
   1761 
   1762 void *
   1763 eap_malloc(addr, direction, size, pool, flags)
   1764 	void *addr;
   1765 	int direction;
   1766 	size_t size;
   1767 	int pool, flags;
   1768 {
   1769 	struct eap_softc *sc = addr;
   1770 	struct eap_dma *p;
   1771 	int error;
   1772 
   1773 	p = malloc(sizeof(*p), pool, flags);
   1774 	if (!p)
   1775 		return (0);
   1776 	error = eap_allocmem(sc, size, 16, p);
   1777 	if (error) {
   1778 		free(p, pool);
   1779 		return (0);
   1780 	}
   1781 	p->next = sc->sc_dmas;
   1782 	sc->sc_dmas = p;
   1783 	return (KERNADDR(p));
   1784 }
   1785 
   1786 void
   1787 eap_free(addr, ptr, pool)
   1788 	void *addr;
   1789 	void *ptr;
   1790 	int pool;
   1791 {
   1792 	struct eap_softc *sc = addr;
   1793 	struct eap_dma **pp, *p;
   1794 
   1795 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1796 		if (KERNADDR(p) == ptr) {
   1797 			eap_freemem(sc, p);
   1798 			*pp = p->next;
   1799 			free(p, pool);
   1800 			return;
   1801 		}
   1802 	}
   1803 }
   1804 
   1805 size_t
   1806 eap_round_buffersize(addr, direction, size)
   1807 	void *addr;
   1808 	int direction;
   1809 	size_t size;
   1810 {
   1811 	return (size);
   1812 }
   1813 
   1814 int
   1815 eap_mappage(addr, mem, off, prot)
   1816 	void *addr;
   1817 	void *mem;
   1818 	int off;
   1819 	int prot;
   1820 {
   1821 	struct eap_softc *sc = addr;
   1822 	struct eap_dma *p;
   1823 
   1824 	if (off < 0)
   1825 		return (-1);
   1826 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1827 		;
   1828 	if (!p)
   1829 		return (-1);
   1830 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1831 				off, prot, BUS_DMA_WAITOK));
   1832 }
   1833 
   1834 int
   1835 eap_get_props(addr)
   1836 	void *addr;
   1837 {
   1838 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1839                 AUDIO_PROP_FULLDUPLEX);
   1840 }
   1841 
   1842 #if NMIDI > 0
   1843 int
   1844 eap_midi_open(addr, flags, iintr, ointr, arg)
   1845 	void *addr;
   1846 	int flags;
   1847 	void (*iintr)__P((void *, int));
   1848 	void (*ointr)__P((void *));
   1849 	void *arg;
   1850 {
   1851 	struct eap_softc *sc = addr;
   1852 	u_int32_t uctrl;
   1853 
   1854 	sc->sc_iintr = iintr;
   1855 	sc->sc_ointr = ointr;
   1856 	sc->sc_arg = arg;
   1857 
   1858 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
   1859 	uctrl = 0;
   1860 	if (flags & FREAD)
   1861 		uctrl |= EAP_UC_RXINTEN;
   1862 #if 0
   1863 	/* I don't understand ../midi.c well enough to use output interrupts */
   1864 	if (flags & FWRITE)
   1865 		uctrl |= EAP_UC_TXINTEN; */
   1866 #endif
   1867 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1868 
   1869 	return (0);
   1870 }
   1871 
   1872 void
   1873 eap_midi_close(addr)
   1874 	void *addr;
   1875 {
   1876 	struct eap_softc *sc = addr;
   1877 
   1878 	EWRITE1(sc, EAP_UART_CONTROL, 0);
   1879 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
   1880 
   1881 	sc->sc_iintr = 0;
   1882 	sc->sc_ointr = 0;
   1883 }
   1884 
   1885 int
   1886 eap_midi_output(addr, d)
   1887 	void *addr;
   1888 	int d;
   1889 {
   1890 	struct eap_softc *sc = addr;
   1891 	int x;
   1892 
   1893 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
   1894 		if (EREAD1(sc, EAP_UART_STATUS) & EAP_US_TXRDY) {
   1895 			EWRITE1(sc, EAP_UART_DATA, d);
   1896 			return (0);
   1897 		}
   1898 		delay(MIDI_BUSY_DELAY);
   1899 	}
   1900 	return (EIO);
   1901 }
   1902 
   1903 void
   1904 eap_midi_getinfo(addr, mi)
   1905 	void *addr;
   1906 	struct midi_info *mi;
   1907 {
   1908 	mi->name = "AudioPCI MIDI UART";
   1909 	mi->props = MIDI_PROP_CAN_INPUT;
   1910 }
   1911 
   1912 #endif
   1913