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