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