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