1 /* $NetBSD: sb.c,v 1.38 1996/10/13 01:38:00 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/param.h> 38 #include <sys/systm.h> 39 #include <sys/errno.h> 40 #include <sys/ioctl.h> 41 #include <sys/syslog.h> 42 #include <sys/device.h> 43 #include <sys/proc.h> 44 45 #include <machine/cpu.h> 46 #include <machine/intr.h> 47 #include <machine/pio.h> 48 49 #include <sys/audioio.h> 50 #include <dev/audio_if.h> 51 #include <dev/mulaw.h> 52 53 #include <dev/isa/isavar.h> 54 #include <dev/isa/isadmavar.h> 55 56 #include <dev/isa/sbdspvar.h> 57 #include <dev/isa/sbreg.h> 58 59 #ifdef AUDIO_DEBUG 60 extern void Dprintf __P((const char *, ...)); 61 #define DPRINTF(x) if (sbdebug) Dprintf x 62 int sbdebug = 0; 63 #else 64 #define DPRINTF(x) 65 #endif 66 67 struct sb_softc { 68 struct device sc_dev; /* base device */ 69 struct isadev sc_id; /* ISA device */ 70 void *sc_ih; /* interrupt vectoring */ 71 72 struct sbdsp_softc sc_sbdsp; 73 }; 74 75 int sbprobe __P((struct device *, void *, void *)); 76 void sbattach __P((struct device *, struct device *, void *)); 77 78 struct cfattach sb_ca = { 79 sizeof(struct sbdsp_softc), sbprobe, sbattach 80 }; 81 82 struct cfdriver sb_cd = { 83 NULL, "sb", DV_DULL 84 }; 85 86 struct audio_device sb_device = { 87 "SoundBlaster", 88 "x", 89 "sb" 90 }; 91 92 int sbopen __P((dev_t, int)); 93 int sb_getdev __P((void *, struct audio_device *)); 94 95 /* 96 * Define our interface to the higher level audio driver. 97 */ 98 99 struct audio_hw_if sb_hw_if = { 100 sbopen, 101 sbdsp_close, 102 NULL, 103 sbdsp_set_in_sr, 104 sbdsp_get_in_sr, 105 sbdsp_set_out_sr, 106 sbdsp_get_out_sr, 107 sbdsp_query_encoding, 108 sbdsp_set_encoding, 109 sbdsp_get_encoding, 110 sbdsp_set_precision, 111 sbdsp_get_precision, 112 sbdsp_set_channels, 113 sbdsp_get_channels, 114 sbdsp_round_blocksize, 115 sbdsp_set_out_port, 116 sbdsp_get_out_port, 117 sbdsp_set_in_port, 118 sbdsp_get_in_port, 119 sbdsp_commit_settings, 120 sbdsp_get_silence, 121 mulaw_expand, 122 mulaw_compress, 123 sbdsp_dma_output, 124 sbdsp_dma_input, 125 sbdsp_haltdma, 126 sbdsp_haltdma, 127 sbdsp_contdma, 128 sbdsp_contdma, 129 sbdsp_speaker_ctl, 130 sb_getdev, 131 sbdsp_setfd, 132 sbdsp_mixer_set_port, 133 sbdsp_mixer_get_port, 134 sbdsp_mixer_query_devinfo, 135 0, /* not full-duplex */ 136 0 137 }; 138 139 /* 140 * Probe / attach routines. 141 */ 142 143 /* 144 * Probe for the soundblaster hardware. 145 */ 146 int 147 sbprobe(parent, match, aux) 148 struct device *parent; 149 void *match, *aux; 150 { 151 register struct sbdsp_softc *sc = match; 152 register struct isa_attach_args *ia = aux; 153 register int iobase = ia->ia_iobase; 154 static u_char drq_conf[4] = { 155 0x01, 0x02, -1, 0x08 156 }; 157 static u_char irq_conf[11] = { 158 -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08 159 }; 160 161 if (!SB_BASE_VALID(ia->ia_iobase)) { 162 printf("sb: configured iobase %d invalid\n", ia->ia_iobase); 163 return 0; 164 } 165 sc->sc_iobase = iobase; 166 sc->sc_irq = ia->ia_irq; 167 sc->sc_drq = ia->ia_drq; 168 169 if (sbdsp_probe(sc) == 0) { 170 DPRINTF(("sb: sbdsp probe failed\n")); 171 return 0; 172 } 173 174 /* 175 * Cannot auto-discover DMA channel. 176 */ 177 if (ISSBPROCLASS(sc)) { 178 if (!SBP_DRQ_VALID(ia->ia_drq)) { 179 printf("sb: configured dma chan %d invalid\n", ia->ia_drq); 180 return 0; 181 } 182 if (ISSB16CLASS(sc)) 183 sbdsp_mix_write(sc, SBP_SET_DRQ, drq_conf[ia->ia_drq]); 184 } 185 else { 186 if (!SB_DRQ_VALID(ia->ia_drq)) { 187 printf("sb: configured dma chan %d invalid\n", ia->ia_drq); 188 return 0; 189 } 190 } 191 192 #ifdef NEWCONFIG 193 /* 194 * If the IRQ wasn't compiled in, auto-detect it. 195 */ 196 if (ia->ia_irq == IRQUNK) { 197 ia->ia_irq = isa_discoverintr(sbforceintr, aux); 198 sbdsp_reset(sc); 199 if (ISSBPROCLASS(sc)) { 200 if (!SBP_IRQ_VALID(ia->ia_irq)) { 201 printf("sb: couldn't auto-detect interrupt"); 202 return 0; 203 } 204 } 205 else { 206 if (!SB_IRQ_VALID(ia->ia_irq)) { 207 printf("sb: couldn't auto-detect interrupt"); 208 return 0; 209 } 210 } 211 } else 212 #endif 213 if (ISSBPROCLASS(sc)) { 214 if (!SBP_IRQ_VALID(ia->ia_irq)) { 215 printf("sb: configured irq %d invalid\n", ia->ia_irq); 216 return 0; 217 } 218 if (ISSB16CLASS(sc)) 219 sbdsp_mix_write(sc, SBP_SET_IRQ, irq_conf[ia->ia_irq]); 220 } 221 else { 222 if (!SB_IRQ_VALID(ia->ia_irq)) { 223 printf("sb: configured irq %d invalid\n", ia->ia_irq); 224 return 0; 225 } 226 } 227 228 if (ISSBPROCLASS(sc)) 229 ia->ia_iosize = SBP_NPORT; 230 else 231 ia->ia_iosize = SB_NPORT; 232 return 1; 233 } 234 235 #ifdef NEWCONFIG 236 void 237 sbforceintr(aux) 238 void *aux; 239 { 240 static char dmabuf; 241 struct isa_attach_args *ia = aux; 242 int iobase = ia->ia_iobase; 243 244 /* 245 * Set up a DMA read of one byte. 246 * XXX Note that at this point we haven't called 247 * at_setup_dmachan(). This is okay because it just 248 * allocates a buffer in case it needs to make a copy, 249 * and it won't need to make a copy for a 1 byte buffer. 250 * (I think that calling at_setup_dmachan() should be optional; 251 * if you don't call it, it will be called the first time 252 * it is needed (and you pay the latency). Also, you might 253 * never need the buffer anyway.) 254 */ 255 at_dma(DMAMODE_READ, &dmabuf, 1, ia->ia_drq); 256 if (sbdsp_wdsp(iobase, SB_DSP_RDMA) == 0) { 257 (void)sbdsp_wdsp(iobase, 0); 258 (void)sbdsp_wdsp(iobase, 0); 259 } 260 } 261 #endif 262 263 /* 264 * Attach hardware to driver, attach hardware driver to audio 265 * pseudo-device driver . 266 */ 267 void 268 sbattach(parent, self, aux) 269 struct device *parent, *self; 270 void *aux; 271 { 272 register struct sbdsp_softc *sc = (struct sbdsp_softc *)self; 273 struct isa_attach_args *ia = (struct isa_attach_args *)aux; 274 int err; 275 276 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, 277 IPL_AUDIO, sbdsp_intr, sc); 278 279 sbdsp_attach(sc); 280 281 if ((err = audio_hardware_attach(&sb_hw_if, sc)) != 0) 282 printf("sb: could not attach to audio pseudo-device driver (%d)\n", err); 283 } 284 285 /* 286 * Various routines to interface to higher level audio driver 287 */ 288 289 int 290 sbopen(dev, flags) 291 dev_t dev; 292 int flags; 293 { 294 struct sbdsp_softc *sc; 295 int unit = AUDIOUNIT(dev); 296 297 if (unit >= sb_cd.cd_ndevs) 298 return ENODEV; 299 300 sc = sb_cd.cd_devs[unit]; 301 if (!sc) 302 return ENXIO; 303 304 return sbdsp_open(sc, dev, flags); 305 } 306 307 int 308 sb_getdev(addr, retp) 309 void *addr; 310 struct audio_device *retp; 311 { 312 struct sbdsp_softc *sc = addr; 313 314 if (sc->sc_model & MODEL_JAZZ16) 315 strncpy(retp->name, "MV Jazz16", sizeof(retp->name)); 316 else 317 strncpy(retp->name, "SoundBlaster", sizeof(retp->name)); 318 sprintf(retp->version, "%d.%d", 319 SBVER_MAJOR(sc->sc_model), 320 SBVER_MINOR(sc->sc_model)); 321 strncpy(retp->config, "sb", sizeof(retp->config)); 322 323 return 0; 324 } 325