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