Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.87
      1 /*	$NetBSD: sb.c,v 1.87 2007/10/19 12:00:22 ad 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.87 2007/10/19 12:00:22 ad 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)
    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) == 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 			printf("%s: configured DMA chan %d invalid\n",
    137 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    138 			return 0;
    139 		}
    140 	} else {
    141 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
    142 			printf("%s: configured DMA chan %d invalid\n",
    143 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    144 			return 0;
    145 		}
    146 	}
    147 
    148 	if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
    149 		/*
    150                  * XXX Some ViBRA16 cards seem to have two 8 bit DMA
    151                  * channels.  I've no clue how to use them, so ignore
    152                  * one of them for now.  -- augustss (at) NetBSD.org
    153                  */
    154 		sc->sc_drq16 = -1;
    155 
    156 	if (ISSB16CLASS(sc)) {
    157 		if (sc->sc_drq16 == -1)
    158 			sc->sc_drq16 = sc->sc_drq8;
    159 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
    160 			printf("%s: configured DMA chan %d invalid\n",
    161 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    162 			return 0;
    163 		}
    164 	} else
    165 		sc->sc_drq16 = sc->sc_drq8;
    166 
    167 	if (ISSBPROCLASS(sc)) {
    168 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
    169 			printf("%s: configured irq %d invalid\n",
    170 			    sc->sc_dev.dv_xname, sc->sc_irq);
    171 			return 0;
    172 		}
    173 	} else {
    174 		if (!SB_IRQ_VALID(sc->sc_irq)) {
    175 			printf("%s: configured irq %d invalid\n",
    176 			    sc->sc_dev.dv_xname, sc->sc_irq);
    177 			return 0;
    178 		}
    179 	}
    180 
    181 	if (ISSB16CLASS(sc) && !(sc->sc_quirks & SB_QUIRK_NO_INIT_DRQ)) {
    182 		int w, r;
    183 		if (sc->sc_irq >= __arraycount(irq_conf)) {
    184 			printf("%s: Cannot handle irq %d\n",
    185 			    sc->sc_dev.dv_xname, sc->sc_irq);
    186 			return 0;
    187 		}
    188 		if (sc->sc_drq16 >= __arraycount(drq_conf)) {
    189 			printf("%s: Cannot handle drq16 %d\n",
    190 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    191 			return 0;
    192 		}
    193 		if (sc->sc_drq8 >= __arraycount(drq_conf)) {
    194 			printf("%s: Cannot handle drq8 %d\n",
    195 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    196 			return 0;
    197 		}
    198 #if 0
    199 		printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
    200 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    201 		printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
    202 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    203 #endif
    204 		w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
    205 		sbdsp_mix_write(sc, SBP_SET_DRQ, w);
    206 		r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
    207 		if (r != w) {
    208 			printf("%s: setting drq mask %02x failed, got %02x\n",
    209 			    sc->sc_dev.dv_xname, w, r);
    210 			return 0;
    211 		}
    212 #if 0
    213 		printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
    214 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    215 #endif
    216 
    217 #if 0
    218 		printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
    219 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    220 		printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
    221 		    irq_conf[sc->sc_irq]);
    222 #endif
    223 		w = irq_conf[sc->sc_irq];
    224 		sbdsp_mix_write(sc, SBP_SET_IRQ, w);
    225 		r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
    226 		if (r != w) {
    227 			printf("%s: setting irq mask %02x failed, got %02x\n",
    228 			    sc->sc_dev.dv_xname, w, r);
    229 			return 0;
    230 		}
    231 #if 0
    232 		printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
    233 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    234 #endif
    235 	}
    236 
    237 	return 1;
    238 }
    239 
    240 
    241 void
    242 sbattach(struct sbdsp_softc *sc)
    243 {
    244 	struct audio_attach_args arg;
    245 
    246 	sbdsp_attach(sc);
    247 
    248 	audio_attach_mi(&sb_hw_if, sc, &sc->sc_dev);
    249 
    250 #if NMPU > 0
    251 	switch(sc->sc_hasmpu) {
    252 	default:
    253 	case SBMPU_NONE:	/* no mpu */
    254 		break;
    255 	case SBMPU_INTERNAL:	/* try to attach midi directly */
    256 		midi_attach_mi(&sb_midi_hw_if, sc, &sc->sc_dev);
    257 		break;
    258 	case SBMPU_EXTERNAL:	/* search for mpu */
    259 		arg.type = AUDIODEV_TYPE_MPU;
    260 		arg.hwif = 0;
    261 		arg.hdl = 0;
    262 		sc->sc_mpudev = config_found(&sc->sc_dev, &arg, audioprint);
    263 		break;
    264 	}
    265 #endif
    266 	if (sc->sc_model >= SB_20) {
    267 		arg.type = AUDIODEV_TYPE_OPL;
    268 		arg.hwif = 0;
    269 		arg.hdl = 0;
    270 		(void)config_found(&sc->sc_dev, &arg, audioprint);
    271 	}
    272 }
    273 
    274 /*
    275  * Various routines to interface to higher level audio driver
    276  */
    277 
    278 int
    279 sb_getdev(void *addr, struct audio_device *retp)
    280 {
    281 	static const char * const names[] = SB_NAMES;
    282 	struct sbdsp_softc *sc;
    283 	const char *config;
    284 
    285 	sc = addr;
    286 	if (sc->sc_model == SB_JAZZ)
    287 		strlcpy(retp->name, "MV Jazz16", sizeof(retp->name));
    288 	else
    289 		strlcpy(retp->name, "SoundBlaster", sizeof(retp->name));
    290 	snprintf(retp->version, sizeof(retp->version), "%d.%02d",
    291 	    SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version));
    292 	if (sc->sc_model < sizeof names / sizeof names[0])
    293 		config = names[sc->sc_model];
    294 	else
    295 		config = "??";
    296 	strlcpy(retp->config, config, sizeof(retp->config));
    297 
    298 	return 0;
    299 }
    300