Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.27
      1 /*	$NetBSD: sb.c,v 1.27 1995/07/19 19:58:53 brezak 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/pio.h>
     47 
     48 #include <sys/audioio.h>
     49 #include <dev/audio_if.h>
     50 #include <dev/mulaw.h>
     51 
     52 #include <dev/isa/isavar.h>
     53 #include <dev/isa/isadmavar.h>
     54 
     55 #include <dev/isa/sbdspvar.h>
     56 #include <dev/isa/sbreg.h>
     57 
     58 #ifdef AUDIO_DEBUG
     59 extern void Dprintf __P((const char *, ...));
     60 #define DPRINTF(x)	if (sbdebug) Dprintf x
     61 int	sbdebug = 0;
     62 #else
     63 #define DPRINTF(x)
     64 #endif
     65 
     66 struct sb_softc {
     67 	struct	device sc_dev;		/* base device */
     68 	struct	isadev sc_id;		/* ISA device */
     69 	void	*sc_ih;			/* interrupt vectoring */
     70 
     71 	struct	sbdsp_softc sc_sbdsp;
     72 };
     73 
     74 int	sbprobe();
     75 void	sbattach __P((struct device *, struct device *, void *));
     76 
     77 struct cfdriver sbcd = {
     78 	NULL, "sb", sbprobe, sbattach, DV_DULL, sizeof(struct sbdsp_softc)
     79 };
     80 
     81 struct audio_device sb_device = {
     82 	"SoundBlaster",
     83 	"x",
     84 	"sb"
     85 };
     86 
     87 int	sbopen __P((dev_t, int));
     88 
     89 int	sbprobe();
     90 void	sbattach();
     91 
     92 int	sb_getdev __P((void *, struct audio_device *));
     93 
     94 /*
     95  * Define our interface to the higher level audio driver.
     96  */
     97 
     98 struct audio_hw_if sb_hw_if = {
     99 	sbopen,
    100 	sbdsp_close,
    101 	NULL,
    102 	sbdsp_set_in_sr,
    103 	sbdsp_get_in_sr,
    104 	sbdsp_set_out_sr,
    105 	sbdsp_get_out_sr,
    106 	sbdsp_query_encoding,
    107 	sbdsp_set_encoding,
    108 	sbdsp_get_encoding,
    109 	sbdsp_set_precision,
    110 	sbdsp_get_precision,
    111 	sbdsp_set_channels,
    112 	sbdsp_get_channels,
    113 	sbdsp_round_blocksize,
    114 	sbdsp_set_out_port,
    115 	sbdsp_get_out_port,
    116 	sbdsp_set_in_port,
    117 	sbdsp_get_in_port,
    118 	sbdsp_commit_settings,
    119 	sbdsp_get_silence,
    120 	mulaw_expand,
    121 	mulaw_compress,
    122 	sbdsp_dma_output,
    123 	sbdsp_dma_input,
    124 	sbdsp_haltdma,
    125 	sbdsp_haltdma,
    126 	sbdsp_contdma,
    127 	sbdsp_contdma,
    128 	sbdsp_speaker_ctl,
    129 	sb_getdev,
    130 	sbdsp_setfd,
    131 	sbdsp_mixer_set_port,
    132 	sbdsp_mixer_get_port,
    133 	sbdsp_mixer_query_devinfo,
    134 	0,	/* not full-duplex */
    135 	0
    136 };
    137 
    138 /*
    139  * Probe / attach routines.
    140  */
    141 
    142 /*
    143  * Probe for the soundblaster hardware.
    144  */
    145 int
    146 sbprobe(parent, self, aux)
    147 	struct device *parent, *self;
    148 	void *aux;
    149 {
    150 	register struct sbdsp_softc *sc = (void *)self;
    151 	register struct isa_attach_args *ia = aux;
    152 	register u_short iobase = ia->ia_iobase;
    153 	static u_char irq_conf[11] = {
    154 	    -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    155 	};
    156 
    157 	if (!SB_BASE_VALID(ia->ia_iobase)) {
    158 		printf("sb: configured iobase %d invalid\n", ia->ia_iobase);
    159 		return 0;
    160 	}
    161 	sc->sc_iobase = iobase;
    162 	if (sbdsp_probe(sc) == 0) {
    163 		DPRINTF(("sb: sbdsp probe failed\n"));
    164 		return 0;
    165 	}
    166 
    167 	/*
    168 	 * Cannot auto-discover DMA channel.
    169 	 */
    170 	if (ISSBPROCLASS(sc)) {
    171 		if (!SBP_DRQ_VALID(ia->ia_drq)) {
    172 			printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
    173 			return 0;
    174 		}
    175 		if (ISSB16CLASS(sc)) {
    176 			sbdsp_mix_write(sc, SBP_SET_DRQ,
    177 					1 << ia->ia_drq);
    178 		}
    179 	}
    180 	else {
    181 		if (!SB_DRQ_VALID(ia->ia_drq)) {
    182 			printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
    183 			return 0;
    184 		}
    185 	}
    186 
    187 #ifdef NEWCONFIG
    188 	/*
    189 	 * If the IRQ wasn't compiled in, auto-detect it.
    190 	 */
    191 	if (ia->ia_irq == IRQUNK) {
    192 		ia->ia_irq = isa_discoverintr(sbforceintr, aux);
    193 		sbdsp_reset(sc);
    194 		if (ISSBPROCLASS(sc)) {
    195 			if (!SBP_IRQ_VALID(ia->ia_irq)) {
    196 				printf("sb: couldn't auto-detect interrupt");
    197 				return 0;
    198 			}
    199 		}
    200 		else {
    201 			if (!SB_IRQ_VALID(ia->ia_irq)) {
    202 				printf("sb: couldn't auto-detect interrupt");
    203 				return 0;
    204 			}
    205 		}
    206 	} else
    207 #endif
    208 	if (ISSBPROCLASS(sc)) {
    209 		if (!SBP_IRQ_VALID(ia->ia_irq)) {
    210 			printf("sb: configured irq %d invalid\n", ia->ia_irq);
    211 			return 0;
    212 		}
    213 		if (ISSB16CLASS(sc)) {
    214 			sbdsp_mix_write(sc, SBP_SET_IRQ,
    215 					irq_conf[ia->ia_irq]);
    216 		}
    217 	}
    218 	else {
    219 		if (!SB_IRQ_VALID(ia->ia_irq)) {
    220 			printf("sb: configured irq %d invalid\n", ia->ia_irq);
    221 			return 0;
    222 		}
    223 	}
    224 
    225 	sc->sc_irq = ia->ia_irq;
    226 	sc->sc_drq = ia->ia_drq;
    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 	u_short 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(B_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 	register u_short iobase = ia->ia_iobase;
    275 	int err;
    276 
    277 	sc->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE, ISA_IPL_AUDIO,
    278 				       sbdsp_intr, sc);
    279 
    280 	sbdsp_attach(sc);
    281 
    282 	sprintf(sb_device.version, "%d.%d",
    283 		SBVER_MAJOR(sc->sc_model),
    284 		SBVER_MINOR(sc->sc_model));
    285 
    286 	if ((err = audio_hardware_attach(&sb_hw_if, sc)) != 0)
    287 		printf("sb: could not attach to audio pseudo-device driver (%d)\n", err);
    288 }
    289 
    290 /*
    291  * Various routines to interface to higher level audio driver
    292  */
    293 
    294 int
    295 sbopen(dev, flags)
    296     dev_t dev;
    297     int flags;
    298 {
    299     struct sbdsp_softc *sc;
    300     int unit = AUDIOUNIT(dev);
    301 
    302     if (unit >= sbcd.cd_ndevs)
    303 	return ENODEV;
    304 
    305     sc = sbcd.cd_devs[unit];
    306     if (!sc)
    307 	return ENXIO;
    308 
    309     return sbdsp_open(sc, dev, flags);
    310 }
    311 
    312 int
    313 sb_getdev(addr, retp)
    314 	void *addr;
    315 	struct audio_device *retp;
    316 {
    317 	*retp = sb_device;
    318 	return 0;
    319 }
    320