1 /* $OpenBSD: sb.c,v 1.29 2016/09/19 06:46:44 ratchov Exp $ */ 2 /* $NetBSD: sb.c,v 1.57 1998/01/12 09:43:46 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1991-1993 Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the Computer Systems 19 * Engineering Group at Lawrence Berkeley Laboratory. 20 * 4. Neither the name of the University nor of the Laboratory may be used 21 * to endorse or promote products derived from this software without 22 * specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 */ 37 38 #include "midi.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/errno.h> 43 #include <sys/ioctl.h> 44 #include <sys/syslog.h> 45 #include <sys/device.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/isa/isavar.h> 56 #include <dev/isa/isadmavar.h> 57 58 #include <dev/isa/sbreg.h> 59 #include <dev/isa/sbvar.h> 60 #include <dev/isa/sbdspvar.h> 61 62 struct cfdriver sb_cd = { 63 NULL, "sb", DV_DULL 64 }; 65 66 #if NMIDI > 0 67 int sb_mpu401_open(void *, int, void (*iintr)(void *, int), 68 void (*ointr)(void *), void *arg); 69 void sb_mpu401_close(void *); 70 int sb_mpu401_output(void *, int); 71 void sb_mpu401_getinfo(void *, struct midi_info *); 72 73 struct midi_hw_if sb_midi_hw_if = { 74 sbdsp_midi_open, 75 sbdsp_midi_close, 76 sbdsp_midi_output, 77 0, /* flush */ 78 sbdsp_midi_getinfo, 79 0, /* ioctl */ 80 }; 81 82 struct midi_hw_if sb_mpu401_hw_if = { 83 sb_mpu401_open, 84 sb_mpu401_close, 85 sb_mpu401_output, 86 0, /* flush */ 87 sb_mpu401_getinfo, 88 0, /* ioctl */ 89 }; 90 #endif 91 92 /* 93 * Define our interface to the higher level audio driver. 94 */ 95 96 struct audio_hw_if sb_hw_if = { 97 sbdsp_open, 98 sbdsp_close, 99 sbdsp_set_params, 100 sbdsp_round_blocksize, 101 0, 102 0, 103 0, 104 0, 105 0, 106 sbdsp_haltdma, 107 sbdsp_haltdma, 108 sbdsp_speaker_ctl, 109 0, 110 sbdsp_mixer_set_port, 111 sbdsp_mixer_get_port, 112 sbdsp_mixer_query_devinfo, 113 sb_malloc, 114 sb_free, 115 sb_round, 116 sbdsp_get_props, 117 sbdsp_trigger_output, 118 sbdsp_trigger_input 119 }; 120 121 #ifdef AUDIO_DEBUG 122 #define DPRINTF(x) if (sbdebug) printf x 123 int sbdebug = 0; 124 #else 125 #define DPRINTF(x) 126 #endif 127 128 /* 129 * Probe / attach routines. 130 */ 131 132 133 int 134 sbmatch(sc) 135 struct sbdsp_softc *sc; 136 { 137 static u_char drq_conf[8] = { 138 0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80 139 }; 140 141 static u_char irq_conf[11] = { 142 -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08 143 }; 144 145 if (sbdsp_probe(sc) == 0) 146 return 0; 147 148 /* 149 * Cannot auto-discover DMA channel. 150 */ 151 if (ISSBPROCLASS(sc)) { 152 if (!SBP_DRQ_VALID(sc->sc_drq8)) { 153 DPRINTF(("%s: configured dma chan %d invalid\n", 154 sc->sc_dev.dv_xname, sc->sc_drq8)); 155 return 0; 156 } 157 } else { 158 if (!SB_DRQ_VALID(sc->sc_drq8)) { 159 DPRINTF(("%s: configured dma chan %d invalid\n", 160 sc->sc_dev.dv_xname, sc->sc_drq8)); 161 return 0; 162 } 163 } 164 165 if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3) 166 /* 167 * XXX Some ViBRA16 cards seem to have two 8 bit DMA 168 * channels. I've no clue how to use them, so ignore 169 * one of them for now. -- augustss@netbsd.org 170 */ 171 sc->sc_drq16 = -1; 172 173 if (ISSB16CLASS(sc)) { 174 if (sc->sc_drq16 == -1) 175 sc->sc_drq16 = sc->sc_drq8; 176 if (!SB16_DRQ_VALID(sc->sc_drq16)) { 177 DPRINTF(("%s: configured dma chan %d invalid\n", 178 sc->sc_dev.dv_xname, sc->sc_drq16)); 179 return 0; 180 } 181 } else 182 sc->sc_drq16 = sc->sc_drq8; 183 184 if (ISSBPROCLASS(sc)) { 185 if (!SBP_IRQ_VALID(sc->sc_irq)) { 186 DPRINTF(("%s: configured irq %d invalid\n", 187 sc->sc_dev.dv_xname, sc->sc_irq)); 188 return 0; 189 } 190 } else { 191 if (!SB_IRQ_VALID(sc->sc_irq)) { 192 DPRINTF(("%s: configured irq %d invalid\n", 193 sc->sc_dev.dv_xname, sc->sc_irq)); 194 return 0; 195 } 196 } 197 198 if (ISSB16CLASS(sc)) { 199 int w, r; 200 #if 0 201 DPRINTF(("%s: old drq conf %02x\n", sc->sc_dev.dv_xname, 202 sbdsp_mix_read(sc, SBP_SET_DRQ))); 203 DPRINTF(("%s: try drq conf %02x\n", sc->sc_dev.dv_xname, 204 drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8])); 205 #endif 206 w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]; 207 sbdsp_mix_write(sc, SBP_SET_DRQ, w); 208 r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb; 209 if (r != w) { 210 DPRINTF(("%s: setting drq mask %02x failed, got %02x\n", sc->sc_dev.dv_xname, w, r)); 211 return 0; 212 } 213 #if 0 214 DPRINTF(("%s: new drq conf %02x\n", sc->sc_dev.dv_xname, 215 sbdsp_mix_read(sc, SBP_SET_DRQ))); 216 #endif 217 218 #if 0 219 DPRINTF(("%s: old irq conf %02x\n", sc->sc_dev.dv_xname, 220 sbdsp_mix_read(sc, SBP_SET_IRQ))); 221 DPRINTF(("%s: try irq conf %02x\n", sc->sc_dev.dv_xname, 222 irq_conf[sc->sc_irq])); 223 #endif 224 w = irq_conf[sc->sc_irq]; 225 sbdsp_mix_write(sc, SBP_SET_IRQ, w); 226 r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f; 227 if (r != w) { 228 DPRINTF(("%s: setting irq mask %02x failed, got %02x\n", 229 sc->sc_dev.dv_xname, w, r)); 230 return 0; 231 } 232 #if 0 233 DPRINTF(("%s: new irq conf %02x\n", sc->sc_dev.dv_xname, 234 sbdsp_mix_read(sc, SBP_SET_IRQ))); 235 #endif 236 } 237 238 return 1; 239 } 240 241 242 void 243 sbattach(sc) 244 struct sbdsp_softc *sc; 245 { 246 struct audio_attach_args arg; 247 #if NMIDI > 0 248 struct midi_hw_if *mhw = &sb_midi_hw_if; 249 #endif 250 251 sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, 252 IST_EDGE, IPL_AUDIO | IPL_MPSAFE, 253 sbdsp_intr, sc, sc->sc_dev.dv_xname); 254 255 sbdsp_attach(sc); 256 257 #if NMIDI > 0 258 sc->sc_hasmpu = 0; 259 if (ISSB16CLASS(sc) && sc->sc_mpu_sc.iobase != 0) { 260 sc->sc_mpu_sc.iot = sc->sc_iot; 261 if (mpu_find(&sc->sc_mpu_sc)) { 262 sc->sc_hasmpu = 1; 263 mhw = &sb_mpu401_hw_if; 264 } 265 } 266 midi_attach_mi(mhw, sc, &sc->sc_dev); 267 #endif 268 269 audio_attach_mi(&sb_hw_if, sc, &sc->sc_dev); 270 271 arg.type = AUDIODEV_TYPE_OPL; 272 arg.hwif = 0; 273 arg.hdl = 0; 274 (void)config_found(&sc->sc_dev, &arg, audioprint); 275 } 276 277 /* 278 * Various routines to interface to higher level audio driver 279 */ 280 281 #if NMIDI > 0 282 283 #define SBMPU(a) (&((struct sbdsp_softc *)addr)->sc_mpu_sc) 284 285 int 286 sb_mpu401_open(addr, flags, iintr, ointr, arg) 287 void *addr; 288 int flags; 289 void (*iintr)(void *, int); 290 void (*ointr)(void *); 291 void *arg; 292 { 293 return mpu_open(SBMPU(addr), flags, iintr, ointr, arg); 294 } 295 296 int 297 sb_mpu401_output(addr, d) 298 void *addr; 299 int d; 300 { 301 return mpu_output(SBMPU(addr), d); 302 } 303 304 void 305 sb_mpu401_close(addr) 306 void *addr; 307 { 308 mpu_close(SBMPU(addr)); 309 } 310 311 void 312 sb_mpu401_getinfo(addr, mi) 313 void *addr; 314 struct midi_info *mi; 315 { 316 mi->name = "SB MPU-401 UART"; 317 mi->props = 0; 318 } 319 #endif 320