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