Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.90
      1 /*	$NetBSD: sb.c,v 1.90 2019/03/16 12:09:58 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1991-1993 Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the Computer Systems
     18  *	Engineering Group at Lawrence Berkeley Laboratory.
     19  * 4. Neither the name of the University nor of the Laboratory may be used
     20  *    to endorse or promote products derived from this software without
     21  *    specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: sb.c,v 1.90 2019/03/16 12:09:58 isaki Exp $");
     39 
     40 #include "midi.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/errno.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/syslog.h>
     47 #include <sys/device.h>
     48 #include <sys/proc.h>
     49 
     50 #include <sys/cpu.h>
     51 #include <sys/intr.h>
     52 #include <sys/bus.h>
     53 
     54 #include <sys/audioio.h>
     55 #include <dev/audio_if.h>
     56 #include <dev/midi_if.h>
     57 
     58 #include <dev/isa/isavar.h>
     59 #include <dev/isa/isadmavar.h>
     60 
     61 #include <dev/isa/sbreg.h>
     62 #include <dev/isa/sbvar.h>
     63 #include <dev/isa/sbdspvar.h>
     64 
     65 #if NMPU > 0
     66 const struct midi_hw_if sb_midi_hw_if = {
     67 	sbdsp_midi_open,
     68 	sbdsp_midi_close,
     69 	sbdsp_midi_output,
     70 	sbdsp_midi_getinfo,
     71 	0,			/* ioctl */
     72 	sbdsp_get_locks,
     73 };
     74 #endif
     75 
     76 int	sb_getdev(void *, struct audio_device *);
     77 
     78 /*
     79  * Define our interface to the higher level audio driver.
     80  */
     81 
     82 const struct audio_hw_if sb_hw_if = {
     83 	.open			= sbdsp_open,
     84 	.close			= sbdsp_close,
     85 	.query_encoding		= sbdsp_query_encoding,
     86 	.set_params		= sbdsp_set_params,
     87 	.round_blocksize	= sbdsp_round_blocksize,
     88 	.halt_output		= sbdsp_halt_output,
     89 	.halt_input		= sbdsp_halt_input,
     90 	.speaker_ctl		= sbdsp_speaker_ctl,
     91 	.getdev			= sb_getdev,
     92 	.set_port		= sbdsp_mixer_set_port,
     93 	.get_port		= sbdsp_mixer_get_port,
     94 	.query_devinfo		= sbdsp_mixer_query_devinfo,
     95 	.allocm			= sb_malloc,
     96 	.freem			= sb_free,
     97 	.round_buffersize	= sb_round_buffersize,
     98 	.mappage		= sb_mappage,
     99 	.get_props		= sbdsp_get_props,
    100 	.trigger_output		= sbdsp_trigger_output,
    101 	.trigger_input		= sbdsp_trigger_input,
    102 	.get_locks		= sbdsp_get_locks,
    103 };
    104 
    105 /*
    106  * Probe / attach routines.
    107  */
    108 
    109 
    110 int
    111 sbmatch(struct sbdsp_softc *sc, int probing, cfdata_t match)
    112 {
    113 	static const u_char drq_conf[8] = {
    114 		0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
    115 	};
    116 
    117 	static const u_char irq_conf[11] = {
    118 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    119 	};
    120 
    121 	if (sbdsp_probe(sc, match) == 0)
    122 		return 0;
    123 
    124 	/*
    125 	 * Cannot auto-discover DMA channel.
    126 	 */
    127 	if (ISSBPROCLASS(sc)) {
    128 		if (!SBP_DRQ_VALID(sc->sc_drq8)) {
    129 			aprint_error("%s: configured DMA chan %d invalid\n",
    130 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    131 			    sc->sc_drq8);
    132 			return 0;
    133 		}
    134 	} else {
    135 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
    136 			aprint_error("%s: configured DMA chan %d invalid\n",
    137 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    138 			    sc->sc_drq8);
    139 			return 0;
    140 		}
    141 	}
    142 
    143 	if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
    144 		/*
    145                  * XXX Some ViBRA16 cards seem to have two 8 bit DMA
    146                  * channels.  I've no clue how to use them, so ignore
    147                  * one of them for now.  -- augustss (at) NetBSD.org
    148                  */
    149 		sc->sc_drq16 = -1;
    150 
    151 	if (ISSB16CLASS(sc)) {
    152 		if (sc->sc_drq16 == -1)
    153 			sc->sc_drq16 = sc->sc_drq8;
    154 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
    155 			aprint_error("%s: configured DMA chan %d invalid\n",
    156 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    157 			    sc->sc_drq16);
    158 			return 0;
    159 		}
    160 	} else
    161 		sc->sc_drq16 = sc->sc_drq8;
    162 
    163 	if (ISSBPROCLASS(sc)) {
    164 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
    165 			aprint_error("%s: configured irq %d invalid\n",
    166 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    167 			    sc->sc_irq);
    168 			return 0;
    169 		}
    170 	} else {
    171 		if (!SB_IRQ_VALID(sc->sc_irq)) {
    172 			aprint_error("%s: configured irq %d invalid\n",
    173 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    174 			    sc->sc_irq);
    175 			return 0;
    176 		}
    177 	}
    178 
    179 	if (ISSB16CLASS(sc) && !(sc->sc_quirks & SB_QUIRK_NO_INIT_DRQ)) {
    180 		int w, r;
    181 		if (sc->sc_irq >= __arraycount(irq_conf)) {
    182 			aprint_error("%s: Cannot handle irq %d\n",
    183 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    184 			    sc->sc_irq);
    185 			return 0;
    186 		}
    187 		if (sc->sc_drq16 >= __arraycount(drq_conf)) {
    188 			aprint_error("%s: Cannot handle drq16 %d\n",
    189 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    190 			    sc->sc_drq16);
    191 			return 0;
    192 		}
    193 		if (sc->sc_drq8 >= __arraycount(drq_conf)) {
    194 			aprint_error("%s: Cannot handle drq8 %d\n",
    195 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    196 			    sc->sc_drq8);
    197 			return 0;
    198 		}
    199 #if 0
    200 		printf("%s: old drq conf %02x\n", device_xname(sc->sc_dev),
    201 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    202 		printf("%s: try drq conf %02x\n", device_xname(sc->sc_dev),
    203 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    204 #endif
    205 		w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
    206 		sbdsp_mix_write(sc, SBP_SET_DRQ, w);
    207 		r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
    208 		if (r != w) {
    209 			aprint_error("%s: setting drq mask %02x failed, "
    210 			    "got %02x\n",
    211 			    probing ? "sbmatch" : device_xname(sc->sc_dev), w,
    212 			    r);
    213 			return 0;
    214 		}
    215 #if 0
    216 		printf("%s: new drq conf %02x\n", device_xname(sc->sc_dev),
    217 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    218 #endif
    219 
    220 #if 0
    221 		printf("%s: old irq conf %02x\n", device_xname(sc->sc_dev),
    222 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    223 		printf("%s: try irq conf %02x\n", device_xname(sc->sc_dev),
    224 		    irq_conf[sc->sc_irq]);
    225 #endif
    226 		w = irq_conf[sc->sc_irq];
    227 		sbdsp_mix_write(sc, SBP_SET_IRQ, w);
    228 		r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
    229 		if (r != w) {
    230 			aprint_error("%s: setting irq mask %02x failed, "
    231 			    "got %02x\n",
    232 			    probing ? "sbmatch" : device_xname(sc->sc_dev), w,
    233 			    r);
    234 			return 0;
    235 		}
    236 #if 0
    237 		printf("%s: new irq conf %02x\n", device_xname(sc->sc_dev),
    238 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    239 #endif
    240 	}
    241 
    242 	return 1;
    243 }
    244 
    245 
    246 void
    247 sbattach(struct sbdsp_softc *sc)
    248 {
    249 	struct audio_attach_args arg;
    250 
    251 	sbdsp_attach(sc);
    252 
    253 	audio_attach_mi(&sb_hw_if, sc, sc->sc_dev);
    254 
    255 #if NMPU > 0
    256 	switch(sc->sc_hasmpu) {
    257 	default:
    258 	case SBMPU_NONE:	/* no mpu */
    259 		break;
    260 	case SBMPU_INTERNAL:	/* try to attach midi directly */
    261 		midi_attach_mi(&sb_midi_hw_if, sc, sc->sc_dev);
    262 		break;
    263 	case SBMPU_EXTERNAL:	/* search for mpu */
    264 		arg.type = AUDIODEV_TYPE_MPU;
    265 		arg.hwif = 0;
    266 		arg.hdl = 0;
    267 		sc->sc_mpudev = config_found_ia(sc->sc_dev, "sbdsp", &arg, audioprint);
    268 		break;
    269 	}
    270 #endif
    271 	if (sc->sc_model >= SB_20) {
    272 		arg.type = AUDIODEV_TYPE_OPL;
    273 		arg.hwif = 0;
    274 		arg.hdl = 0;
    275 		(void)config_found_ia(sc->sc_dev, "sbdsp", &arg, audioprint);
    276 	}
    277 }
    278 
    279 /*
    280  * Various routines to interface to higher level audio driver
    281  */
    282 
    283 int
    284 sb_getdev(void *addr, struct audio_device *retp)
    285 {
    286 	static const char * const names[] = SB_NAMES;
    287 	struct sbdsp_softc *sc;
    288 	const char *config;
    289 
    290 	sc = addr;
    291 	if (sc->sc_model == SB_JAZZ)
    292 		strlcpy(retp->name, "MV Jazz16", sizeof(retp->name));
    293 	else
    294 		strlcpy(retp->name, "SoundBlaster", sizeof(retp->name));
    295 	snprintf(retp->version, sizeof(retp->version), "%d.%02d",
    296 	    SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version));
    297 	if (sc->sc_model < sizeof names / sizeof names[0])
    298 		config = names[sc->sc_model];
    299 	else
    300 		config = "??";
    301 	strlcpy(retp->config, config, sizeof(retp->config));
    302 
    303 	return 0;
    304 }
    305