Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.52
      1 /*	$NetBSD: sb.c,v 1.52 1997/07/28 20:56:20 augustss 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/bus.h>
     48 #include <machine/pio.h>
     49 
     50 #include <sys/audioio.h>
     51 #include <dev/audio_if.h>
     52 
     53 #include <dev/isa/isavar.h>
     54 #include <dev/isa/isadmavar.h>
     55 
     56 #include <dev/isa/sbreg.h>
     57 #include <dev/isa/sbvar.h>
     58 #include <dev/isa/sbdspvar.h>
     59 
     60 struct cfdriver sb_cd = {
     61 	NULL, "sb", DV_DULL
     62 };
     63 
     64 struct audio_device sb_device = {
     65 	"SoundBlaster",
     66 	"x",
     67 	"sb"
     68 };
     69 
     70 int	sbopen __P((dev_t, int));
     71 int	sb_getdev __P((void *, struct audio_device *));
     72 
     73 /*
     74  * Define our interface to the higher level audio driver.
     75  */
     76 
     77 struct audio_hw_if sb_hw_if = {
     78 	sbopen,
     79 	sbdsp_close,
     80 	0,
     81 	sbdsp_query_encoding,
     82 	sbdsp_set_params,
     83 	sbdsp_round_blocksize,
     84 	sbdsp_set_out_port,
     85 	sbdsp_get_out_port,
     86 	sbdsp_set_in_port,
     87 	sbdsp_get_in_port,
     88 	0,
     89 	sbdsp_dma_init_output,
     90 	sbdsp_dma_init_input,
     91 	sbdsp_dma_output,
     92 	sbdsp_dma_input,
     93 	sbdsp_haltdma,
     94 	sbdsp_haltdma,
     95 	sbdsp_contdma,
     96 	sbdsp_contdma,
     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,
    106         sb_mappage,
    107 	AUDIO_PROP_MMAP
    108 };
    109 
    110 /*
    111  * Probe / attach routines.
    112  */
    113 
    114 
    115 int
    116 sbmatch(sc)
    117 	struct sbdsp_softc *sc;
    118 {
    119 	static u_char drq_conf[8] = {
    120 		0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
    121 	};
    122 
    123 	static u_char irq_conf[11] = {
    124 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    125 	};
    126 
    127 	if (sbdsp_probe(sc) == 0)
    128 		return 0;
    129 
    130 	/*
    131 	 * Cannot auto-discover DMA channel.
    132 	 */
    133 	if (ISSBPROCLASS(sc)) {
    134 		if (!SBP_DRQ_VALID(sc->sc_drq8)) {
    135 			printf("%s: configured dma chan %d invalid\n",
    136 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    137 			return 0;
    138 		}
    139 	} else {
    140 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
    141 			printf("%s: configured dma chan %d invalid\n",
    142 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    143 			return 0;
    144 		}
    145 	}
    146 
    147 	/*
    148 	 * XXX THIS IS WRONG!  My ViBRA16S PnP
    149 	 * XXX has 1 drqs, but this resets it to use drq8 everywhere!
    150 	 * XXX --thorpej (at) netbsd.org
    151 	 */
    152 	if (ISSB16CLASS(sc)) {
    153 		if (sc->sc_drq16 == -1)
    154 			sc->sc_drq16 = sc->sc_drq8;
    155 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
    156 			printf("%s: configured dma chan %d invalid\n",
    157 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    158 			return 0;
    159 		}
    160 	} else
    161 		sc->sc_drq16 = sc->sc_drq8;
    162 
    163 #ifdef NEWCONFIG
    164 	/*
    165 	 * If the IRQ wasn't compiled in, auto-detect it.
    166 	 */
    167 	if (sc->sc_irq == IRQUNK) {
    168 		sc->sc_irq = isa_discoverintr(sbforceintr, sc);
    169 		sbdsp_reset(sc);
    170 		if (ISSBPROCLASS(sc)) {
    171 			if (!SBP_IRQ_VALID(sc->sc_irq)) {
    172 				printf("%s: couldn't auto-detect interrupt\n",
    173 					sc->sc_dev.dv_xname);
    174 				return 0;
    175 			}
    176 		}
    177 		else {
    178 			if (!SB_IRQ_VALID(sc->sc_irq)) {
    179 				printf("%s: couldn't auto-detect interrupt\n");
    180 					sc->sc_dev.dv_xname);
    181 				return 0;
    182 			}
    183 		}
    184 	} else
    185 #endif
    186 	if (ISSBPROCLASS(sc)) {
    187 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
    188 			printf("%s: configured irq %d invalid\n",
    189 			    sc->sc_dev.dv_xname, sc->sc_irq);
    190 			return 0;
    191 		}
    192 	} else {
    193 		if (!SB_IRQ_VALID(sc->sc_irq)) {
    194 			printf("%s: configured irq %d invalid\n",
    195 			    sc->sc_dev.dv_xname, sc->sc_irq);
    196 			return 0;
    197 		}
    198 	}
    199 
    200 	if (ISSB16CLASS(sc)) {
    201 		int w, r;
    202 #if 0
    203 		printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
    204 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    205 		printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
    206 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    207 #endif
    208 		w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
    209 		sbdsp_mix_write(sc, SBP_SET_DRQ, w);
    210 		r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
    211 		if (r != w) {
    212 			printf("%s: setting drq mask %02x failed, got %02x\n",
    213 			    sc->sc_dev.dv_xname, w, r);
    214 			return 0;
    215 		}
    216 #if 0
    217 		printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
    218 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    219 #endif
    220 
    221 #if 0
    222 		printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
    223 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    224 		printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
    225 		    irq_conf[sc->sc_irq]);
    226 #endif
    227 		w = irq_conf[sc->sc_irq];
    228 		sbdsp_mix_write(sc, SBP_SET_IRQ, w);
    229 		r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
    230 		if (r != w) {
    231 			printf("%s: setting irq mask %02x failed, got %02x\n",
    232 			    sc->sc_dev.dv_xname, w, r);
    233 			return 0;
    234 		}
    235 #if 0
    236 		printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
    237 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    238 #endif
    239 	}
    240 
    241 	return 1;
    242 }
    243 
    244 
    245 void
    246 sbattach(sc)
    247 	struct sbdsp_softc *sc;
    248 {
    249 	int error;
    250 
    251 	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, IST_EDGE,
    252 	    IPL_AUDIO, sbdsp_intr, sc);
    253 
    254 	sbdsp_attach(sc);
    255 
    256 	if ((error = audio_hardware_attach(&sb_hw_if, sc)) != 0)
    257 		printf("%s: could not attach to audio device driver (%d)\n",
    258 		    sc->sc_dev.dv_xname, error);
    259 }
    260 
    261 #ifdef NEWCONFIG
    262 void
    263 sbforceintr(aux)
    264 	void *aux;
    265 {
    266 	static char dmabuf;
    267 	struct sbdsp_softc *sc = aux;
    268 
    269 	/*
    270 	 * Set up a DMA read of one byte.
    271 	 * XXX Note that at this point we haven't called
    272 	 * at_setup_dmachan().  This is okay because it just
    273 	 * allocates a buffer in case it needs to make a copy,
    274 	 * and it won't need to make a copy for a 1 byte buffer.
    275 	 * (I think that calling at_setup_dmachan() should be optional;
    276 	 * if you don't call it, it will be called the first time
    277 	 * it is needed (and you pay the latency).  Also, you might
    278 	 * never need the buffer anyway.)
    279 	 */
    280 	at_dma(DMAMODE_READ, &dmabuf, 1, sc->sc_drq8);
    281 	if (sbdsp_wdsp(sc, SB_DSP_RDMA) == 0) {
    282 		(void)sbdsp_wdsp(sc, 0);
    283 		(void)sbdsp_wdsp(sc, 0);
    284 	}
    285 }
    286 #endif
    287 
    288 
    289 /*
    290  * Various routines to interface to higher level audio driver
    291  */
    292 
    293 int
    294 sbopen(dev, flags)
    295     dev_t dev;
    296     int flags;
    297 {
    298     struct sbdsp_softc *sc;
    299     int unit = AUDIOUNIT(dev);
    300 
    301     if (unit >= sb_cd.cd_ndevs)
    302 	return ENODEV;
    303 
    304     sc = sb_cd.cd_devs[unit];
    305     if (!sc)
    306 	return ENXIO;
    307 
    308     return sbdsp_open(sc, dev, flags);
    309 }
    310 
    311 int
    312 sb_getdev(addr, retp)
    313 	void *addr;
    314 	struct audio_device *retp;
    315 {
    316 	struct sbdsp_softc *sc = addr;
    317 
    318 	if (sc->sc_model == SB_JAZZ)
    319 		strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
    320 	else
    321 		strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
    322 	sprintf(retp->version, "%d.%02d",
    323 		SBVER_MAJOR(sc->sc_version),
    324 		SBVER_MINOR(sc->sc_version));
    325 	strncpy(retp->config, "sb", sizeof(retp->config));
    326 
    327 	return 0;
    328 }
    329