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