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