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