Home | History | Annotate | Line # | Download | only in isa
wss.c revision 1.74.8.1
      1 /*	$NetBSD: wss.c,v 1.74.8.1 2021/03/21 21:09:12 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.74.8.1 2021/03/21 21:09:12 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, CFARG_EOL);
    165 	}
    166 }
    167 
    168 int
    169 wss_intr(void *addr)
    170 {
    171 	struct ad1848_isa_softc *sc;
    172 	int handled;
    173 
    174 	sc = addr;
    175 
    176 	mutex_spin_enter(&sc->sc_ad1848.sc_intr_lock);
    177 
    178 	handled = ad1848_isa_intr(sc);
    179 
    180 	mutex_spin_exit(&sc->sc_ad1848.sc_intr_lock);
    181 
    182 	return handled;
    183 }
    184 
    185 int
    186 wss_getdev(void *addr, struct audio_device *retp)
    187 {
    188 
    189 	*retp = wss_device;
    190 	return 0;
    191 }
    192 
    193 static ad1848_devmap_t mappings[] = {
    194 	{ WSS_MIC_IN_LVL, AD1848_KIND_LVL, AD1848_AUX2_CHANNEL },
    195 	{ WSS_LINE_IN_LVL, AD1848_KIND_LVL, AD1848_AUX1_CHANNEL },
    196 	{ WSS_DAC_LVL, AD1848_KIND_LVL, AD1848_DAC_CHANNEL },
    197 	{ WSS_MONITOR_LVL, AD1848_KIND_LVL, AD1848_MONO_CHANNEL },
    198 	{ WSS_MIC_IN_MUTE, AD1848_KIND_MUTE, AD1848_AUX2_CHANNEL },
    199 	{ WSS_LINE_IN_MUTE, AD1848_KIND_MUTE, AD1848_AUX1_CHANNEL },
    200 	{ WSS_DAC_MUTE, AD1848_KIND_MUTE, AD1848_DAC_CHANNEL },
    201 	{ WSS_MONITOR_MUTE, AD1848_KIND_MUTE, AD1848_MONO_CHANNEL },
    202 	{ WSS_REC_LVL, AD1848_KIND_RECORDGAIN, -1 },
    203 	{ WSS_RECORD_SOURCE, AD1848_KIND_RECORDSOURCE, -1}
    204 };
    205 
    206 static int nummap = sizeof(mappings) / sizeof(mappings[0]);
    207 
    208 int
    209 wss_mixer_set_port(void *addr, mixer_ctrl_t *cp)
    210 {
    211 	struct ad1848_softc *ac;
    212 
    213 	ac = addr;
    214 	return ad1848_mixer_set_port(ac, mappings, nummap, cp);
    215 }
    216 
    217 int
    218 wss_mixer_get_port(void *addr, mixer_ctrl_t *cp)
    219 {
    220 	struct ad1848_softc *ac;
    221 
    222 	ac = addr;
    223 	return ad1848_mixer_get_port(ac, mappings, nummap, cp);
    224 }
    225 
    226 int
    227 wss_query_devinfo(void *addr, mixer_devinfo_t *dip)
    228 {
    229 
    230 	DPRINTF(("wss_query_devinfo: index=%d\n", dip->index));
    231 	switch(dip->index) {
    232 	case WSS_MIC_IN_LVL:	/* Microphone */
    233 		dip->type = AUDIO_MIXER_VALUE;
    234 		dip->mixer_class = WSS_INPUT_CLASS;
    235 		dip->prev = AUDIO_MIXER_LAST;
    236 		dip->next = WSS_MIC_IN_MUTE;
    237 		strcpy(dip->label.name, AudioNmicrophone);
    238 		dip->un.v.num_channels = 2;
    239 		strcpy(dip->un.v.units.name, AudioNvolume);
    240 		break;
    241 
    242 	case WSS_LINE_IN_LVL:	/* line/CD */
    243 		dip->type = AUDIO_MIXER_VALUE;
    244 		dip->mixer_class = WSS_INPUT_CLASS;
    245 		dip->prev = AUDIO_MIXER_LAST;
    246 		dip->next = WSS_LINE_IN_MUTE;
    247 		strcpy(dip->label.name, AudioNcd);
    248 		dip->un.v.num_channels = 2;
    249 		strcpy(dip->un.v.units.name, AudioNvolume);
    250 		break;
    251 
    252 	case WSS_DAC_LVL:		/*  dacout */
    253 		dip->type = AUDIO_MIXER_VALUE;
    254 		dip->mixer_class = WSS_INPUT_CLASS;
    255 		dip->prev = AUDIO_MIXER_LAST;
    256 		dip->next = WSS_DAC_MUTE;
    257 		strcpy(dip->label.name, AudioNdac);
    258 		dip->un.v.num_channels = 2;
    259 		strcpy(dip->un.v.units.name, AudioNvolume);
    260 		break;
    261 
    262 	case WSS_REC_LVL:	/* record level */
    263 		dip->type = AUDIO_MIXER_VALUE;
    264 		dip->mixer_class = WSS_RECORD_CLASS;
    265 		dip->prev = AUDIO_MIXER_LAST;
    266 		dip->next = WSS_RECORD_SOURCE;
    267 		strcpy(dip->label.name, AudioNrecord);
    268 		dip->un.v.num_channels = 2;
    269 		strcpy(dip->un.v.units.name, AudioNvolume);
    270 		break;
    271 
    272 	case WSS_MONITOR_LVL:	/* monitor level */
    273 		dip->type = AUDIO_MIXER_VALUE;
    274 		dip->mixer_class = WSS_MONITOR_CLASS;
    275 		dip->prev = AUDIO_MIXER_LAST;
    276 		dip->next = WSS_MONITOR_MUTE;
    277 		strcpy(dip->label.name, AudioNmonitor);
    278 		dip->un.v.num_channels = 1;
    279 		strcpy(dip->un.v.units.name, AudioNvolume);
    280 		break;
    281 
    282 	case WSS_INPUT_CLASS:			/* input class descriptor */
    283 		dip->type = AUDIO_MIXER_CLASS;
    284 		dip->mixer_class = WSS_INPUT_CLASS;
    285 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    286 		strcpy(dip->label.name, AudioCinputs);
    287 		break;
    288 
    289 	case WSS_MONITOR_CLASS:			/* monitor class descriptor */
    290 		dip->type = AUDIO_MIXER_CLASS;
    291 		dip->mixer_class = WSS_MONITOR_CLASS;
    292 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    293 		strcpy(dip->label.name, AudioCmonitor);
    294 		break;
    295 
    296 	case WSS_RECORD_CLASS:			/* record source class */
    297 		dip->type = AUDIO_MIXER_CLASS;
    298 		dip->mixer_class = WSS_RECORD_CLASS;
    299 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    300 		strcpy(dip->label.name, AudioCrecord);
    301 		break;
    302 
    303 	case WSS_MIC_IN_MUTE:
    304 		dip->mixer_class = WSS_INPUT_CLASS;
    305 		dip->type = AUDIO_MIXER_ENUM;
    306 		dip->prev = WSS_MIC_IN_LVL;
    307 		dip->next = AUDIO_MIXER_LAST;
    308 		goto mute;
    309 
    310 	case WSS_LINE_IN_MUTE:
    311 		dip->mixer_class = WSS_INPUT_CLASS;
    312 		dip->type = AUDIO_MIXER_ENUM;
    313 		dip->prev = WSS_LINE_IN_LVL;
    314 		dip->next = AUDIO_MIXER_LAST;
    315 		goto mute;
    316 
    317 	case WSS_DAC_MUTE:
    318 		dip->mixer_class = WSS_INPUT_CLASS;
    319 		dip->type = AUDIO_MIXER_ENUM;
    320 		dip->prev = WSS_DAC_LVL;
    321 		dip->next = AUDIO_MIXER_LAST;
    322 		goto mute;
    323 
    324 	case WSS_MONITOR_MUTE:
    325 		dip->mixer_class = WSS_MONITOR_CLASS;
    326 		dip->type = AUDIO_MIXER_ENUM;
    327 		dip->prev = WSS_MONITOR_LVL;
    328 		dip->next = AUDIO_MIXER_LAST;
    329 	mute:
    330 		strcpy(dip->label.name, AudioNmute);
    331 		dip->un.e.num_mem = 2;
    332 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
    333 		dip->un.e.member[0].ord = 0;
    334 		strcpy(dip->un.e.member[1].label.name, AudioNon);
    335 		dip->un.e.member[1].ord = 1;
    336 		break;
    337 
    338 	case WSS_RECORD_SOURCE:
    339 		dip->mixer_class = WSS_RECORD_CLASS;
    340 		dip->type = AUDIO_MIXER_ENUM;
    341 		dip->prev = WSS_REC_LVL;
    342 		dip->next = AUDIO_MIXER_LAST;
    343 		strcpy(dip->label.name, AudioNsource);
    344 		dip->un.e.num_mem = 3;
    345 		strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
    346 		dip->un.e.member[0].ord = WSS_MIC_IN_LVL;
    347 		strcpy(dip->un.e.member[1].label.name, AudioNcd);
    348 		dip->un.e.member[1].ord = WSS_LINE_IN_LVL;
    349 		strcpy(dip->un.e.member[2].label.name, AudioNdac);
    350 		dip->un.e.member[2].ord = WSS_DAC_LVL;
    351 		break;
    352 
    353 	default:
    354 		return ENXIO;
    355 		/*NOTREACHED*/
    356 	}
    357 	DPRINTF(("AUDIO_MIXER_DEVINFO: name=%s\n", dip->label.name));
    358 
    359 	return 0;
    360 }
    361 
    362 
    363 /*
    364  * Copyright by Hannu Savolainen 1994
    365  *
    366  * Redistribution and use in source and binary forms, with or without
    367  * modification, are permitted provided that the following conditions are
    368  * met: 1. Redistributions of source code must retain the above copyright
    369  * notice, this list of conditions and the following disclaimer. 2.
    370  * Redistributions in binary form must reproduce the above copyright notice,
    371  * this list of conditions and the following disclaimer in the documentation
    372  * and/or other materials provided with the distribution.
    373  *
    374  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
    375  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    376  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    377  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
    378  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    379  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    380  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    381  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    382  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    383  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    384  * SUCH DAMAGE.
    385  *
    386  */
    387 /*
    388  * Initialization code for OPTi MAD16 compatible audio chips. Including
    389  *
    390  *      OPTi 82C928     MAD16           (replaced by C929)
    391  *      OAK OTI-601D    Mozart
    392  *      OPTi 82C929     MAD16 Pro
    393  *
    394  */
    395 
    396 u_int
    397 mad_read(struct wss_softc *sc, int port)
    398 {
    399 	u_int tmp;
    400 	int pwd;
    401 
    402 	switch (sc->mad_chip_type) {	/* Output password */
    403 	case MAD_82C928:
    404 	case MAD_OTI601D:
    405 		pwd = M_PASSWD_928;
    406 		break;
    407 	case MAD_82C929:
    408 		pwd = M_PASSWD_929;
    409 		break;
    410 	case MAD_82C931:
    411 		pwd = M_PASSWD_931;
    412 		break;
    413 	default:
    414 		panic("mad_read: Bad chip type=%d", sc->mad_chip_type);
    415 	}
    416 	bus_space_write_1(sc->sc_iot, sc->mad_ioh, MC_PASSWD_REG, pwd);
    417 	tmp = bus_space_read_1(sc->sc_iot, sc->mad_ioh, port);
    418 	return tmp;
    419 }
    420 
    421 void
    422 mad_write(struct wss_softc *sc, int port, int value)
    423 {
    424 	int pwd;
    425 
    426 	switch (sc->mad_chip_type) {	/* Output password */
    427 	case MAD_82C928:
    428 	case MAD_OTI601D:
    429 		pwd = M_PASSWD_928;
    430 		break;
    431 	case MAD_82C929:
    432 		pwd = M_PASSWD_929;
    433 		break;
    434 	case MAD_82C931:
    435 		pwd = M_PASSWD_931;
    436 		break;
    437 	default:
    438 		panic("mad_write: Bad chip type=%d", sc->mad_chip_type);
    439 	}
    440 	bus_space_write_1(sc->sc_iot, sc->mad_ioh, MC_PASSWD_REG, pwd);
    441 	bus_space_write_1(sc->sc_iot, sc->mad_ioh, port, value & 0xff);
    442 }
    443 
    444 void
    445 madattach(struct wss_softc *sc)
    446 {
    447 	struct ad1848_softc *ac;
    448 	unsigned char cs4231_mode;
    449 	int joy;
    450 
    451 	ac = (struct ad1848_softc *)&sc->sc_ad1848;
    452 	if (sc->mad_chip_type == MAD_NONE)
    453 		return;
    454 
    455 	/* Do we want the joystick disabled? */
    456 	joy = device_cfdata(ac->sc_dev)->cf_flags & 2 ? MC1_JOYDISABLE : 0;
    457 
    458 	/* enable WSS emulation at the I/O port */
    459 	mad_write(sc, MC1_PORT, M_WSS_PORT_SELECT(sc->mad_ioindex) | joy);
    460 	mad_write(sc, MC2_PORT, MC2_NO_CD_DRQ); /* disable CD */
    461 	mad_write(sc, MC3_PORT, 0xf0); /* Disable SB */
    462 
    463 	cs4231_mode =
    464 		strncmp(ac->chip_name, "CS4248", 6) == 0 ||
    465 		strncmp(ac->chip_name, "CS4231", 6) == 0 ? 0x02 : 0;
    466 
    467 	if (sc->mad_chip_type == MAD_82C929) {
    468 		mad_write(sc, MC4_PORT, 0x92);
    469 		mad_write(sc, MC5_PORT, 0xA5 | cs4231_mode);
    470 		mad_write(sc, MC6_PORT, 0x03);	/* Disable MPU401 */
    471 	} else {
    472 		mad_write(sc, MC4_PORT, 0x02);
    473 		mad_write(sc, MC5_PORT, 0x30 | cs4231_mode);
    474 	}
    475 
    476 #ifdef AUDIO_DEBUG
    477 	if (wssdebug) {
    478 		int i;
    479 		for (i = MC1_PORT; i <= MC7_PORT; i++)
    480 			DPRINTF(("port %03x after init = %02x\n",
    481 				 i, mad_read(sc, i)));
    482 	}
    483 #endif
    484 }
    485