Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.46.2.1
      1 /*	$NetBSD: sb.c,v 1.46.2.1 1997/05/13 03:37:19 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 #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 	NULL,
     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 	sbdsp_commit_settings,
     89 	sbdsp_dma_output,
     90 	sbdsp_dma_input,
     91 	sbdsp_haltdma,
     92 	sbdsp_haltdma,
     93 	sbdsp_contdma,
     94 	sbdsp_contdma,
     95 	sbdsp_speaker_ctl,
     96 	sb_getdev,
     97 	sbdsp_setfd,
     98 	sbdsp_mixer_set_port,
     99 	sbdsp_mixer_get_port,
    100 	sbdsp_mixer_query_devinfo,
    101 	0,	/* not full-duplex */
    102 	0
    103 };
    104 
    105 /*
    106  * Probe / attach routines.
    107  */
    108 
    109 
    110 int
    111 sbmatch(sc)
    112 	struct sbdsp_softc *sc;
    113 {
    114 	static u_char drq_conf[8] = {
    115 		0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
    116 	};
    117 
    118 	static u_char irq_conf[11] = {
    119 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    120 	};
    121 
    122 	if (sbdsp_probe(sc) == 0)
    123 		return 0;
    124 
    125 	/*
    126 	 * Cannot auto-discover DMA channel.
    127 	 */
    128 	if (ISSBPROCLASS(sc)) {
    129 		if (!SBP_DRQ_VALID(sc->sc_drq8)) {
    130 			printf("%s: configured dma chan %d invalid\n",
    131 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    132 			return 0;
    133 		}
    134 	} else {
    135 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
    136 			printf("%s: configured dma chan %d invalid\n",
    137 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    138 			return 0;
    139 		}
    140 	}
    141 
    142 	/*
    143 	 * XXX THIS IS WRONG!  My ViBRA16S PnP
    144 	 * XXX has 1 drqs, but this resets it to use drq8 everywhere!
    145 	 * XXX --thorpej (at) netbsd.org
    146 	 */
    147 	if (ISSB16CLASS(sc)) {
    148 		if (sc->sc_drq16 == -1)
    149 			sc->sc_drq16 = sc->sc_drq8;
    150 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
    151 			printf("%s: configured dma chan %d invalid\n",
    152 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    153 			return 0;
    154 		}
    155 	} else
    156 		sc->sc_drq16 = sc->sc_drq8;
    157 
    158 #ifdef NEWCONFIG
    159 	/*
    160 	 * If the IRQ wasn't compiled in, auto-detect it.
    161 	 */
    162 	if (sc->sc_irq == IRQUNK) {
    163 		sc->sc_irq = isa_discoverintr(sbforceintr, sc);
    164 		sbdsp_reset(sc);
    165 		if (ISSBPROCLASS(sc)) {
    166 			if (!SBP_IRQ_VALID(sc->sc_irq)) {
    167 				printf("%s: couldn't auto-detect interrupt\n",
    168 					sc->sc_dev.dv_xname);
    169 				return 0;
    170 			}
    171 		}
    172 		else {
    173 			if (!SB_IRQ_VALID(sc->sc_irq)) {
    174 				printf("%s: couldn't auto-detect interrupt\n");
    175 					sc->sc_dev.dv_xname);
    176 				return 0;
    177 			}
    178 		}
    179 	} else
    180 #endif
    181 	if (ISSBPROCLASS(sc)) {
    182 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
    183 			printf("%s: configured irq %d invalid\n",
    184 			    sc->sc_dev.dv_xname, sc->sc_irq);
    185 			return 0;
    186 		}
    187 	} else {
    188 		if (!SB_IRQ_VALID(sc->sc_irq)) {
    189 			printf("%s: configured irq %d invalid\n",
    190 			    sc->sc_dev.dv_xname, sc->sc_irq);
    191 			return 0;
    192 		}
    193 	}
    194 
    195 	if (ISSB16CLASS(sc)) {
    196 #if 0
    197 		printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
    198 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    199 		printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
    200 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    201 #endif
    202 		sbdsp_mix_write(sc, SBP_SET_DRQ,
    203 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    204 #if 0
    205 		printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
    206 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    207 #endif
    208 
    209 #if 0
    210 		printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
    211 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    212 		printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
    213 		    irq_conf[sc->sc_irq]);
    214 #endif
    215 		sbdsp_mix_write(sc, SBP_SET_IRQ,
    216 		    irq_conf[sc->sc_irq]);
    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(sc)
    229 	struct sbdsp_softc *sc;
    230 {
    231 	int error;
    232 
    233 	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, IST_EDGE,
    234 	    IPL_AUDIO, sbdsp_intr, sc);
    235 
    236 	sbdsp_attach(sc);
    237 
    238 	if ((error = audio_hardware_attach(&sb_hw_if, sc)) != 0)
    239 		printf("%s: could not attach to audio device driver (%d)\n",
    240 		    sc->sc_dev.dv_xname, error);
    241 }
    242 
    243 #ifdef NEWCONFIG
    244 void
    245 sbforceintr(aux)
    246 	void *aux;
    247 {
    248 	static char dmabuf;
    249 	struct sbdsp_softc *sc = aux;
    250 
    251 	/*
    252 	 * Set up a DMA read of one byte.
    253 	 * XXX Note that at this point we haven't called
    254 	 * at_setup_dmachan().  This is okay because it just
    255 	 * allocates a buffer in case it needs to make a copy,
    256 	 * and it won't need to make a copy for a 1 byte buffer.
    257 	 * (I think that calling at_setup_dmachan() should be optional;
    258 	 * if you don't call it, it will be called the first time
    259 	 * it is needed (and you pay the latency).  Also, you might
    260 	 * never need the buffer anyway.)
    261 	 */
    262 	at_dma(DMAMODE_READ, &dmabuf, 1, sc->sc_drq8);
    263 	if (sbdsp_wdsp(sc, SB_DSP_RDMA) == 0) {
    264 		(void)sbdsp_wdsp(sc, 0);
    265 		(void)sbdsp_wdsp(sc, 0);
    266 	}
    267 }
    268 #endif
    269 
    270 
    271 /*
    272  * Various routines to interface to higher level audio driver
    273  */
    274 
    275 int
    276 sbopen(dev, flags)
    277     dev_t dev;
    278     int flags;
    279 {
    280     struct sbdsp_softc *sc;
    281     int unit = AUDIOUNIT(dev);
    282 
    283     if (unit >= sb_cd.cd_ndevs)
    284 	return ENODEV;
    285 
    286     sc = sb_cd.cd_devs[unit];
    287     if (!sc)
    288 	return ENXIO;
    289 
    290     return sbdsp_open(sc, dev, flags);
    291 }
    292 
    293 int
    294 sb_getdev(addr, retp)
    295 	void *addr;
    296 	struct audio_device *retp;
    297 {
    298 	struct sbdsp_softc *sc = addr;
    299 
    300 	if (sc->sc_model & MODEL_JAZZ16)
    301 		strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
    302 	else
    303 		strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
    304 	sprintf(retp->version, "%d.%02d",
    305 		SBVER_MAJOR(sc->sc_model),
    306 		SBVER_MINOR(sc->sc_model));
    307 	strncpy(retp->config, "sb", sizeof(retp->config));
    308 
    309 	return 0;
    310 }
    311