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