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