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