Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.24
      1 /*	$NetBSD: sb.c,v 1.24 1995/04/17 15:48:32 cgd 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  *	$Id: sb.c,v 1.24 1995/04/17 15:48:32 cgd Exp $
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/errno.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/syslog.h>
     43 #include <sys/device.h>
     44 #include <sys/proc.h>
     45 
     46 #include <machine/cpu.h>
     47 #include <machine/pio.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 #include <i386/isa/icu.h>			/* XXX BROKEN; WHY? */
     55 
     56 #include <dev/isa/sbdspvar.h>
     57 #include <dev/isa/sbreg.h>
     58 
     59 #define DEBUG	/*XXX*/
     60 #ifdef DEBUG
     61 #define DPRINTF(x)	if (sbdebug) printf x
     62 int	sbdebug = 0;
     63 #else
     64 #define DPRINTF(x)
     65 #endif
     66 
     67 struct sb_softc {
     68 	struct	device sc_dev;		/* base device */
     69 	struct	isadev sc_id;		/* ISA device */
     70 	void	*sc_ih;			/* interrupt vectoring */
     71 
     72 	struct	sbdsp_softc sc_sbdsp;
     73 };
     74 
     75 int	sbprobe();
     76 void	sbattach __P((struct device *, struct device *, void *));
     77 
     78 struct cfdriver sbcd = {
     79 	NULL, "sb", sbprobe, sbattach, DV_DULL, sizeof(struct sb_softc)
     80 };
     81 
     82 struct audio_device sb_device = {
     83 	"SoundBlaster",
     84 	"x",
     85 	"sb"
     86 };
     87 
     88 int	sbopen __P((dev_t, int));
     89 
     90 int	sbprobe();
     91 void	sbattach();
     92 
     93 int	sb_getdev __P((void *, struct audio_device *));
     94 
     95 /*
     96  * Define our interface to the higher level audio driver.
     97  */
     98 
     99 struct audio_hw_if sb_hw_if = {
    100 	sbopen,
    101 	sbdsp_close,
    102 	NULL,
    103 	sbdsp_set_in_sr,
    104 	sbdsp_get_in_sr,
    105 	sbdsp_set_out_sr,
    106 	sbdsp_get_out_sr,
    107 	sbdsp_query_encoding,
    108 	sbdsp_set_encoding,
    109 	sbdsp_get_encoding,
    110 	sbdsp_set_precision,
    111 	sbdsp_get_precision,
    112 	sbdsp_set_channels,
    113 	sbdsp_get_channels,
    114 	sbdsp_round_blocksize,
    115 	sbdsp_set_out_port,
    116 	sbdsp_get_out_port,
    117 	sbdsp_set_in_port,
    118 	sbdsp_get_in_port,
    119 	sbdsp_commit_settings,
    120 	sbdsp_get_silence,
    121 	sbdsp_expand,
    122 	sbdsp_compress,
    123 	sbdsp_dma_output,
    124 	sbdsp_dma_input,
    125 	sbdsp_haltdma,
    126 	sbdsp_haltdma,
    127 	sbdsp_contdma,
    128 	sbdsp_contdma,
    129 	sbdsp_speaker_ctl,
    130 	sb_getdev,
    131 	sbdsp_setfd,
    132 	sbdsp_mixer_set_port,
    133 	sbdsp_mixer_get_port,
    134 	sbdsp_mixer_query_devinfo,
    135 	0,	/* not full-duplex */
    136 	0
    137 };
    138 
    139 /*
    140  * Probe / attach routines.
    141  */
    142 
    143 /*
    144  * Probe for the soundblaster hardware.
    145  */
    146 int
    147 sbprobe(parent, self, aux)
    148 	struct device *parent, *self;
    149 	void *aux;
    150 {
    151 	register struct sb_softc *sc = (void *)self;
    152 	register struct isa_attach_args *ia = aux;
    153 	register u_short iobase = ia->ia_iobase;
    154 	static u_char irq_conf[11] = {
    155 	    -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    156 	};
    157 
    158 	if (!SB_BASE_VALID(ia->ia_iobase)) {
    159 		printf("sb: configured iobase %d invalid\n", ia->ia_iobase);
    160 		return 0;
    161 	}
    162 	sc->sc_sbdsp.sc_iobase = iobase;
    163 	if (sbdsp_probe(&sc->sc_sbdsp) == 0) {
    164 		DPRINTF(("sb: sbdsp probe failed\n"));
    165 		return 0;
    166 	}
    167 
    168 	/*
    169 	 * Cannot auto-discover DMA channel.
    170 	 */
    171 	if (ISSBPROCLASS(&sc->sc_sbdsp)) {
    172 		if (!SBP_DRQ_VALID(ia->ia_drq)) {
    173 			printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
    174 			return 0;
    175 		}
    176 		if (ISSB16CLASS(&sc->sc_sbdsp)) {
    177 			sbdsp_mix_write(&sc->sc_sbdsp, SBP_SET_DRQ,
    178 					1 << ia->ia_drq);
    179 		}
    180 	}
    181 	else {
    182 		if (!SB_DRQ_VALID(ia->ia_drq)) {
    183 			printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
    184 			return 0;
    185 		}
    186 	}
    187 
    188 #ifdef NEWCONFIG
    189 	/*
    190 	 * If the IRQ wasn't compiled in, auto-detect it.
    191 	 */
    192 	if (ia->ia_irq == IRQUNK) {
    193 		ia->ia_irq = isa_discoverintr(sbforceintr, aux);
    194 		sbdsp_reset(&sc->sc_sbdsp);
    195 		if (ISSBPROCLASS(&sc->sc_sbdsp)) {
    196 			if (!SBP_IRQ_VALID(ia->ia_irq)) {
    197 				printf("sb: couldn't auto-detect interrupt");
    198 				return 0;
    199 			}
    200 		}
    201 		else {
    202 			if (!SB_IRQ_VALID(ia->ia_irq)) {
    203 				printf("sb: couldn't auto-detect interrupt");
    204 				return 0;
    205 			}
    206 		}
    207 	} else
    208 #endif
    209 	if (ISSBPROCLASS(&sc->sc_sbdsp)) {
    210 		if (!SBP_IRQ_VALID(ia->ia_irq)) {
    211 			printf("sb: configured irq %d invalid\n", ia->ia_irq);
    212 			return 0;
    213 		}
    214 		if (ISSB16CLASS(&sc->sc_sbdsp)) {
    215 			sbdsp_mix_write(&sc->sc_sbdsp, SBP_SET_IRQ,
    216 					irq_conf[ia->ia_irq]);
    217 		}
    218 	}
    219 	else {
    220 		if (!SB_IRQ_VALID(ia->ia_irq)) {
    221 			printf("sb: configured irq %d invalid\n", ia->ia_irq);
    222 			return 0;
    223 		}
    224 	}
    225 
    226 	sc->sc_sbdsp.sc_irq = ia->ia_irq;
    227 	sc->sc_sbdsp.sc_drq = ia->ia_drq;
    228 
    229 	if (ISSBPROCLASS(&sc->sc_sbdsp))
    230 		ia->ia_iosize = SBP_NPORT;
    231 	else
    232 		ia->ia_iosize = SB_NPORT;
    233 	return 1;
    234 }
    235 
    236 #ifdef NEWCONFIG
    237 void
    238 sbforceintr(aux)
    239 	void *aux;
    240 {
    241 	static char dmabuf;
    242 	struct isa_attach_args *ia = aux;
    243 	u_short iobase = ia->ia_iobase;
    244 
    245 	/*
    246 	 * Set up a DMA read of one byte.
    247 	 * XXX Note that at this point we haven't called
    248 	 * at_setup_dmachan().  This is okay because it just
    249 	 * allocates a buffer in case it needs to make a copy,
    250 	 * and it won't need to make a copy for a 1 byte buffer.
    251 	 * (I think that calling at_setup_dmachan() should be optional;
    252 	 * if you don't call it, it will be called the first time
    253 	 * it is needed (and you pay the latency).  Also, you might
    254 	 * never need the buffer anyway.)
    255 	 */
    256 	at_dma(B_READ, &dmabuf, 1, ia->ia_drq);
    257 	if (sbdsp_wdsp(iobase, SB_DSP_RDMA) == 0) {
    258 		(void)sbdsp_wdsp(iobase, 0);
    259 		(void)sbdsp_wdsp(iobase, 0);
    260 	}
    261 }
    262 #endif
    263 
    264 /*
    265  * Attach hardware to driver, attach hardware driver to audio
    266  * pseudo-device driver .
    267  */
    268 void
    269 sbattach(parent, self, aux)
    270 	struct device *parent, *self;
    271 	void *aux;
    272 {
    273 	register struct sb_softc *sc = (struct sb_softc *)self;
    274 	struct isa_attach_args *ia = (struct isa_attach_args *)aux;
    275 	register u_short iobase = ia->ia_iobase;
    276 
    277 #ifdef NEWCONFIG
    278 	isa_establish(&sc->sc_id, &sc->sc_dev);
    279 #endif
    280 	sc->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE, ISA_IPL_BIO,
    281 	    sbdsp_intr, &sc->sc_sbdsp);
    282 
    283 	sbdsp_attach(&sc->sc_sbdsp);
    284 
    285 	sprintf(sb_device.version, "%d.%d",
    286 		SBVER_MAJOR(sc->sc_sbdsp.sc_model),
    287 		SBVER_MINOR(sc->sc_sbdsp.sc_model));
    288 
    289 	if (audio_hardware_attach(&sb_hw_if, &sc->sc_sbdsp) != 0)
    290 		printf("sb: could not attach to audio pseudo-device driver\n");
    291 }
    292 
    293 /*
    294  * Various routines to interface to higher level audio driver
    295  */
    296 
    297 int
    298 sbopen(dev, flags)
    299     dev_t dev;
    300     int flags;
    301 {
    302     struct sb_softc *sc;
    303     int unit = AUDIOUNIT(dev);
    304 
    305     if (unit >= sbcd.cd_ndevs)
    306 	return ENODEV;
    307 
    308     sc = sbcd.cd_devs[unit];
    309     if (!sc)
    310 	return ENXIO;
    311 
    312     return sbdsp_open(&sc->sc_sbdsp, dev, flags);
    313 }
    314 
    315 int
    316 sb_getdev(addr, retp)
    317 	void *addr;
    318 	struct audio_device *retp;
    319 {
    320 	*retp = sb_device;
    321 	return 0;
    322 }
    323