Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.65
      1 /*	$NetBSD: sb.c,v 1.65 1999/02/18 07:08:35 mycroft 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 "midi.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/errno.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/syslog.h>
     44 #include <sys/device.h>
     45 #include <sys/proc.h>
     46 
     47 #include <machine/cpu.h>
     48 #include <machine/intr.h>
     49 #include <machine/bus.h>
     50 
     51 #include <sys/audioio.h>
     52 #include <dev/audio_if.h>
     53 #include <dev/midi_if.h>
     54 
     55 #include <dev/midi_if.h>
     56 
     57 #include <dev/isa/isavar.h>
     58 #include <dev/isa/isadmavar.h>
     59 
     60 #include <dev/isa/sbreg.h>
     61 #include <dev/isa/sbvar.h>
     62 #include <dev/isa/sbdspvar.h>
     63 
     64 #if NMIDI > 0
     65 int	sb_mpu401_open __P((void *, int,
     66 			     void (*iintr)__P((void *, int)),
     67 			     void (*ointr)__P((void *)), void *arg));
     68 void	sb_mpu401_close __P((void *));
     69 int	sb_mpu401_output __P((void *, int));
     70 void	sb_mpu401_getinfo __P((void *, struct midi_info *));
     71 
     72 struct midi_hw_if sb_midi_hw_if = {
     73 	sbdsp_midi_open,
     74 	sbdsp_midi_close,
     75 	sbdsp_midi_output,
     76 	sbdsp_midi_getinfo,
     77 	0,			/* ioctl */
     78 };
     79 
     80 struct midi_hw_if sb_mpu401_hw_if = {
     81 	sb_mpu401_open,
     82 	sb_mpu401_close,
     83 	sb_mpu401_output,
     84 	sb_mpu401_getinfo,
     85 	0,			/* ioctl */
     86 };
     87 #endif
     88 
     89 struct audio_device sb_device = {
     90 	"SoundBlaster",
     91 	"x",
     92 	"sb"
     93 };
     94 
     95 int	sb_getdev __P((void *, struct audio_device *));
     96 
     97 /*
     98  * Define our interface to the higher level audio driver.
     99  */
    100 
    101 struct audio_hw_if sb_hw_if = {
    102 	sbdsp_open,
    103 	sbdsp_close,
    104 	0,
    105 	sbdsp_query_encoding,
    106 	sbdsp_set_params,
    107 	sbdsp_round_blocksize,
    108 	0,
    109 	0,
    110 	0,
    111 	0,
    112 	0,
    113 	sbdsp_halt_output,
    114 	sbdsp_halt_input,
    115 	sbdsp_speaker_ctl,
    116 	sb_getdev,
    117 	0,
    118 	sbdsp_mixer_set_port,
    119 	sbdsp_mixer_get_port,
    120 	sbdsp_mixer_query_devinfo,
    121 	sb_malloc,
    122 	sb_free,
    123 	sb_round_buffersize,
    124         sb_mappage,
    125 	sbdsp_get_props,
    126 	sbdsp_trigger_output,
    127 	sbdsp_trigger_input,
    128 };
    129 
    130 /*
    131  * Probe / attach routines.
    132  */
    133 
    134 
    135 int
    136 sbmatch(sc)
    137 	struct sbdsp_softc *sc;
    138 {
    139 	static u_char drq_conf[8] = {
    140 		0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
    141 	};
    142 
    143 	static u_char irq_conf[11] = {
    144 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    145 	};
    146 
    147 	if (sbdsp_probe(sc) == 0)
    148 		return 0;
    149 
    150 	/*
    151 	 * Cannot auto-discover DMA channel.
    152 	 */
    153 	if (ISSBPROCLASS(sc)) {
    154 		if (!SBP_DRQ_VALID(sc->sc_drq8)) {
    155 			printf("%s: configured dma chan %d invalid\n",
    156 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    157 			return 0;
    158 		}
    159 	} else {
    160 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
    161 			printf("%s: configured dma chan %d invalid\n",
    162 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    163 			return 0;
    164 		}
    165 	}
    166 
    167         if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
    168         	/*
    169                  * XXX Some ViBRA16 cards seem to have two 8 bit DMA
    170                  * channels.  I've no clue how to use them, so ignore
    171                  * one of them for now.  -- augustss (at) netbsd.org
    172                  */
    173         	sc->sc_drq16 = -1;
    174 
    175 	if (ISSB16CLASS(sc)) {
    176 		if (sc->sc_drq16 == -1)
    177 			sc->sc_drq16 = sc->sc_drq8;
    178 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
    179 			printf("%s: configured dma chan %d invalid\n",
    180 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    181 			return 0;
    182 		}
    183 	} else
    184 		sc->sc_drq16 = sc->sc_drq8;
    185 
    186 	if (ISSBPROCLASS(sc)) {
    187 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
    188 			printf("%s: configured irq %d invalid\n",
    189 			    sc->sc_dev.dv_xname, sc->sc_irq);
    190 			return 0;
    191 		}
    192 	} else {
    193 		if (!SB_IRQ_VALID(sc->sc_irq)) {
    194 			printf("%s: configured irq %d invalid\n",
    195 			    sc->sc_dev.dv_xname, sc->sc_irq);
    196 			return 0;
    197 		}
    198 	}
    199 
    200 	if (ISSB16CLASS(sc)) {
    201 		int w, r;
    202 #if 0
    203 		printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
    204 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    205 		printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
    206 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    207 #endif
    208 		w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
    209 		sbdsp_mix_write(sc, SBP_SET_DRQ, w);
    210 		r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
    211 		if (r != w) {
    212 			printf("%s: setting drq mask %02x failed, got %02x\n",
    213 			    sc->sc_dev.dv_xname, w, r);
    214 			return 0;
    215 		}
    216 #if 0
    217 		printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
    218 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    219 #endif
    220 
    221 #if 0
    222 		printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
    223 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    224 		printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
    225 		    irq_conf[sc->sc_irq]);
    226 #endif
    227 		w = irq_conf[sc->sc_irq];
    228 		sbdsp_mix_write(sc, SBP_SET_IRQ, w);
    229 		r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
    230 		if (r != w) {
    231 			printf("%s: setting irq mask %02x failed, got %02x\n",
    232 			    sc->sc_dev.dv_xname, w, r);
    233 			return 0;
    234 		}
    235 #if 0
    236 		printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
    237 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    238 #endif
    239 	}
    240 
    241 	return 1;
    242 }
    243 
    244 
    245 void
    246 sbattach(sc)
    247 	struct sbdsp_softc *sc;
    248 {
    249 	struct audio_attach_args arg;
    250 #if NMIDI > 0
    251 	struct midi_hw_if *mhw = &sb_midi_hw_if;
    252 #endif
    253 
    254 	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, sc->sc_ist,
    255 	    IPL_AUDIO, sbdsp_intr, sc);
    256 
    257 	sbdsp_attach(sc);
    258 
    259 #if NMIDI > 0
    260 	sc->sc_hasmpu = 0;
    261 	if (ISSB16CLASS(sc) && sc->sc_mpu_sc.iobase != 0) {
    262 		sc->sc_mpu_sc.iot = sc->sc_iot;
    263 		if (mpu401_find(&sc->sc_mpu_sc)) {
    264 			sc->sc_hasmpu = 1;
    265 			mhw = &sb_mpu401_hw_if;
    266 		}
    267 	}
    268 	midi_attach_mi(mhw, sc, &sc->sc_dev);
    269 #endif
    270 
    271 	audio_attach_mi(&sb_hw_if, sc, &sc->sc_dev);
    272 
    273 	arg.type = AUDIODEV_TYPE_OPL;
    274 	arg.hwif = 0;
    275 	arg.hdl = 0;
    276 	(void)config_found(&sc->sc_dev, &arg, audioprint);
    277 }
    278 
    279 /*
    280  * Various routines to interface to higher level audio driver
    281  */
    282 
    283 int
    284 sb_getdev(addr, retp)
    285 	void *addr;
    286 	struct audio_device *retp;
    287 {
    288 	struct sbdsp_softc *sc = addr;
    289 	static char *names[] = SB_NAMES;
    290 	char *config;
    291 
    292 	if (sc->sc_model == SB_JAZZ)
    293 		strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
    294 	else
    295 		strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
    296 	sprintf(retp->version, "%d.%02d",
    297 		SBVER_MAJOR(sc->sc_version),
    298 		SBVER_MINOR(sc->sc_version));
    299 	if (0 <= sc->sc_model && sc->sc_model < sizeof names / sizeof names[0])
    300 		config = names[sc->sc_model];
    301 	else
    302 		config = "??";
    303 	strncpy(retp->config, config, sizeof(retp->config));
    304 
    305 	return 0;
    306 }
    307 
    308 #if NMIDI > 0
    309 
    310 #define SBMPU(a) (&((struct sbdsp_softc *)addr)->sc_mpu_sc)
    311 
    312 int
    313 sb_mpu401_open(addr, flags, iintr, ointr, arg)
    314 	void *addr;
    315 	int flags;
    316 	void (*iintr)__P((void *, int));
    317 	void (*ointr)__P((void *));
    318 	void *arg;
    319 {
    320 	return mpu401_open(SBMPU(addr), flags, iintr, ointr, arg);
    321 }
    322 
    323 int
    324 sb_mpu401_output(addr, d)
    325 	void *addr;
    326 	int d;
    327 {
    328 	return mpu401_output(SBMPU(addr), d);
    329 }
    330 
    331 void
    332 sb_mpu401_close(addr)
    333 	void *addr;
    334 {
    335 	mpu401_close(SBMPU(addr));
    336 }
    337 
    338 void
    339 sb_mpu401_getinfo(addr, mi)
    340 	void *addr;
    341 	struct midi_info *mi;
    342 {
    343 	mi->name = "SB MPU-401 UART";
    344 	mi->props = 0;
    345 }
    346 #endif
    347 
    348