Home | History | Annotate | Line # | Download | only in pci
eap.c revision 1.92.12.3
      1 /*	$NetBSD: eap.c,v 1.92.12.3 2008/12/09 13:09:13 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.3 2008/12/09 13:09:13 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 	eap_get_locks,
    311 };
    312 #endif
    313 
    314 static struct audio_device eap_device = {
    315 	"Ensoniq AudioPCI",
    316 	"",
    317 	"eap"
    318 };
    319 
    320 #define EAP_NFORMATS	4
    321 static const struct audio_format eap_formats[EAP_NFORMATS] = {
    322 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    323 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    324 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    325 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    326 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    327 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    328 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    329 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    330 };
    331 
    332 static int
    333 eap_match(device_t parent, cfdata_t match, void *aux)
    334 {
    335 	struct pci_attach_args *pa;
    336 
    337 	pa = (struct pci_attach_args *)aux;
    338 	switch (PCI_VENDOR(pa->pa_id)) {
    339 	case PCI_VENDOR_CREATIVELABS:
    340 		switch (PCI_PRODUCT(pa->pa_id)) {
    341 		case PCI_PRODUCT_CREATIVELABS_EV1938:
    342 			return 1;
    343 		}
    344 		break;
    345 	case PCI_VENDOR_ENSONIQ:
    346 		switch (PCI_PRODUCT(pa->pa_id)) {
    347 		case PCI_PRODUCT_ENSONIQ_AUDIOPCI:
    348 		case PCI_PRODUCT_ENSONIQ_AUDIOPCI97:
    349 		case PCI_PRODUCT_ENSONIQ_CT5880:
    350 			return 1;
    351 		}
    352 		break;
    353 	}
    354 
    355 	return 0;
    356 }
    357 
    358 static void
    359 eap1370_write_codec(struct eap_softc *sc, int a, int d)
    360 {
    361 	int icss, to;
    362 
    363 	to = EAP_WRITE_TIMEOUT;
    364 	do {
    365 		icss = EREAD4(sc, EAP_ICSS);
    366 		DPRINTFN(5,("eap: codec %d prog: icss=0x%08x\n", a, icss));
    367 		if (!to--) {
    368 			printf("eap: timeout writing to codec\n");
    369 			return;
    370 		}
    371 	} while(icss & EAP_CWRIP);  /* XXX could use CSTAT here */
    372 	EWRITE4(sc, EAP_CODEC, EAP_SET_CODEC(a, d));
    373 }
    374 
    375 /*
    376  * Reading and writing the CODEC is very convoluted.  This mimics the
    377  * FreeBSD and Linux drivers.
    378  */
    379 
    380 static inline void
    381 eap1371_ready_codec(struct eap_softc *sc, uint8_t a, uint32_t wd)
    382 {
    383 	int to;
    384 	uint32_t src, t;
    385 
    386 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    387 		if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
    388 			break;
    389 		delay(1);
    390 	}
    391 	if (to >= EAP_WRITE_TIMEOUT)
    392 		aprint_error_dev(sc->sc_dev,
    393 		    "eap1371_ready_codec timeout 1\n");
    394 
    395 	mutex_spin_enter(&sc->sc_intr_lock);
    396 	src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    397 	EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
    398 
    399 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    400 		t = EREAD4(sc, E1371_SRC);
    401 		if ((t & E1371_SRC_STATE_MASK) == 0)
    402 			break;
    403 		delay(1);
    404 	}
    405 	if (to >= EAP_READ_TIMEOUT)
    406 		aprint_error_dev(sc->sc_dev,
    407 		    "eap1371_ready_codec timeout 2\n");
    408 
    409 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    410 		t = EREAD4(sc, E1371_SRC);
    411 		if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
    412 			break;
    413 		delay(1);
    414 	}
    415 	if (to >= EAP_READ_TIMEOUT)
    416 		aprint_error_dev(sc->sc_dev,
    417 		    "eap1371_ready_codec timeout 3\n");
    418 
    419 	EWRITE4(sc, E1371_CODEC, wd);
    420 
    421 	eap1371_src_wait(sc);
    422 	EWRITE4(sc, E1371_SRC, src);
    423 
    424 	mutex_spin_exit(&sc->sc_intr_lock);
    425 }
    426 
    427 static int
    428 eap1371_read_codec(void *sc_, uint8_t a, uint16_t *d)
    429 {
    430 	struct eap_softc *sc;
    431 	int to;
    432 	uint32_t t;
    433 
    434 	sc = sc_;
    435 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, 0) | E1371_CODEC_READ);
    436 
    437 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    438 		if (!(EREAD4(sc, E1371_CODEC) & E1371_CODEC_WIP))
    439 			break;
    440 	}
    441 	if (to > EAP_WRITE_TIMEOUT)
    442 		aprint_error_dev(sc->sc_dev,
    443 		    "eap1371_read_codec timeout 1\n");
    444 
    445 	for (to = 0; to < EAP_WRITE_TIMEOUT; to++) {
    446 		t = EREAD4(sc, E1371_CODEC);
    447 		if (t & E1371_CODEC_VALID)
    448 			break;
    449 	}
    450 	if (to > EAP_WRITE_TIMEOUT)
    451 		aprint_error_dev(sc->sc_dev, "eap1371_read_codec timeout 2\n");
    452 
    453 	*d = (uint16_t)t;
    454 
    455 	DPRINTFN(10, ("eap1371: reading codec (%x) = %x\n", a, *d));
    456 
    457 	return 0;
    458 }
    459 
    460 static int
    461 eap1371_write_codec(void *sc_, uint8_t a, uint16_t d)
    462 {
    463 	struct eap_softc *sc;
    464 
    465 	sc = sc_;
    466 	eap1371_ready_codec(sc, a, E1371_SET_CODEC(a, d));
    467 
    468 	DPRINTFN(10, ("eap1371: writing codec %x --> %x\n", d, a));
    469 
    470 	return 0;
    471 }
    472 
    473 static uint32_t
    474 eap1371_src_wait(struct eap_softc *sc)
    475 {
    476 	int to;
    477 	u_int32_t src;
    478 
    479 	for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    480 		src = EREAD4(sc, E1371_SRC);
    481 		if (!(src & E1371_SRC_RBUSY))
    482 			return src;
    483 		delay(1);
    484 	}
    485 	aprint_error_dev(sc->sc_dev, "eap1371_src_wait timeout\n");
    486 	return src;
    487 }
    488 
    489 static int
    490 eap1371_src_read(struct eap_softc *sc, int a)
    491 {
    492 	int to;
    493 	uint32_t src, t;
    494 
    495 	src = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    496 	src |= E1371_SRC_ADDR(a);
    497 	EWRITE4(sc, E1371_SRC, src | E1371_SRC_STATE_OK);
    498 
    499 	t = eap1371_src_wait(sc);
    500 	if ((t & E1371_SRC_STATE_MASK) != E1371_SRC_STATE_OK) {
    501 		for (to = 0; to < EAP_READ_TIMEOUT; to++) {
    502 			t = EREAD4(sc, E1371_SRC);
    503 			if ((t & E1371_SRC_STATE_MASK) == E1371_SRC_STATE_OK)
    504 				break;
    505 			delay(1);
    506 		}
    507 	}
    508 
    509 	EWRITE4(sc, E1371_SRC, src);
    510 
    511 	return t & E1371_SRC_DATAMASK;
    512 }
    513 
    514 static void
    515 eap1371_src_write(struct eap_softc *sc, int a, int d)
    516 {
    517 	uint32_t r;
    518 
    519 	r = eap1371_src_wait(sc) & E1371_SRC_CTLMASK;
    520 	r |= E1371_SRC_RAMWE | E1371_SRC_ADDR(a) | E1371_SRC_DATA(d);
    521 	EWRITE4(sc, E1371_SRC, r);
    522 }
    523 
    524 static void
    525 eap1371_set_adc_rate(struct eap_softc *sc, int rate)
    526 {
    527 	int freq, n, truncm;
    528 	int out;
    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 	mutex_spin_enter(&sc->sc_intr_lock);
    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 	mutex_spin_exit(&sc->sc_intr_lock);
    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 
    570 	DPRINTFN(2, ("eap1371_set_dac_date: set rate for %d\n", ei->index));
    571 	sc = device_private(ei->parent);
    572 	dac = ei->index == EAP_DAC1 ? ESRC_DAC1 : ESRC_DAC2;
    573 
    574 	/* Whatever, it works, so I'll leave it :) */
    575 
    576 	if (rate > 48000)
    577 	    rate = 48000;
    578 	if (rate < 4000)
    579 	    rate = 4000;
    580 	freq = ((rate << 15) + 1500) / 3000;
    581 
    582 	mutex_spin_enter(&sc->sc_intr_lock);
    583 	eap1371_src_wait(sc);
    584 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    585 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    586 	r |= ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2;
    587 	EWRITE4(sc, E1371_SRC, r);
    588 	r = eap1371_src_read(sc, dac + ESRC_IREGS) & 0x00ff;
    589 	eap1371_src_write(sc, dac + ESRC_IREGS, r | ((freq >> 5) & 0xfc00));
    590 	eap1371_src_write(sc, dac + ESRC_VFF, freq & 0x7fff);
    591 	r = EREAD4(sc, E1371_SRC) & (E1371_SRC_DISABLE |
    592 	    E1371_SRC_DISP2 | E1371_SRC_DISP1 | E1371_SRC_DISREC);
    593 	r &= ~(ei->index == EAP_DAC1 ? E1371_SRC_DISP1 : E1371_SRC_DISP2);
    594 	EWRITE4(sc, E1371_SRC, r);
    595 	mutex_spin_exit(&sc->sc_intr_lock);
    596 }
    597 
    598 static void
    599 eap_attach(device_t parent, device_t self, void *aux)
    600 {
    601 	struct eap_softc *sc;
    602 	struct pci_attach_args *pa;
    603 	pci_chipset_tag_t pc;
    604 	const struct audio_hw_if *eap_hw_if;
    605 	char const *intrstr;
    606 	pci_intr_handle_t ih;
    607 	pcireg_t csr;
    608 	char devinfo[256];
    609 	mixer_ctrl_t ctl;
    610 	int i;
    611 	int revision, ct5880;
    612 	const char *revstr;
    613 #if NJOY_EAP > 0
    614 	struct eap_gameport_args gpargs;
    615 #endif
    616 
    617 	sc = device_private(self);
    618 	sc->sc_dev = self;
    619 	pa = (struct pci_attach_args *)aux;
    620 	pc = pa->pa_pc;
    621 	revstr = "";
    622 	aprint_naive(": Audio controller\n");
    623 
    624 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    625 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    626 
    627 	/* Stash this away for detach */
    628 	sc->sc_pc = pc;
    629 
    630 	/* Flag if we're "creative" */
    631 	sc->sc_1371 = !(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
    632 			PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_AUDIOPCI);
    633 
    634 	/*
    635 	 * The vendor and product ID's are quite "interesting". Just
    636 	 * trust the following and be happy.
    637 	 */
    638 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    639 	revision = PCI_REVISION(pa->pa_class);
    640 	ct5880 = 0;
    641 	if (sc->sc_1371) {
    642 		if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENSONIQ &&
    643 		    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENSONIQ_CT5880) {
    644 			ct5880 = 1;
    645 			switch (revision) {
    646 			case EAP_CT5880_C: revstr = "CT5880-C "; break;
    647 			case EAP_CT5880_D: revstr = "CT5880-D "; break;
    648 			case EAP_CT5880_E: revstr = "CT5880-E "; break;
    649 			}
    650 		} else {
    651 			switch (revision) {
    652 			case EAP_EV1938_A: revstr = "EV1938-A "; break;
    653 			case EAP_ES1373_A: revstr = "ES1373-A "; break;
    654 			case EAP_ES1373_B: revstr = "ES1373-B "; break;
    655 			case EAP_CT5880_A: revstr = "CT5880-A "; ct5880=1;break;
    656 			case EAP_ES1373_8: revstr = "ES1373-8" ; ct5880=1;break;
    657 			case EAP_ES1371_B: revstr = "ES1371-B "; break;
    658 			}
    659 		}
    660 	}
    661 	aprint_normal(": %s %s(rev. 0x%02x)\n", devinfo, revstr, revision);
    662 
    663 	/* Map I/O register */
    664 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    665 	      &sc->iot, &sc->ioh, NULL, &sc->iosz)) {
    666 		aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
    667 		return;
    668 	}
    669 
    670 	sc->sc_dmatag = pa->pa_dmat;
    671 
    672 	/* Enable the device. */
    673 	csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    674 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    675 		       csr | PCI_COMMAND_MASTER_ENABLE);
    676 
    677 	/* Map and establish the interrupt. */
    678 	if (pci_intr_map(pa, &ih)) {
    679 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
    680 		return;
    681 	}
    682 	intrstr = pci_intr_string(pc, ih);
    683 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_SCHED, eap_intr, sc);
    684 	if (sc->sc_ih == NULL) {
    685 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
    686 		if (intrstr != NULL)
    687 			aprint_error(" at %s", intrstr);
    688 		aprint_error("\n");
    689 		return;
    690 	}
    691 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    692 
    693 	sc->sc_ei[EAP_I1].parent = self;
    694 	sc->sc_ei[EAP_I1].index = EAP_DAC2;
    695 	sc->sc_ei[EAP_I2].parent = self;
    696 	sc->sc_ei[EAP_I2].index = EAP_DAC1;
    697 
    698 	if (!sc->sc_1371) {
    699 		/* Enable interrupts and looping mode. */
    700 		/* enable the parts we need */
    701 		EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    702 		EWRITE4(sc, EAP_ICSC, EAP_CDC_EN);
    703 
    704 		/* reset codec */
    705 		/* normal operation */
    706 		/* select codec clocks */
    707 		eap1370_write_codec(sc, AK_RESET, AK_PD);
    708 		eap1370_write_codec(sc, AK_RESET, AK_PD | AK_NRST);
    709 		eap1370_write_codec(sc, AK_CS, 0x0);
    710 
    711 		eap_hw_if = &eap1370_hw_if;
    712 
    713 		/* Enable all relevant mixer switches. */
    714 		ctl.dev = EAP_INPUT_SOURCE;
    715 		ctl.type = AUDIO_MIXER_SET;
    716 		ctl.un.mask = 1 << EAP_VOICE_VOL | 1 << EAP_FM_VOL |
    717 			1 << EAP_CD_VOL | 1 << EAP_LINE_VOL | 1 << EAP_AUX_VOL |
    718 			1 << EAP_MIC_VOL;
    719 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    720 
    721 		ctl.type = AUDIO_MIXER_VALUE;
    722 		ctl.un.value.num_channels = 1;
    723 		for (ctl.dev = EAP_MASTER_VOL; ctl.dev < EAP_MIC_VOL;
    724 		     ctl.dev++) {
    725 			ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = VOL_0DB;
    726 			eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    727 		}
    728 		ctl.un.value.level[AUDIO_MIXER_LEVEL_MONO] = 0;
    729 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    730 		ctl.dev = EAP_MIC_PREAMP;
    731 		ctl.type = AUDIO_MIXER_ENUM;
    732 		ctl.un.ord = 0;
    733 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    734 		ctl.dev = EAP_RECORD_SOURCE;
    735 		ctl.type = AUDIO_MIXER_SET;
    736 		ctl.un.mask = 1 << EAP_MIC_VOL;
    737 		eap_hw_if->set_port(&sc->sc_ei[EAP_I1], &ctl);
    738 	} else {
    739 		/* clean slate */
    740 
    741 		EWRITE4(sc, EAP_SIC, 0);
    742 		EWRITE4(sc, EAP_ICSC, 0);
    743 		EWRITE4(sc, E1371_LEGACY, 0);
    744 
    745 		if (ct5880) {
    746 			EWRITE4(sc, EAP_ICSS, EAP_CT5880_AC97_RESET);
    747 			/* Let codec wake up */
    748 			delay(20000);
    749 		}
    750 
    751 		/* Reset from es1371's perspective */
    752 		EWRITE4(sc, EAP_ICSC, E1371_SYNC_RES);
    753 		delay(20);
    754 		EWRITE4(sc, EAP_ICSC, 0);
    755 
    756 		/*
    757 		 * Must properly reprogram sample rate converter,
    758 		 * or it locks up.  Set some defaults for the life of the
    759 		 * machine, and set up a sb default sample rate.
    760 		 */
    761 		EWRITE4(sc, E1371_SRC, E1371_SRC_DISABLE);
    762 		for (i = 0; i < 0x80; i++)
    763 			eap1371_src_write(sc, i, 0);
    764 		eap1371_src_write(sc, ESRC_DAC1+ESRC_TRUNC_N, ESRC_SET_N(16));
    765 		eap1371_src_write(sc, ESRC_DAC2+ESRC_TRUNC_N, ESRC_SET_N(16));
    766 		eap1371_src_write(sc, ESRC_DAC1+ESRC_IREGS, ESRC_SET_VFI(16));
    767 		eap1371_src_write(sc, ESRC_DAC2+ESRC_IREGS, ESRC_SET_VFI(16));
    768 		eap1371_src_write(sc, ESRC_ADC_VOLL, ESRC_SET_ADC_VOL(16));
    769 		eap1371_src_write(sc, ESRC_ADC_VOLR, ESRC_SET_ADC_VOL(16));
    770 		eap1371_src_write(sc, ESRC_DAC1_VOLL, ESRC_SET_DAC_VOLI(1));
    771 		eap1371_src_write(sc, ESRC_DAC1_VOLR, ESRC_SET_DAC_VOLI(1));
    772 		eap1371_src_write(sc, ESRC_DAC2_VOLL, ESRC_SET_DAC_VOLI(1));
    773 		eap1371_src_write(sc, ESRC_DAC2_VOLR, ESRC_SET_DAC_VOLI(1));
    774 		eap1371_set_adc_rate(sc, 22050);
    775 		eap1371_set_dac_rate(&sc->sc_ei[0], 22050);
    776 		eap1371_set_dac_rate(&sc->sc_ei[1], 22050);
    777 
    778 		EWRITE4(sc, E1371_SRC, 0);
    779 
    780 		/* Reset codec */
    781 
    782 		/* Interrupt enable */
    783 		sc->host_if.arg = sc;
    784 		sc->host_if.attach = eap1371_attach_codec;
    785 		sc->host_if.read = eap1371_read_codec;
    786 		sc->host_if.write = eap1371_write_codec;
    787 		sc->host_if.reset = eap1371_reset_codec;
    788 
    789 		if (ac97_attach(&sc->host_if, self, &sc->sc_lock) == 0) {
    790 			/* Interrupt enable */
    791 			EWRITE4(sc, EAP_SIC, EAP_P2_INTR_EN | EAP_R1_INTR_EN);
    792 		} else
    793 			return;
    794 
    795 		eap_hw_if = &eap1371_hw_if;
    796 	}
    797 
    798 	sc->sc_ei[EAP_I1].ei_audiodev =
    799 	    audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I1], sc->sc_dev);
    800 
    801 #ifdef EAP_USE_BOTH_DACS
    802 	aprint_normal_dev(self, "attaching secondary DAC\n");
    803 	sc->sc_ei[EAP_I2].ei_audiodev =
    804 	    audio_attach_mi(eap_hw_if, &sc->sc_ei[EAP_I2], sc->sc_dev);
    805 #endif
    806 
    807 #if NMIDI > 0
    808 	sc->sc_mididev = midi_attach_mi(&eap_midi_hw_if, sc, sc->sc_dev);
    809 #endif
    810 
    811 #if NJOY_EAP > 0
    812 	if (sc->sc_1371) {
    813 		gpargs.gpa_iot = sc->iot;
    814 		gpargs.gpa_ioh = sc->ioh;
    815 		sc->sc_gameport = eap_joy_attach(sc->sc_dev, &gpargs);
    816 	}
    817 #endif
    818 }
    819 
    820 static int
    821 eap_detach(device_t self, int flags)
    822 {
    823 	struct eap_softc *sc;
    824 	int res;
    825 #if NJOY_EAP > 0
    826 	struct eap_gameport_args gpargs;
    827 
    828 	sc = device_private(self);
    829 	if (sc->sc_gameport) {
    830 		gpargs.gpa_iot = sc->iot;
    831 		gpargs.gpa_ioh = sc->ioh;
    832 		res = eap_joy_detach(sc->sc_gameport, &gpargs);
    833 		if (res)
    834 			return res;
    835 	}
    836 #else
    837 	sc = device_private(self);
    838 #endif
    839 #if NMIDI > 0
    840 	if (sc->sc_mididev != NULL) {
    841 		res = config_detach(sc->sc_mididev, 0);
    842 		if (res)
    843 			return res;
    844 	}
    845 #endif
    846 #ifdef EAP_USE_BOTH_DACS
    847 	if (sc->sc_ei[EAP_I2].ei_audiodev != NULL) {
    848 		res = config_detach(sc->sc_ei[EAP_I2].ei_audiodev, 0);
    849 		if (res)
    850 			return res;
    851 	}
    852 #endif
    853 	if (sc->sc_ei[EAP_I1].ei_audiodev != NULL) {
    854 		res = config_detach(sc->sc_ei[EAP_I1].ei_audiodev, 0);
    855 		if (res)
    856 			return res;
    857 	}
    858 
    859 	bus_space_unmap(sc->iot, sc->ioh, sc->iosz);
    860 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    861 	mutex_destroy(&sc->sc_lock);
    862 	mutex_destroy(&sc->sc_intr_lock);
    863 
    864 	return 0;
    865 }
    866 
    867 static int
    868 eap1371_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    869 {
    870 	struct eap_softc *sc;
    871 
    872 	sc = sc_;
    873 	sc->codec_if = codec_if;
    874 	return 0;
    875 }
    876 
    877 static int
    878 eap1371_reset_codec(void *sc_)
    879 {
    880 	struct eap_softc *sc;
    881 	uint32_t icsc;
    882 
    883 	sc = sc_;
    884 	mutex_spin_enter(&sc->sc_intr_lock);
    885 	icsc = EREAD4(sc, EAP_ICSC);
    886 	EWRITE4(sc, EAP_ICSC, icsc | E1371_SYNC_RES);
    887 	delay(20);
    888 	EWRITE4(sc, EAP_ICSC, icsc & ~E1371_SYNC_RES);
    889 	delay(1);
    890 	mutex_spin_exit(&sc->sc_intr_lock);
    891 
    892 	return 0;
    893 }
    894 
    895 static int
    896 eap_intr(void *p)
    897 {
    898 	struct eap_softc *sc;
    899 	uint32_t intr, sic;
    900 
    901 	sc = p;
    902 	mutex_spin_enter(&sc->sc_intr_lock);
    903 	intr = EREAD4(sc, EAP_ICSS);
    904 	if (!(intr & EAP_INTR)) {
    905 		mutex_spin_exit(&sc->sc_intr_lock);
    906 		return 0;
    907 	}
    908 	sic = EREAD4(sc, EAP_SIC);
    909 	DPRINTFN(5, ("eap_intr: ICSS=0x%08x, SIC=0x%08x\n", intr, sic));
    910 	if (intr & EAP_I_ADC) {
    911 #if 0
    912 		/*
    913 		 * XXX This is a hack!
    914 		 * The EAP chip sometimes generates the recording interrupt
    915 		 * while it is still transferring the data.  To make sure
    916 		 * it has all arrived we busy wait until the count is right.
    917 		 * The transfer we are waiting for is 8 longwords.
    918 		 */
    919 		int s, nw, n;
    920 		EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
    921 		s = EREAD4(sc, EAP_ADC_CSR);
    922 		nw = ((s & 0xffff) + 1) >> 2; /* # of words in DMA */
    923 		n = 0;
    924 		while (((EREAD4(sc, EAP_ADC_SIZE) >> 16) + 8) % nw == 0) {
    925 			delay(10);
    926 			if (++n > 100) {
    927 				printf("eapintr: DMA fix timeout");
    928 				break;
    929 			}
    930 		}
    931 		/* Continue with normal interrupt handling. */
    932 #endif
    933 		EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
    934 		EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
    935 		if (sc->sc_rintr)
    936 			sc->sc_rintr(sc->sc_rarg);
    937 	}
    938 
    939 	if (intr & EAP_I_DAC2) {
    940 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P2_INTR_EN);
    941 		EWRITE4(sc, EAP_SIC, sic | EAP_P2_INTR_EN);
    942 		if (sc->sc_ei[EAP_DAC2].ei_pintr)
    943 			sc->sc_ei[EAP_DAC2].ei_pintr(sc->sc_ei[EAP_DAC2].ei_parg);
    944 	}
    945 
    946 	if (intr & EAP_I_DAC1) {
    947 		EWRITE4(sc, EAP_SIC, sic & ~EAP_P1_INTR_EN);
    948 		EWRITE4(sc, EAP_SIC, sic | EAP_P1_INTR_EN);
    949 		if (sc->sc_ei[EAP_DAC1].ei_pintr)
    950 			sc->sc_ei[EAP_DAC1].ei_pintr(sc->sc_ei[EAP_DAC1].ei_parg);
    951 	}
    952 
    953 	if (intr & EAP_I_MCCB)
    954 		panic("eap_intr: unexpected MCCB interrupt");
    955 #if NMIDI > 0
    956 	if (intr & EAP_I_UART) {
    957 		uint8_t ustat;
    958 		uint32_t data;
    959 
    960 		ustat = EREAD1(sc, EAP_UART_STATUS);
    961 
    962 		if (ustat & EAP_US_RXINT) {
    963 			while (EREAD1(sc, EAP_UART_STATUS) & EAP_US_RXRDY) {
    964 				data = EREAD1(sc, EAP_UART_DATA);
    965 				sc->sc_iintr(sc->sc_arg, data);
    966 			}
    967 		}
    968 
    969 		if (ustat & EAP_US_TXINT)
    970 			eap_uart_txrdy(sc);
    971 	}
    972 #endif
    973 	mutex_spin_exit(&sc->sc_intr_lock);
    974 	return 1;
    975 }
    976 
    977 static int
    978 eap_allocmem(struct eap_softc *sc, size_t size, size_t align, struct eap_dma *p)
    979 {
    980 	int error;
    981 
    982 	p->size = size;
    983 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    984 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    985 				 &p->nsegs, BUS_DMA_WAITOK);
    986 	if (error)
    987 		return error;
    988 
    989 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    990 			       &p->addr, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    991 	if (error)
    992 		goto free;
    993 
    994 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    995 				  0, BUS_DMA_WAITOK, &p->map);
    996 	if (error)
    997 		goto unmap;
    998 
    999 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
   1000 				BUS_DMA_WAITOK);
   1001 	if (error)
   1002 		goto destroy;
   1003 	return (0);
   1004 
   1005 destroy:
   1006 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1007 unmap:
   1008 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1009 free:
   1010 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1011 	return error;
   1012 }
   1013 
   1014 static int
   1015 eap_freemem(struct eap_softc *sc, struct eap_dma *p)
   1016 {
   1017 
   1018 	bus_dmamap_unload(sc->sc_dmatag, p->map);
   1019 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1020 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1021 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1022 	return 0;
   1023 }
   1024 
   1025 static int
   1026 eap_open(void *addr, int flags)
   1027 {
   1028 	struct eap_instance *ei;
   1029 
   1030 	ei = addr;
   1031 	/* there is only one ADC */
   1032 	if (ei->index == EAP_I2 && flags & FREAD)
   1033 		return EOPNOTSUPP;
   1034 
   1035 	return 0;
   1036 }
   1037 
   1038 static int
   1039 eap_query_encoding(void *addr, struct audio_encoding *fp)
   1040 {
   1041 
   1042 	switch (fp->index) {
   1043 	case 0:
   1044 		strcpy(fp->name, AudioEulinear);
   1045 		fp->encoding = AUDIO_ENCODING_ULINEAR;
   1046 		fp->precision = 8;
   1047 		fp->flags = 0;
   1048 		return 0;
   1049 	case 1:
   1050 		strcpy(fp->name, AudioEmulaw);
   1051 		fp->encoding = AUDIO_ENCODING_ULAW;
   1052 		fp->precision = 8;
   1053 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1054 		return 0;
   1055 	case 2:
   1056 		strcpy(fp->name, AudioEalaw);
   1057 		fp->encoding = AUDIO_ENCODING_ALAW;
   1058 		fp->precision = 8;
   1059 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1060 		return 0;
   1061 	case 3:
   1062 		strcpy(fp->name, AudioEslinear);
   1063 		fp->encoding = AUDIO_ENCODING_SLINEAR;
   1064 		fp->precision = 8;
   1065 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1066 		return 0;
   1067 	case 4:
   1068 		strcpy(fp->name, AudioEslinear_le);
   1069 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
   1070 		fp->precision = 16;
   1071 		fp->flags = 0;
   1072 		return 0;
   1073 	case 5:
   1074 		strcpy(fp->name, AudioEulinear_le);
   1075 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
   1076 		fp->precision = 16;
   1077 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1078 		return 0;
   1079 	case 6:
   1080 		strcpy(fp->name, AudioEslinear_be);
   1081 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
   1082 		fp->precision = 16;
   1083 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1084 		return 0;
   1085 	case 7:
   1086 		strcpy(fp->name, AudioEulinear_be);
   1087 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
   1088 		fp->precision = 16;
   1089 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1090 		return 0;
   1091 	default:
   1092 		return EINVAL;
   1093 	}
   1094 }
   1095 
   1096 static int
   1097 eap_set_params(void *addr, int setmode, int usemode,
   1098 	       audio_params_t *play, audio_params_t *rec,
   1099 	       stream_filter_list_t *pfil, stream_filter_list_t *rfil)
   1100 {
   1101 	struct eap_instance *ei;
   1102 	struct eap_softc *sc;
   1103 	struct audio_params *p;
   1104 	stream_filter_list_t *fil;
   1105 	int mode, i;
   1106 	uint32_t div;
   1107 
   1108 	ei = addr;
   1109 	sc = device_private(ei->parent);
   1110 	/*
   1111 	 * The es1370 only has one clock, so make the sample rates match.
   1112 	 * This only applies for ADC/DAC2. The FM DAC is handled below.
   1113 	 */
   1114 	if (!sc->sc_1371 && ei->index == EAP_DAC2) {
   1115 		if (play->sample_rate != rec->sample_rate &&
   1116 		    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1117 			if (setmode == AUMODE_PLAY) {
   1118 				rec->sample_rate = play->sample_rate;
   1119 				setmode |= AUMODE_RECORD;
   1120 			} else if (setmode == AUMODE_RECORD) {
   1121 				play->sample_rate = rec->sample_rate;
   1122 				setmode |= AUMODE_PLAY;
   1123 			} else
   1124 				return EINVAL;
   1125 		}
   1126 	}
   1127 
   1128 	for (mode = AUMODE_RECORD; mode != -1;
   1129 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1130 		if ((setmode & mode) == 0)
   1131 			continue;
   1132 
   1133 		p = mode == AUMODE_PLAY ? play : rec;
   1134 
   1135 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
   1136 		    (p->precision != 8 && p->precision != 16) ||
   1137 		    (p->channels != 1 && p->channels != 2))
   1138 			return EINVAL;
   1139 
   1140 		fil = mode == AUMODE_PLAY ? pfil : rfil;
   1141 		i = auconv_set_converter(eap_formats, EAP_NFORMATS,
   1142 					 mode, p, FALSE, fil);
   1143 		if (i < 0)
   1144 			return EINVAL;
   1145 	}
   1146 
   1147 	if (sc->sc_1371) {
   1148 		eap1371_set_dac_rate(ei, play->sample_rate);
   1149 		eap1371_set_adc_rate(sc, rec->sample_rate);
   1150 	} else if (ei->index == EAP_DAC2) {
   1151 		/* Set the speed */
   1152 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n",
   1153 			     EREAD4(sc, EAP_ICSC)));
   1154 		div = EREAD4(sc, EAP_ICSC) & ~EAP_PCLKBITS;
   1155 		/*
   1156 		 * XXX
   1157 		 * The -2 isn't documented, but seemed to make the wall
   1158 		 * time match
   1159 		 * what I expect.  - mycroft
   1160 		 */
   1161 		if (usemode == AUMODE_RECORD)
   1162 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1163 				rec->sample_rate - 2);
   1164 		else
   1165 			div |= EAP_SET_PCLKDIV(EAP_XTAL_FREQ /
   1166 				play->sample_rate - 2);
   1167 #if 0
   1168 		div |= EAP_CCB_INTRM;
   1169 #else
   1170 		/*
   1171 		 * It is not obvious how to acknowledge MCCB interrupts, so
   1172 		 * we had better not enable them.
   1173 		 */
   1174 #endif
   1175 		EWRITE4(sc, EAP_ICSC, div);
   1176 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1177 	} else {
   1178 		/*
   1179 		 * The FM DAC has only a few fixed-frequency choises, so
   1180 		 * pick out the best candidate.
   1181 		 */
   1182 		div = EREAD4(sc, EAP_ICSC);
   1183 		DPRINTFN(2, ("eap_set_params: old ICSC = 0x%08x\n", div));
   1184 
   1185 		div &= ~EAP_WTSRSEL;
   1186 		if (play->sample_rate < 8268)
   1187 			div |= EAP_WTSRSEL_5;
   1188 		else if (play->sample_rate < 16537)
   1189 			div |= EAP_WTSRSEL_11;
   1190 		else if (play->sample_rate < 33075)
   1191 			div |= EAP_WTSRSEL_22;
   1192 		else
   1193 			div |= EAP_WTSRSEL_44;
   1194 
   1195 		EWRITE4(sc, EAP_ICSC, div);
   1196 		DPRINTFN(2, ("eap_set_params: set ICSC = 0x%08x\n", div));
   1197 	}
   1198 
   1199 	return 0;
   1200 }
   1201 
   1202 static int
   1203 eap_round_blocksize(void *addr, int blk, int mode,
   1204     const audio_params_t *param)
   1205 {
   1206 
   1207 	return blk & -32;	/* keep good alignment */
   1208 }
   1209 
   1210 static int
   1211 eap_trigger_output(
   1212 	void *addr,
   1213 	void *start,
   1214 	void *end,
   1215 	int blksize,
   1216 	void (*intr)(void *),
   1217 	void *arg,
   1218 	const audio_params_t *param)
   1219 {
   1220 	struct eap_instance *ei;
   1221 	struct eap_softc *sc;
   1222 	struct eap_dma *p;
   1223 	uint32_t icsc, sic;
   1224 	int sampshift;
   1225 
   1226 	ei = addr;
   1227 	sc = device_private(ei->parent);
   1228 #ifdef DIAGNOSTIC
   1229 	if (ei->ei_prun)
   1230 		panic("eap_trigger_output: already running");
   1231 	ei->ei_prun = 1;
   1232 #endif
   1233 
   1234 	DPRINTFN(1, ("eap_trigger_output: sc=%p start=%p end=%p "
   1235 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1236 	ei->ei_pintr = intr;
   1237 	ei->ei_parg = arg;
   1238 
   1239 	sic = EREAD4(sc, EAP_SIC);
   1240 	sic &= ~(EAP_S_EB(ei->index) | EAP_S_MB(ei->index) | EAP_INC_BITS);
   1241 
   1242 	if (ei->index == EAP_DAC2)
   1243 		sic |= EAP_SET_P2_ST_INC(0)
   1244 		    | EAP_SET_P2_END_INC(param->precision / 8);
   1245 
   1246 	sampshift = 0;
   1247 	if (param->precision == 16) {
   1248 		sic |= EAP_S_EB(ei->index);
   1249 		sampshift++;
   1250 	}
   1251 	if (param->channels == 2) {
   1252 		sic |= EAP_S_MB(ei->index);
   1253 		sampshift++;
   1254 	}
   1255 	EWRITE4(sc, EAP_SIC, sic & ~EAP_P_INTR_EN(ei->index));
   1256 	EWRITE4(sc, EAP_SIC, sic | EAP_P_INTR_EN(ei->index));
   1257 
   1258 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1259 		continue;
   1260 	if (!p) {
   1261 		printf("eap_trigger_output: bad addr %p\n", start);
   1262 		return EINVAL;
   1263 	}
   1264 
   1265 	if (ei->index == EAP_DAC2) {
   1266 		DPRINTF(("eap_trigger_output: DAC2_ADDR=0x%x, DAC2_SIZE=0x%x\n",
   1267 			 (int)DMAADDR(p),
   1268 			 (int)EAP_SET_SIZE(0,
   1269 			 (((char *)end - (char *)start) >> 2) - 1)));
   1270 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1271 		EWRITE4(sc, EAP_DAC2_ADDR, DMAADDR(p));
   1272 		EWRITE4(sc, EAP_DAC2_SIZE,
   1273 			EAP_SET_SIZE(0,
   1274 			((char *)end - (char *)start) >> 2) - 1);
   1275 		EWRITE4(sc, EAP_DAC2_CSR, (blksize >> sampshift) - 1);
   1276 	} else if (ei->index == EAP_DAC1) {
   1277 		DPRINTF(("eap_trigger_output: DAC1_ADDR=0x%x, DAC1_SIZE=0x%x\n",
   1278 			 (int)DMAADDR(p),
   1279 			 (int)EAP_SET_SIZE(0,
   1280 			 (((char *)end - (char *)start) >> 2) - 1)));
   1281 		EWRITE4(sc, EAP_MEMPAGE, EAP_DAC_PAGE);
   1282 		EWRITE4(sc, EAP_DAC1_ADDR, DMAADDR(p));
   1283 		EWRITE4(sc, EAP_DAC1_SIZE,
   1284 			EAP_SET_SIZE(0,
   1285 			((char *)end - (char *)start) >> 2) - 1);
   1286 		EWRITE4(sc, EAP_DAC1_CSR, (blksize >> sampshift) - 1);
   1287 	}
   1288 #ifdef DIAGNOSTIC
   1289 	else
   1290 		panic("eap_trigger_output: impossible instance %d", ei->index);
   1291 #endif
   1292 
   1293 	if (sc->sc_1371)
   1294 		EWRITE4(sc, E1371_SRC, 0);
   1295 
   1296 	icsc = EREAD4(sc, EAP_ICSC);
   1297 	icsc |= EAP_DAC_EN(ei->index);
   1298 	EWRITE4(sc, EAP_ICSC, icsc);
   1299 
   1300 	DPRINTFN(1, ("eap_trigger_output: set ICSC = 0x%08x\n", icsc));
   1301 
   1302 	return 0;
   1303 }
   1304 
   1305 static int
   1306 eap_trigger_input(
   1307 	void *addr,
   1308 	void *start,
   1309 	void *end,
   1310 	int blksize,
   1311 	void (*intr)(void *),
   1312 	void *arg,
   1313 	const audio_params_t *param)
   1314 {
   1315 	struct eap_instance *ei;
   1316 	struct eap_softc *sc;
   1317 	struct eap_dma *p;
   1318 	uint32_t icsc, sic;
   1319 	int sampshift;
   1320 
   1321 	ei = addr;
   1322 	sc = device_private(ei->parent);
   1323 #ifdef DIAGNOSTIC
   1324 	if (sc->sc_rrun)
   1325 		panic("eap_trigger_input: already running");
   1326 	sc->sc_rrun = 1;
   1327 #endif
   1328 
   1329 	DPRINTFN(1, ("eap_trigger_input: ei=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1330 	    addr, start, end, blksize, intr, arg));
   1331 	sc->sc_rintr = intr;
   1332 	sc->sc_rarg = arg;
   1333 
   1334 	sic = EREAD4(sc, EAP_SIC);
   1335 	sic &= ~(EAP_R1_S_EB | EAP_R1_S_MB);
   1336 	sampshift = 0;
   1337 	if (param->precision == 16) {
   1338 		sic |= EAP_R1_S_EB;
   1339 		sampshift++;
   1340 	}
   1341 	if (param->channels == 2) {
   1342 		sic |= EAP_R1_S_MB;
   1343 		sampshift++;
   1344 	}
   1345 	EWRITE4(sc, EAP_SIC, sic & ~EAP_R1_INTR_EN);
   1346 	EWRITE4(sc, EAP_SIC, sic | EAP_R1_INTR_EN);
   1347 
   1348 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
   1349 		continue;
   1350 	if (!p) {
   1351 		printf("eap_trigger_input: bad addr %p\n", start);
   1352 		return (EINVAL);
   1353 	}
   1354 
   1355 	DPRINTF(("eap_trigger_input: ADC_ADDR=0x%x, ADC_SIZE=0x%x\n",
   1356 		 (int)DMAADDR(p),
   1357 		 (int)EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1)));
   1358 	EWRITE4(sc, EAP_MEMPAGE, EAP_ADC_PAGE);
   1359 	EWRITE4(sc, EAP_ADC_ADDR, DMAADDR(p));
   1360 	EWRITE4(sc, EAP_ADC_SIZE,
   1361 		EAP_SET_SIZE(0, (((char *)end - (char *)start) >> 2) - 1));
   1362 
   1363 	EWRITE4(sc, EAP_ADC_CSR, (blksize >> sampshift) - 1);
   1364 
   1365 	if (sc->sc_1371)
   1366 		EWRITE4(sc, E1371_SRC, 0);
   1367 
   1368 	icsc = EREAD4(sc, EAP_ICSC);
   1369 	icsc |= EAP_ADC_EN;
   1370 	EWRITE4(sc, EAP_ICSC, icsc);
   1371 
   1372 	DPRINTFN(1, ("eap_trigger_input: set ICSC = 0x%08x\n", icsc));
   1373 
   1374 	return 0;
   1375 }
   1376 
   1377 static int
   1378 eap_halt_output(void *addr)
   1379 {
   1380 	struct eap_instance *ei;
   1381 	struct eap_softc *sc;
   1382 	uint32_t icsc;
   1383 
   1384 	DPRINTF(("eap: eap_halt_output\n"));
   1385 	ei = addr;
   1386 	sc = device_private(ei->parent);
   1387 	icsc = EREAD4(sc, EAP_ICSC);
   1388 	EWRITE4(sc, EAP_ICSC, icsc & ~(EAP_DAC_EN(ei->index)));
   1389 	ei->ei_pintr = 0;
   1390 #ifdef DIAGNOSTIC
   1391 	ei->ei_prun = 0;
   1392 #endif
   1393 
   1394 	return 0;
   1395 }
   1396 
   1397 static int
   1398 eap_halt_input(void *addr)
   1399 {
   1400 	struct eap_instance *ei;
   1401 	struct eap_softc *sc;
   1402 	uint32_t icsc;
   1403 
   1404 #define EAP_USE_FMDAC_ALSO
   1405 	DPRINTF(("eap: eap_halt_input\n"));
   1406 	ei = addr;
   1407 	sc = device_private(ei->parent);
   1408 	icsc = EREAD4(sc, EAP_ICSC);
   1409 	EWRITE4(sc, EAP_ICSC, icsc & ~EAP_ADC_EN);
   1410 	sc->sc_rintr = 0;
   1411 #ifdef DIAGNOSTIC
   1412 	sc->sc_rrun = 0;
   1413 #endif
   1414 
   1415 	return 0;
   1416 }
   1417 
   1418 static int
   1419 eap_getdev(void *addr, struct audio_device *retp)
   1420 {
   1421 
   1422 	*retp = eap_device;
   1423 	return 0;
   1424 }
   1425 
   1426 static int
   1427 eap1371_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1428 {
   1429 	struct eap_instance *ei;
   1430 	struct eap_softc *sc;
   1431 
   1432 	ei = addr;
   1433 	sc = device_private(ei->parent);
   1434 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
   1435 }
   1436 
   1437 static int
   1438 eap1371_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1439 {
   1440 	struct eap_instance *ei;
   1441 	struct eap_softc *sc;
   1442 
   1443 	ei = addr;
   1444 	sc = device_private(ei->parent);
   1445 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
   1446 }
   1447 
   1448 static int
   1449 eap1371_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1450 {
   1451 	struct eap_instance *ei;
   1452 	struct eap_softc *sc;
   1453 
   1454 	ei = addr;
   1455 	sc = device_private(ei->parent);
   1456 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
   1457 }
   1458 
   1459 static void
   1460 eap1370_set_mixer(struct eap_softc *sc, int a, int d)
   1461 {
   1462 	eap1370_write_codec(sc, a, d);
   1463 
   1464 	sc->sc_port[a] = d;
   1465 	DPRINTFN(1, ("eap1370_mixer_set_port port 0x%02x = 0x%02x\n", a, d));
   1466 }
   1467 
   1468 static int
   1469 eap1370_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1470 {
   1471 	struct eap_instance *ei;
   1472 	struct eap_softc *sc;
   1473 	int lval, rval, l, r, la, ra;
   1474 	int l1, r1, l2, r2, m, o1, o2;
   1475 
   1476 	ei = addr;
   1477 	sc = device_private(ei->parent);
   1478 	if (cp->dev == EAP_RECORD_SOURCE) {
   1479 		if (cp->type != AUDIO_MIXER_SET)
   1480 			return EINVAL;
   1481 		m = sc->sc_record_source = cp->un.mask;
   1482 		l1 = l2 = r1 = r2 = 0;
   1483 		if (m & (1 << EAP_VOICE_VOL))
   1484 			l2 |= AK_M_VOICE, r2 |= AK_M_VOICE;
   1485 		if (m & (1 << EAP_FM_VOL))
   1486 			l1 |= AK_M_FM_L, r1 |= AK_M_FM_R;
   1487 		if (m & (1 << EAP_CD_VOL))
   1488 			l1 |= AK_M_CD_L, r1 |= AK_M_CD_R;
   1489 		if (m & (1 << EAP_LINE_VOL))
   1490 			l1 |= AK_M_LINE_L, r1 |= AK_M_LINE_R;
   1491 		if (m & (1 << EAP_AUX_VOL))
   1492 			l2 |= AK_M2_AUX_L, r2 |= AK_M2_AUX_R;
   1493 		if (m & (1 << EAP_MIC_VOL))
   1494 			l2 |= AK_M_TMIC, r2 |= AK_M_TMIC;
   1495 		eap1370_set_mixer(sc, AK_IN_MIXER1_L, l1);
   1496 		eap1370_set_mixer(sc, AK_IN_MIXER1_R, r1);
   1497 		eap1370_set_mixer(sc, AK_IN_MIXER2_L, l2);
   1498 		eap1370_set_mixer(sc, AK_IN_MIXER2_R, r2);
   1499 		return 0;
   1500 	}
   1501 	if (cp->dev == EAP_INPUT_SOURCE) {
   1502 		if (cp->type != AUDIO_MIXER_SET)
   1503 			return EINVAL;
   1504 		m = sc->sc_input_source = cp->un.mask;
   1505 		o1 = o2 = 0;
   1506 		if (m & (1 << EAP_VOICE_VOL))
   1507 			o2 |= AK_M_VOICE_L | AK_M_VOICE_R;
   1508 		if (m & (1 << EAP_FM_VOL))
   1509 			o1 |= AK_M_FM_L | AK_M_FM_R;
   1510 		if (m & (1 << EAP_CD_VOL))
   1511 			o1 |= AK_M_CD_L | AK_M_CD_R;
   1512 		if (m & (1 << EAP_LINE_VOL))
   1513 			o1 |= AK_M_LINE_L | AK_M_LINE_R;
   1514 		if (m & (1 << EAP_AUX_VOL))
   1515 			o2 |= AK_M_AUX_L | AK_M_AUX_R;
   1516 		if (m & (1 << EAP_MIC_VOL))
   1517 			o1 |= AK_M_MIC;
   1518 		eap1370_set_mixer(sc, AK_OUT_MIXER1, o1);
   1519 		eap1370_set_mixer(sc, AK_OUT_MIXER2, o2);
   1520 		return 0;
   1521 	}
   1522 	if (cp->dev == EAP_MIC_PREAMP) {
   1523 		if (cp->type != AUDIO_MIXER_ENUM)
   1524 			return EINVAL;
   1525 		if (cp->un.ord != 0 && cp->un.ord != 1)
   1526 			return EINVAL;
   1527 		sc->sc_mic_preamp = cp->un.ord;
   1528 		eap1370_set_mixer(sc, AK_MGAIN, cp->un.ord);
   1529 		return 0;
   1530 	}
   1531 	if (cp->type != AUDIO_MIXER_VALUE)
   1532 		return EINVAL;
   1533 	if (cp->un.value.num_channels == 1)
   1534 		lval = rval = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1535 	else if (cp->un.value.num_channels == 2) {
   1536 		lval = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1537 		rval = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1538 	} else
   1539 		return EINVAL;
   1540 	ra = -1;
   1541 	switch (cp->dev) {
   1542 	case EAP_MASTER_VOL:
   1543 		l = VOL_TO_ATT5(lval);
   1544 		r = VOL_TO_ATT5(rval);
   1545 		la = AK_MASTER_L;
   1546 		ra = AK_MASTER_R;
   1547 		break;
   1548 	case EAP_MIC_VOL:
   1549 		if (cp->un.value.num_channels != 1)
   1550 			return EINVAL;
   1551 		la = AK_MIC;
   1552 		goto lr;
   1553 	case EAP_VOICE_VOL:
   1554 		la = AK_VOICE_L;
   1555 		ra = AK_VOICE_R;
   1556 		goto lr;
   1557 	case EAP_FM_VOL:
   1558 		la = AK_FM_L;
   1559 		ra = AK_FM_R;
   1560 		goto lr;
   1561 	case EAP_CD_VOL:
   1562 		la = AK_CD_L;
   1563 		ra = AK_CD_R;
   1564 		goto lr;
   1565 	case EAP_LINE_VOL:
   1566 		la = AK_LINE_L;
   1567 		ra = AK_LINE_R;
   1568 		goto lr;
   1569 	case EAP_AUX_VOL:
   1570 		la = AK_AUX_L;
   1571 		ra = AK_AUX_R;
   1572 	lr:
   1573 		l = VOL_TO_GAIN5(lval);
   1574 		r = VOL_TO_GAIN5(rval);
   1575 		break;
   1576 	default:
   1577 		return EINVAL;
   1578 	}
   1579 	eap1370_set_mixer(sc, la, l);
   1580 	if (ra >= 0) {
   1581 		eap1370_set_mixer(sc, ra, r);
   1582 	}
   1583 	return 0;
   1584 }
   1585 
   1586 static int
   1587 eap1370_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1588 {
   1589 	struct eap_instance *ei;
   1590 	struct eap_softc *sc;
   1591 	int la, ra, l, r;
   1592 
   1593 	ei = addr;
   1594 	sc = device_private(ei->parent);
   1595 	switch (cp->dev) {
   1596 	case EAP_RECORD_SOURCE:
   1597 		if (cp->type != AUDIO_MIXER_SET)
   1598 			return EINVAL;
   1599 		cp->un.mask = sc->sc_record_source;
   1600 		return 0;
   1601 	case EAP_INPUT_SOURCE:
   1602 		if (cp->type != AUDIO_MIXER_SET)
   1603 			return EINVAL;
   1604 		cp->un.mask = sc->sc_input_source;
   1605 		return 0;
   1606 	case EAP_MIC_PREAMP:
   1607 		if (cp->type != AUDIO_MIXER_ENUM)
   1608 			return EINVAL;
   1609 		cp->un.ord = sc->sc_mic_preamp;
   1610 		return 0;
   1611 	case EAP_MASTER_VOL:
   1612 		l = ATT5_TO_VOL(sc->sc_port[AK_MASTER_L]);
   1613 		r = ATT5_TO_VOL(sc->sc_port[AK_MASTER_R]);
   1614 		break;
   1615 	case EAP_MIC_VOL:
   1616 		if (cp->un.value.num_channels != 1)
   1617 			return EINVAL;
   1618 		la = ra = AK_MIC;
   1619 		goto lr;
   1620 	case EAP_VOICE_VOL:
   1621 		la = AK_VOICE_L;
   1622 		ra = AK_VOICE_R;
   1623 		goto lr;
   1624 	case EAP_FM_VOL:
   1625 		la = AK_FM_L;
   1626 		ra = AK_FM_R;
   1627 		goto lr;
   1628 	case EAP_CD_VOL:
   1629 		la = AK_CD_L;
   1630 		ra = AK_CD_R;
   1631 		goto lr;
   1632 	case EAP_LINE_VOL:
   1633 		la = AK_LINE_L;
   1634 		ra = AK_LINE_R;
   1635 		goto lr;
   1636 	case EAP_AUX_VOL:
   1637 		la = AK_AUX_L;
   1638 		ra = AK_AUX_R;
   1639 	lr:
   1640 		l = GAIN5_TO_VOL(sc->sc_port[la]);
   1641 		r = GAIN5_TO_VOL(sc->sc_port[ra]);
   1642 		break;
   1643 	default:
   1644 		return EINVAL;
   1645 	}
   1646 	if (cp->un.value.num_channels == 1)
   1647 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r) / 2;
   1648 	else if (cp->un.value.num_channels == 2) {
   1649 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  = l;
   1650 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1651 	} else
   1652 		return EINVAL;
   1653 	return 0;
   1654 }
   1655 
   1656 static int
   1657 eap1370_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1658 {
   1659 
   1660 	switch (dip->index) {
   1661 	case EAP_MASTER_VOL:
   1662 		dip->type = AUDIO_MIXER_VALUE;
   1663 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1664 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1665 		strcpy(dip->label.name, AudioNmaster);
   1666 		dip->un.v.num_channels = 2;
   1667 		dip->un.v.delta = 8;
   1668 		strcpy(dip->un.v.units.name, AudioNvolume);
   1669 		return 0;
   1670 	case EAP_VOICE_VOL:
   1671 		dip->type = AUDIO_MIXER_VALUE;
   1672 		dip->mixer_class = EAP_INPUT_CLASS;
   1673 		dip->prev = AUDIO_MIXER_LAST;
   1674 		dip->next = AUDIO_MIXER_LAST;
   1675 		strcpy(dip->label.name, AudioNdac);
   1676 		dip->un.v.num_channels = 2;
   1677 		dip->un.v.delta = 8;
   1678 		strcpy(dip->un.v.units.name, AudioNvolume);
   1679 		return 0;
   1680 	case EAP_FM_VOL:
   1681 		dip->type = AUDIO_MIXER_VALUE;
   1682 		dip->mixer_class = EAP_INPUT_CLASS;
   1683 		dip->prev = AUDIO_MIXER_LAST;
   1684 		dip->next = AUDIO_MIXER_LAST;
   1685 		strcpy(dip->label.name, AudioNfmsynth);
   1686 		dip->un.v.num_channels = 2;
   1687 		dip->un.v.delta = 8;
   1688 		strcpy(dip->un.v.units.name, AudioNvolume);
   1689 		return 0;
   1690 	case EAP_CD_VOL:
   1691 		dip->type = AUDIO_MIXER_VALUE;
   1692 		dip->mixer_class = EAP_INPUT_CLASS;
   1693 		dip->prev = AUDIO_MIXER_LAST;
   1694 		dip->next = AUDIO_MIXER_LAST;
   1695 		strcpy(dip->label.name, AudioNcd);
   1696 		dip->un.v.num_channels = 2;
   1697 		dip->un.v.delta = 8;
   1698 		strcpy(dip->un.v.units.name, AudioNvolume);
   1699 		return 0;
   1700 	case EAP_LINE_VOL:
   1701 		dip->type = AUDIO_MIXER_VALUE;
   1702 		dip->mixer_class = EAP_INPUT_CLASS;
   1703 		dip->prev = AUDIO_MIXER_LAST;
   1704 		dip->next = AUDIO_MIXER_LAST;
   1705 		strcpy(dip->label.name, AudioNline);
   1706 		dip->un.v.num_channels = 2;
   1707 		dip->un.v.delta = 8;
   1708 		strcpy(dip->un.v.units.name, AudioNvolume);
   1709 		return 0;
   1710 	case EAP_AUX_VOL:
   1711 		dip->type = AUDIO_MIXER_VALUE;
   1712 		dip->mixer_class = EAP_INPUT_CLASS;
   1713 		dip->prev = AUDIO_MIXER_LAST;
   1714 		dip->next = AUDIO_MIXER_LAST;
   1715 		strcpy(dip->label.name, AudioNaux);
   1716 		dip->un.v.num_channels = 2;
   1717 		dip->un.v.delta = 8;
   1718 		strcpy(dip->un.v.units.name, AudioNvolume);
   1719 		return 0;
   1720 	case EAP_MIC_VOL:
   1721 		dip->type = AUDIO_MIXER_VALUE;
   1722 		dip->mixer_class = EAP_INPUT_CLASS;
   1723 		dip->prev = AUDIO_MIXER_LAST;
   1724 		dip->next = EAP_MIC_PREAMP;
   1725 		strcpy(dip->label.name, AudioNmicrophone);
   1726 		dip->un.v.num_channels = 1;
   1727 		dip->un.v.delta = 8;
   1728 		strcpy(dip->un.v.units.name, AudioNvolume);
   1729 		return 0;
   1730 	case EAP_RECORD_SOURCE:
   1731 		dip->mixer_class = EAP_RECORD_CLASS;
   1732 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1733 		strcpy(dip->label.name, AudioNsource);
   1734 		dip->type = AUDIO_MIXER_SET;
   1735 		dip->un.s.num_mem = 6;
   1736 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1737 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1738 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1739 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1740 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1741 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1742 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1743 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1744 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1745 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1746 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1747 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1748 		return 0;
   1749 	case EAP_INPUT_SOURCE:
   1750 		dip->mixer_class = EAP_INPUT_CLASS;
   1751 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1752 		strcpy(dip->label.name, AudioNsource);
   1753 		dip->type = AUDIO_MIXER_SET;
   1754 		dip->un.s.num_mem = 6;
   1755 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1756 		dip->un.s.member[0].mask = 1 << EAP_MIC_VOL;
   1757 		strcpy(dip->un.s.member[1].label.name, AudioNcd);
   1758 		dip->un.s.member[1].mask = 1 << EAP_CD_VOL;
   1759 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1760 		dip->un.s.member[2].mask = 1 << EAP_LINE_VOL;
   1761 		strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
   1762 		dip->un.s.member[3].mask = 1 << EAP_FM_VOL;
   1763 		strcpy(dip->un.s.member[4].label.name, AudioNaux);
   1764 		dip->un.s.member[4].mask = 1 << EAP_AUX_VOL;
   1765 		strcpy(dip->un.s.member[5].label.name, AudioNdac);
   1766 		dip->un.s.member[5].mask = 1 << EAP_VOICE_VOL;
   1767 		return 0;
   1768 	case EAP_MIC_PREAMP:
   1769 		dip->type = AUDIO_MIXER_ENUM;
   1770 		dip->mixer_class = EAP_INPUT_CLASS;
   1771 		dip->prev = EAP_MIC_VOL;
   1772 		dip->next = AUDIO_MIXER_LAST;
   1773 		strcpy(dip->label.name, AudioNpreamp);
   1774 		dip->un.e.num_mem = 2;
   1775 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
   1776 		dip->un.e.member[0].ord = 0;
   1777 		strcpy(dip->un.e.member[1].label.name, AudioNon);
   1778 		dip->un.e.member[1].ord = 1;
   1779 		return 0;
   1780 	case EAP_OUTPUT_CLASS:
   1781 		dip->type = AUDIO_MIXER_CLASS;
   1782 		dip->mixer_class = EAP_OUTPUT_CLASS;
   1783 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1784 		strcpy(dip->label.name, AudioCoutputs);
   1785 		return 0;
   1786 	case EAP_RECORD_CLASS:
   1787 		dip->type = AUDIO_MIXER_CLASS;
   1788 		dip->mixer_class = EAP_RECORD_CLASS;
   1789 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1790 		strcpy(dip->label.name, AudioCrecord);
   1791 		return 0;
   1792 	case EAP_INPUT_CLASS:
   1793 		dip->type = AUDIO_MIXER_CLASS;
   1794 		dip->mixer_class = EAP_INPUT_CLASS;
   1795 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1796 		strcpy(dip->label.name, AudioCinputs);
   1797 		return 0;
   1798 	}
   1799 	return ENXIO;
   1800 }
   1801 
   1802 static void *
   1803 eap_malloc(void *addr, int direction, size_t size,
   1804     struct malloc_type *pool, int flags)
   1805 {
   1806 	struct eap_instance *ei;
   1807 	struct eap_softc *sc;
   1808 	struct eap_dma *p;
   1809 	int error;
   1810 
   1811 	p = malloc(sizeof(*p), pool, flags);
   1812 	if (!p)
   1813 		return NULL;
   1814 	ei = addr;
   1815 	sc = device_private(ei->parent);
   1816 	error = eap_allocmem(sc, size, 16, p);
   1817 	if (error) {
   1818 		free(p, pool);
   1819 		return NULL;
   1820 	}
   1821 	p->next = sc->sc_dmas;
   1822 	sc->sc_dmas = p;
   1823 	return KERNADDR(p);
   1824 }
   1825 
   1826 static void
   1827 eap_free(void *addr, void *ptr, struct malloc_type *pool)
   1828 {
   1829 	struct eap_instance *ei;
   1830 	struct eap_softc *sc;
   1831 	struct eap_dma **pp, *p;
   1832 
   1833 	ei = addr;
   1834 	sc = device_private(ei->parent);
   1835 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1836 		if (KERNADDR(p) == ptr) {
   1837 			eap_freemem(sc, p);
   1838 			*pp = p->next;
   1839 			free(p, pool);
   1840 			return;
   1841 		}
   1842 	}
   1843 }
   1844 
   1845 static size_t
   1846 eap_round_buffersize(void *addr, int direction, size_t size)
   1847 {
   1848 
   1849 	return size;
   1850 }
   1851 
   1852 static paddr_t
   1853 eap_mappage(void *addr, void *mem, off_t off, int prot)
   1854 {
   1855 	struct eap_instance *ei;
   1856 	struct eap_softc *sc;
   1857 	struct eap_dma *p;
   1858 
   1859 	if (off < 0)
   1860 		return -1;
   1861 	ei = addr;
   1862 	sc = device_private(ei->parent);
   1863 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1864 		continue;
   1865 	if (!p)
   1866 		return -1;
   1867 
   1868 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1869 			       off, prot, BUS_DMA_WAITOK);
   1870 }
   1871 
   1872 static int
   1873 eap_get_props(void *addr)
   1874 {
   1875 
   1876 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1877 	    AUDIO_PROP_FULLDUPLEX;
   1878 }
   1879 
   1880 static void
   1881 eap_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc)
   1882 {
   1883 	struct eap_instance *ei;
   1884 	struct eap_softc *sc;
   1885 
   1886 	ei = addr;
   1887 	sc = device_private(ei->parent);
   1888 	*intr = &sc->sc_intr_lock;
   1889 	*proc = &sc->sc_lock;
   1890 }
   1891 
   1892 #if NMIDI > 0
   1893 static int
   1894 eap_midi_open(void *addr, int flags,
   1895 	      void (*iintr)(void *, int),
   1896 	      void (*ointr)(void *),
   1897 	      void *arg)
   1898 {
   1899 	struct eap_softc *sc;
   1900 	uint8_t uctrl;
   1901 
   1902 	sc = addr;
   1903 	sc->sc_arg = arg;
   1904 
   1905 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) | EAP_UART_EN);
   1906 	uctrl = 0;
   1907 	if (flags & FREAD) {
   1908 		uctrl |= EAP_UC_RXINTEN;
   1909 		sc->sc_iintr = iintr;
   1910 	}
   1911 	if (flags & FWRITE)
   1912 		sc->sc_ointr = ointr;
   1913 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1914 
   1915 	return 0;
   1916 }
   1917 
   1918 static void
   1919 eap_midi_close(void *addr)
   1920 {
   1921 	struct eap_softc *sc;
   1922 
   1923 	sc = addr;
   1924 	tsleep(sc, PWAIT, "eapclm", hz/10); /* give uart a chance to drain */
   1925 	EWRITE1(sc, EAP_UART_CONTROL, 0);
   1926 	EWRITE4(sc, EAP_ICSC, EREAD4(sc, EAP_ICSC) & ~EAP_UART_EN);
   1927 
   1928 	sc->sc_iintr = 0;
   1929 	sc->sc_ointr = 0;
   1930 }
   1931 
   1932 static int
   1933 eap_midi_output(void *addr, int d)
   1934 {
   1935 	struct eap_softc *sc;
   1936 	uint8_t uctrl;
   1937 
   1938 	sc = addr;
   1939 	EWRITE1(sc, EAP_UART_DATA, d);
   1940 
   1941 	uctrl = EAP_UC_TXINTEN;
   1942 	if (sc->sc_iintr)
   1943 		uctrl |= EAP_UC_RXINTEN;
   1944 	/*
   1945 	 * This is a write-only register, so we have to remember the right
   1946 	 * value of RXINTEN as well as setting TXINTEN. But if we are open
   1947 	 * for reading, it will always be correct to set RXINTEN here; only
   1948 	 * during service of a receive interrupt could it be momentarily
   1949 	 * toggled off, and whether we got here from the top half or from
   1950 	 * an interrupt, that won't be the current state.
   1951 	 */
   1952 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1953 	return 0;
   1954 }
   1955 
   1956 static void
   1957 eap_midi_getinfo(void *addr, struct midi_info *mi)
   1958 {
   1959 	mi->name = "AudioPCI MIDI UART";
   1960 	mi->props = MIDI_PROP_CAN_INPUT | MIDI_PROP_OUT_INTR;
   1961 }
   1962 
   1963 static void
   1964 eap_uart_txrdy(struct eap_softc *sc)
   1965 {
   1966 	uint8_t uctrl;
   1967 	uctrl = 0;
   1968 	if (sc->sc_iintr)
   1969 		uctrl = EAP_UC_RXINTEN;
   1970 	EWRITE1(sc, EAP_UART_CONTROL, uctrl);
   1971 	sc->sc_ointr(sc->sc_arg);
   1972 }
   1973 
   1974 #endif
   1975