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