Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.88.38.1
      1 /*	$NetBSD: sb.c,v 1.88.38.1 2012/04/17 00:07:39 yamt 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.88.38.1 2012/04/17 00:07:39 yamt 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 	sbdsp_open,
     84 	sbdsp_close,
     85 	0,
     86 	sbdsp_query_encoding,
     87 	sbdsp_set_params,
     88 	sbdsp_round_blocksize,
     89 	0,
     90 	0,
     91 	0,
     92 	0,
     93 	0,
     94 	sbdsp_halt_output,
     95 	sbdsp_halt_input,
     96 	sbdsp_speaker_ctl,
     97 	sb_getdev,
     98 	0,
     99 	sbdsp_mixer_set_port,
    100 	sbdsp_mixer_get_port,
    101 	sbdsp_mixer_query_devinfo,
    102 	sb_malloc,
    103 	sb_free,
    104 	sb_round_buffersize,
    105 	sb_mappage,
    106 	sbdsp_get_props,
    107 	sbdsp_trigger_output,
    108 	sbdsp_trigger_input,
    109 	NULL,
    110 	sbdsp_get_locks,
    111 };
    112 
    113 /*
    114  * Probe / attach routines.
    115  */
    116 
    117 
    118 int
    119 sbmatch(struct sbdsp_softc *sc, int probing, cfdata_t match)
    120 {
    121 	static const u_char drq_conf[8] = {
    122 		0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
    123 	};
    124 
    125 	static const u_char irq_conf[11] = {
    126 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    127 	};
    128 
    129 	if (sbdsp_probe(sc, match) == 0)
    130 		return 0;
    131 
    132 	/*
    133 	 * Cannot auto-discover DMA channel.
    134 	 */
    135 	if (ISSBPROCLASS(sc)) {
    136 		if (!SBP_DRQ_VALID(sc->sc_drq8)) {
    137 			aprint_error("%s: configured DMA chan %d invalid\n",
    138 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    139 			    sc->sc_drq8);
    140 			return 0;
    141 		}
    142 	} else {
    143 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
    144 			aprint_error("%s: configured DMA chan %d invalid\n",
    145 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    146 			    sc->sc_drq8);
    147 			return 0;
    148 		}
    149 	}
    150 
    151 	if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
    152 		/*
    153                  * XXX Some ViBRA16 cards seem to have two 8 bit DMA
    154                  * channels.  I've no clue how to use them, so ignore
    155                  * one of them for now.  -- augustss (at) NetBSD.org
    156                  */
    157 		sc->sc_drq16 = -1;
    158 
    159 	if (ISSB16CLASS(sc)) {
    160 		if (sc->sc_drq16 == -1)
    161 			sc->sc_drq16 = sc->sc_drq8;
    162 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
    163 			aprint_error("%s: configured DMA chan %d invalid\n",
    164 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    165 			    sc->sc_drq16);
    166 			return 0;
    167 		}
    168 	} else
    169 		sc->sc_drq16 = sc->sc_drq8;
    170 
    171 	if (ISSBPROCLASS(sc)) {
    172 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
    173 			aprint_error("%s: configured irq %d invalid\n",
    174 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    175 			    sc->sc_irq);
    176 			return 0;
    177 		}
    178 	} else {
    179 		if (!SB_IRQ_VALID(sc->sc_irq)) {
    180 			aprint_error("%s: configured irq %d invalid\n",
    181 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    182 			    sc->sc_irq);
    183 			return 0;
    184 		}
    185 	}
    186 
    187 	if (ISSB16CLASS(sc) && !(sc->sc_quirks & SB_QUIRK_NO_INIT_DRQ)) {
    188 		int w, r;
    189 		if (sc->sc_irq >= __arraycount(irq_conf)) {
    190 			aprint_error("%s: Cannot handle irq %d\n",
    191 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    192 			    sc->sc_irq);
    193 			return 0;
    194 		}
    195 		if (sc->sc_drq16 >= __arraycount(drq_conf)) {
    196 			aprint_error("%s: Cannot handle drq16 %d\n",
    197 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    198 			    sc->sc_drq16);
    199 			return 0;
    200 		}
    201 		if (sc->sc_drq8 >= __arraycount(drq_conf)) {
    202 			aprint_error("%s: Cannot handle drq8 %d\n",
    203 			    probing ? "sbmatch" : device_xname(sc->sc_dev),
    204 			    sc->sc_drq8);
    205 			return 0;
    206 		}
    207 #if 0
    208 		printf("%s: old drq conf %02x\n", device_xname(sc->sc_dev),
    209 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    210 		printf("%s: try drq conf %02x\n", device_xname(sc->sc_dev),
    211 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    212 #endif
    213 		w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
    214 		sbdsp_mix_write(sc, SBP_SET_DRQ, w);
    215 		r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
    216 		if (r != w) {
    217 			aprint_error("%s: setting drq mask %02x failed, "
    218 			    "got %02x\n",
    219 			    probing ? "sbmatch" : device_xname(sc->sc_dev), w,
    220 			    r);
    221 			return 0;
    222 		}
    223 #if 0
    224 		printf("%s: new drq conf %02x\n", device_xname(sc->sc_dev),
    225 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    226 #endif
    227 
    228 #if 0
    229 		printf("%s: old irq conf %02x\n", device_xname(sc->sc_dev),
    230 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    231 		printf("%s: try irq conf %02x\n", device_xname(sc->sc_dev),
    232 		    irq_conf[sc->sc_irq]);
    233 #endif
    234 		w = irq_conf[sc->sc_irq];
    235 		sbdsp_mix_write(sc, SBP_SET_IRQ, w);
    236 		r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
    237 		if (r != w) {
    238 			aprint_error("%s: setting irq mask %02x failed, "
    239 			    "got %02x\n",
    240 			    probing ? "sbmatch" : device_xname(sc->sc_dev), w,
    241 			    r);
    242 			return 0;
    243 		}
    244 #if 0
    245 		printf("%s: new irq conf %02x\n", device_xname(sc->sc_dev),
    246 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    247 #endif
    248 	}
    249 
    250 	return 1;
    251 }
    252 
    253 
    254 void
    255 sbattach(struct sbdsp_softc *sc)
    256 {
    257 	struct audio_attach_args arg;
    258 
    259 	sbdsp_attach(sc);
    260 
    261 	audio_attach_mi(&sb_hw_if, sc, sc->sc_dev);
    262 
    263 #if NMPU > 0
    264 	switch(sc->sc_hasmpu) {
    265 	default:
    266 	case SBMPU_NONE:	/* no mpu */
    267 		break;
    268 	case SBMPU_INTERNAL:	/* try to attach midi directly */
    269 		midi_attach_mi(&sb_midi_hw_if, sc, sc->sc_dev);
    270 		break;
    271 	case SBMPU_EXTERNAL:	/* search for mpu */
    272 		arg.type = AUDIODEV_TYPE_MPU;
    273 		arg.hwif = 0;
    274 		arg.hdl = 0;
    275 		sc->sc_mpudev = config_found_ia(sc->sc_dev, "sbdsp", &arg, audioprint);
    276 		break;
    277 	}
    278 #endif
    279 	if (sc->sc_model >= SB_20) {
    280 		arg.type = AUDIODEV_TYPE_OPL;
    281 		arg.hwif = 0;
    282 		arg.hdl = 0;
    283 		(void)config_found_ia(sc->sc_dev, "sbdsp", &arg, audioprint);
    284 	}
    285 }
    286 
    287 /*
    288  * Various routines to interface to higher level audio driver
    289  */
    290 
    291 int
    292 sb_getdev(void *addr, struct audio_device *retp)
    293 {
    294 	static const char * const names[] = SB_NAMES;
    295 	struct sbdsp_softc *sc;
    296 	const char *config;
    297 
    298 	sc = addr;
    299 	if (sc->sc_model == SB_JAZZ)
    300 		strlcpy(retp->name, "MV Jazz16", sizeof(retp->name));
    301 	else
    302 		strlcpy(retp->name, "SoundBlaster", sizeof(retp->name));
    303 	snprintf(retp->version, sizeof(retp->version), "%d.%02d",
    304 	    SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version));
    305 	if (sc->sc_model < sizeof names / sizeof names[0])
    306 		config = names[sc->sc_model];
    307 	else
    308 		config = "??";
    309 	strlcpy(retp->config, config, sizeof(retp->config));
    310 
    311 	return 0;
    312 }
    313