Home | History | Annotate | Line # | Download | only in isa
wss.c revision 1.76
      1 /*	$NetBSD: wss.c,v 1.76 2021/04/26 19:21:55 thorpej 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  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the Computer Systems
     19  *	Engineering Group at Lawrence Berkeley Laboratory.
     20  * 4. Neither the name of the University nor of the Laboratory may be used
     21  *    to endorse or promote products derived from this software without
     22  *    specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: wss.c,v 1.76 2021/04/26 19:21:55 thorpej Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 #include <sys/errno.h>
     45 #include <sys/cpu.h>
     46 #include <sys/intr.h>
     47 #include <sys/bus.h>
     48 #include <sys/audioio.h>
     49 
     50 #include <dev/audio/audio_if.h>
     51 
     52 #include <dev/isa/isavar.h>
     53 #include <dev/isa/isadmavar.h>
     54 
     55 #include <dev/ic/ad1848reg.h>
     56 #include <dev/ic/cs4231reg.h>
     57 #include <dev/isa/ad1848var.h>
     58 #include <dev/isa/cs4231var.h>
     59 #include <dev/isa/wssreg.h>
     60 #include <dev/isa/wssvar.h>
     61 #include <dev/isa/madreg.h>
     62 
     63 #ifdef AUDIO_DEBUG
     64 #define DPRINTF(x)	if (wssdebug) printf x
     65 int	wssdebug = 0;
     66 #else
     67 #define DPRINTF(x)
     68 #endif
     69 
     70 struct audio_device wss_device = {
     71 	"wss,ad1848",
     72 	"",
     73 	"WSS"
     74 };
     75 
     76 int	wss_intr(void *);
     77 int	wss_getdev(void *, struct audio_device *);
     78 
     79 int	wss_mixer_set_port(void *, mixer_ctrl_t *);
     80 int	wss_mixer_get_port(void *, mixer_ctrl_t *);
     81 int	wss_query_devinfo(void *, mixer_devinfo_t *);
     82 
     83 /*
     84  * Define our interface to the higher level audio driver.
     85  */
     86 
     87 const struct audio_hw_if wss_hw_if = {
     88 	.open			= ad1848_isa_open,
     89 	.close			= ad1848_isa_close,
     90 	.query_format		= ad1848_query_format,
     91 	.set_format		= ad1848_set_format,
     92 	.commit_settings	= ad1848_commit_settings,
     93 	.halt_output		= ad1848_isa_halt_output,
     94 	.halt_input		= ad1848_isa_halt_input,
     95 	.getdev			= wss_getdev,
     96 	.set_port		= wss_mixer_set_port,
     97 	.get_port		= wss_mixer_get_port,
     98 	.query_devinfo		= wss_query_devinfo,
     99 	.allocm			= ad1848_isa_malloc,
    100 	.freem			= ad1848_isa_free,
    101 	.round_buffersize	= ad1848_isa_round_buffersize,
    102 	.get_props		= ad1848_isa_get_props,
    103 	.trigger_output		= ad1848_isa_trigger_output,
    104 	.trigger_input		= ad1848_isa_trigger_input,
    105 	.get_locks		= ad1848_get_locks,
    106 };
    107 
    108 /*
    109  * Attach hardware to driver, attach hardware driver to audio
    110  * pseudo-device driver .
    111  */
    112 void
    113 wssattach(struct wss_softc *sc)
    114 {
    115 	struct ad1848_softc *ac;
    116 #if 0 /* loses on CS423X chips */
    117 	int version;
    118 #endif
    119 
    120 	ac = &sc->sc_ad1848.sc_ad1848;
    121 
    122 	ad1848_init_locks(ac, IPL_AUDIO);
    123 
    124 	madattach(sc);
    125 
    126 	sc->sc_ad1848.sc_ih = isa_intr_establish(sc->wss_ic, sc->wss_irq,
    127 	    IST_EDGE, IPL_AUDIO, wss_intr, &sc->sc_ad1848);
    128 
    129 	ad1848_isa_attach(&sc->sc_ad1848);
    130 
    131 #if 0 /* loses on CS423X chips */
    132 	version = bus_space_read_1(sc->sc_iot, sc->sc_ioh, WSS_STATUS)
    133 	    & WSS_VERSMASK;
    134 	printf(" (vers %d)", version);
    135 #endif
    136 
    137 	switch(sc->mad_chip_type) {
    138 	case MAD_82C928:
    139 		printf(", 82C928");
    140 		break;
    141 	case MAD_OTI601D:
    142 		printf(", OTI-601D");
    143 		break;
    144 	case MAD_82C929:
    145 		printf(", 82C929");
    146 		break;
    147 	case MAD_82C931:
    148 		printf(", 82C931");
    149 		break;
    150 	default:
    151 		break;
    152 	}
    153 	printf("\n");
    154 
    155 	ac->parent = sc;
    156 
    157 	audio_attach_mi(&wss_hw_if, &sc->sc_ad1848, ac->sc_dev);
    158 
    159 	if (sc->mad_chip_type != MAD_NONE) {
    160 		struct audio_attach_args arg;
    161 		arg.type = AUDIODEV_TYPE_OPL;
    162 		arg.hwif = 0;
    163 		arg.hdl = 0;
    164 		(void)config_found(ac->sc_dev, &arg, audioprint,
    165 		    CFARG_IATTR, "wss",
    166 		    CFARG_EOL);
    167 	}
    168 }
    169 
    170 int
    171 wss_intr(void *addr)
    172 {
    173 	struct ad1848_isa_softc *sc;
    174 	int handled;
    175 
    176 	sc = addr;
    177 
    178 	mutex_spin_enter(&sc->sc_ad1848.sc_intr_lock);
    179 
    180 	handled = ad1848_isa_intr(sc);
    181 
    182 	mutex_spin_exit(&sc->sc_ad1848.sc_intr_lock);
    183 
    184 	return handled;
    185 }
    186 
    187 int
    188 wss_getdev(void *addr, struct audio_device *retp)
    189 {
    190 
    191 	*retp = wss_device;
    192 	return 0;
    193 }
    194 
    195 static ad1848_devmap_t mappings[] = {
    196 	{ WSS_MIC_IN_LVL, AD1848_KIND_LVL, AD1848_AUX2_CHANNEL },
    197 	{ WSS_LINE_IN_LVL, AD1848_KIND_LVL, AD1848_AUX1_CHANNEL },
    198 	{ WSS_DAC_LVL, AD1848_KIND_LVL, AD1848_DAC_CHANNEL },
    199 	{ WSS_MONITOR_LVL, AD1848_KIND_LVL, AD1848_MONO_CHANNEL },
    200 	{ WSS_MIC_IN_MUTE, AD1848_KIND_MUTE, AD1848_AUX2_CHANNEL },
    201 	{ WSS_LINE_IN_MUTE, AD1848_KIND_MUTE, AD1848_AUX1_CHANNEL },
    202 	{ WSS_DAC_MUTE, AD1848_KIND_MUTE, AD1848_DAC_CHANNEL },
    203 	{ WSS_MONITOR_MUTE, AD1848_KIND_MUTE, AD1848_MONO_CHANNEL },
    204 	{ WSS_REC_LVL, AD1848_KIND_RECORDGAIN, -1 },
    205 	{ WSS_RECORD_SOURCE, AD1848_KIND_RECORDSOURCE, -1}
    206 };
    207 
    208 static int nummap = sizeof(mappings) / sizeof(mappings[0]);
    209 
    210 int
    211 wss_mixer_set_port(void *addr, mixer_ctrl_t *cp)
    212 {
    213 	struct ad1848_softc *ac;
    214 
    215 	ac = addr;
    216 	return ad1848_mixer_set_port(ac, mappings, nummap, cp);
    217 }
    218 
    219 int
    220 wss_mixer_get_port(void *addr, mixer_ctrl_t *cp)
    221 {
    222 	struct ad1848_softc *ac;
    223 
    224 	ac = addr;
    225 	return ad1848_mixer_get_port(ac, mappings, nummap, cp);
    226 }
    227 
    228 int
    229 wss_query_devinfo(void *addr, mixer_devinfo_t *dip)
    230 {
    231 
    232 	DPRINTF(("wss_query_devinfo: index=%d\n", dip->index));
    233 	switch(dip->index) {
    234 	case WSS_MIC_IN_LVL:	/* Microphone */
    235 		dip->type = AUDIO_MIXER_VALUE;
    236 		dip->mixer_class = WSS_INPUT_CLASS;
    237 		dip->prev = AUDIO_MIXER_LAST;
    238 		dip->next = WSS_MIC_IN_MUTE;
    239 		strcpy(dip->label.name, AudioNmicrophone);
    240 		dip->un.v.num_channels = 2;
    241 		strcpy(dip->un.v.units.name, AudioNvolume);
    242 		break;
    243 
    244 	case WSS_LINE_IN_LVL:	/* line/CD */
    245 		dip->type = AUDIO_MIXER_VALUE;
    246 		dip->mixer_class = WSS_INPUT_CLASS;
    247 		dip->prev = AUDIO_MIXER_LAST;
    248 		dip->next = WSS_LINE_IN_MUTE;
    249 		strcpy(dip->label.name, AudioNcd);
    250 		dip->un.v.num_channels = 2;
    251 		strcpy(dip->un.v.units.name, AudioNvolume);
    252 		break;
    253 
    254 	case WSS_DAC_LVL:		/*  dacout */
    255 		dip->type = AUDIO_MIXER_VALUE;
    256 		dip->mixer_class = WSS_INPUT_CLASS;
    257 		dip->prev = AUDIO_MIXER_LAST;
    258 		dip->next = WSS_DAC_MUTE;
    259 		strcpy(dip->label.name, AudioNdac);
    260 		dip->un.v.num_channels = 2;
    261 		strcpy(dip->un.v.units.name, AudioNvolume);
    262 		break;
    263 
    264 	case WSS_REC_LVL:	/* record level */
    265 		dip->type = AUDIO_MIXER_VALUE;
    266 		dip->mixer_class = WSS_RECORD_CLASS;
    267 		dip->prev = AUDIO_MIXER_LAST;
    268 		dip->next = WSS_RECORD_SOURCE;
    269 		strcpy(dip->label.name, AudioNrecord);
    270 		dip->un.v.num_channels = 2;
    271 		strcpy(dip->un.v.units.name, AudioNvolume);
    272 		break;
    273 
    274 	case WSS_MONITOR_LVL:	/* monitor level */
    275 		dip->type = AUDIO_MIXER_VALUE;
    276 		dip->mixer_class = WSS_MONITOR_CLASS;
    277 		dip->prev = AUDIO_MIXER_LAST;
    278 		dip->next = WSS_MONITOR_MUTE;
    279 		strcpy(dip->label.name, AudioNmonitor);
    280 		dip->un.v.num_channels = 1;
    281 		strcpy(dip->un.v.units.name, AudioNvolume);
    282 		break;
    283 
    284 	case WSS_INPUT_CLASS:			/* input class descriptor */
    285 		dip->type = AUDIO_MIXER_CLASS;
    286 		dip->mixer_class = WSS_INPUT_CLASS;
    287 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    288 		strcpy(dip->label.name, AudioCinputs);
    289 		break;
    290 
    291 	case WSS_MONITOR_CLASS:			/* monitor class descriptor */
    292 		dip->type = AUDIO_MIXER_CLASS;
    293 		dip->mixer_class = WSS_MONITOR_CLASS;
    294 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    295 		strcpy(dip->label.name, AudioCmonitor);
    296 		break;
    297 
    298 	case WSS_RECORD_CLASS:			/* record source class */
    299 		dip->type = AUDIO_MIXER_CLASS;
    300 		dip->mixer_class = WSS_RECORD_CLASS;
    301 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    302 		strcpy(dip->label.name, AudioCrecord);
    303 		break;
    304 
    305 	case WSS_MIC_IN_MUTE:
    306 		dip->mixer_class = WSS_INPUT_CLASS;
    307 		dip->type = AUDIO_MIXER_ENUM;
    308 		dip->prev = WSS_MIC_IN_LVL;
    309 		dip->next = AUDIO_MIXER_LAST;
    310 		goto mute;
    311 
    312 	case WSS_LINE_IN_MUTE:
    313 		dip->mixer_class = WSS_INPUT_CLASS;
    314 		dip->type = AUDIO_MIXER_ENUM;
    315 		dip->prev = WSS_LINE_IN_LVL;
    316 		dip->next = AUDIO_MIXER_LAST;
    317 		goto mute;
    318 
    319 	case WSS_DAC_MUTE:
    320 		dip->mixer_class = WSS_INPUT_CLASS;
    321 		dip->type = AUDIO_MIXER_ENUM;
    322 		dip->prev = WSS_DAC_LVL;
    323 		dip->next = AUDIO_MIXER_LAST;
    324 		goto mute;
    325 
    326 	case WSS_MONITOR_MUTE:
    327 		dip->mixer_class = WSS_MONITOR_CLASS;
    328 		dip->type = AUDIO_MIXER_ENUM;
    329 		dip->prev = WSS_MONITOR_LVL;
    330 		dip->next = AUDIO_MIXER_LAST;
    331 	mute:
    332 		strcpy(dip->label.name, AudioNmute);
    333 		dip->un.e.num_mem = 2;
    334 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
    335 		dip->un.e.member[0].ord = 0;
    336 		strcpy(dip->un.e.member[1].label.name, AudioNon);
    337 		dip->un.e.member[1].ord = 1;
    338 		break;
    339 
    340 	case WSS_RECORD_SOURCE:
    341 		dip->mixer_class = WSS_RECORD_CLASS;
    342 		dip->type = AUDIO_MIXER_ENUM;
    343 		dip->prev = WSS_REC_LVL;
    344 		dip->next = AUDIO_MIXER_LAST;
    345 		strcpy(dip->label.name, AudioNsource);
    346 		dip->un.e.num_mem = 3;
    347 		strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
    348 		dip->un.e.member[0].ord = WSS_MIC_IN_LVL;
    349 		strcpy(dip->un.e.member[1].label.name, AudioNcd);
    350 		dip->un.e.member[1].ord = WSS_LINE_IN_LVL;
    351 		strcpy(dip->un.e.member[2].label.name, AudioNdac);
    352 		dip->un.e.member[2].ord = WSS_DAC_LVL;
    353 		break;
    354 
    355 	default:
    356 		return ENXIO;
    357 		/*NOTREACHED*/
    358 	}
    359 	DPRINTF(("AUDIO_MIXER_DEVINFO: name=%s\n", dip->label.name));
    360 
    361 	return 0;
    362 }
    363 
    364 
    365 /*
    366  * Copyright by Hannu Savolainen 1994
    367  *
    368  * Redistribution and use in source and binary forms, with or without
    369  * modification, are permitted provided that the following conditions are
    370  * met: 1. Redistributions of source code must retain the above copyright
    371  * notice, this list of conditions and the following disclaimer. 2.
    372  * Redistributions in binary form must reproduce the above copyright notice,
    373  * this list of conditions and the following disclaimer in the documentation
    374  * and/or other materials provided with the distribution.
    375  *
    376  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
    377  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    378  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    379  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
    380  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    381  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    382  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    383  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    384  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    385  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    386  * SUCH DAMAGE.
    387  *
    388  */
    389 /*
    390  * Initialization code for OPTi MAD16 compatible audio chips. Including
    391  *
    392  *      OPTi 82C928     MAD16           (replaced by C929)
    393  *      OAK OTI-601D    Mozart
    394  *      OPTi 82C929     MAD16 Pro
    395  *
    396  */
    397 
    398 u_int
    399 mad_read(struct wss_softc *sc, int port)
    400 {
    401 	u_int tmp;
    402 	int pwd;
    403 
    404 	switch (sc->mad_chip_type) {	/* Output password */
    405 	case MAD_82C928:
    406 	case MAD_OTI601D:
    407 		pwd = M_PASSWD_928;
    408 		break;
    409 	case MAD_82C929:
    410 		pwd = M_PASSWD_929;
    411 		break;
    412 	case MAD_82C931:
    413 		pwd = M_PASSWD_931;
    414 		break;
    415 	default:
    416 		panic("mad_read: Bad chip type=%d", sc->mad_chip_type);
    417 	}
    418 	bus_space_write_1(sc->sc_iot, sc->mad_ioh, MC_PASSWD_REG, pwd);
    419 	tmp = bus_space_read_1(sc->sc_iot, sc->mad_ioh, port);
    420 	return tmp;
    421 }
    422 
    423 void
    424 mad_write(struct wss_softc *sc, int port, int value)
    425 {
    426 	int pwd;
    427 
    428 	switch (sc->mad_chip_type) {	/* Output password */
    429 	case MAD_82C928:
    430 	case MAD_OTI601D:
    431 		pwd = M_PASSWD_928;
    432 		break;
    433 	case MAD_82C929:
    434 		pwd = M_PASSWD_929;
    435 		break;
    436 	case MAD_82C931:
    437 		pwd = M_PASSWD_931;
    438 		break;
    439 	default:
    440 		panic("mad_write: Bad chip type=%d", sc->mad_chip_type);
    441 	}
    442 	bus_space_write_1(sc->sc_iot, sc->mad_ioh, MC_PASSWD_REG, pwd);
    443 	bus_space_write_1(sc->sc_iot, sc->mad_ioh, port, value & 0xff);
    444 }
    445 
    446 void
    447 madattach(struct wss_softc *sc)
    448 {
    449 	struct ad1848_softc *ac;
    450 	unsigned char cs4231_mode;
    451 	int joy;
    452 
    453 	ac = (struct ad1848_softc *)&sc->sc_ad1848;
    454 	if (sc->mad_chip_type == MAD_NONE)
    455 		return;
    456 
    457 	/* Do we want the joystick disabled? */
    458 	joy = device_cfdata(ac->sc_dev)->cf_flags & 2 ? MC1_JOYDISABLE : 0;
    459 
    460 	/* enable WSS emulation at the I/O port */
    461 	mad_write(sc, MC1_PORT, M_WSS_PORT_SELECT(sc->mad_ioindex) | joy);
    462 	mad_write(sc, MC2_PORT, MC2_NO_CD_DRQ); /* disable CD */
    463 	mad_write(sc, MC3_PORT, 0xf0); /* Disable SB */
    464 
    465 	cs4231_mode =
    466 		strncmp(ac->chip_name, "CS4248", 6) == 0 ||
    467 		strncmp(ac->chip_name, "CS4231", 6) == 0 ? 0x02 : 0;
    468 
    469 	if (sc->mad_chip_type == MAD_82C929) {
    470 		mad_write(sc, MC4_PORT, 0x92);
    471 		mad_write(sc, MC5_PORT, 0xA5 | cs4231_mode);
    472 		mad_write(sc, MC6_PORT, 0x03);	/* Disable MPU401 */
    473 	} else {
    474 		mad_write(sc, MC4_PORT, 0x02);
    475 		mad_write(sc, MC5_PORT, 0x30 | cs4231_mode);
    476 	}
    477 
    478 #ifdef AUDIO_DEBUG
    479 	if (wssdebug) {
    480 		int i;
    481 		for (i = MC1_PORT; i <= MC7_PORT; i++)
    482 			DPRINTF(("port %03x after init = %02x\n",
    483 				 i, mad_read(sc, i)));
    484 	}
    485 #endif
    486 }
    487