1 /* $NetBSD: sb.c,v 1.73 2002/11/02 10:47:49 kristerw 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.73 2002/11/02 10:47:49 kristerw 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 int sb_getdev __P((void *, struct audio_device *)); 78 79 /* 80 * Define our interface to the higher level audio driver. 81 */ 82 83 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 0, 111 }; 112 113 /* 114 * Probe / attach routines. 115 */ 116 117 118 int 119 sbmatch(sc) 120 struct sbdsp_softc *sc; 121 { 122 static u_char drq_conf[8] = { 123 0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80 124 }; 125 126 static 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@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 0 186 printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname, 187 sbdsp_mix_read(sc, SBP_SET_DRQ)); 188 printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname, 189 drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]); 190 #endif 191 w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]; 192 sbdsp_mix_write(sc, SBP_SET_DRQ, w); 193 r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb; 194 if (r != w) { 195 printf("%s: setting drq mask %02x failed, got %02x\n", 196 sc->sc_dev.dv_xname, w, r); 197 return 0; 198 } 199 #if 0 200 printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname, 201 sbdsp_mix_read(sc, SBP_SET_DRQ)); 202 #endif 203 204 #if 0 205 printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname, 206 sbdsp_mix_read(sc, SBP_SET_IRQ)); 207 printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname, 208 irq_conf[sc->sc_irq]); 209 #endif 210 w = irq_conf[sc->sc_irq]; 211 sbdsp_mix_write(sc, SBP_SET_IRQ, w); 212 r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f; 213 if (r != w) { 214 printf("%s: setting irq mask %02x failed, got %02x\n", 215 sc->sc_dev.dv_xname, w, r); 216 return 0; 217 } 218 #if 0 219 printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname, 220 sbdsp_mix_read(sc, SBP_SET_IRQ)); 221 #endif 222 } 223 224 return 1; 225 } 226 227 228 void 229 sbattach(sc) 230 struct sbdsp_softc *sc; 231 { 232 struct audio_attach_args arg; 233 234 sbdsp_attach(sc); 235 236 audio_attach_mi(&sb_hw_if, sc, &sc->sc_dev); 237 238 #if NMPU > 0 239 if (sc->sc_hasmpu) { 240 arg.type = AUDIODEV_TYPE_MPU; 241 arg.hwif = 0; 242 arg.hdl = 0; 243 sc->sc_mpudev = config_found(&sc->sc_dev, &arg, audioprint); 244 } else { 245 midi_attach_mi(&sb_midi_hw_if, sc, &sc->sc_dev); 246 } 247 #endif 248 249 if (sc->sc_model >= SB_20) { 250 arg.type = AUDIODEV_TYPE_OPL; 251 arg.hwif = 0; 252 arg.hdl = 0; 253 (void)config_found(&sc->sc_dev, &arg, audioprint); 254 } 255 } 256 257 /* 258 * Various routines to interface to higher level audio driver 259 */ 260 261 int 262 sb_getdev(addr, retp) 263 void *addr; 264 struct audio_device *retp; 265 { 266 struct sbdsp_softc *sc = addr; 267 static char *names[] = SB_NAMES; 268 char *config; 269 270 if (sc->sc_model == SB_JAZZ) 271 strncpy(retp->name, "MV Jazz16", sizeof(retp->name)); 272 else 273 strncpy(retp->name, "SoundBlaster", sizeof(retp->name)); 274 sprintf(retp->version, "%d.%02d", 275 SBVER_MAJOR(sc->sc_version), 276 SBVER_MINOR(sc->sc_version)); 277 if (0 <= sc->sc_model && sc->sc_model < sizeof names / sizeof names[0]) 278 config = names[sc->sc_model]; 279 else 280 config = "??"; 281 strncpy(retp->config, config, sizeof(retp->config)); 282 283 return 0; 284 } 285