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