Home | History | Annotate | Line # | Download | only in isa
wss.c revision 1.37
      1 /*	$NetBSD: wss.c,v 1.37 1997/08/28 00:02:13 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 John Brezak
      5  * Copyright (c) 1991-1993 Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * MAD support:
      9  * Copyright (c) 1996 Lennart Augustsson
     10  * Based on code which is
     11  * Copyright (c) 1995 Hannu Savolainen
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the Computer Systems
     24  *	Engineering Group at Lawrence Berkeley Laboratory.
     25  * 4. Neither the name of the University nor of the Laboratory may be used
     26  *    to endorse or promote products derived from this software without
     27  *    specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  */
     42 /*
     43  * Copyright by Hannu Savolainen 1994
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions are
     47  * met: 1. Redistributions of source code must retain the above copyright
     48  * notice, this list of conditions and the following disclaimer. 2.
     49  * Redistributions in binary form must reproduce the above copyright notice,
     50  * this list of conditions and the following disclaimer in the documentation
     51  * and/or other materials provided with the distribution.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
     54  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     55  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     56  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     57  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     60  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  */
     66 
     67 #include <sys/param.h>
     68 #include <sys/systm.h>
     69 #include <sys/errno.h>
     70 #include <sys/ioctl.h>
     71 #include <sys/syslog.h>
     72 #include <sys/device.h>
     73 #include <sys/proc.h>
     74 #include <sys/buf.h>
     75 
     76 #include <machine/cpu.h>
     77 #include <machine/intr.h>
     78 #include <machine/bus.h>
     79 #include <machine/pio.h>
     80 
     81 #include <sys/audioio.h>
     82 #include <dev/audio_if.h>
     83 
     84 #include <dev/isa/isavar.h>
     85 #include <dev/isa/isadmavar.h>
     86 
     87 #include <dev/ic/ad1848reg.h>
     88 #include <dev/isa/ad1848var.h>
     89 #include <dev/isa/wssreg.h>
     90 #include <dev/isa/madreg.h>
     91 
     92 /*
     93  * Mixer devices
     94  */
     95 #define WSS_MIC_IN_LVL		0
     96 #define WSS_LINE_IN_LVL		1
     97 #define WSS_DAC_LVL		2
     98 #define WSS_REC_LVL		3
     99 #define WSS_MON_LVL		4
    100 #define WSS_MIC_IN_MUTE		5
    101 #define WSS_LINE_IN_MUTE	6
    102 #define WSS_DAC_MUTE		7
    103 
    104 #define WSS_RECORD_SOURCE	8
    105 
    106 /* Classes */
    107 #define WSS_INPUT_CLASS		9
    108 #define WSS_RECORD_CLASS	10
    109 #define WSS_MONITOR_CLASS	11
    110 
    111 #ifdef AUDIO_DEBUG
    112 #define DPRINTF(x)	if (wssdebug) printf x
    113 int	wssdebug = 0;
    114 #else
    115 #define DPRINTF(x)
    116 #endif
    117 
    118 struct wss_softc {
    119 	struct	device sc_dev;		/* base device */
    120 	struct	isadev sc_id;		/* ISA device */
    121 	void	*sc_ih;			/* interrupt vectoring */
    122 	bus_space_tag_t sc_iot;		/* tag */
    123 	bus_space_handle_t sc_ioh;	/* handle */
    124 
    125 	struct  ad1848_softc sc_ad1848;
    126 #define wss_irq    sc_ad1848.sc_irq
    127 #define wss_drq    sc_ad1848.sc_drq
    128 
    129 	int 	mic_mute, cd_mute, dac_mute;
    130 	int	mad_chip_type;		/* chip type if MAD emulation of WSS */
    131 	bus_space_handle_t sc_mad_ioh;	/* MAD handle */
    132 	bus_space_handle_t sc_mad_ioh1, sc_mad_ioh2, sc_mad_ioh3;
    133 };
    134 
    135 struct audio_device wss_device = {
    136 	"wss,ad1848",
    137 	"",
    138 	"WSS"
    139 };
    140 
    141 int	wss_getdev __P((void *, struct audio_device *));
    142 
    143 int	wss_set_out_port __P((void *, int));
    144 int	wss_get_out_port __P((void *));
    145 int	wss_set_in_port __P((void *, int));
    146 int	wss_get_in_port __P((void *));
    147 int	wss_mixer_set_port __P((void *, mixer_ctrl_t *));
    148 int	wss_mixer_get_port __P((void *, mixer_ctrl_t *));
    149 int	wss_query_devinfo __P((void *, mixer_devinfo_t *));
    150 
    151 static int wss_to_vol __P((mixer_ctrl_t *, struct ad1848_volume *));
    152 static int wss_from_vol __P((mixer_ctrl_t *, struct ad1848_volume *));
    153 
    154 static int	wssfind __P((struct device *, struct wss_softc *, struct isa_attach_args *));
    155 
    156 static int 	madprobe __P((struct wss_softc *, int));
    157 static void	madattach __P((struct wss_softc *));
    158 static void	madunmap __P((struct wss_softc *));
    159 
    160 /*
    161  * Define our interface to the higher level audio driver.
    162  */
    163 
    164 struct audio_hw_if wss_hw_if = {
    165 	ad1848_open,
    166 	ad1848_close,
    167 	NULL,
    168 	ad1848_query_encoding,
    169 	ad1848_set_params,
    170 	ad1848_round_blocksize,
    171 	wss_set_out_port,
    172 	wss_get_out_port,
    173 	wss_set_in_port,
    174 	wss_get_in_port,
    175 	ad1848_commit_settings,
    176 	ad1848_dma_init_output,
    177 	ad1848_dma_init_input,
    178 	ad1848_dma_output,
    179 	ad1848_dma_input,
    180 	ad1848_halt_out_dma,
    181 	ad1848_halt_in_dma,
    182 	ad1848_cont_out_dma,
    183 	ad1848_cont_in_dma,
    184 	NULL,
    185 	wss_getdev,
    186 	NULL,
    187 	wss_mixer_set_port,
    188 	wss_mixer_get_port,
    189 	wss_query_devinfo,
    190 	ad1848_malloc,
    191 	ad1848_free,
    192 	ad1848_round,
    193         ad1848_mappage,
    194 	ad1848_get_props,
    195 };
    196 
    197 int	wssprobe __P((struct device *, void *, void *));
    198 void	wssattach __P((struct device *, struct device *, void *));
    199 
    200 struct cfattach wss_ca = {
    201 	sizeof(struct wss_softc), wssprobe, wssattach
    202 };
    203 
    204 struct cfdriver wss_cd = {
    205 	NULL, "wss", DV_DULL
    206 };
    207 
    208 /*
    209  * Probe for the Microsoft Sound System hardware.
    210  */
    211 int
    212 wssprobe(parent, match, aux)
    213     struct device *parent;
    214 #ifdef __BROKEN_INDIRECT_CONFIG
    215     void *match;
    216 #else
    217     struct cfdata *match;
    218 #endif
    219     void *aux;
    220 {
    221     struct wss_softc probesc, *sc = &probesc;
    222 
    223     bzero(sc, sizeof *sc);
    224 #ifdef __BROKEN_INDIRECT_CONFIG
    225     sc->sc_dev.dv_cfdata = ((struct device *)match)->dv_cfdata;
    226 #else
    227     sc->sc_dev.dv_cfdata = match;
    228 #endif
    229     if (wssfind(parent, sc, aux)) {
    230         bus_space_unmap(sc->sc_iot, sc->sc_ioh, WSS_CODEC);
    231         ad1848_unmap(&sc->sc_ad1848);
    232         madunmap(sc);
    233         return 1;
    234     } else
    235         /* Everything is already unmapped */
    236         return 0;
    237 }
    238 
    239 static int
    240 wssfind(parent, sc, ia)
    241     struct device *parent;
    242     struct wss_softc *sc;
    243     struct isa_attach_args *ia;
    244 {
    245     static u_char interrupt_bits[12] = {
    246 	-1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20
    247     };
    248     static u_char dma_bits[4] = {1, 2, 0, 3};
    249 
    250     sc->sc_iot = ia->ia_iot;
    251     if (sc->sc_dev.dv_cfdata->cf_flags & 1)
    252 	sc->mad_chip_type = madprobe(sc, ia->ia_iobase);
    253     else
    254 	sc->mad_chip_type = MAD_NONE;
    255 
    256     if (!WSS_BASE_VALID(ia->ia_iobase)) {
    257 	DPRINTF(("wss: configured iobase %x invalid\n", ia->ia_iobase));
    258 	goto bad1;
    259     }
    260 
    261     /* Map the ports upto the AD1848 port */
    262     if (bus_space_map(sc->sc_iot, ia->ia_iobase, WSS_CODEC, 0, &sc->sc_ioh))
    263 	goto bad1;
    264 
    265     sc->sc_ad1848.sc_iot = sc->sc_iot;
    266     sc->sc_ad1848.sc_iobase = ia->ia_iobase + WSS_CODEC;
    267 
    268     /* Is there an ad1848 chip at (WSS iobase + WSS_CODEC)? */
    269     if (ad1848_probe(&sc->sc_ad1848) == 0)
    270 	goto bad;
    271 
    272     ia->ia_iosize = WSS_NPORT;
    273 
    274     /* Setup WSS interrupt and DMA */
    275     if (!WSS_DRQ_VALID(ia->ia_drq)) {
    276 	DPRINTF(("wss: configured dma chan %d invalid\n", ia->ia_drq));
    277 	goto bad;
    278     }
    279     sc->wss_drq = ia->ia_drq;
    280 
    281     /* XXX reqdrq? */
    282     if (sc->wss_drq != -1 && isa_drq_isfree(parent, sc->wss_drq) == 0)
    283 	    goto bad;
    284 
    285 #ifdef NEWCONFIG
    286     /*
    287      * If the IRQ wasn't compiled in, auto-detect it.
    288      */
    289     if (ia->ia_irq == IRQUNK) {
    290 	ia->ia_irq = isa_discoverintr(ad1848_forceintr, &sc->sc_ad1848);
    291 	if (!WSS_IRQ_VALID(ia->ia_irq)) {
    292 	    printf("wss: couldn't auto-detect interrupt\n");
    293 	    goto bad;
    294 	}
    295     }
    296     else
    297 #endif
    298     if (!WSS_IRQ_VALID(ia->ia_irq)) {
    299 	DPRINTF(("wss: configured interrupt %d invalid\n", ia->ia_irq));
    300 	goto bad;
    301     }
    302 
    303     sc->wss_irq = ia->ia_irq;
    304 
    305     bus_space_write_1(sc->sc_iot, sc->sc_ioh, WSS_CONFIG,
    306 		      (interrupt_bits[ia->ia_irq] | dma_bits[ia->ia_drq]));
    307 
    308     if (sc->sc_ad1848.mode <= 1)
    309 	ia->ia_drq2 = -1;
    310     return 1;
    311 
    312 bad:
    313     bus_space_unmap(sc->sc_iot, sc->sc_ioh, WSS_CODEC);
    314 bad1:
    315     madunmap(sc);
    316     return 0;
    317 }
    318 
    319 /*
    320  * Attach hardware to driver, attach hardware driver to audio
    321  * pseudo-device driver .
    322  */
    323 void
    324 wssattach(parent, self, aux)
    325     struct device *parent, *self;
    326     void *aux;
    327 {
    328     struct wss_softc *sc = (struct wss_softc *)self;
    329     struct isa_attach_args *ia = (struct isa_attach_args *)aux;
    330     int version;
    331 
    332     if (!wssfind(parent, sc, ia)) {
    333         printf("%s: wssfind failed\n", sc->sc_dev.dv_xname);
    334         return;
    335     }
    336 
    337     madattach(sc);
    338 
    339     sc->sc_ad1848.sc_recdrq = sc->sc_ad1848.mode > 1 && ia->ia_drq2 != -1 ? ia->ia_drq2 : ia->ia_drq;
    340     sc->sc_ad1848.sc_isa = parent;
    341 
    342 #ifdef NEWCONFIG
    343     isa_establish(&sc->sc_id, &sc->sc_dev);
    344 #endif
    345     sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, IPL_AUDIO,
    346         ad1848_intr, &sc->sc_ad1848);
    347 
    348     ad1848_attach(&sc->sc_ad1848);
    349 
    350     version = bus_space_read_1(sc->sc_iot, sc->sc_ioh, WSS_STATUS) & WSS_VERSMASK;
    351     printf(" (vers %d)", version);
    352     if (sc->mad_chip_type != MAD_NONE)
    353         printf(", %s",
    354                sc->mad_chip_type == MAD_82C929 ? "82C929" :
    355                sc->mad_chip_type == MAD_82C928 ? "82C928" :
    356                "OTI-601D");
    357     printf("\n");
    358 
    359     sc->sc_ad1848.parent = sc;
    360 
    361     audio_attach_mi(&wss_hw_if, 0, &sc->sc_ad1848, &sc->sc_dev);
    362 }
    363 
    364 static int
    365 wss_to_vol(cp, vol)
    366     mixer_ctrl_t *cp;
    367     struct ad1848_volume *vol;
    368 {
    369     if (cp->un.value.num_channels == 1) {
    370 	vol->left = vol->right = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    371 	return(1);
    372     }
    373     else if (cp->un.value.num_channels == 2) {
    374 	vol->left  = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    375 	vol->right = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    376 	return(1);
    377     }
    378     return(0);
    379 }
    380 
    381 static int
    382 wss_from_vol(cp, vol)
    383     mixer_ctrl_t *cp;
    384     struct ad1848_volume *vol;
    385 {
    386     if (cp->un.value.num_channels == 1) {
    387 	cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = vol->left;
    388 	return(1);
    389     }
    390     else if (cp->un.value.num_channels == 2) {
    391 	cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = vol->left;
    392 	cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = vol->right;
    393 	return(1);
    394     }
    395     return(0);
    396 }
    397 
    398 int
    399 wss_getdev(addr, retp)
    400     void *addr;
    401     struct audio_device *retp;
    402 {
    403     *retp = wss_device;
    404     return 0;
    405 }
    406 
    407 int
    408 wss_set_out_port(addr, port)
    409     void *addr;
    410     int port;
    411 {
    412     DPRINTF(("wss_set_out_port:\n"));
    413     return(EINVAL);
    414 }
    415 
    416 int
    417 wss_get_out_port(addr)
    418     void *addr;
    419 {
    420     DPRINTF(("wss_get_out_port:\n"));
    421     return(WSS_DAC_LVL);
    422 }
    423 
    424 int
    425 wss_set_in_port(addr, port)
    426     void *addr;
    427     int port;
    428 {
    429     struct ad1848_softc *ac = addr;
    430 
    431     DPRINTF(("wss_set_in_port: %d\n", port));
    432 
    433     switch(port) {
    434     case WSS_MIC_IN_LVL:
    435 	port = MIC_IN_PORT;
    436 	break;
    437     case WSS_LINE_IN_LVL:
    438 	port = LINE_IN_PORT;
    439 	break;
    440     case WSS_DAC_LVL:
    441 	port = DAC_IN_PORT;
    442 	break;
    443     default:
    444 	return(EINVAL);
    445 	/*NOTREACHED*/
    446     }
    447 
    448     return(ad1848_set_rec_port(ac, port));
    449 }
    450 
    451 int
    452 wss_get_in_port(addr)
    453     void *addr;
    454 {
    455     struct ad1848_softc *ac = addr;
    456     int port = WSS_MIC_IN_LVL;
    457 
    458     switch(ad1848_get_rec_port(ac)) {
    459     case MIC_IN_PORT:
    460 	port = WSS_MIC_IN_LVL;
    461 	break;
    462     case LINE_IN_PORT:
    463 	port = WSS_LINE_IN_LVL;
    464 	break;
    465     case DAC_IN_PORT:
    466 	port = WSS_DAC_LVL;
    467 	break;
    468     }
    469 
    470     DPRINTF(("wss_get_in_port: %d\n", port));
    471 
    472     return(port);
    473 }
    474 
    475 int
    476 wss_mixer_set_port(addr, cp)
    477     void *addr;
    478     mixer_ctrl_t *cp;
    479 {
    480     struct ad1848_softc *ac = addr;
    481     struct wss_softc *sc = ac->parent;
    482     struct ad1848_volume vol;
    483     int error = EINVAL;
    484 
    485     DPRINTF(("wss_mixer_set_port: dev=%d type=%d\n", cp->dev, cp->type));
    486 
    487     switch (cp->dev) {
    488     case WSS_MIC_IN_LVL:	/* Microphone */
    489 	if (cp->type == AUDIO_MIXER_VALUE) {
    490 	    if (wss_to_vol(cp, &vol))
    491 		error = ad1848_set_aux2_gain(ac, &vol);
    492 	}
    493 	break;
    494 
    495     case WSS_MIC_IN_MUTE:	/* Microphone */
    496 	if (cp->type == AUDIO_MIXER_ENUM) {
    497 	    sc->mic_mute = cp->un.ord;
    498 	    DPRINTF(("mic mute %d\n", cp->un.ord));
    499 	    error = 0;
    500 	}
    501 	break;
    502 
    503     case WSS_LINE_IN_LVL:	/* linein/CD */
    504 	if (cp->type == AUDIO_MIXER_VALUE) {
    505 	    if (wss_to_vol(cp, &vol))
    506 		error = ad1848_set_aux1_gain(ac, &vol);
    507 	}
    508 	break;
    509 
    510     case WSS_LINE_IN_MUTE:	/* linein/CD */
    511 	if (cp->type == AUDIO_MIXER_ENUM) {
    512 	    sc->cd_mute = cp->un.ord;
    513 	    DPRINTF(("CD mute %d\n", cp->un.ord));
    514 	    error = 0;
    515 	}
    516 	break;
    517 
    518     case WSS_DAC_LVL:		/* dac out */
    519 	if (cp->type == AUDIO_MIXER_VALUE) {
    520 	    if (wss_to_vol(cp, &vol))
    521 		error = ad1848_set_out_gain(ac, &vol);
    522 	}
    523 	break;
    524 
    525     case WSS_DAC_MUTE:		/* dac out */
    526 	if (cp->type == AUDIO_MIXER_ENUM) {
    527 	    sc->dac_mute = cp->un.ord;
    528 	    DPRINTF(("DAC mute %d\n", cp->un.ord));
    529 	    error = 0;
    530 	}
    531 	break;
    532 
    533     case WSS_REC_LVL:		/* record level */
    534 	if (cp->type == AUDIO_MIXER_VALUE) {
    535 	    if (wss_to_vol(cp, &vol))
    536 		error = ad1848_set_rec_gain(ac, &vol);
    537 	}
    538 	break;
    539 
    540     case WSS_RECORD_SOURCE:
    541 	if (cp->type == AUDIO_MIXER_ENUM) {
    542 	    error = ad1848_set_rec_port(ac, cp->un.ord);
    543 	}
    544 	break;
    545 
    546     case WSS_MON_LVL:
    547 	if (cp->type == AUDIO_MIXER_VALUE && cp->un.value.num_channels == 1) {
    548 	    vol.left  = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    549 	    error = ad1848_set_mon_gain(ac, &vol);
    550 	}
    551 	break;
    552 
    553     default:
    554 	    return ENXIO;
    555 	    /*NOTREACHED*/
    556     }
    557 
    558     return 0;
    559 }
    560 
    561 int
    562 wss_mixer_get_port(addr, cp)
    563     void *addr;
    564     mixer_ctrl_t *cp;
    565 {
    566     struct ad1848_softc *ac = addr;
    567     struct wss_softc *sc = ac->parent;
    568     struct ad1848_volume vol;
    569     int error = EINVAL;
    570 
    571     DPRINTF(("wss_mixer_get_port: port=%d\n", cp->dev));
    572 
    573     switch (cp->dev) {
    574     case WSS_MIC_IN_LVL:	/* Microphone */
    575 	if (cp->type == AUDIO_MIXER_VALUE) {
    576 	    error = ad1848_get_aux2_gain(ac, &vol);
    577 	    if (!error)
    578 		wss_from_vol(cp, &vol);
    579 	}
    580 	break;
    581 
    582     case WSS_MIC_IN_MUTE:
    583 	if (cp->type == AUDIO_MIXER_ENUM) {
    584 	    cp->un.ord = sc->mic_mute;
    585 	    error = 0;
    586 	}
    587 	break;
    588 
    589     case WSS_LINE_IN_LVL:	/* linein/CD */
    590 	if (cp->type == AUDIO_MIXER_VALUE) {
    591 	    error = ad1848_get_aux1_gain(ac, &vol);
    592 	    if (!error)
    593 		wss_from_vol(cp, &vol);
    594 	}
    595 	break;
    596 
    597     case WSS_LINE_IN_MUTE:
    598 	if (cp->type == AUDIO_MIXER_ENUM) {
    599 	    cp->un.ord = sc->cd_mute;
    600 	    error = 0;
    601 	}
    602 	break;
    603 
    604     case WSS_DAC_LVL:		/* dac out */
    605 	if (cp->type == AUDIO_MIXER_VALUE) {
    606 	    error = ad1848_get_out_gain(ac, &vol);
    607 	    if (!error)
    608 		wss_from_vol(cp, &vol);
    609 	}
    610 	break;
    611 
    612     case WSS_DAC_MUTE:
    613 	if (cp->type == AUDIO_MIXER_ENUM) {
    614 	    cp->un.ord = sc->dac_mute;
    615 	    error = 0;
    616 	}
    617 	break;
    618 
    619     case WSS_REC_LVL:		/* record level */
    620 	if (cp->type == AUDIO_MIXER_VALUE) {
    621 	    error = ad1848_get_rec_gain(ac, &vol);
    622 	    if (!error)
    623 		wss_from_vol(cp, &vol);
    624 	}
    625 	break;
    626 
    627     case WSS_RECORD_SOURCE:
    628 	if (cp->type == AUDIO_MIXER_ENUM) {
    629 	    cp->un.ord = ad1848_get_rec_port(ac);
    630 	    error = 0;
    631 	}
    632 	break;
    633 
    634     case WSS_MON_LVL:		/* monitor level */
    635 	if (cp->type == AUDIO_MIXER_VALUE && cp->un.value.num_channels == 1) {
    636 	    error = ad1848_get_mon_gain(ac, &vol);
    637 	    if (!error)
    638 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = vol.left;
    639 	}
    640 	break;
    641 
    642     default:
    643 	error = ENXIO;
    644 	break;
    645     }
    646 
    647     return(error);
    648 }
    649 
    650 int
    651 wss_query_devinfo(addr, dip)
    652     void *addr;
    653     mixer_devinfo_t *dip;
    654 {
    655     DPRINTF(("wss_query_devinfo: index=%d\n", dip->index));
    656 
    657     switch(dip->index) {
    658     case WSS_MIC_IN_LVL:	/* Microphone */
    659 	dip->type = AUDIO_MIXER_VALUE;
    660 	dip->mixer_class = WSS_INPUT_CLASS;
    661 	dip->prev = AUDIO_MIXER_LAST;
    662 	dip->next = WSS_MIC_IN_MUTE;
    663 	strcpy(dip->label.name, AudioNmicrophone);
    664 	dip->un.v.num_channels = 2;
    665 	strcpy(dip->un.v.units.name, AudioNvolume);
    666 	break;
    667 
    668     case WSS_LINE_IN_LVL:	/* line/CD */
    669 	dip->type = AUDIO_MIXER_VALUE;
    670 	dip->mixer_class = WSS_INPUT_CLASS;
    671 	dip->prev = AUDIO_MIXER_LAST;
    672 	dip->next = WSS_LINE_IN_MUTE;
    673 	strcpy(dip->label.name, AudioNcd);
    674 	dip->un.v.num_channels = 2;
    675 	strcpy(dip->un.v.units.name, AudioNvolume);
    676 	break;
    677 
    678     case WSS_DAC_LVL:		/*  dacout */
    679 	dip->type = AUDIO_MIXER_VALUE;
    680 	dip->mixer_class = WSS_INPUT_CLASS;
    681 	dip->prev = AUDIO_MIXER_LAST;
    682 	dip->next = WSS_DAC_MUTE;
    683 	strcpy(dip->label.name, AudioNdac);
    684 	dip->un.v.num_channels = 2;
    685 	strcpy(dip->un.v.units.name, AudioNvolume);
    686 	break;
    687 
    688     case WSS_REC_LVL:	/* record level */
    689 	dip->type = AUDIO_MIXER_VALUE;
    690 	dip->mixer_class = WSS_RECORD_CLASS;
    691 	dip->prev = AUDIO_MIXER_LAST;
    692 	dip->next = WSS_RECORD_SOURCE;
    693 	strcpy(dip->label.name, AudioNrecord);
    694 	dip->un.v.num_channels = 2;
    695 	strcpy(dip->un.v.units.name, AudioNvolume);
    696 	break;
    697 
    698     case WSS_MON_LVL:	/* monitor level */
    699 	dip->type = AUDIO_MIXER_VALUE;
    700 	dip->mixer_class = WSS_MONITOR_CLASS;
    701 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    702 	strcpy(dip->label.name, AudioNmonitor);
    703 	dip->un.v.num_channels = 1;
    704 	strcpy(dip->un.v.units.name, AudioNvolume);
    705 	break;
    706 
    707     case WSS_INPUT_CLASS:			/* input class descriptor */
    708 	dip->type = AUDIO_MIXER_CLASS;
    709 	dip->mixer_class = WSS_INPUT_CLASS;
    710 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    711 	strcpy(dip->label.name, AudioCInputs);
    712 	break;
    713 
    714     case WSS_MONITOR_CLASS:			/* monitor class descriptor */
    715 	dip->type = AUDIO_MIXER_CLASS;
    716 	dip->mixer_class = WSS_MONITOR_CLASS;
    717 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    718 	strcpy(dip->label.name, AudioCMonitor);
    719 	break;
    720 
    721     case WSS_RECORD_CLASS:			/* record source class */
    722 	dip->type = AUDIO_MIXER_CLASS;
    723 	dip->mixer_class = WSS_RECORD_CLASS;
    724 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    725 	strcpy(dip->label.name, AudioCRecord);
    726 	break;
    727 
    728     case WSS_MIC_IN_MUTE:
    729 	dip->mixer_class = WSS_INPUT_CLASS;
    730 	dip->type = AUDIO_MIXER_ENUM;
    731 	dip->prev = WSS_MIC_IN_LVL;
    732 	dip->next = AUDIO_MIXER_LAST;
    733 	goto mute;
    734 
    735     case WSS_LINE_IN_MUTE:
    736 	dip->mixer_class = WSS_INPUT_CLASS;
    737 	dip->type = AUDIO_MIXER_ENUM;
    738 	dip->prev = WSS_LINE_IN_LVL;
    739 	dip->next = AUDIO_MIXER_LAST;
    740 	goto mute;
    741 
    742     case WSS_DAC_MUTE:
    743 	dip->mixer_class = WSS_INPUT_CLASS;
    744 	dip->type = AUDIO_MIXER_ENUM;
    745 	dip->prev = WSS_DAC_LVL;
    746 	dip->next = AUDIO_MIXER_LAST;
    747     mute:
    748 	strcpy(dip->label.name, AudioNmute);
    749 	dip->un.e.num_mem = 2;
    750 	strcpy(dip->un.e.member[0].label.name, AudioNoff);
    751 	dip->un.e.member[0].ord = 0;
    752 	strcpy(dip->un.e.member[1].label.name, AudioNon);
    753 	dip->un.e.member[1].ord = 1;
    754 	break;
    755 
    756     case WSS_RECORD_SOURCE:
    757 	dip->mixer_class = WSS_RECORD_CLASS;
    758 	dip->type = AUDIO_MIXER_ENUM;
    759 	dip->prev = WSS_REC_LVL;
    760 	dip->next = AUDIO_MIXER_LAST;
    761 	strcpy(dip->label.name, AudioNsource);
    762 	dip->un.e.num_mem = 3;
    763 	strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
    764 	dip->un.e.member[0].ord = WSS_MIC_IN_LVL;
    765 	strcpy(dip->un.e.member[1].label.name, AudioNcd);
    766 	dip->un.e.member[1].ord = WSS_LINE_IN_LVL;
    767 	strcpy(dip->un.e.member[2].label.name, AudioNdac);
    768 	dip->un.e.member[2].ord = WSS_DAC_LVL;
    769 	break;
    770 
    771     default:
    772 	return ENXIO;
    773 	/*NOTREACHED*/
    774     }
    775     DPRINTF(("AUDIO_MIXER_DEVINFO: name=%s\n", dip->label.name));
    776 
    777     return 0;
    778 }
    779 
    780 /*
    781  * Initialization code for OPTi MAD16 compatible audio chips. Including
    782  *
    783  *      OPTi 82C928     MAD16           (replaced by C929)
    784  *      OAK OTI-601D    Mozart
    785  *      OPTi 82C929     MAD16 Pro
    786  *
    787  */
    788 static unsigned int mad_read __P((struct wss_softc *, int, int));
    789 static void mad_write __P((struct wss_softc *, int, int, int));
    790 static int detect_mad16 __P((struct wss_softc *, int));
    791 
    792 static unsigned int
    793 mad_read(sc, chip_type, port)
    794     struct wss_softc *sc;
    795     int chip_type;
    796     int port;
    797 {
    798     unsigned int tmp;
    799     int s = splaudio();		/* don't want an interrupt between outb&inb */
    800 
    801     switch (chip_type) {	/* Output password */
    802     case MAD_82C928:
    803     case MAD_OTI601D:
    804 	bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, MC_PASSWD_REG, M_PASSWD_928);
    805 	break;
    806     case MAD_82C929:
    807 	bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, MC_PASSWD_REG, M_PASSWD_929);
    808 	break;
    809     }
    810     tmp = bus_space_read_1(sc->sc_iot, sc->sc_mad_ioh, port);
    811     splx(s);
    812     return tmp;
    813 }
    814 
    815 static void
    816 mad_write(sc, chip_type, port, value)
    817     struct wss_softc *sc;
    818     int chip_type;
    819     int port;
    820     int value;
    821 {
    822     int s = splaudio();		/* don't want an interrupt between outb&outb */
    823 
    824     switch (chip_type) {	/* Output password */
    825     case MAD_82C928:
    826     case MAD_OTI601D:
    827 	bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, MC_PASSWD_REG, M_PASSWD_928);
    828 	break;
    829     case MAD_82C929:
    830 	bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, MC_PASSWD_REG, M_PASSWD_929);
    831 	break;
    832     }
    833     bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, port, value & 0xff);
    834     splx(s);
    835 }
    836 
    837 static int
    838 detect_mad16(sc, chip_type)
    839     struct wss_softc *sc;
    840     int chip_type;
    841 {
    842     unsigned char tmp, tmp2;
    843 
    844     /*
    845      * Check that reading a register doesn't return bus float (0xff)
    846      * when the card is accessed using password. This may fail in case
    847      * the card is in low power mode. Normally at least the power saving mode
    848      * bit should be 0.
    849      */
    850     if ((tmp = mad_read(sc, chip_type, MC1_PORT)) == 0xff) {
    851 	DPRINTF(("MC1_PORT returned 0xff\n"));
    852 	return 0;
    853     }
    854 
    855     /*
    856      * Now check that the gate is closed on first I/O after writing
    857      * the password. (This is how a MAD16 compatible card works).
    858      */
    859     if ((tmp2 = bus_space_read_1(sc->sc_iot, sc->sc_mad_ioh, MC1_PORT)) == tmp)	{ /* It didn't close */
    860 	DPRINTF(("MC1_PORT didn't close after read (0x%02x)\n", tmp2));
    861 	return 0;
    862     }
    863 
    864     mad_write(sc, chip_type, MC1_PORT, tmp ^ 0x80);	/* Toggle a bit */
    865 
    866     /* Compare the bit */
    867     if ((tmp2 = mad_read(sc, chip_type, MC1_PORT)) != (tmp ^ 0x80)) {
    868 	mad_write(sc, chip_type, MC1_PORT, tmp);	/* Restore */
    869 	DPRINTF(("Bit revert test failed (0x%02x, 0x%02x)\n", tmp, tmp2));
    870 	return 0;
    871     }
    872 
    873     mad_write(sc, chip_type, MC1_PORT, tmp);	/* Restore */
    874     return 1;
    875 }
    876 
    877 static int
    878 madprobe(sc, iobase)
    879     struct wss_softc *sc;
    880     int iobase;
    881 {
    882     static int valid_ports[M_WSS_NPORTS] =
    883         { M_WSS_PORT0, M_WSS_PORT1, M_WSS_PORT2, M_WSS_PORT3 };
    884     int i;
    885     int chip_type;
    886 
    887     if (bus_space_map(sc->sc_iot, MAD_BASE, MAD_NPORT, 0, &sc->sc_mad_ioh))
    888 	return MAD_NONE;
    889 
    890     /* Allocate bus space that the MAD chip wants */
    891     if (bus_space_map(sc->sc_iot, MAD_REG1, MAD_LEN1, 0, &sc->sc_mad_ioh1))
    892         goto bad1;
    893     if (bus_space_map(sc->sc_iot, MAD_REG2, MAD_LEN2, 0, &sc->sc_mad_ioh2))
    894         goto bad2;
    895     if (bus_space_map(sc->sc_iot, MAD_REG3, MAD_LEN3, 0, &sc->sc_mad_ioh3))
    896         goto bad3;
    897 
    898     DPRINTF(("mad: Detect using password = 0xE2\n"));
    899     if (!detect_mad16(sc, MAD_82C928)) {
    900 	/* No luck. Try different model */
    901 	DPRINTF(("mad: Detect using password = 0xE3\n"));
    902 	if (!detect_mad16(sc, MAD_82C929))
    903 	    goto bad;
    904 	chip_type = MAD_82C929;
    905 	DPRINTF(("mad: 82C929 detected\n"));
    906     } else {
    907 	if ((mad_read(sc, MAD_82C928, MC3_PORT) & 0x03) == 0x03) {
    908 	    DPRINTF(("mad: Mozart detected\n"));
    909 	    chip_type = MAD_OTI601D;
    910 	} else {
    911 	    DPRINTF(("mad: 82C928 detected?\n"));
    912 	    chip_type = MAD_82C928;
    913 	}
    914     }
    915 
    916 #ifdef AUDIO_DEBUG
    917     if (wssdebug)
    918 	for (i = MC1_PORT; i <= MC7_PORT; i++)
    919 	    printf("mad: port %03x = %02x\n", i, mad_read(sc, chip_type, i));
    920 #endif
    921 
    922     /* Set the WSS address. */
    923     for (i = 0; i < M_WSS_NPORTS; i++)
    924 	if (valid_ports[i] == iobase)
    925 	    break;
    926     if (i >= M_WSS_NPORTS) {		/* Not a valid port */
    927 	printf("mad: Bad WSS base address 0x%x\n", iobase);
    928 	goto bad;
    929     }
    930     /* enable WSS emulation at the I/O port, no joystick */
    931     mad_write(sc, chip_type, MC1_PORT, M_WSS_PORT_SELECT(i) | MC1_JOYDISABLE);
    932 
    933     mad_write(sc, chip_type, MC2_PORT, 0x03); /* ? */
    934     mad_write(sc, chip_type, MC3_PORT, 0xf0); /* Disable SB */
    935 
    936     return chip_type;
    937 bad:
    938     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh3, MAD_LEN3);
    939 bad3:
    940     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh2, MAD_LEN2);
    941 bad2:
    942     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh1, MAD_LEN1);
    943 bad1:
    944     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh, MAD_NPORT);
    945     return MAD_NONE;
    946 }
    947 
    948 static void
    949 madunmap(sc)
    950     struct wss_softc *sc;
    951 {
    952     if (sc->mad_chip_type == MAD_NONE)
    953         return;
    954     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh, MAD_NPORT);
    955     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh1, MAD_LEN1);
    956     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh2, MAD_LEN2);
    957     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh3, MAD_LEN3);
    958 }
    959 
    960 static void
    961 madattach(sc)
    962     struct wss_softc *sc;
    963 {
    964     int chip_type = sc->mad_chip_type;
    965     unsigned char cs4231_mode;
    966 
    967     if (chip_type == MAD_NONE)
    968         return;
    969 
    970     cs4231_mode =
    971 	strncmp(sc->sc_ad1848.chip_name, "CS4248", 6) == 0 ||
    972 	strncmp(sc->sc_ad1848.chip_name, "CS4231", 6) == 0 ? 0x02 : 0;
    973 
    974     if (chip_type == MAD_82C929) {
    975 	mad_write(sc, chip_type, MC4_PORT, 0xa2);
    976 	mad_write(sc, chip_type, MC5_PORT, 0xA5 | cs4231_mode);
    977 	mad_write(sc, chip_type, MC6_PORT, 0x03);	/* Disable MPU401 */
    978     } else {
    979 	mad_write(sc, chip_type, MC4_PORT, 0x02);
    980 	mad_write(sc, chip_type, MC5_PORT, 0x30 | cs4231_mode);
    981     }
    982 
    983 #ifdef AUDIO_DEBUG
    984     if (wssdebug) {
    985 	int i;
    986 	for (i = MC1_PORT; i <= MC7_PORT; i++)
    987 	    DPRINTF(("port %03x after init = %02x\n", i, mad_read(sc, chip_type, i)));
    988     }
    989 #endif
    990 }
    991