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