Home | History | Annotate | Line # | Download | only in isa
wss.c revision 1.33
      1 /*	$NetBSD: wss.c,v 1.33 1997/08/20 15:26:29 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     if (wssfind(parent, sc, aux)) {
    224         bus_space_unmap(sc->sc_iot, sc->sc_ioh, WSS_CODEC);
    225         ad1848_unmap(&sc->sc_ad1848);
    226         madunmap(sc);
    227         return 1;
    228     } else
    229         /* Everything is already unmapped */
    230         return 0;
    231 }
    232 
    233 static int
    234 wssfind(parent, sc, ia)
    235     struct device *parent;
    236     struct wss_softc *sc;
    237     struct isa_attach_args *ia;
    238 {
    239     static u_char interrupt_bits[12] = {
    240 	-1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20
    241     };
    242     static u_char dma_bits[4] = {1, 2, 0, 3};
    243 
    244     sc->sc_iot = ia->ia_iot;
    245     if (sc->sc_dev.dv_cfdata->cf_flags & 1)
    246 	sc->mad_chip_type = madprobe(sc, ia->ia_iobase);
    247     else
    248 	sc->mad_chip_type = MAD_NONE;
    249 
    250     if (!WSS_BASE_VALID(ia->ia_iobase)) {
    251 	DPRINTF(("wss: configured iobase %x invalid\n", ia->ia_iobase));
    252 	goto bad1;
    253     }
    254 
    255     /* Map the ports upto the AD1848 port */
    256     if (bus_space_map(sc->sc_iot, ia->ia_iobase, WSS_CODEC, 0, &sc->sc_ioh))
    257 	goto bad1;
    258 
    259     sc->sc_ad1848.sc_iot = sc->sc_iot;
    260     sc->sc_ad1848.sc_iobase = ia->ia_iobase + WSS_CODEC;
    261 
    262     /* Is there an ad1848 chip at (WSS iobase + WSS_CODEC)? */
    263     if (ad1848_probe(&sc->sc_ad1848) == 0)
    264 	goto bad;
    265 
    266     ia->ia_iosize = WSS_NPORT;
    267 
    268     /* Setup WSS interrupt and DMA */
    269     if (!WSS_DRQ_VALID(ia->ia_drq)) {
    270 	DPRINTF(("wss: configured dma chan %d invalid\n", ia->ia_drq));
    271 	goto bad;
    272     }
    273     sc->wss_drq = ia->ia_drq;
    274 
    275     /* XXX reqdrq? */
    276     if (sc->wss_drq != -1 && isa_drq_isfree(parent, sc->wss_drq) == 0)
    277 	    goto bad;
    278 
    279 #ifdef NEWCONFIG
    280     /*
    281      * If the IRQ wasn't compiled in, auto-detect it.
    282      */
    283     if (ia->ia_irq == IRQUNK) {
    284 	ia->ia_irq = isa_discoverintr(ad1848_forceintr, &sc->sc_ad1848);
    285 	if (!WSS_IRQ_VALID(ia->ia_irq)) {
    286 	    printf("wss: couldn't auto-detect interrupt\n");
    287 	    goto bad;
    288 	}
    289     }
    290     else
    291 #endif
    292     if (!WSS_IRQ_VALID(ia->ia_irq)) {
    293 	DPRINTF(("wss: configured interrupt %d invalid\n", ia->ia_irq));
    294 	goto bad;
    295     }
    296 
    297     sc->wss_irq = ia->ia_irq;
    298 
    299     bus_space_write_1(sc->sc_iot, sc->sc_ioh, WSS_CONFIG,
    300 		      (interrupt_bits[ia->ia_irq] | dma_bits[ia->ia_drq]));
    301 
    302     return 1;
    303 
    304 bad:
    305     bus_space_unmap(sc->sc_iot, sc->sc_ioh, WSS_CODEC);
    306 bad1:
    307     madunmap(sc);
    308     return 0;
    309 }
    310 
    311 /*
    312  * Attach hardware to driver, attach hardware driver to audio
    313  * pseudo-device driver .
    314  */
    315 void
    316 wssattach(parent, self, aux)
    317     struct device *parent, *self;
    318     void *aux;
    319 {
    320     struct wss_softc *sc = (struct wss_softc *)self;
    321     struct isa_attach_args *ia = (struct isa_attach_args *)aux;
    322     int version;
    323 
    324     if (!wssfind(parent, sc, ia)) {
    325         printf("wssattach: wssfind failed\n");
    326         return;
    327     }
    328 
    329     madattach(sc);
    330 
    331     sc->sc_ad1848.sc_recdrq = ia->ia_drq;
    332     sc->sc_ad1848.sc_isa = parent;
    333 
    334 #ifdef NEWCONFIG
    335     isa_establish(&sc->sc_id, &sc->sc_dev);
    336 #endif
    337     sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, IPL_AUDIO,
    338         ad1848_intr, &sc->sc_ad1848);
    339 
    340     ad1848_attach(&sc->sc_ad1848);
    341 
    342     version = bus_space_read_1(sc->sc_iot, sc->sc_ioh, WSS_STATUS) & WSS_VERSMASK;
    343     printf(" (vers %d)", version);
    344     if (sc->mad_chip_type != MAD_NONE)
    345         printf(", %s",
    346                sc->mad_chip_type == MAD_82C929 ? "82C929" :
    347                sc->mad_chip_type == MAD_82C928 ? "82C928" :
    348                "OTI-601D");
    349     printf("\n");
    350 
    351     sc->sc_ad1848.parent = sc;
    352 
    353     audio_attach_mi(&wss_hw_if, 0, &sc->sc_ad1848, &sc->sc_dev);
    354 }
    355 
    356 static int
    357 wss_to_vol(cp, vol)
    358     mixer_ctrl_t *cp;
    359     struct ad1848_volume *vol;
    360 {
    361     if (cp->un.value.num_channels == 1) {
    362 	vol->left = vol->right = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    363 	return(1);
    364     }
    365     else if (cp->un.value.num_channels == 2) {
    366 	vol->left  = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    367 	vol->right = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    368 	return(1);
    369     }
    370     return(0);
    371 }
    372 
    373 static int
    374 wss_from_vol(cp, vol)
    375     mixer_ctrl_t *cp;
    376     struct ad1848_volume *vol;
    377 {
    378     if (cp->un.value.num_channels == 1) {
    379 	cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = vol->left;
    380 	return(1);
    381     }
    382     else if (cp->un.value.num_channels == 2) {
    383 	cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = vol->left;
    384 	cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = vol->right;
    385 	return(1);
    386     }
    387     return(0);
    388 }
    389 
    390 int
    391 wss_getdev(addr, retp)
    392     void *addr;
    393     struct audio_device *retp;
    394 {
    395     *retp = wss_device;
    396     return 0;
    397 }
    398 
    399 int
    400 wss_set_out_port(addr, port)
    401     void *addr;
    402     int port;
    403 {
    404     DPRINTF(("wss_set_out_port:\n"));
    405     return(EINVAL);
    406 }
    407 
    408 int
    409 wss_get_out_port(addr)
    410     void *addr;
    411 {
    412     DPRINTF(("wss_get_out_port:\n"));
    413     return(WSS_DAC_LVL);
    414 }
    415 
    416 int
    417 wss_set_in_port(addr, port)
    418     void *addr;
    419     int port;
    420 {
    421     struct ad1848_softc *ac = addr;
    422 
    423     DPRINTF(("wss_set_in_port: %d\n", port));
    424 
    425     switch(port) {
    426     case WSS_MIC_IN_LVL:
    427 	port = MIC_IN_PORT;
    428 	break;
    429     case WSS_LINE_IN_LVL:
    430 	port = LINE_IN_PORT;
    431 	break;
    432     case WSS_DAC_LVL:
    433 	port = DAC_IN_PORT;
    434 	break;
    435     default:
    436 	return(EINVAL);
    437 	/*NOTREACHED*/
    438     }
    439 
    440     return(ad1848_set_rec_port(ac, port));
    441 }
    442 
    443 int
    444 wss_get_in_port(addr)
    445     void *addr;
    446 {
    447     struct ad1848_softc *ac = addr;
    448     int port = WSS_MIC_IN_LVL;
    449 
    450     switch(ad1848_get_rec_port(ac)) {
    451     case MIC_IN_PORT:
    452 	port = WSS_MIC_IN_LVL;
    453 	break;
    454     case LINE_IN_PORT:
    455 	port = WSS_LINE_IN_LVL;
    456 	break;
    457     case DAC_IN_PORT:
    458 	port = WSS_DAC_LVL;
    459 	break;
    460     }
    461 
    462     DPRINTF(("wss_get_in_port: %d\n", port));
    463 
    464     return(port);
    465 }
    466 
    467 int
    468 wss_mixer_set_port(addr, cp)
    469     void *addr;
    470     mixer_ctrl_t *cp;
    471 {
    472     struct ad1848_softc *ac = addr;
    473     struct wss_softc *sc = ac->parent;
    474     struct ad1848_volume vol;
    475     int error = EINVAL;
    476 
    477     DPRINTF(("wss_mixer_set_port: dev=%d type=%d\n", cp->dev, cp->type));
    478 
    479     switch (cp->dev) {
    480     case WSS_MIC_IN_LVL:	/* Microphone */
    481 	if (cp->type == AUDIO_MIXER_VALUE) {
    482 	    if (wss_to_vol(cp, &vol))
    483 		error = ad1848_set_aux2_gain(ac, &vol);
    484 	}
    485 	break;
    486 
    487     case WSS_MIC_IN_MUTE:	/* Microphone */
    488 	if (cp->type == AUDIO_MIXER_ENUM) {
    489 	    sc->mic_mute = cp->un.ord;
    490 	    DPRINTF(("mic mute %d\n", cp->un.ord));
    491 	    error = 0;
    492 	}
    493 	break;
    494 
    495     case WSS_LINE_IN_LVL:	/* linein/CD */
    496 	if (cp->type == AUDIO_MIXER_VALUE) {
    497 	    if (wss_to_vol(cp, &vol))
    498 		error = ad1848_set_aux1_gain(ac, &vol);
    499 	}
    500 	break;
    501 
    502     case WSS_LINE_IN_MUTE:	/* linein/CD */
    503 	if (cp->type == AUDIO_MIXER_ENUM) {
    504 	    sc->cd_mute = cp->un.ord;
    505 	    DPRINTF(("CD mute %d\n", cp->un.ord));
    506 	    error = 0;
    507 	}
    508 	break;
    509 
    510     case WSS_DAC_LVL:		/* dac out */
    511 	if (cp->type == AUDIO_MIXER_VALUE) {
    512 	    if (wss_to_vol(cp, &vol))
    513 		error = ad1848_set_out_gain(ac, &vol);
    514 	}
    515 	break;
    516 
    517     case WSS_DAC_MUTE:		/* dac out */
    518 	if (cp->type == AUDIO_MIXER_ENUM) {
    519 	    sc->dac_mute = cp->un.ord;
    520 	    DPRINTF(("DAC mute %d\n", cp->un.ord));
    521 	    error = 0;
    522 	}
    523 	break;
    524 
    525     case WSS_REC_LVL:		/* record level */
    526 	if (cp->type == AUDIO_MIXER_VALUE) {
    527 	    if (wss_to_vol(cp, &vol))
    528 		error = ad1848_set_rec_gain(ac, &vol);
    529 	}
    530 	break;
    531 
    532     case WSS_RECORD_SOURCE:
    533 	if (cp->type == AUDIO_MIXER_ENUM) {
    534 	    error = ad1848_set_rec_port(ac, cp->un.ord);
    535 	}
    536 	break;
    537 
    538     case WSS_MON_LVL:
    539 	if (cp->type == AUDIO_MIXER_VALUE && cp->un.value.num_channels == 1) {
    540 	    vol.left  = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    541 	    error = ad1848_set_mon_gain(ac, &vol);
    542 	}
    543 	break;
    544 
    545     default:
    546 	    return ENXIO;
    547 	    /*NOTREACHED*/
    548     }
    549 
    550     return 0;
    551 }
    552 
    553 int
    554 wss_mixer_get_port(addr, cp)
    555     void *addr;
    556     mixer_ctrl_t *cp;
    557 {
    558     struct ad1848_softc *ac = addr;
    559     struct wss_softc *sc = ac->parent;
    560     struct ad1848_volume vol;
    561     int error = EINVAL;
    562 
    563     DPRINTF(("wss_mixer_get_port: port=%d\n", cp->dev));
    564 
    565     switch (cp->dev) {
    566     case WSS_MIC_IN_LVL:	/* Microphone */
    567 	if (cp->type == AUDIO_MIXER_VALUE) {
    568 	    error = ad1848_get_aux2_gain(ac, &vol);
    569 	    if (!error)
    570 		wss_from_vol(cp, &vol);
    571 	}
    572 	break;
    573 
    574     case WSS_MIC_IN_MUTE:
    575 	if (cp->type == AUDIO_MIXER_ENUM) {
    576 	    cp->un.ord = sc->mic_mute;
    577 	    error = 0;
    578 	}
    579 	break;
    580 
    581     case WSS_LINE_IN_LVL:	/* linein/CD */
    582 	if (cp->type == AUDIO_MIXER_VALUE) {
    583 	    error = ad1848_get_aux1_gain(ac, &vol);
    584 	    if (!error)
    585 		wss_from_vol(cp, &vol);
    586 	}
    587 	break;
    588 
    589     case WSS_LINE_IN_MUTE:
    590 	if (cp->type == AUDIO_MIXER_ENUM) {
    591 	    cp->un.ord = sc->cd_mute;
    592 	    error = 0;
    593 	}
    594 	break;
    595 
    596     case WSS_DAC_LVL:		/* dac out */
    597 	if (cp->type == AUDIO_MIXER_VALUE) {
    598 	    error = ad1848_get_out_gain(ac, &vol);
    599 	    if (!error)
    600 		wss_from_vol(cp, &vol);
    601 	}
    602 	break;
    603 
    604     case WSS_DAC_MUTE:
    605 	if (cp->type == AUDIO_MIXER_ENUM) {
    606 	    cp->un.ord = sc->dac_mute;
    607 	    error = 0;
    608 	}
    609 	break;
    610 
    611     case WSS_REC_LVL:		/* record level */
    612 	if (cp->type == AUDIO_MIXER_VALUE) {
    613 	    error = ad1848_get_rec_gain(ac, &vol);
    614 	    if (!error)
    615 		wss_from_vol(cp, &vol);
    616 	}
    617 	break;
    618 
    619     case WSS_RECORD_SOURCE:
    620 	if (cp->type == AUDIO_MIXER_ENUM) {
    621 	    cp->un.ord = ad1848_get_rec_port(ac);
    622 	    error = 0;
    623 	}
    624 	break;
    625 
    626     case WSS_MON_LVL:		/* monitor level */
    627 	if (cp->type == AUDIO_MIXER_VALUE && cp->un.value.num_channels == 1) {
    628 	    error = ad1848_get_mon_gain(ac, &vol);
    629 	    if (!error)
    630 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = vol.left;
    631 	}
    632 	break;
    633 
    634     default:
    635 	error = ENXIO;
    636 	break;
    637     }
    638 
    639     return(error);
    640 }
    641 
    642 int
    643 wss_query_devinfo(addr, dip)
    644     void *addr;
    645     mixer_devinfo_t *dip;
    646 {
    647     DPRINTF(("wss_query_devinfo: index=%d\n", dip->index));
    648 
    649     switch(dip->index) {
    650     case WSS_MIC_IN_LVL:	/* Microphone */
    651 	dip->type = AUDIO_MIXER_VALUE;
    652 	dip->mixer_class = WSS_INPUT_CLASS;
    653 	dip->prev = AUDIO_MIXER_LAST;
    654 	dip->next = WSS_MIC_IN_MUTE;
    655 	strcpy(dip->label.name, AudioNmicrophone);
    656 	dip->un.v.num_channels = 2;
    657 	strcpy(dip->un.v.units.name, AudioNvolume);
    658 	break;
    659 
    660     case WSS_LINE_IN_LVL:	/* line/CD */
    661 	dip->type = AUDIO_MIXER_VALUE;
    662 	dip->mixer_class = WSS_INPUT_CLASS;
    663 	dip->prev = AUDIO_MIXER_LAST;
    664 	dip->next = WSS_LINE_IN_MUTE;
    665 	strcpy(dip->label.name, AudioNcd);
    666 	dip->un.v.num_channels = 2;
    667 	strcpy(dip->un.v.units.name, AudioNvolume);
    668 	break;
    669 
    670     case WSS_DAC_LVL:		/*  dacout */
    671 	dip->type = AUDIO_MIXER_VALUE;
    672 	dip->mixer_class = WSS_INPUT_CLASS;
    673 	dip->prev = AUDIO_MIXER_LAST;
    674 	dip->next = WSS_DAC_MUTE;
    675 	strcpy(dip->label.name, AudioNdac);
    676 	dip->un.v.num_channels = 2;
    677 	strcpy(dip->un.v.units.name, AudioNvolume);
    678 	break;
    679 
    680     case WSS_REC_LVL:	/* record level */
    681 	dip->type = AUDIO_MIXER_VALUE;
    682 	dip->mixer_class = WSS_RECORD_CLASS;
    683 	dip->prev = AUDIO_MIXER_LAST;
    684 	dip->next = WSS_RECORD_SOURCE;
    685 	strcpy(dip->label.name, AudioNrecord);
    686 	dip->un.v.num_channels = 2;
    687 	strcpy(dip->un.v.units.name, AudioNvolume);
    688 	break;
    689 
    690     case WSS_MON_LVL:	/* monitor level */
    691 	dip->type = AUDIO_MIXER_VALUE;
    692 	dip->mixer_class = WSS_MONITOR_CLASS;
    693 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    694 	strcpy(dip->label.name, AudioNmonitor);
    695 	dip->un.v.num_channels = 1;
    696 	strcpy(dip->un.v.units.name, AudioNvolume);
    697 	break;
    698 
    699     case WSS_INPUT_CLASS:			/* input class descriptor */
    700 	dip->type = AUDIO_MIXER_CLASS;
    701 	dip->mixer_class = WSS_INPUT_CLASS;
    702 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    703 	strcpy(dip->label.name, AudioCInputs);
    704 	break;
    705 
    706     case WSS_MONITOR_CLASS:			/* monitor class descriptor */
    707 	dip->type = AUDIO_MIXER_CLASS;
    708 	dip->mixer_class = WSS_MONITOR_CLASS;
    709 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    710 	strcpy(dip->label.name, AudioCMonitor);
    711 	break;
    712 
    713     case WSS_RECORD_CLASS:			/* record source class */
    714 	dip->type = AUDIO_MIXER_CLASS;
    715 	dip->mixer_class = WSS_RECORD_CLASS;
    716 	dip->next = dip->prev = AUDIO_MIXER_LAST;
    717 	strcpy(dip->label.name, AudioCRecord);
    718 	break;
    719 
    720     case WSS_MIC_IN_MUTE:
    721 	dip->mixer_class = WSS_INPUT_CLASS;
    722 	dip->type = AUDIO_MIXER_ENUM;
    723 	dip->prev = WSS_MIC_IN_LVL;
    724 	dip->next = AUDIO_MIXER_LAST;
    725 	goto mute;
    726 
    727     case WSS_LINE_IN_MUTE:
    728 	dip->mixer_class = WSS_INPUT_CLASS;
    729 	dip->type = AUDIO_MIXER_ENUM;
    730 	dip->prev = WSS_LINE_IN_LVL;
    731 	dip->next = AUDIO_MIXER_LAST;
    732 	goto mute;
    733 
    734     case WSS_DAC_MUTE:
    735 	dip->mixer_class = WSS_INPUT_CLASS;
    736 	dip->type = AUDIO_MIXER_ENUM;
    737 	dip->prev = WSS_DAC_LVL;
    738 	dip->next = AUDIO_MIXER_LAST;
    739     mute:
    740 	strcpy(dip->label.name, AudioNmute);
    741 	dip->un.e.num_mem = 2;
    742 	strcpy(dip->un.e.member[0].label.name, AudioNoff);
    743 	dip->un.e.member[0].ord = 0;
    744 	strcpy(dip->un.e.member[1].label.name, AudioNon);
    745 	dip->un.e.member[1].ord = 1;
    746 	break;
    747 
    748     case WSS_RECORD_SOURCE:
    749 	dip->mixer_class = WSS_RECORD_CLASS;
    750 	dip->type = AUDIO_MIXER_ENUM;
    751 	dip->prev = WSS_REC_LVL;
    752 	dip->next = AUDIO_MIXER_LAST;
    753 	strcpy(dip->label.name, AudioNsource);
    754 	dip->un.e.num_mem = 3;
    755 	strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
    756 	dip->un.e.member[0].ord = WSS_MIC_IN_LVL;
    757 	strcpy(dip->un.e.member[1].label.name, AudioNcd);
    758 	dip->un.e.member[1].ord = WSS_LINE_IN_LVL;
    759 	strcpy(dip->un.e.member[2].label.name, AudioNdac);
    760 	dip->un.e.member[2].ord = WSS_DAC_LVL;
    761 	break;
    762 
    763     default:
    764 	return ENXIO;
    765 	/*NOTREACHED*/
    766     }
    767     DPRINTF(("AUDIO_MIXER_DEVINFO: name=%s\n", dip->label.name));
    768 
    769     return 0;
    770 }
    771 
    772 /*
    773  * Initialization code for OPTi MAD16 compatible audio chips. Including
    774  *
    775  *      OPTi 82C928     MAD16           (replaced by C929)
    776  *      OAK OTI-601D    Mozart
    777  *      OPTi 82C929     MAD16 Pro
    778  *
    779  */
    780 static unsigned int mad_read __P((struct wss_softc *, int, int));
    781 static void mad_write __P((struct wss_softc *, int, int, int));
    782 static int detect_mad16 __P((struct wss_softc *, int));
    783 
    784 static unsigned int
    785 mad_read(sc, chip_type, port)
    786     struct wss_softc *sc;
    787     int chip_type;
    788     int port;
    789 {
    790     unsigned int tmp;
    791     int s = splaudio();		/* don't want an interrupt between outb&inb */
    792 
    793     switch (chip_type) {	/* Output password */
    794     case MAD_82C928:
    795     case MAD_OTI601D:
    796 	bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, MC_PASSWD_REG, M_PASSWD_928);
    797 	break;
    798     case MAD_82C929:
    799 	bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, MC_PASSWD_REG, M_PASSWD_929);
    800 	break;
    801     }
    802     tmp = bus_space_read_1(sc->sc_iot, sc->sc_mad_ioh, port);
    803     splx(s);
    804     return tmp;
    805 }
    806 
    807 static void
    808 mad_write(sc, chip_type, port, value)
    809     struct wss_softc *sc;
    810     int chip_type;
    811     int port;
    812     int value;
    813 {
    814     int s = splaudio();		/* don't want an interrupt between outb&outb */
    815 
    816     switch (chip_type) {	/* Output password */
    817     case MAD_82C928:
    818     case MAD_OTI601D:
    819 	bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, MC_PASSWD_REG, M_PASSWD_928);
    820 	break;
    821     case MAD_82C929:
    822 	bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, MC_PASSWD_REG, M_PASSWD_929);
    823 	break;
    824     }
    825     bus_space_write_1(sc->sc_iot, sc->sc_mad_ioh, port, value & 0xff);
    826     splx(s);
    827 }
    828 
    829 static int
    830 detect_mad16(sc, chip_type)
    831     struct wss_softc *sc;
    832     int chip_type;
    833 {
    834     unsigned char tmp, tmp2;
    835 
    836     /*
    837      * Check that reading a register doesn't return bus float (0xff)
    838      * when the card is accessed using password. This may fail in case
    839      * the card is in low power mode. Normally at least the power saving mode
    840      * bit should be 0.
    841      */
    842     if ((tmp = mad_read(sc, chip_type, MC1_PORT)) == 0xff) {
    843 	DPRINTF(("MC1_PORT returned 0xff\n"));
    844 	return 0;
    845     }
    846 
    847     /*
    848      * Now check that the gate is closed on first I/O after writing
    849      * the password. (This is how a MAD16 compatible card works).
    850      */
    851     if ((tmp2 = bus_space_read_1(sc->sc_iot, sc->sc_mad_ioh, MC1_PORT)) == tmp)	{ /* It didn't close */
    852 	DPRINTF(("MC1_PORT didn't close after read (0x%02x)\n", tmp2));
    853 	return 0;
    854     }
    855 
    856     mad_write(sc, chip_type, MC1_PORT, tmp ^ 0x80);	/* Toggle a bit */
    857 
    858     /* Compare the bit */
    859     if ((tmp2 = mad_read(sc, chip_type, MC1_PORT)) != (tmp ^ 0x80)) {
    860 	mad_write(sc, chip_type, MC1_PORT, tmp);	/* Restore */
    861 	DPRINTF(("Bit revert test failed (0x%02x, 0x%02x)\n", tmp, tmp2));
    862 	return 0;
    863     }
    864 
    865     mad_write(sc, chip_type, MC1_PORT, tmp);	/* Restore */
    866     return 1;
    867 }
    868 
    869 static int
    870 madprobe(sc, iobase)
    871     struct wss_softc *sc;
    872     int iobase;
    873 {
    874     static int valid_ports[M_WSS_NPORTS] =
    875         { M_WSS_PORT0, M_WSS_PORT1, M_WSS_PORT2, M_WSS_PORT3 };
    876     int i;
    877     int chip_type;
    878 
    879     if (bus_space_map(sc->sc_iot, MAD_BASE, MAD_NPORT, 0, &sc->sc_mad_ioh))
    880 	return MAD_NONE;
    881 
    882     /* Allocate bus space that the MAD chip wants */
    883     if (bus_space_map(sc->sc_iot, MAD_REG1, MAD_LEN1, 0, &sc->sc_mad_ioh1))
    884         goto bad1;
    885     if (bus_space_map(sc->sc_iot, MAD_REG2, MAD_LEN2, 0, &sc->sc_mad_ioh2))
    886         goto bad2;
    887     if (bus_space_map(sc->sc_iot, MAD_REG3, MAD_LEN3, 0, &sc->sc_mad_ioh3))
    888         goto bad3;
    889 
    890     DPRINTF(("mad: Detect using password = 0xE2\n"));
    891     if (!detect_mad16(sc, MAD_82C928)) {
    892 	/* No luck. Try different model */
    893 	DPRINTF(("mad: Detect using password = 0xE3\n"));
    894 	if (!detect_mad16(sc, MAD_82C929))
    895 	    goto bad;
    896 	chip_type = MAD_82C929;
    897 	DPRINTF(("mad: 82C929 detected\n"));
    898     } else {
    899 	if ((mad_read(sc, MAD_82C928, MC3_PORT) & 0x03) == 0x03) {
    900 	    DPRINTF(("mad: Mozart detected\n"));
    901 	    chip_type = MAD_OTI601D;
    902 	} else {
    903 	    DPRINTF(("mad: 82C928 detected?\n"));
    904 	    chip_type = MAD_82C928;
    905 	}
    906     }
    907 
    908 #ifdef AUDIO_DEBUG
    909     if (wssdebug)
    910 	for (i = MC1_PORT; i <= MC7_PORT; i++)
    911 	    printf("mad: port %03x = %02x\n", i, mad_read(sc, chip_type, i));
    912 #endif
    913 
    914     /* Set the WSS address. */
    915     for (i = 0; i < M_WSS_NPORTS; i++)
    916 	if (valid_ports[i] == iobase)
    917 	    break;
    918     if (i >= M_WSS_NPORTS) {		/* Not a valid port */
    919 	printf("mad: Bad WSS base address 0x%x\n", iobase);
    920 	goto bad;
    921     }
    922     /* enable WSS emulation at the I/O port, no joystick */
    923     mad_write(sc, chip_type, MC1_PORT, M_WSS_PORT_SELECT(i) | MC1_JOYDISABLE);
    924 
    925     mad_write(sc, chip_type, MC2_PORT, 0x03); /* ? */
    926     mad_write(sc, chip_type, MC3_PORT, 0xf0); /* Disable SB */
    927 
    928     return chip_type;
    929 bad:
    930     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh3, MAD_LEN3);
    931 bad3:
    932     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh2, MAD_LEN2);
    933 bad2:
    934     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh1, MAD_LEN1);
    935 bad1:
    936     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh, MAD_NPORT);
    937     return MAD_NONE;
    938 }
    939 
    940 static void
    941 madunmap(sc)
    942     struct wss_softc *sc;
    943 {
    944     if (sc->mad_chip_type == MAD_NONE)
    945         return;
    946     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh, MAD_NPORT);
    947     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh1, MAD_LEN1);
    948     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh2, MAD_LEN2);
    949     bus_space_unmap(sc->sc_iot, sc->sc_mad_ioh3, MAD_LEN3);
    950 }
    951 
    952 static void
    953 madattach(sc)
    954     struct wss_softc *sc;
    955 {
    956     int chip_type = sc->mad_chip_type;
    957     unsigned char cs4231_mode;
    958 
    959     if (chip_type == MAD_NONE)
    960         return;
    961 
    962     cs4231_mode =
    963 	strncmp(sc->sc_ad1848.chip_name, "CS4248", 6) == 0 ||
    964 	strncmp(sc->sc_ad1848.chip_name, "CS4231", 6) == 0 ? 0x02 : 0;
    965 
    966     if (chip_type == MAD_82C929) {
    967 	mad_write(sc, chip_type, MC4_PORT, 0xa2);
    968 	mad_write(sc, chip_type, MC5_PORT, 0xA5 | cs4231_mode);
    969 	mad_write(sc, chip_type, MC6_PORT, 0x03);	/* Disable MPU401 */
    970     } else {
    971 	mad_write(sc, chip_type, MC4_PORT, 0x02);
    972 	mad_write(sc, chip_type, MC5_PORT, 0x30 | cs4231_mode);
    973     }
    974 
    975 #ifdef AUDIO_DEBUG
    976     if (wssdebug) {
    977 	int i;
    978 	for (i = MC1_PORT; i <= MC7_PORT; i++)
    979 	    DPRINTF(("port %03x after init = %02x\n", i, mad_read(sc, chip_type, i)));
    980     }
    981 #endif
    982 }
    983