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