1 /* $NetBSD: sb.c,v 1.81 2005/12/11 12:22:03 christos 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.81 2005/12/11 12:22:03 christos 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 const 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(void *, struct audio_device *); 78 79 /* 80 * Define our interface to the higher level audio driver. 81 */ 82 83 const 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(struct sbdsp_softc *sc) 120 { 121 static const u_char drq_conf[8] = { 122 0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80 123 }; 124 125 static const u_char irq_conf[11] = { 126 -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08 127 }; 128 129 if (sbdsp_probe(sc) == 0) 130 return 0; 131 132 /* 133 * Cannot auto-discover DMA channel. 134 */ 135 if (ISSBPROCLASS(sc)) { 136 if (!SBP_DRQ_VALID(sc->sc_drq8)) { 137 printf("%s: configured DMA chan %d invalid\n", 138 sc->sc_dev.dv_xname, sc->sc_drq8); 139 return 0; 140 } 141 } else { 142 if (!SB_DRQ_VALID(sc->sc_drq8)) { 143 printf("%s: configured DMA chan %d invalid\n", 144 sc->sc_dev.dv_xname, sc->sc_drq8); 145 return 0; 146 } 147 } 148 149 if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3) 150 /* 151 * XXX Some ViBRA16 cards seem to have two 8 bit DMA 152 * channels. I've no clue how to use them, so ignore 153 * one of them for now. -- augustss@NetBSD.org 154 */ 155 sc->sc_drq16 = -1; 156 157 if (ISSB16CLASS(sc)) { 158 if (sc->sc_drq16 == -1) 159 sc->sc_drq16 = sc->sc_drq8; 160 if (!SB16_DRQ_VALID(sc->sc_drq16)) { 161 printf("%s: configured DMA chan %d invalid\n", 162 sc->sc_dev.dv_xname, sc->sc_drq16); 163 return 0; 164 } 165 } else 166 sc->sc_drq16 = sc->sc_drq8; 167 168 if (ISSBPROCLASS(sc)) { 169 if (!SBP_IRQ_VALID(sc->sc_irq)) { 170 printf("%s: configured irq %d invalid\n", 171 sc->sc_dev.dv_xname, sc->sc_irq); 172 return 0; 173 } 174 } else { 175 if (!SB_IRQ_VALID(sc->sc_irq)) { 176 printf("%s: configured irq %d invalid\n", 177 sc->sc_dev.dv_xname, sc->sc_irq); 178 return 0; 179 } 180 } 181 182 if (ISSB16CLASS(sc) && !(sc->sc_quirks & SB_QUIRK_NO_INIT_DRQ)) { 183 int w, r; 184 #if 0 185 printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname, 186 sbdsp_mix_read(sc, SBP_SET_DRQ)); 187 printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname, 188 drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]); 189 #endif 190 w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]; 191 sbdsp_mix_write(sc, SBP_SET_DRQ, w); 192 r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb; 193 if (r != w) { 194 printf("%s: setting drq mask %02x failed, got %02x\n", 195 sc->sc_dev.dv_xname, w, r); 196 return 0; 197 } 198 #if 0 199 printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname, 200 sbdsp_mix_read(sc, SBP_SET_DRQ)); 201 #endif 202 203 #if 0 204 printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname, 205 sbdsp_mix_read(sc, SBP_SET_IRQ)); 206 printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname, 207 irq_conf[sc->sc_irq]); 208 #endif 209 w = irq_conf[sc->sc_irq]; 210 sbdsp_mix_write(sc, SBP_SET_IRQ, w); 211 r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f; 212 if (r != w) { 213 printf("%s: setting irq mask %02x failed, got %02x\n", 214 sc->sc_dev.dv_xname, w, r); 215 return 0; 216 } 217 #if 0 218 printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname, 219 sbdsp_mix_read(sc, SBP_SET_IRQ)); 220 #endif 221 } 222 223 return 1; 224 } 225 226 227 void 228 sbattach(struct sbdsp_softc *sc) 229 { 230 struct audio_attach_args arg; 231 232 sbdsp_attach(sc); 233 234 audio_attach_mi(&sb_hw_if, sc, &sc->sc_dev); 235 236 #if NMPU > 0 237 if (sc->sc_hasmpu) { 238 arg.type = AUDIODEV_TYPE_MPU; 239 arg.hwif = 0; 240 arg.hdl = 0; 241 sc->sc_mpudev = config_found(&sc->sc_dev, &arg, audioprint); 242 } else { 243 midi_attach_mi(&sb_midi_hw_if, sc, &sc->sc_dev); 244 } 245 #endif 246 247 if (sc->sc_model >= SB_20) { 248 arg.type = AUDIODEV_TYPE_OPL; 249 arg.hwif = 0; 250 arg.hdl = 0; 251 (void)config_found(&sc->sc_dev, &arg, audioprint); 252 } 253 } 254 255 /* 256 * Various routines to interface to higher level audio driver 257 */ 258 259 int 260 sb_getdev(void *addr, struct audio_device *retp) 261 { 262 static const char * const names[] = SB_NAMES; 263 struct sbdsp_softc *sc; 264 const char *config; 265 266 sc = addr; 267 if (sc->sc_model == SB_JAZZ) 268 strlcpy(retp->name, "MV Jazz16", sizeof(retp->name)); 269 else 270 strlcpy(retp->name, "SoundBlaster", sizeof(retp->name)); 271 snprintf(retp->version, sizeof(retp->version), "%d.%02d", 272 SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version)); 273 if (0 <= sc->sc_model && sc->sc_model < sizeof names / sizeof names[0]) 274 config = names[sc->sc_model]; 275 else 276 config = "??"; 277 strlcpy(retp->config, config, sizeof(retp->config)); 278 279 return 0; 280 } 281