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