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