Home | History | Annotate | Line # | Download | only in pci
eap.c revision 1.81.14.2
      1 /*	$NetBSD: eap.c,v 1.81.14.2 2006/06/19 04:01:35 chap Exp $	*/
      2 /*      $OpenBSD: eap.c,v 1.6 1999/10/05 19:24:42 csapuntz Exp $ */
      3 
      4 /*
      5  * Copyright (c) 1998, 1999, 2002 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>, Charles M. Hannum, and
     10  * Antti Kantee <pooka (at) NetBSD.org>.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * Debugging:   Andreas Gustafsson <gson (at) araneus.fi>
     43  * Testing:     Chuck Cranor       <chuck (at) maria.wustl.edu>
     44  *              Phil Nelson        <phil (at) cs.wwu.edu>
     45  *
     46  * ES1371/AC97:	Ezra Story         <ezy (at) panix.com>
     47  */
     48 
     49 /*
     50  * Ensoniq ES1370 + AK4531 and ES1371/ES1373 + AC97
     51  *
     52  * Documentation links:
     53  *
     54  * ftp://ftp.alsa-project.org/pub/manuals/ensoniq/ (ES1370 and 1371 datasheets)
     55  * http://web.archive.org/web/20040622012936/http://www.corbac.com/Data/Misc/es1373.ps.gz
     56  * ftp://ftp.alsa-project.org/pub/manuals/asahi_kasei/4531.pdf
     57  * ftp://download.intel.com/ial/scalableplatforms/audio/ac97r21.pdf
     58  */
     59 
     60 #include <sys/cdefs.h>
     61 __KERNEL_RCSID(0, "$NetBSD: eap.c,v 1.81.14.2 2006/06/19 04:01:35 chap Exp $");
     62 
     63 #include "midi.h"
     64 #include "joy_eap.h"
     65 
     66 #include <sys/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/kernel.h>
     69 #include <sys/fcntl.h>
     70 #include <sys/malloc.h>
     71 #include <sys/device.h>
     72 #include <sys/proc.h>
     73 #include <sys/select.h>
     74 
     75 #include <dev/pci/pcidevs.h>
     76 #include <dev/pci/pcivar.h>
     77 
     78 #include <sys/audioio.h>
     79 #include <dev/audio_if.h>
     80 #include <dev/midi_if.h>
     81 #include <dev/audiovar.h>
     82 #include <dev/mulaw.h>
     83 #include <dev/auconv.h>
     84 #include <dev/ic/ac97var.h>
     85 
     86 #include <machine/bus.h>
     87 
     88 #include <dev/pci/eapreg.h>
     89 #include <dev/pci/eapvar.h>
     90 
     91 #define	PCI_CBIO		0x10
     92 
     93 /* Debug */
     94 #ifdef AUDIO_DEBUG
     95 #define DPRINTF(x)	if (eapdebug) printf x
     96 #define DPRINTFN(n,x)	if (eapdebug>(n)) printf x
     97 int	eapdebug = 0;
     98 #else
     99 #define DPRINTF(x)
    100 #define DPRINTFN(n,x)
    101 #endif
    102 
    103 static int	eap_match(struct device *, struct cfdata *, void *);
    104 static void	eap_attach(struct device *, struct device *, void *);
    105 static int	eap_detach(struct device *, int);
    106 static int	eap_intr(void *);
    107 
    108 struct eap_dma {
    109 	bus_dmamap_t map;
    110 	caddr_t addr;
    111 	bus_dma_segment_t segs[1];
    112 	int nsegs;
    113 	size_t size;
    114 	struct eap_dma *next;
    115 };
    116 
    117 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
    118 #define KERNADDR(p) ((void *)((p)->addr))
    119 
    120 /*
    121  * The card has two DACs. Using them is a bit twisted: we use DAC2
    122  * as default and DAC1 as the optional secondary DAC.
    123  */
    124 #define EAP_DAC1 1
    125 #define EAP_DAC2 0
    126 #define EAP_I1 EAP_DAC2
    127 #define EAP_I2 EAP_DAC1
    128 struct eap_instance {
    129 	struct device *parent;
    130 	int index;
    131 
    132 	void	(*ei_pintr)(void *);	/* DMA completion intr handler */
    133 	void	*ei_parg;		/* arg for ei_intr() */
    134 	struct device *ei_audiodev;		/* audio device, for detach */
    135 #ifdef DIAGNOSTIC
    136 	char	ei_prun;
    137 #endif
    138 };
    139 
    140 struct eap_softc {
    141 	struct device sc_dev;		/* base device */
    142 	void *sc_ih;			/* interrupt vectoring */
    143 	bus_space_tag_t iot;
    144 	bus_space_handle_t ioh;
    145 	bus_size_t iosz;
    146 	bus_dma_tag_t sc_dmatag;	/* DMA tag */
    147 
    148 	struct eap_dma *sc_dmas;
    149 
    150 	void	(*sc_rintr)(void *);	/* DMA completion intr handler */
    151 	void	*sc_rarg;		/* arg for sc_intr() */
    152 #ifdef DIAGNOSTIC
    153 	char	sc_rrun;
    154 #endif
    155 
    156 #if NMIDI > 0
    157 	void	(*sc_iintr)(void *, int); /* midi input ready handler */
    158 	void	(*sc_ointr)(void *);	/* midi output ready handler */
    159 	void	*sc_arg;
    160 	struct device *sc_mididev;
    161 #endif
    162 #if NJOY_EAP > 0
    163 	struct device *sc_gameport;
    164 #endif
    165 
    166 	u_short	sc_port[AK_NPORTS];	/* mirror of the hardware setting */
    167 	u_int	sc_record_source;	/* recording source mask */
    168 	u_int	sc_input_source;	/* input source mask */
    169 	u_int	sc_mic_preamp;
    170 	char    sc_1371;		/* Using ES1371/AC97 codec */
    171 
    172 	struct ac97_codec_if *codec_if;
    173 	struct ac97_host_if host_if;
    174 
    175 	struct eap_instance sc_ei[2];
    176 
    177 	pci_chipset_tag_t sc_pc;	/* For detach */
    178 };
    179 
    180 static void	eap_uart_txrdy(struct eap_softc *);
    181 
    182 static int	eap_allocmem(struct eap_softc *, size_t, size_t,
    183 			     struct eap_dma *);
    184 static int	eap_freemem(struct eap_softc *, struct eap_dma *);
    185 
    186 #define EWRITE1(sc, r, x) bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x))
    187 #define EWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
    188 #define EWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
    189 #define EREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
    190 #define EREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
    191 #define EREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
    192 
    193 CFATTACH_DECL(eap, sizeof(struct eap_softc),
    194     eap_match, eap_attach, eap_detach, NULL);
    195 
    196 static int	eap_open(void *, int);
    197 static int	eap_query_encoding(void *, struct audio_encoding *);
    198 static int	eap_set_params(void *, int, int, audio_params_t *,
    199 			       audio_params_t *, stream_filter_list_t *,
    200 			       stream_filter_list_t *);
    201 static int	eap_round_blocksize(void *, int, int, const audio_params_t *);
    202 static int	eap_trigger_output(void *, void *, void *, int,
    203 				   void (*)(void *), void *,
    204 				   const audio_params_t *);
    205 static int	eap_trigger_input(void *, void *, void *, int,
    206 				  void (*)(void *), void *,
    207 				  const audio_params_t *);
    208 static int	eap_halt_output(void *);
    209 static int	eap_halt_input(void *);
    210 static void	eap1370_write_codec(struct eap_softc *, int, int);
    211 static int	eap_getdev(void *, struct audio_device *);
    212 static int	eap1370_mixer_set_port(void *, mixer_ctrl_t *);
    213 static int	eap1370_mixer_get_port(void *, mixer_ctrl_t *);
    214 static int	eap1371_mixer_set_port(void *, mixer_ctrl_t *);
    215 static int	eap1371_mixer_get_port(void *, mixer_ctrl_t *);
    216 static int	eap1370_query_devinfo(void *, mixer_devinfo_t *);
    217 static void	*eap_malloc(void *, int, size_t, struct malloc_type *, int);
    218 static void	eap_free(void *, void *, struct malloc_type *);
    219 static size_t	eap_round_buffersize(void *, int, size_t);
    220 static paddr_t	eap_mappage(void *, void *, off_t, int);
    221 static int	eap_get_props(void *);
    222 static void	eap1370_set_mixer(struct eap_softc *, int, int);
    223 static uint32_t eap1371_src_wait(struct eap_softc *);
    224 static void	eap1371_set_adc_rate(struct eap_softc *, int);
    225 static void	eap1371_set_dac_rate(struct eap_instance *, int);
    226 static int	eap1371_src_read(struct eap_softc *, int);
    227 static void	eap1371_src_write(struct eap_softc *, int, int);
    228 static int	eap1371_query_devinfo(void *, mixer_devinfo_t *);
    229 
    230 static int	eap1371_attach_codec(void *, struct ac97_codec_if *);
    231 static int	eap1371_read_codec(void *, uint8_t, uint16_t *);
    232 static int	eap1371_write_codec(void *, uint8_t, uint16_t );
    233 static int	eap1371_reset_codec(void *);
    234 #if NMIDI > 0
    235 static void	eap_midi_close(void *);
    236 static void	eap_midi_getinfo(void *, struct midi_info *);
    237 static int	eap_midi_open(void *, int, void (*)(void *, int),
    238 			      void (*)(void *), void *);
    239 static int	eap_midi_output(void *, int);
    240 #endif
    241 
    242 static const struct audio_hw_if eap1370_hw_if = {
    243 	eap_open,
    244 	NULL,			/* close */
    245 	NULL,
    246 	eap_query_encoding,
    247 	eap_set_params,
    248 	eap_round_blocksize,
    249 	NULL,
    250 	NULL,
    251 	NULL,
    252 	NULL,
    253 	NULL,
    254 	eap_halt_output,
    255 	eap_halt_input,
    256 	NULL,
    257 	eap_getdev,
    258 	NULL,
    259 	eap1370_mixer_set_port,
    260 	eap1370_mixer_get_port,
    261 	eap1370_query_devinfo,
    262 	eap_malloc,
    263 	eap_free,
    264 	eap_round_buffersize,
    265 	eap_mappage,
    266 	eap_get_props,
    267 	eap_trigger_output,
    268 	eap_trigger_input,
    269 	NULL,
    270 };
    271 
    272 static const struct audio_hw_if eap1371_hw_if = {
    273 	eap_open,
    274 	NULL,			/* close */
    275 	NULL,
    276 	eap_query_encoding,
    277 	eap_set_params,
    278 	eap_round_blocksize,
    279 	NULL,
    280 	NULL,
    281 	NULL,
    282 	NULL,
    283 	NULL,
    284 	eap_halt_output,
    285 	eap_halt_input,
    286 	NULL,
    287 	eap_getdev,
    288 	NULL,
    289 	eap1371_mixer_set_port,
    290 	eap1371_mixer_get_port,
    291 	eap1371_query_devinfo,
    292 	eap_malloc,
    293 	eap_free,
    294 	eap_round_buffersize,
    295 	eap_mappage,
    296 	eap_get_props,
    297 	eap_trigger_output,
    298 	eap_trigger_input,
    299 	NULL,
    300 };
    301 
    302 #if NMIDI > 0
    303 static const struct midi_hw_if eap_midi_hw_if = {
    304 	eap_midi_open,
    305 	eap_midi_close,
    306 	eap_midi_output,
    307 	eap_midi_getinfo,
    308 	0,				/* ioctl */
    309 };
    310 #endif
    311 
    312 static struct audio_device eap_device = {
    313 	"Ensoniq AudioPCI",
    314 	"",
    315 	"eap"
    316 };
    317 
    318 #define EAP_NFORMATS	4
    319 static const struct audio_format eap_formats[EAP_NFORMATS] = {
    320 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    321 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    322 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    323 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    324 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    325 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    326 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    327 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    328 };
    329 
    330 static int
    331 eap_match(struct device *parent, struct cfdata *match, void *aux)
    332 {
    333 	struct pci_attach_args *pa;
    334 
    335 	pa = (struct pci_attach_args *)aux;
    336 	switch (PCI_VENDOR(pa->pa_id)) {
    337 	case PCI_VENDOR_CREATIVELABS:
    338 		switch (PCI_PRODUCT(pa->pa_id)) {
    339 		case PCI_PRODUCT_CREATIVELABS_EV1938:
    340 			return 1;
    341 		}
    342 		break;
    343 	case PCI_VENDOR_ENSONIQ:
    344 		switch (PCI_PRODUCT(pa->pa_id)) {
    345 		case PCI_PRODUCT_ENSONIQ_AUDIOPCI:
    346 		case PCI_PRODUCT_ENSONIQ_AUDIOPCI97:
    347 		case PCI_PRODUCT_ENSONIQ_CT5880:
    348 			return 1;
    349 		}
    350 		break;
    351 	}
    352 
    353 	return 0;
    354 }
    355 
    356 static void
    357 eap1370_write_codec(struct eap_softc *sc, int a, int d)
    358 {
    359 	int icss, to;
    360 
    361 	to = EAP_WRITE_TIMEOUT;
    362 	do {
    363 		icss = EREAD4(sc, EAP_ICSS);
    364 		DPRINTFN(5,("eap: codec %d prog: icss=0x%08x\n", a, icss));
    365 		if (!to--) {
    366 			printf("eap: timeout writing to codec\n");
    367 			return;
    368 		}
    369 	} while(icss & EAP_CWRIP);  /* XXX could use CSTAT here */
    370 	EWRITE4(sc, EAP_CODEC, EAP_SET_CODEC(a, d));
    371 }
    372 
    373 /*
    374  * Reading and writing the CODEC is very convoluted.  This mimics the
    375  * FreeBSD and Linux drivers.
    376  */
    377 
    378 static inline void
    379 eap1371_ready_codec(struct eap_softc *sc, uint8_t a, uint32_t wd)
    380 {
    381 	int to, s;
    382 	uint32_t src, t;
    383 
    384 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    385 		if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
    386 			break;
    387 		delay(1);
    388 	}
    389 	if (to >= EAP_WRITE_TIMEOUT)
    390 		printf("%s: eap1371_ready_codec timeout 1\n",
    391 		       sc->sc_dev.dv_xname);
    392 
    393 	s = splaudio();
    394 	src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    395 	EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
    396 
    397 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    398 		t = EREAD4(sc, E1371_SRC);
    399 		if ((t & E1371_SRC_STATE_MASK) == 0)
    400 			break;
    401 		delay(1);
    402 	}
    403 	if (to >= EAP_READ_TIMEOUT)
    404 		printf("%s: eap1371_ready_codec timeout 2\n",
    405 		       sc->sc_dev.dv_xname);
    406 
    407 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    408 		t = EREAD4(sc, E1371_SRC);
    409 		if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
    410 			break;
    411 		delay(1);
    412 	}
    413 	if (to >= EAP_READ_TIMEOUT)
    414 		printf("%s: eap1371_ready_codec timeout 3\n",
    415 		       sc->sc_dev.dv_xname);
    416 
    417 	EWRITE4(sc, E1371_CODEC, wd);
    418 
    419 	eap1371_src_wait(sc);
    420 	EWRITE4(sc, E1371_SRC, src);
    421 
    422 	splx(s);
    423 }
    424 
    425 static int
    426 eap1371_read_codec(void *sc_, uint8_t a, uint16_t *d)
    427 {
    428 	struct eap_softc *sc;
    429 	int to;
    430 	uint32_t t;
    431 
    432 	sc = sc_;
    433 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, 0) | E1371_CODEC_READ);
    434 
    435 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    436 		if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
    437 			break;
    438 	}
    439 	if (to > EAP_WRITE_TIMEOUT)
    440 		printf("%s: eap1371_read_codec timeout 1\n",
    441 		       sc->sc_dev.dv_xname);
    442 
    443 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    444 		t = EREAD4(sc, E1371_CODEC);
    445 		if (t & E1371_CODEC_VALID)
    446 			break;
    447 	}
    448 	if (to > EAP_WRITE_TIMEOUT)
    449 		printf("%s: eap1371_read_codec timeout 2\n",
    450 		       sc->sc_dev.dv_xname);
    451 
    452 	*d = (uint16_t)t;
    453 
    454 	DPRINTFN(10, ("eap1371: reading codec (%x) = %x\n", a, *d));
    455 
    456 	return 0;
    457 }
    458 
    459 static int
    460 eap1371_write_codec(void *sc_, uint8_t a, uint16_t d)
    461 {
    462 	struct eap_softc *sc;
    463 
    464 	sc = sc_;
    465 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, d));
    466 
    467 	DPRINTFN(10, ("eap1371: writing codec %x --> %x\n", d, a));
    468 
    469 	return 0;
    470 }
    471 
    472 static uint32_t
    473 eap1371_src_wait(struct eap_softc *sc)
    474 {
    475 	int to;
    476 	u_int32_t src;
    477 
    478 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    479 		src = EREAD4(sc, E1371_SRC);
    480 		if (!(src & E1371_SRC_RBUSY))
    481 			return src;
    482 		delay(1);
    483 	}
    484 	printf("%s: eap1371_src_wait timeout\n", sc->sc_dev.dv_xname);
    485 	return src;
    486 }
    487 
    488 static int
    489 eap1371_src_read(struct eap_softc *sc, int a)
    490 {
    491 	int to;
    492 	uint32_t src, t;
    493 
    494 	src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    495 	src |= E1371_SRC_ADDR(a);
    496 	EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
    497 
    498 	t = eap1371_src_wait(sc);
    499 	if ((t & E1371_SRC_STATE_MASK) != E1371_SRC_STATE_OK) {
    500 		for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    501 			t = EREAD4(sc, E1371_SRC);
    502 			if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
    503 				break;
    504 			delay(1);
    505 		}
    506 	}
    507 
    508 	EWRITE4(sc, E1371_SRC, src);
    509 
    510 	return t & E1371_SRC_DATAMASK;
    511 }
    512 
    513 static void
    514 eap1371_src_write(struct eap_softc *sc, int a, int d)
    515 {
    516 	uint32_t r;
    517 
    518 	r = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    519 	r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
    520 	EWRITE4(sc, E1371_SRC, r);
    521 }
    522 
    523 static void
    524 eap1371_set_adc_rate(struct eap_softc *sc, int rate)
    525 {
    526 	int freq, n, truncm;
    527 	int out;
    528 	int s;
    529 
    530 	/* Whatever, it works, so I'll leave it :) */
    531 
    532 	if (rate > 48000)
    533 		rate = 48000;
    534 	if (rate < 4000)
    535 		rate = 4000;
    536 	n = rate / 3000;
    537 	if ((1 << n) & SRC_MAGIC)
    538 		n--;
    539 	truncm = ((21 * n) - 1) | 1;
    540 	freq = ((48000 << 15) / rate) * n;
    541 	if (rate >= 24000) {
    542 		if (truncm > 239)
    543 			truncm = 239;
    544 		out = ESRC_SET_TRUNC((239 - truncm) / 2);
    545 	} else {
    546 		if (truncm > 119)
    547 			truncm = 119;
    548 		out = ESRC_SMF | ESRC_SET_TRUNC((119 - truncm) / 2);
    549 	}
    550 	out |= ESRC_SET_N(n);
    551 	s = splaudio();
    552 	eap1371_src_write(sc, ESRC_ADC+ESRC_TRUNC_N, out);
    553 
    554 	out = eap1371_src_read(sc, ESRC_ADC+ESRC_IREGS) & 0xff;
    555 	eap1371_src_write(sc, ESRC_ADC+ESRC_IREGS, out |
    556 			  ESRC_SET_VFI(freq >> 15));
    557 	eap1371_src_write(sc, ESRC_ADC+ESRC_VFF, freq & 0x7fff);
    558 	eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(n));
    559 	eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(n));
    560 	splx(s);
    561 }
    562 
    563 static void
    564 eap1371_set_dac_rate(struct eap_instance *ei, int rate)
    565 {
    566 	struct eap_softc *sc;
    567 	int dac;
    568 	int freq, r;
    569 	int s;
    570 
    571 	DPRINTFN(2, ("eap1371_set_dac_date: set rate for %d\n", ei->index));
    572 	sc = (struct eap_softc *)ei->parent;
    573 	dac = ei->index == EAP_DAC1 ? ESRC_DAC1 : ESRC_DAC2;
    574 
    575 	/* Whatever, it works, so I'll leave it :) */
    576 
    577 	if (rate > 48000)
    578 	    rate = 48000;
    579 	if (rate < 4000)
    580 	    rate = 4000;
    581 	freq = ((rate << 15) + 1500) / 3000;
    582 
    583 	s = splaudio();
    584 	eap1371_src_wait(sc);
    585 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    586 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    587 	r |= ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2;
    588 	EWRITE4(sc, E1371_SRC, r);
    589 	r = eap1371_src_read(sc, dac + ESRC_IREGS) & 0x00ff;
    590 	eap1371_src_write(sc, dac + ESRC_IREGS, r | ((freq >> 5) & 0xfc00));
    591 	eap1371_src_write(sc, dac + ESRC_VFF, freq & 0x7fff);
    592 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    593 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    594 	r &= ~(ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2);
    595 	EWRITE4(sc, E1371_SRC, r);
    596 	splx(s);
    597 }
    598 
    599 static void
    600 eap_attach(struct device *parent, struct device *self, void *aux)
    601 {
    602 	struct eap_softc *sc;
    603 	struct pci_attach_args *pa;
    604 	pci_chipset_tag_t pc;
    605 	const struct audio_hw_if *eap_hw_if;
    606 	char const *intrstr;
    607 	pci_intr_handle_t ih;
    608 	pcireg_t csr;
    609 	char devinfo[256];
    610 	mixer_ctrl_t ctl;
    611 	int i;
    612 	int revision, ct5880;
    613 	const char *revstr;
    614 #if NJOY_EAP > 0
    615 	struct eap_gameport_args gpargs;
    616 #endif
    617 
    618 	sc = (struct eap_softc *)self;
    619 	pa = (struct pci_attach_args *)aux;
    620 	pc = pa->pa_pc;
    621 	revstr = "";
    622 	aprint_naive(": Audio controller\n");
    623 
    624 	/* Stash this away for detach */
    625 	sc->sc_pc = pc;
    626 
    627 	/* Flag if we're "creative" */
    628 	sc->sc_1371 = !(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
    629 			PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI);
    630 
    631 	/*
    632 	 * The vendor and product ID's are quite "interesting". Just
    633 	 * trust the following and be happy.
    634 	 */
    635 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    636 	revision = PCI_REVISION(pa->pa_class);
    637 	ct5880 = 0;
    638 	if (sc->sc_1371) {
    639 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
    640 		    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_CT5880) {
    641 			ct5880 = 1;
    642 			switch (revision) {
    643 			case EAP_CT5880_C: revstr = "CT5880-C "; break;
    644 			case EAP_CT5880_D: revstr = "CT5880-D "; break;
    645 			case EAP_CT5880_E: revstr = "CT5880-E "; break;
    646 			}
    647 		} else {
    648 			switch (revision) {
    649 			case EAP_EV1938_A: revstr = "EV1938-A "; break;
    650 			case EAP_ES1373_A: revstr = "ES1373-A "; break;
    651 			case EAP_ES1373_B: revstr = "ES1373-B "; break;
    652 			case EAP_CT5880_A: revstr = "CT5880-A "; ct5880=1;break;
    653 			case EAP_ES1373_8: revstr = "ES1373-8" ; ct5880=1;break;
    654 			case EAP_ES1371_B: revstr = "ES1371-B "; break;
    655 			}
    656 		}
    657 	}
    658 	aprint_normal(": %s %s(rev. 0x%02x)\n", devinfo, revstr, revision);
    659 
    660 	/* Map I/O register */
    661 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    662 	      &sc->iot, &sc->ioh, NULL, &sc->iosz)) {
    663 		aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    664 		return;
    665 	}
    666 
    667 	sc->sc_dmatag = pa->pa_dmat;
    668 
    669 	/* Enable the device. */
    670 	csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    671 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    672 		       csr | PCI_COMMAND_MASTER_ENABLE);
    673 
    674 	/* Map and establish the interrupt. */
    675 	if (pci_intr_map(pa, &ih)) {
    676 		aprint_error("%s: couldn't map interrupt\n",
    677 		    sc->sc_dev.dv_xname);
    678 		return;
    679 	}
    680 	intrstr = pci_intr_string(pc, ih);
    681 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, eap_intr, sc);
    682 	if (sc->sc_ih == NULL) {
    683 		aprint_error("%s: couldn't establish interrupt",
    684 		    sc->sc_dev.dv_xname);
    685 		if (intrstr != NULL)
    686 			aprint_normal(" at %s", intrstr);
    687 		aprint_normal("\n");
    688 		return;
    689 	}
    690 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    691 
    692 	sc->sc_ei[EAP_I1].parent = (struct device *)sc;
    693 	sc->sc_ei[EAP_I1].index = EAP_DAC2;
    694 	sc->sc_ei[EAP_I2].parent = (struct device *)sc;
    695 	sc->sc_ei[EAP_I2].index = EAP_DAC1;
    696 
    697 	if (!sc->sc_1371) {
    698 		/* Enable interrupts and looping mode. */
    699 		/* enable the parts we need */
    700 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    701 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
    702 
    703 		/* reset codec */
    704 		/* normal operation */
    705 		/* select codec clocks */
    706 		eap1370_write_codec(sc, AK_RESET, AK_PD);
    707 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
    708 		eap1370_write_codec(sc, AK_CS, 0x0);
    709 
    710 		eap_hw_if = &eap1370_hw_if;
    711 
    712 		/* Enable all relevant mixer switches. */
    713 		ctl.dev = EAP_INPUT_SOURCE;
    714 		ctl.type = AUDIO_MIXER_SET;
    715 		ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
    716 			1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
    717 			1 << EAP_MIC_VOL;
    718 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    719 
    720 		ctl.type = AUDIO_MIXER_VALUE;
    721 		ctl.un.value.num_channels = 1;
    722 		for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
    723 		     ctl.dev++) {
    724 			ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
    725 			eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    726 		}
    727 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
    728 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    729 		ctl.dev = EAP_MIC_PREAMP;
    730 		ctl.type = AUDIO_MIXER_ENUM;
    731 		ctl.un.ord = 0;
    732 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    733 		ctl.dev = EAP_RECORD_SOURCE;
    734 		ctl.type = AUDIO_MIXER_SET;
    735 		ctl.un.mask = 1 << EAP_MIC_VOL;
    736 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    737 	} else {
    738 		/* clean slate */
    739 
    740 		EWRITE4(sc, EAP_SIC, 0);
    741 		EWRITE4(sc, EAP_ICSC, 0);
    742 		EWRITE4(sc, E1371_LEGACY, 0);
    743 
    744 		if (ct5880) {
    745 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
    746 			/* Let codec wake up */
    747 			delay(20000);
    748 		}
    749 
    750 		/* Reset from es1371's perspective */
    751 		EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
    752 		delay(20);
    753 		EWRITE4(sc, EAP_ICSC, 0);
    754 
    755 		/*
    756 		 * Must properly reprogram sample rate converter,
    757 		 * or it locks up.  Set some defaults for the life of the
    758 		 * machine, and set up a sb default sample rate.
    759 		 */
    760 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
    761 		for (i = 0; i < 0x80; i++)
    762 			eap1371_src_write(sc, i, 0);
    763 		eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
    764 		eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
    765 		eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
    766 		eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
    767 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
    768 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
    769 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
    770 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
    771 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
    772 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
    773 		eap1371_set_adc_rate(sc, 22050);
    774 		eap1371_set_dac_rate(&sc->sc_ei[0], 22050);
    775 		eap1371_set_dac_rate(&sc->sc_ei[1], 22050);
    776 
    777 		EWRITE4(sc, E1371_SRC, 0);
    778 
    779 		/* Reset codec */
    780 
    781 		/* Interrupt enable */
    782 		sc->host_if.arg = sc;
    783 		sc->host_if.attach = eap1371_attach_codec;
    784 		sc->host_if.read = eap1371_read_codec;
    785 		sc->host_if.write = eap1371_write_codec;
    786 		sc->host_if.reset = eap1371_reset_codec;
    787 
    788 		if (ac97_attach(&sc->host_if, self) == 0) {
    789 			/* Interrupt enable */
    790 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    791 		} else
    792 			return;
    793 
    794 		eap_hw_if = &eap1371_hw_if;
    795 	}
    796 
    797 	sc->sc_ei[EAP_I1].ei_audiodev =
    798 	    audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I1], &sc->sc_dev);
    799 
    800 #ifdef EAP_USE_BOTH_DACS
    801 	aprint_normal("%s: attaching secondary DAC\n", sc->sc_dev.dv_xname);
    802 	sc->sc_ei[EAP_I2].ei_audiodev =
    803 	    audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I2], &sc->sc_dev);
    804 #endif
    805 
    806 #if NMIDI > 0
    807 	sc->sc_mididev = midi_attach_mi(&eap_midi_hw_if, sc, &sc->sc_dev);
    808 #endif
    809 
    810 #if NJOY_EAP > 0
    811 	if (sc->sc_1371) {
    812 		gpargs.gpa_iot = sc->iot;
    813 		gpargs.gpa_ioh = sc->ioh;
    814 		sc->sc_gameport = eap_joy_attach(&sc->sc_dev, &gpargs);
    815 	}
    816 #endif
    817 }
    818 
    819 static int
    820 eap_detach(struct device *self, int flags)
    821 {
    822 	struct eap_softc *sc;
    823 	int res;
    824 #if NJOY_EAP > 0
    825 	struct eap_gameport_args gpargs;
    826 
    827 	sc = (struct eap_softc *)self;
    828 	if (sc->sc_gameport) {
    829 		gpargs.gpa_iot = sc->iot;
    830 		gpargs.gpa_ioh = sc->ioh;
    831 		res = eap_joy_detach(sc->sc_gameport, &gpargs);
    832 		if (res)
    833 			return res;
    834 	}
    835 #else
    836 	sc = (struct eap_softc *)self;
    837 #endif
    838 #if NMIDI > 0
    839 	if (sc->sc_mididev != NULL) {
    840 		res = config_detach(sc->sc_mididev, 0);
    841 		if (res)
    842 			return res;
    843 	}
    844 #endif
    845 #ifdef EAP_USE_BOTH_DACS
    846 	if (sc->sc_ei[EAP_I2].ei_audiodev != NULL) {
    847 		res = config_detach(sc->sc_ei[EAP_I2].ei_audiodev, 0);
    848 		if (res)
    849 			return res;
    850 	}
    851 #endif
    852 	if (sc->sc_ei[EAP_I1].ei_audiodev != NULL) {
    853 		res = config_detach(sc->sc_ei[EAP_I1].ei_audiodev, 0);
    854 		if (res)
    855 			return res;
    856 	}
    857 
    858 	bus_space_unmap(sc->iot, sc->ioh, sc->iosz);
    859 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    860 
    861 	return 0;
    862 }
    863 
    864 static int
    865 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    866 {
    867 	struct eap_softc *sc;
    868 
    869 	sc = sc_;
    870 	sc->codec_if = codec_if;
    871 	return 0;
    872 }
    873 
    874 static int
    875 eap1371_reset_codec(void *sc_)
    876 {
    877 	struct eap_softc *sc;
    878 	uint32_t icsc;
    879 	int s;
    880 
    881 	sc = sc_;
    882 	s = splaudio();
    883 	icsc = EREAD4(sc, EAP_ICSC);
    884 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
    885 	delay(20);
    886 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
    887 	delay(1);
    888 	splx(s);
    889 
    890 	return 0;
    891 }
    892 
    893 static int
    894 eap_intr(void *p)
    895 {
    896 	struct eap_softc *sc;
    897 	uint32_t intr, sic;
    898 
    899 	sc = p;
    900 	intr = EREAD4(sc, EAP_ICSS);
    901 	if (!(intr & EAP_INTR))
    902 		return 0;
    903 	sic = EREAD4(sc, EAP_SIC);
    904 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
    905 	if (intr & EAP_I_ADC) {
    906 #if 0
    907 		/*
    908 		 * XXX This is a hack!
    909 		 * The EAP chip sometimes generates the recording interrupt
    910 		 * while it is still transferring the data.  To make sure
    911 		 * it has all arrived we busy wait until the count is right.
    912 		 * The transfer we are waiting for is 8 longwords.
    913 		 */
    914 		int s, nw, n;
    915 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
    916 		s = EREAD4(sc, EAP_ADC_CSR);
    917 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
    918 		n = 0;
    919 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
    920 			delay(10);
    921 			if (++n > 100) {
    922 				printf("eapintr: DMA fix timeout");
    923 				break;
    924 			}
    925 		}
    926 		/* Continue with normal interrupt handling. */
    927 #endif
    928 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
    929 		EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
    930 		if (sc->sc_rintr)
    931 			sc->sc_rintr(sc->sc_rarg);
    932 	}
    933 
    934 	if (intr & EAP_I_DAC2) {
    935 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
    936 		EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
    937 		if (sc->sc_ei[EAP_DAC2].ei_pintr)
    938 			sc->sc_ei[EAP_DAC2].ei_pintr(sc->sc_ei[EAP_DAC2].ei_parg);
    939 	}
    940 
    941 	if (intr & EAP_I_DAC1) {
    942 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P1_INTR_EN);
    943 		EWRITE4(sc, EAP_SIC, sic | EAP_P1_INTR_EN);
    944 		if (sc->sc_ei[EAP_DAC1].ei_pintr)
    945 			sc->sc_ei[EAP_DAC1].ei_pintr(sc->sc_ei[EAP_DAC1].ei_parg);
    946 	}
    947 
    948 	if (intr & EAP_I_MCCB)
    949 		panic("eap_intr: unexpected MCCB interrupt");
    950 #if NMIDI > 0
    951 	if (intr & EAP_I_UART) {
    952 		uint8_t ustat;
    953 		uint32_t data;
    954 
    955 		ustat = EREAD1(sc, EAP_UART_STATUS);
    956 
    957 		if (ustat & EAP_US_RXINT) {
    958 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
    959 				data = EREAD1(sc, EAP_UART_DATA);
    960 				sc->sc_iintr(sc->sc_arg, data);
    961 			}
    962 		}
    963 
    964 		if (ustat & EAP_US_TXINT)
    965 			eap_uart_txrdy(sc);
    966 	}
    967 #endif
    968 	return 1;
    969 }
    970 
    971 static void
    972 eap_uart_txrdy(struct eap_softc *sc)
    973 {
    974 	uint8_t uctrl;
    975 	uctrl = 0;
    976 	if (sc->sc_iintr)
    977 		uctrl = EAP_UC_RXINTEN;
    978 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
    979 	sc->sc_ointr(sc->sc_arg);
    980 }
    981 
    982 static int
    983 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
    984 {
    985 	int error;
    986 
    987 	p->size = size;
    988 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    989 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    990 				 &p->nsegs, BUS_DMA_NOWAIT);
    991 	if (error)
    992 		return error;
    993 
    994 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    995 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    996 	if (error)
    997 		goto free;
    998 
    999 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
   1000 				  0, BUS_DMA_NOWAIT, &p->map);
   1001 	if (error)
   1002 		goto unmap;
   1003 
   1004 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
   1005 				BUS_DMA_NOWAIT);
   1006 	if (error)
   1007 		goto destroy;
   1008 	return (0);
   1009 
   1010 destroy:
   1011 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1012 unmap:
   1013 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1014 free:
   1015 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1016 	return error;
   1017 }
   1018 
   1019 static int
   1020 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
   1021 {
   1022 
   1023 	bus_dmamap_unload(sc->sc_dmatag, p->map);
   1024 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1025 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1026 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1027 	return 0;
   1028 }
   1029 
   1030 static int
   1031 eap_open(void *addr, int flags)
   1032 {
   1033 	struct eap_instance *ei;
   1034 
   1035 	ei = addr;
   1036 	/* there is only one ADC */
   1037 	if (ei->index == EAP_I2 && flags & FREAD)
   1038 		return EOPNOTSUPP;
   1039 
   1040 	return 0;
   1041 }
   1042 
   1043 static int
   1044 eap_query_encoding(void *addr, struct audio_encoding *fp)
   1045 {
   1046 
   1047 	switch (fp->index) {
   1048 	case 0:
   1049 		strcpy(fp->name, AudioEulinear);
   1050 		fp->encoding = AUDIO_ENCODING_ULINEAR;
   1051 		fp->precision = 8;
   1052 		fp->flags = 0;
   1053 		return 0;
   1054 	case 1:
   1055 		strcpy(fp->name, AudioEmulaw);
   1056 		fp->encoding = AUDIO_ENCODING_ULAW;
   1057 		fp->precision = 8;
   1058 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1059 		return 0;
   1060 	case 2:
   1061 		strcpy(fp->name, AudioEalaw);
   1062 		fp->encoding = AUDIO_ENCODING_ALAW;
   1063 		fp->precision = 8;
   1064 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1065 		return 0;
   1066 	case 3:
   1067 		strcpy(fp->name, AudioEslinear);
   1068 		fp->encoding = AUDIO_ENCODING_SLINEAR;
   1069 		fp->precision = 8;
   1070 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1071 		return 0;
   1072 	case 4:
   1073 		strcpy(fp->name, AudioEslinear_le);
   1074 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
   1075 		fp->precision = 16;
   1076 		fp->flags = 0;
   1077 		return 0;
   1078 	case 5:
   1079 		strcpy(fp->name, AudioEulinear_le);
   1080 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
   1081 		fp->precision = 16;
   1082 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1083 		return 0;
   1084 	case 6:
   1085 		strcpy(fp->name, AudioEslinear_be);
   1086 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
   1087 		fp->precision = 16;
   1088 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1089 		return 0;
   1090 	case 7:
   1091 		strcpy(fp->name, AudioEulinear_be);
   1092 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
   1093 		fp->precision = 16;
   1094 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1095 		return 0;
   1096 	default:
   1097 		return EINVAL;
   1098 	}
   1099 }
   1100 
   1101 static int
   1102 eap_set_params(void *addr, int setmode, int usemode,
   1103 	       audio_params_t *play, audio_params_t *rec,
   1104 	       stream_filter_list_t *pfil, stream_filter_list_t *rfil)
   1105 {
   1106 	struct eap_instance *ei;
   1107 	struct eap_softc *sc;
   1108 	struct audio_params *p;
   1109 	stream_filter_list_t *fil;
   1110 	int mode, i;
   1111 	uint32_t div;
   1112 
   1113 	ei = addr;
   1114 	sc = (struct eap_softc *)ei->parent;
   1115 	/*
   1116 	 * The es1370 only has one clock, so make the sample rates match.
   1117 	 * This only applies for ADC/DAC2. The FM DAC is handled below.
   1118 	 */
   1119 	if (!sc->sc_1371 && ei->index == EAP_DAC2) {
   1120 		if (play->sample_rate != rec->sample_rate &&
   1121 		    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1122 			if (setmode == AUMODE_PLAY) {
   1123 				rec->sample_rate = play->sample_rate;
   1124 				setmode |= AUMODE_RECORD;
   1125 			} else if (setmode == AUMODE_RECORD) {
   1126 				play->sample_rate = rec->sample_rate;
   1127 				setmode |= AUMODE_PLAY;
   1128 			} else
   1129 				return EINVAL;
   1130 		}
   1131 	}
   1132 
   1133 	for (mode = AUMODE_RECORD; mode != -1;
   1134 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1135 		if ((setmode & mode) == 0)
   1136 			continue;
   1137 
   1138 		p = mode == AUMODE_PLAY ? play : rec;
   1139 
   1140 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
   1141 		    (p->precision != 8 && p->precision != 16) ||
   1142 		    (p->channels != 1 && p->channels != 2))
   1143 			return EINVAL;
   1144 
   1145 		fil = mode == AUMODE_PLAY ? pfil : rfil;
   1146 		i = auconv_set_converter(eap_formats, EAP_NFORMATS,
   1147 					 mode, p, FALSE, fil);
   1148 		if (i < 0)
   1149 			return EINVAL;
   1150 	}
   1151 
   1152 	if (sc->sc_1371) {
   1153 		eap1371_set_dac_rate(ei, play->sample_rate);
   1154 		eap1371_set_adc_rate(sc, rec->sample_rate);
   1155 	} else if (ei->index == EAP_DAC2) {
   1156 		/* Set the speed */
   1157 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
   1158 			     EREAD4(sc, EAP_ICSC)));
   1159 		div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
   1160 		/*
   1161 		 * XXX
   1162 		 * The -2 isn't documented, but seemed to make the wall
   1163 		 * time match
   1164 		 * what I expect.  - mycroft
   1165 		 */
   1166 		if (usemode == AUMODE_RECORD)
   1167 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1168 				rec->sample_rate - 2);
   1169 		else
   1170 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1171 				play->sample_rate - 2);
   1172 #if 0
   1173 		div |= EAP_CCB_INTRM;
   1174 #else
   1175 		/*
   1176 		 * It is not obvious how to acknowledge MCCB interrupts, so
   1177 		 * we had better not enable them.
   1178 		 */
   1179 #endif
   1180 		EWRITE4(sc, EAP_ICSC, div);
   1181 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1182 	} else {
   1183 		/*
   1184 		 * The FM DAC has only a few fixed-frequency choises, so
   1185 		 * pick out the best candidate.
   1186 		 */
   1187 		div = EREAD4(sc, EAP_ICSC);
   1188 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n", div));
   1189 
   1190 		div &= ~EAP_WTSRSEL;
   1191 		if (play->sample_rate < 8268)
   1192 			div |= EAP_WTSRSEL_5;
   1193 		else if (play->sample_rate < 16537)
   1194 			div |= EAP_WTSRSEL_11;
   1195 		else if (play->sample_rate < 33075)
   1196 			div |= EAP_WTSRSEL_22;
   1197 		else
   1198 			div |= EAP_WTSRSEL_44;
   1199 
   1200 		EWRITE4(sc, EAP_ICSC, div);
   1201 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1202 	}
   1203 
   1204 	return 0;
   1205 }
   1206 
   1207 static int
   1208 eap_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
   1209 {
   1210 
   1211 	return blk & -32;	/* keep good alignment */
   1212 }
   1213 
   1214 static int
   1215 eap_trigger_output(
   1216 	void *addr,
   1217 	void *start,
   1218 	void *end,
   1219 	int blksize,
   1220 	void (*intr)(void *),
   1221 	void *arg,
   1222 	const audio_params_t *param)
   1223 {
   1224 	struct eap_instance *ei;
   1225 	struct eap_softc *sc;
   1226 	struct eap_dma *p;
   1227 	uint32_t icsc, sic;
   1228 	int sampshift;
   1229 
   1230 	ei = addr;
   1231 	sc = (struct eap_softc *)ei->parent;
   1232 #ifdef DIAGNOSTIC
   1233 	if (ei->ei_prun)
   1234 		panic("eap_trigger_output: already running");
   1235 	ei->ei_prun = 1;
   1236 #endif
   1237 
   1238 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
   1239 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1240 	ei->ei_pintr = intr;
   1241 	ei->ei_parg = arg;
   1242 
   1243 	sic = EREAD4(sc, EAP_SIC);
   1244 	sic &= ~(EAP_S_EB(ei->index) | EAP_S_MB(ei->index) | EAP_INC_BITS);
   1245 
   1246 	if (ei->index == EAP_DAC2)
   1247 		sic |= EAP_SET_P2_ST_INC(0)
   1248 		    | EAP_SET_P2_END_INC(param->precision / 8);
   1249 
   1250 	sampshift = 0;
   1251 	if (param->precision == 16) {
   1252 		sic |= EAP_S_EB(ei->index);
   1253 		sampshift++;
   1254 	}
   1255 	if (param->channels == 2) {
   1256 		sic |= EAP_S_MB(ei->index);
   1257 		sampshift++;
   1258 	}
   1259 	EWRITE4(sc, EAP_SIC, sic & ~EAP_P_INTR_EN(ei->index));
   1260 	EWRITE4(sc, EAP_SIC, sic | EAP_P_INTR_EN(ei->index));
   1261 
   1262 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1263 		continue;
   1264 	if (!p) {
   1265 		printf("eap_trigger_output: bad addr %p\n", start);
   1266 		return EINVAL;
   1267 	}
   1268 
   1269 	if (ei->index == EAP_DAC2) {
   1270 		DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
   1271 			 (int)DMAADDR(p),
   1272 			 (int)EAP_SET_SIZE(0,
   1273 			 (((char *)end - (char *)start) >> 2) - 1)));
   1274 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1275 		EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
   1276 		EWRITE4(sc, EAP_DAC2_SIZE,
   1277 			EAP_SET_SIZE(0,
   1278 			((char *)end - (char *)start) >> 2) - 1);
   1279 		EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
   1280 	} else if (ei->index == EAP_DAC1) {
   1281 		DPRINTF(("eap_trigger_output: DAC1_ADDR=0x%x, DAC1_SIZE=0x%x\n",
   1282 			 (int)DMAADDR(p),
   1283 			 (int)EAP_SET_SIZE(0,
   1284 			 (((char *)end - (char *)start) >> 2) - 1)));
   1285 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1286 		EWRITE4(sc, EAP_DAC1_ADDR, DMAADDR(p));
   1287 		EWRITE4(sc, EAP_DAC1_SIZE,
   1288 			EAP_SET_SIZE(0,
   1289 			((char *)end - (char *)start) >> 2) - 1);
   1290 		EWRITE4(sc, EAP_DAC1_CSR, (blksize >> sampshift) - 1);
   1291 	}
   1292 #ifdef DIAGNOSTIC
   1293 	else
   1294 		panic("eap_trigger_output: impossible instance %d", ei->index);
   1295 #endif
   1296 
   1297 	if (sc->sc_1371)
   1298 		EWRITE4(sc, E1371_SRC, 0);
   1299 
   1300 	icsc = EREAD4(sc, EAP_ICSC);
   1301 	icsc |= EAP_DAC_EN(ei->index);
   1302 	EWRITE4(sc, EAP_ICSC, icsc);
   1303 
   1304 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
   1305 
   1306 	return 0;
   1307 }
   1308 
   1309 static int
   1310 eap_trigger_input(
   1311 	void *addr,
   1312 	void *start,
   1313 	void *end,
   1314 	int blksize,
   1315 	void (*intr)(void *),
   1316 	void *arg,
   1317 	const audio_params_t *param)
   1318 {
   1319 	struct eap_instance *ei;
   1320 	struct eap_softc *sc;
   1321 	struct eap_dma *p;
   1322 	uint32_t icsc, sic;
   1323 	int sampshift;
   1324 
   1325 	ei = addr;
   1326 	sc = (struct eap_softc *)ei->parent;
   1327 #ifdef DIAGNOSTIC
   1328 	if (sc->sc_rrun)
   1329 		panic("eap_trigger_input: already running");
   1330 	sc->sc_rrun = 1;
   1331 #endif
   1332 
   1333 	DPRINTFN(1, ("eap_trigger_input: ei=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1334 	    addr, start, end, blksize, intr, arg));
   1335 	sc->sc_rintr = intr;
   1336 	sc->sc_rarg = arg;
   1337 
   1338 	sic = EREAD4(sc, EAP_SIC);
   1339 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
   1340 	sampshift = 0;
   1341 	if (param->precision == 16) {
   1342 		sic |= EAP_R1_S_EB;
   1343 		sampshift++;
   1344 	}
   1345 	if (param->channels == 2) {
   1346 		sic |= EAP_R1_S_MB;
   1347 		sampshift++;
   1348 	}
   1349 	EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
   1350 	EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
   1351 
   1352 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1353 		continue;
   1354 	if (!p) {
   1355 		printf("eap_trigger_input: bad addr %p\n", start);
   1356 		return (EINVAL);
   1357 	}
   1358 
   1359 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
   1360 		 (int)DMAADDR(p),
   1361 		 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
   1362 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
   1363 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
   1364 	EWRITE4(sc, EAP_ADC_SIZE,
   1365 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
   1366 
   1367 	EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
   1368 
   1369 	if (sc->sc_1371)
   1370 		EWRITE4(sc, E1371_SRC, 0);
   1371 
   1372 	icsc = EREAD4(sc, EAP_ICSC);
   1373 	icsc |= EAP_ADC_EN;
   1374 	EWRITE4(sc, EAP_ICSC, icsc);
   1375 
   1376 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
   1377 
   1378 	return 0;
   1379 }
   1380 
   1381 static int
   1382 eap_halt_output(void *addr)
   1383 {
   1384 	struct eap_instance *ei;
   1385 	struct eap_softc *sc;
   1386 	uint32_t icsc;
   1387 
   1388 	DPRINTF(("eap: eap_halt_output\n"));
   1389 	ei = addr;
   1390 	sc = (struct eap_softc *)ei->parent;
   1391 	icsc = EREAD4(sc, EAP_ICSC);
   1392 	EWRITE4(sc, EAP_ICSC, icsc & ~(EAP_DAC_EN(ei->index)));
   1393 	ei->ei_pintr = 0;
   1394 #ifdef DIAGNOSTIC
   1395 	ei->ei_prun = 0;
   1396 #endif
   1397 
   1398 	return 0;
   1399 }
   1400 
   1401 static int
   1402 eap_halt_input(void *addr)
   1403 {
   1404 	struct eap_instance *ei;
   1405 	struct eap_softc *sc;
   1406 	uint32_t icsc;
   1407 
   1408 #define EAP_USE_FMDAC_ALSO
   1409 	DPRINTF(("eap: eap_halt_input\n"));
   1410 	ei = addr;
   1411 	sc = (struct eap_softc *)ei->parent;
   1412 	icsc = EREAD4(sc, EAP_ICSC);
   1413 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
   1414 	sc->sc_rintr = 0;
   1415 #ifdef DIAGNOSTIC
   1416 	sc->sc_rrun = 0;
   1417 #endif
   1418 
   1419 	return 0;
   1420 }
   1421 
   1422 static int
   1423 eap_getdev(void *addr, struct audio_device *retp)
   1424 {
   1425 
   1426 	*retp = eap_device;
   1427 	return 0;
   1428 }
   1429 
   1430 static int
   1431 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1432 {
   1433 	struct eap_instance *ei;
   1434 	struct eap_softc *sc;
   1435 
   1436 	ei = addr;
   1437 	sc = (struct eap_softc *)ei->parent;
   1438 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
   1439 }
   1440 
   1441 static int
   1442 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1443 {
   1444 	struct eap_instance *ei;
   1445 	struct eap_softc *sc;
   1446 
   1447 	ei = addr;
   1448 	sc = (struct eap_softc *)ei->parent;
   1449 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
   1450 }
   1451 
   1452 static int
   1453 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1454 {
   1455 	struct eap_instance *ei;
   1456 	struct eap_softc *sc;
   1457 
   1458 	ei = addr;
   1459 	sc = (struct eap_softc *)ei->parent;
   1460 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
   1461 }
   1462 
   1463 static void
   1464 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
   1465 {
   1466 	eap1370_write_codec(sc, a, d);
   1467 
   1468 	sc->sc_port[a] = d;
   1469 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
   1470 }
   1471 
   1472 static int
   1473 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1474 {
   1475 	struct eap_instance *ei;
   1476 	struct eap_softc *sc;
   1477 	int lval, rval, l, r, la, ra;
   1478 	int l1, r1, l2, r2, m, o1, o2;
   1479 
   1480 	ei = addr;
   1481 	sc = (struct eap_softc *)ei->parent;
   1482 	if (cp->dev == EAP_RECORD_SOURCE) {
   1483 		if (cp->type != AUDIO_MIXER_SET)
   1484 			return EINVAL;
   1485 		m = sc->sc_record_source = cp->un.mask;
   1486 		l1 = l2 = r1 = r2 = 0;
   1487 		if (m & (1 << EAP_VOICE_VOL))
   1488 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
   1489 		if (m & (1 << EAP_FM_VOL))
   1490 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
   1491 		if (m & (1 << EAP_CD_VOL))
   1492 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
   1493 		if (m & (1 << EAP_LINE_VOL))
   1494 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
   1495 		if (m & (1 << EAP_AUX_VOL))
   1496 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
   1497 		if (m & (1 << EAP_MIC_VOL))
   1498 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
   1499 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
   1500 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
   1501 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
   1502 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
   1503 		return 0;
   1504 	}
   1505 	if (cp->dev == EAP_INPUT_SOURCE) {
   1506 		if (cp->type != AUDIO_MIXER_SET)
   1507 			return EINVAL;
   1508 		m = sc->sc_input_source = cp->un.mask;
   1509 		o1 = o2 = 0;
   1510 		if (m & (1 << EAP_VOICE_VOL))
   1511 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
   1512 		if (m & (1 << EAP_FM_VOL))
   1513 			o1 |= AK_M_FM_L | AK_M_FM_R;
   1514 		if (m & (1 << EAP_CD_VOL))
   1515 			o1 |= AK_M_CD_L | AK_M_CD_R;
   1516 		if (m & (1 << EAP_LINE_VOL))
   1517 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
   1518 		if (m & (1 << EAP_AUX_VOL))
   1519 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
   1520 		if (m & (1 << EAP_MIC_VOL))
   1521 			o1 |= AK_M_MIC;
   1522 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
   1523 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
   1524 		return 0;
   1525 	}
   1526 	if (cp->dev == EAP_MIC_PREAMP) {
   1527 		if (cp->type != AUDIO_MIXER_ENUM)
   1528 			return EINVAL;
   1529 		if (cp->un.ord != 0 && cp->un.ord != 1)
   1530 			return EINVAL;
   1531 		sc->sc_mic_preamp = cp->un.ord;
   1532 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
   1533 		return 0;
   1534 	}
   1535 	if (cp->type != AUDIO_MIXER_VALUE)
   1536 		return EINVAL;
   1537 	if (cp->un.value.num_channels == 1)
   1538 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1539 	else if (cp->un.value.num_channels == 2) {
   1540 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1541 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1542 	} else
   1543 		return EINVAL;
   1544 	ra = -1;
   1545 	switch (cp->dev) {
   1546 	case EAP_MASTER_VOL:
   1547 		l = VOL_TO_ATT5(lval);
   1548 		r = VOL_TO_ATT5(rval);
   1549 		la = AK_MASTER_L;
   1550 		ra = AK_MASTER_R;
   1551 		break;
   1552 	case EAP_MIC_VOL:
   1553 		if (cp->un.value.num_channels != 1)
   1554 			return EINVAL;
   1555 		la = AK_MIC;
   1556 		goto lr;
   1557 	case EAP_VOICE_VOL:
   1558 		la = AK_VOICE_L;
   1559 		ra = AK_VOICE_R;
   1560 		goto lr;
   1561 	case EAP_FM_VOL:
   1562 		la = AK_FM_L;
   1563 		ra = AK_FM_R;
   1564 		goto lr;
   1565 	case EAP_CD_VOL:
   1566 		la = AK_CD_L;
   1567 		ra = AK_CD_R;
   1568 		goto lr;
   1569 	case EAP_LINE_VOL:
   1570 		la = AK_LINE_L;
   1571 		ra = AK_LINE_R;
   1572 		goto lr;
   1573 	case EAP_AUX_VOL:
   1574 		la = AK_AUX_L;
   1575 		ra = AK_AUX_R;
   1576 	lr:
   1577 		l = VOL_TO_GAIN5(lval);
   1578 		r = VOL_TO_GAIN5(rval);
   1579 		break;
   1580 	default:
   1581 		return EINVAL;
   1582 	}
   1583 	eap1370_set_mixer(sc, la, l);
   1584 	if (ra >= 0) {
   1585 		eap1370_set_mixer(sc, ra, r);
   1586 	}
   1587 	return 0;
   1588 }
   1589 
   1590 static int
   1591 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1592 {
   1593 	struct eap_instance *ei;
   1594 	struct eap_softc *sc;
   1595 	int la, ra, l, r;
   1596 
   1597 	ei = addr;
   1598 	sc = (struct eap_softc *)ei->parent;
   1599 	switch (cp->dev) {
   1600 	case EAP_RECORD_SOURCE:
   1601 		if (cp->type != AUDIO_MIXER_SET)
   1602 			return EINVAL;
   1603 		cp->un.mask = sc->sc_record_source;
   1604 		return 0;
   1605 	case EAP_INPUT_SOURCE:
   1606 		if (cp->type != AUDIO_MIXER_SET)
   1607 			return EINVAL;
   1608 		cp->un.mask = sc->sc_input_source;
   1609 		return 0;
   1610 	case EAP_MIC_PREAMP:
   1611 		if (cp->type != AUDIO_MIXER_ENUM)
   1612 			return EINVAL;
   1613 		cp->un.ord = sc->sc_mic_preamp;
   1614 		return 0;
   1615 	case EAP_MASTER_VOL:
   1616 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
   1617 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
   1618 		break;
   1619 	case EAP_MIC_VOL:
   1620 		if (cp->un.value.num_channels != 1)
   1621 			return EINVAL;
   1622 		la = ra = AK_MIC;
   1623 		goto lr;
   1624 	case EAP_VOICE_VOL:
   1625 		la = AK_VOICE_L;
   1626 		ra = AK_VOICE_R;
   1627 		goto lr;
   1628 	case EAP_FM_VOL:
   1629 		la = AK_FM_L;
   1630 		ra = AK_FM_R;
   1631 		goto lr;
   1632 	case EAP_CD_VOL:
   1633 		la = AK_CD_L;
   1634 		ra = AK_CD_R;
   1635 		goto lr;
   1636 	case EAP_LINE_VOL:
   1637 		la = AK_LINE_L;
   1638 		ra = AK_LINE_R;
   1639 		goto lr;
   1640 	case EAP_AUX_VOL:
   1641 		la = AK_AUX_L;
   1642 		ra = AK_AUX_R;
   1643 	lr:
   1644 		l = GAIN5_TO_VOL(sc->sc_port[la]);
   1645 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
   1646 		break;
   1647 	default:
   1648 		return EINVAL;
   1649 	}
   1650 	if (cp->un.value.num_channels == 1)
   1651 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
   1652 	else if (cp->un.value.num_channels == 2) {
   1653 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
   1654 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1655 	} else
   1656 		return EINVAL;
   1657 	return 0;
   1658 }
   1659 
   1660 static int
   1661 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1662 {
   1663 
   1664 	switch (dip->index) {
   1665 	case EAP_MASTER_VOL:
   1666 		dip->type = AUDIO_MIXER_VALUE;
   1667 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1668 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1669 		strcpy(dip->label.name, AudioNmaster);
   1670 		dip->un.v.num_channels = 2;
   1671 		dip->un.v.delta = 8;
   1672 		strcpy(dip->un.v.units.name, AudioNvolume);
   1673 		return 0;
   1674 	case EAP_VOICE_VOL:
   1675 		dip->type = AUDIO_MIXER_VALUE;
   1676 		dip->mixer_class = EAP_INPUT_CLASS;
   1677 		dip->prev = AUDIO_MIXER_LAST;
   1678 		dip->next = AUDIO_MIXER_LAST;
   1679 		strcpy(dip->label.name, AudioNdac);
   1680 		dip->un.v.num_channels = 2;
   1681 		dip->un.v.delta = 8;
   1682 		strcpy(dip->un.v.units.name, AudioNvolume);
   1683 		return 0;
   1684 	case EAP_FM_VOL:
   1685 		dip->type = AUDIO_MIXER_VALUE;
   1686 		dip->mixer_class = EAP_INPUT_CLASS;
   1687 		dip->prev = AUDIO_MIXER_LAST;
   1688 		dip->next = AUDIO_MIXER_LAST;
   1689 		strcpy(dip->label.name, AudioNfmsynth);
   1690 		dip->un.v.num_channels = 2;
   1691 		dip->un.v.delta = 8;
   1692 		strcpy(dip->un.v.units.name, AudioNvolume);
   1693 		return 0;
   1694 	case EAP_CD_VOL:
   1695 		dip->type = AUDIO_MIXER_VALUE;
   1696 		dip->mixer_class = EAP_INPUT_CLASS;
   1697 		dip->prev = AUDIO_MIXER_LAST;
   1698 		dip->next = AUDIO_MIXER_LAST;
   1699 		strcpy(dip->label.name, AudioNcd);
   1700 		dip->un.v.num_channels = 2;
   1701 		dip->un.v.delta = 8;
   1702 		strcpy(dip->un.v.units.name, AudioNvolume);
   1703 		return 0;
   1704 	case EAP_LINE_VOL:
   1705 		dip->type = AUDIO_MIXER_VALUE;
   1706 		dip->mixer_class = EAP_INPUT_CLASS;
   1707 		dip->prev = AUDIO_MIXER_LAST;
   1708 		dip->next = AUDIO_MIXER_LAST;
   1709 		strcpy(dip->label.name, AudioNline);
   1710 		dip->un.v.num_channels = 2;
   1711 		dip->un.v.delta = 8;
   1712 		strcpy(dip->un.v.units.name, AudioNvolume);
   1713 		return 0;
   1714 	case EAP_AUX_VOL:
   1715 		dip->type = AUDIO_MIXER_VALUE;
   1716 		dip->mixer_class = EAP_INPUT_CLASS;
   1717 		dip->prev = AUDIO_MIXER_LAST;
   1718 		dip->next = AUDIO_MIXER_LAST;
   1719 		strcpy(dip->label.name, AudioNaux);
   1720 		dip->un.v.num_channels = 2;
   1721 		dip->un.v.delta = 8;
   1722 		strcpy(dip->un.v.units.name, AudioNvolume);
   1723 		return 0;
   1724 	case EAP_MIC_VOL:
   1725 		dip->type = AUDIO_MIXER_VALUE;
   1726 		dip->mixer_class = EAP_INPUT_CLASS;
   1727 		dip->prev = AUDIO_MIXER_LAST;
   1728 		dip->next = EAP_MIC_PREAMP;
   1729 		strcpy(dip->label.name, AudioNmicrophone);
   1730 		dip->un.v.num_channels = 1;
   1731 		dip->un.v.delta = 8;
   1732 		strcpy(dip->un.v.units.name, AudioNvolume);
   1733 		return 0;
   1734 	case EAP_RECORD_SOURCE:
   1735 		dip->mixer_class = EAP_RECORD_CLASS;
   1736 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1737 		strcpy(dip->label.name, AudioNsource);
   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_INPUT_SOURCE:
   1754 		dip->mixer_class = EAP_INPUT_CLASS;
   1755 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1756 		strcpy(dip->label.name, AudioNsource);
   1757 		dip->type = AUDIO_MIXER_SET;
   1758 		dip->un.s.num_mem = 6;
   1759 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1760 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1761 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1762 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1763 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1764 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1765 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1766 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1767 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1768 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1769 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1770 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1771 		return 0;
   1772 	case EAP_MIC_PREAMP:
   1773 		dip->type = AUDIO_MIXER_ENUM;
   1774 		dip->mixer_class = EAP_INPUT_CLASS;
   1775 		dip->prev = EAP_MIC_VOL;
   1776 		dip->next = AUDIO_MIXER_LAST;
   1777 		strcpy(dip->label.name, AudioNpreamp);
   1778 		dip->un.e.num_mem = 2;
   1779 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1780 		dip->un.e.member[0].ord = 0;
   1781 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1782 		dip->un.e.member[1].ord = 1;
   1783 		return 0;
   1784 	case EAP_OUTPUT_CLASS:
   1785 		dip->type = AUDIO_MIXER_CLASS;
   1786 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1787 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1788 		strcpy(dip->label.name, AudioCoutputs);
   1789 		return 0;
   1790 	case EAP_RECORD_CLASS:
   1791 		dip->type = AUDIO_MIXER_CLASS;
   1792 		dip->mixer_class = EAP_RECORD_CLASS;
   1793 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1794 		strcpy(dip->label.name, AudioCrecord);
   1795 		return 0;
   1796 	case EAP_INPUT_CLASS:
   1797 		dip->type = AUDIO_MIXER_CLASS;
   1798 		dip->mixer_class = EAP_INPUT_CLASS;
   1799 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1800 		strcpy(dip->label.name, AudioCinputs);
   1801 		return 0;
   1802 	}
   1803 	return ENXIO;
   1804 }
   1805 
   1806 static void *
   1807 eap_malloc(void *addr, int direction, size_t size,
   1808     struct malloc_type *pool, int flags)
   1809 {
   1810 	struct eap_instance *ei;
   1811 	struct eap_softc *sc;
   1812 	struct eap_dma *p;
   1813 	int error;
   1814 
   1815 	p = malloc(sizeof(*p), pool, flags);
   1816 	if (!p)
   1817 		return NULL;
   1818 	ei = addr;
   1819 	sc = (struct eap_softc *)ei->parent;
   1820 	error = eap_allocmem(sc, size, 16, p);
   1821 	if (error) {
   1822 		free(p, pool);
   1823 		return NULL;
   1824 	}
   1825 	p->next = sc->sc_dmas;
   1826 	sc->sc_dmas = p;
   1827 	return KERNADDR(p);
   1828 }
   1829 
   1830 static void
   1831 eap_free(void *addr, void *ptr, struct malloc_type *pool)
   1832 {
   1833 	struct eap_instance *ei;
   1834 	struct eap_softc *sc;
   1835 	struct eap_dma **pp, *p;
   1836 
   1837 	ei = addr;
   1838 	sc = (struct eap_softc *)ei->parent;
   1839 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1840 		if (KERNADDR(p) == ptr) {
   1841 			eap_freemem(sc, p);
   1842 			*pp = p->next;
   1843 			free(p, pool);
   1844 			return;
   1845 		}
   1846 	}
   1847 }
   1848 
   1849 static size_t
   1850 eap_round_buffersize(void *addr, int direction, size_t size)
   1851 {
   1852 
   1853 	return size;
   1854 }
   1855 
   1856 static paddr_t
   1857 eap_mappage(void *addr, void *mem, off_t off, int prot)
   1858 {
   1859 	struct eap_instance *ei;
   1860 	struct eap_softc *sc;
   1861 	struct eap_dma *p;
   1862 
   1863 	if (off < 0)
   1864 		return -1;
   1865 	ei = addr;
   1866 	sc = (struct eap_softc *)ei->parent;
   1867 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1868 		continue;
   1869 	if (!p)
   1870 		return -1;
   1871 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1872 			       off, prot, BUS_DMA_WAITOK);
   1873 }
   1874 
   1875 static int
   1876 eap_get_props(void *addr)
   1877 {
   1878 
   1879 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1880 	    AUDIO_PROP_FULLDUPLEX;
   1881 }
   1882 
   1883 #if NMIDI > 0
   1884 static int
   1885 eap_midi_open(void *addr, int flags,
   1886 	      void (*iintr)(void *, int),
   1887 	      void (*ointr)(void *),
   1888 	      void *arg)
   1889 {
   1890 	struct eap_softc *sc;
   1891 	uint8_t uctrl;
   1892 
   1893 	sc = addr;
   1894 	sc->sc_arg = arg;
   1895 
   1896 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
   1897 	uctrl = 0;
   1898 	if (flags & FREAD) {
   1899 		uctrl |= EAP_UC_RXINTEN;
   1900 		sc->sc_iintr = iintr;
   1901 	}
   1902 	if (flags & FWRITE)
   1903 		sc->sc_ointr = ointr;
   1904 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1905 
   1906 	return 0;
   1907 }
   1908 
   1909 static void
   1910 eap_midi_close(void *addr)
   1911 {
   1912 	struct eap_softc *sc;
   1913 
   1914 	sc = addr;
   1915 	tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
   1916 	EWRITE1(sc, EAP_UART_CONTROL, 0);
   1917 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
   1918 
   1919 	sc->sc_iintr = 0;
   1920 	sc->sc_ointr = 0;
   1921 }
   1922 
   1923 static int
   1924 eap_midi_output(void *addr, int d)
   1925 {
   1926 	struct eap_softc *sc;
   1927 	uint8_t uctrl;
   1928 
   1929 	sc = addr;
   1930 	EWRITE1(sc, EAP_UART_DATA, d);
   1931 
   1932 	uctrl = EAP_UC_TXINTEN;
   1933 	if (sc->sc_iintr)
   1934 		uctrl |= EAP_UC_RXINTEN;
   1935 	/*
   1936 	 * This is a write-only register, so we have to remember the right
   1937 	 * value of RXINTEN as well as setting TXINTEN. But if we are open
   1938 	 * for reading, it will always be correct to set RXINTEN here; only
   1939 	 * during service of a receive interrupt could it be momentarily
   1940 	 * toggled off, and whether we got here from the top half or from
   1941 	 * an interrupt, that won't be the current state.
   1942 	 */
   1943 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1944 	return 0;
   1945 }
   1946 
   1947 static void
   1948 eap_midi_getinfo(void *addr, struct midi_info *mi)
   1949 {
   1950 	mi->name = "AudioPCI MIDI UART";
   1951 	mi->props = MIDI_PROP_CAN_INPUT | MIDI_PROP_OUT_INTR;
   1952 }
   1953 
   1954 #endif
   1955