Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.30
      1  1.30  mycroft /*	$NetBSD: sb.c,v 1.30 1996/02/16 08:18:32 mycroft Exp $	*/
      2  1.10      cgd 
      3   1.1      cgd /*
      4   1.1      cgd  * Copyright (c) 1991-1993 Regents of the University of California.
      5   1.1      cgd  * All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the Computer Systems
     18   1.1      cgd  *	Engineering Group at Lawrence Berkeley Laboratory.
     19   1.1      cgd  * 4. Neither the name of the University nor of the Laboratory may be used
     20   1.1      cgd  *    to endorse or promote products derived from this software without
     21   1.1      cgd  *    specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34  1.17   brezak  *
     35   1.1      cgd  */
     36   1.1      cgd 
     37   1.1      cgd #include <sys/param.h>
     38   1.1      cgd #include <sys/systm.h>
     39   1.1      cgd #include <sys/errno.h>
     40   1.1      cgd #include <sys/ioctl.h>
     41   1.1      cgd #include <sys/syslog.h>
     42   1.6  mycroft #include <sys/device.h>
     43  1.17   brezak #include <sys/proc.h>
     44   1.1      cgd 
     45   1.1      cgd #include <machine/cpu.h>
     46   1.1      cgd #include <machine/pio.h>
     47   1.1      cgd 
     48  1.17   brezak #include <sys/audioio.h>
     49  1.17   brezak #include <dev/audio_if.h>
     50  1.27   brezak #include <dev/mulaw.h>
     51  1.17   brezak 
     52  1.23      cgd #include <dev/isa/isavar.h>
     53  1.23      cgd #include <dev/isa/isadmavar.h>
     54   1.1      cgd 
     55  1.24      cgd #include <dev/isa/sbdspvar.h>
     56  1.24      cgd #include <dev/isa/sbreg.h>
     57  1.17   brezak 
     58  1.26   brezak #ifdef AUDIO_DEBUG
     59  1.26   brezak extern void Dprintf __P((const char *, ...));
     60  1.26   brezak #define DPRINTF(x)	if (sbdebug) Dprintf x
     61  1.17   brezak int	sbdebug = 0;
     62  1.17   brezak #else
     63  1.17   brezak #define DPRINTF(x)
     64  1.17   brezak #endif
     65   1.1      cgd 
     66   1.1      cgd struct sb_softc {
     67  1.17   brezak 	struct	device sc_dev;		/* base device */
     68  1.17   brezak 	struct	isadev sc_id;		/* ISA device */
     69  1.23      cgd 	void	*sc_ih;			/* interrupt vectoring */
     70  1.17   brezak 
     71  1.23      cgd 	struct	sbdsp_softc sc_sbdsp;
     72   1.1      cgd };
     73   1.1      cgd 
     74  1.30  mycroft int	sbprobe __P((struct device *, void *, void *));
     75  1.17   brezak void	sbattach __P((struct device *, struct device *, void *));
     76   1.1      cgd 
     77  1.17   brezak struct cfdriver sbcd = {
     78  1.26   brezak 	NULL, "sb", sbprobe, sbattach, DV_DULL, sizeof(struct sbdsp_softc)
     79  1.17   brezak };
     80   1.1      cgd 
     81  1.17   brezak struct audio_device sb_device = {
     82  1.17   brezak 	"SoundBlaster",
     83  1.17   brezak 	"x",
     84  1.17   brezak 	"sb"
     85  1.17   brezak };
     86   1.1      cgd 
     87  1.17   brezak int	sbopen __P((dev_t, int));
     88  1.22  mycroft int	sb_getdev __P((void *, struct audio_device *));
     89   1.1      cgd 
     90  1.17   brezak /*
     91  1.17   brezak  * Define our interface to the higher level audio driver.
     92  1.17   brezak  */
     93  1.17   brezak 
     94  1.17   brezak struct audio_hw_if sb_hw_if = {
     95  1.17   brezak 	sbopen,
     96  1.17   brezak 	sbdsp_close,
     97  1.17   brezak 	NULL,
     98  1.17   brezak 	sbdsp_set_in_sr,
     99  1.17   brezak 	sbdsp_get_in_sr,
    100  1.17   brezak 	sbdsp_set_out_sr,
    101  1.17   brezak 	sbdsp_get_out_sr,
    102  1.17   brezak 	sbdsp_query_encoding,
    103  1.17   brezak 	sbdsp_set_encoding,
    104  1.17   brezak 	sbdsp_get_encoding,
    105  1.17   brezak 	sbdsp_set_precision,
    106  1.17   brezak 	sbdsp_get_precision,
    107  1.17   brezak 	sbdsp_set_channels,
    108  1.17   brezak 	sbdsp_get_channels,
    109  1.17   brezak 	sbdsp_round_blocksize,
    110  1.17   brezak 	sbdsp_set_out_port,
    111  1.17   brezak 	sbdsp_get_out_port,
    112  1.17   brezak 	sbdsp_set_in_port,
    113  1.17   brezak 	sbdsp_get_in_port,
    114  1.17   brezak 	sbdsp_commit_settings,
    115  1.17   brezak 	sbdsp_get_silence,
    116  1.27   brezak 	mulaw_expand,
    117  1.27   brezak 	mulaw_compress,
    118  1.17   brezak 	sbdsp_dma_output,
    119  1.17   brezak 	sbdsp_dma_input,
    120  1.17   brezak 	sbdsp_haltdma,
    121  1.17   brezak 	sbdsp_haltdma,
    122  1.17   brezak 	sbdsp_contdma,
    123  1.17   brezak 	sbdsp_contdma,
    124  1.17   brezak 	sbdsp_speaker_ctl,
    125  1.17   brezak 	sb_getdev,
    126  1.17   brezak 	sbdsp_setfd,
    127  1.17   brezak 	sbdsp_mixer_set_port,
    128  1.17   brezak 	sbdsp_mixer_get_port,
    129  1.17   brezak 	sbdsp_mixer_query_devinfo,
    130  1.17   brezak 	0,	/* not full-duplex */
    131  1.17   brezak 	0
    132   1.6  mycroft };
    133   1.1      cgd 
    134  1.17   brezak /*
    135  1.17   brezak  * Probe / attach routines.
    136  1.17   brezak  */
    137  1.17   brezak 
    138  1.17   brezak /*
    139  1.17   brezak  * Probe for the soundblaster hardware.
    140  1.17   brezak  */
    141   1.1      cgd int
    142  1.30  mycroft sbprobe(parent, match, aux)
    143  1.30  mycroft 	struct device *parent;
    144  1.30  mycroft 	void *match, *aux;
    145   1.1      cgd {
    146  1.30  mycroft 	register struct sbdsp_softc *sc = match;
    147   1.6  mycroft 	register struct isa_attach_args *ia = aux;
    148  1.28  mycroft 	register int iobase = ia->ia_iobase;
    149  1.28  mycroft 	static u_char drq_conf[4] = {
    150  1.28  mycroft 		0x01, 0x02, -1, 0x08
    151  1.28  mycroft 	};
    152  1.19   brezak 	static u_char irq_conf[11] = {
    153  1.28  mycroft 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    154  1.19   brezak 	};
    155   1.1      cgd 
    156   1.6  mycroft 	if (!SB_BASE_VALID(ia->ia_iobase)) {
    157   1.6  mycroft 		printf("sb: configured iobase %d invalid\n", ia->ia_iobase);
    158   1.6  mycroft 		return 0;
    159   1.1      cgd 	}
    160  1.26   brezak 	sc->sc_iobase = iobase;
    161  1.26   brezak 	if (sbdsp_probe(sc) == 0) {
    162  1.17   brezak 		DPRINTF(("sb: sbdsp probe failed\n"));
    163   1.6  mycroft 		return 0;
    164   1.1      cgd 	}
    165  1.17   brezak 
    166   1.1      cgd 	/*
    167   1.1      cgd 	 * Cannot auto-discover DMA channel.
    168   1.1      cgd 	 */
    169  1.26   brezak 	if (ISSBPROCLASS(sc)) {
    170  1.17   brezak 		if (!SBP_DRQ_VALID(ia->ia_drq)) {
    171  1.17   brezak 			printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
    172  1.17   brezak 			return 0;
    173  1.17   brezak 		}
    174  1.28  mycroft 		if (ISSB16CLASS(sc))
    175  1.28  mycroft 			sbdsp_mix_write(sc, SBP_SET_DRQ, drq_conf[ia->ia_drq]);
    176  1.17   brezak 	}
    177  1.17   brezak 	else {
    178  1.17   brezak 		if (!SB_DRQ_VALID(ia->ia_drq)) {
    179  1.17   brezak 			printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
    180  1.17   brezak 			return 0;
    181  1.17   brezak 		}
    182   1.1      cgd 	}
    183  1.17   brezak 
    184   1.6  mycroft #ifdef NEWCONFIG
    185   1.1      cgd 	/*
    186   1.1      cgd 	 * If the IRQ wasn't compiled in, auto-detect it.
    187   1.1      cgd 	 */
    188   1.1      cgd 	if (ia->ia_irq == IRQUNK) {
    189   1.1      cgd 		ia->ia_irq = isa_discoverintr(sbforceintr, aux);
    190  1.26   brezak 		sbdsp_reset(sc);
    191  1.26   brezak 		if (ISSBPROCLASS(sc)) {
    192  1.17   brezak 			if (!SBP_IRQ_VALID(ia->ia_irq)) {
    193  1.17   brezak 				printf("sb: couldn't auto-detect interrupt");
    194  1.17   brezak 				return 0;
    195  1.17   brezak 			}
    196  1.17   brezak 		}
    197  1.17   brezak 		else {
    198  1.17   brezak 			if (!SB_IRQ_VALID(ia->ia_irq)) {
    199  1.17   brezak 				printf("sb: couldn't auto-detect interrupt");
    200  1.17   brezak 				return 0;
    201  1.17   brezak 			}
    202  1.17   brezak 		}
    203  1.17   brezak 	} else
    204  1.17   brezak #endif
    205  1.26   brezak 	if (ISSBPROCLASS(sc)) {
    206  1.17   brezak 		if (!SBP_IRQ_VALID(ia->ia_irq)) {
    207  1.18   brezak 			printf("sb: configured irq %d invalid\n", ia->ia_irq);
    208  1.17   brezak 			return 0;
    209  1.19   brezak 		}
    210  1.28  mycroft 		if (ISSB16CLASS(sc))
    211  1.28  mycroft 			sbdsp_mix_write(sc, SBP_SET_IRQ, irq_conf[ia->ia_irq]);
    212  1.17   brezak 	}
    213  1.17   brezak 	else {
    214   1.1      cgd 		if (!SB_IRQ_VALID(ia->ia_irq)) {
    215  1.18   brezak 			printf("sb: configured irq %d invalid\n", ia->ia_irq);
    216   1.6  mycroft 			return 0;
    217   1.1      cgd 		}
    218   1.1      cgd 	}
    219  1.17   brezak 
    220  1.26   brezak 	sc->sc_irq = ia->ia_irq;
    221  1.26   brezak 	sc->sc_drq = ia->ia_drq;
    222  1.17   brezak 
    223  1.26   brezak 	if (ISSBPROCLASS(sc))
    224  1.17   brezak 		ia->ia_iosize = SBP_NPORT;
    225  1.17   brezak 	else
    226  1.17   brezak 		ia->ia_iosize = SB_NPORT;
    227   1.6  mycroft 	return 1;
    228   1.1      cgd }
    229   1.1      cgd 
    230   1.6  mycroft #ifdef NEWCONFIG
    231   1.1      cgd void
    232   1.6  mycroft sbforceintr(aux)
    233   1.6  mycroft 	void *aux;
    234   1.1      cgd {
    235   1.1      cgd 	static char dmabuf;
    236   1.6  mycroft 	struct isa_attach_args *ia = aux;
    237  1.28  mycroft 	int iobase = ia->ia_iobase;
    238   1.6  mycroft 
    239   1.1      cgd 	/*
    240   1.1      cgd 	 * Set up a DMA read of one byte.
    241   1.1      cgd 	 * XXX Note that at this point we haven't called
    242   1.1      cgd 	 * at_setup_dmachan().  This is okay because it just
    243   1.1      cgd 	 * allocates a buffer in case it needs to make a copy,
    244   1.1      cgd 	 * and it won't need to make a copy for a 1 byte buffer.
    245   1.1      cgd 	 * (I think that calling at_setup_dmachan() should be optional;
    246   1.1      cgd 	 * if you don't call it, it will be called the first time
    247   1.1      cgd 	 * it is needed (and you pay the latency).  Also, you might
    248   1.1      cgd 	 * never need the buffer anyway.)
    249   1.1      cgd 	 */
    250  1.15  mycroft 	at_dma(B_READ, &dmabuf, 1, ia->ia_drq);
    251  1.17   brezak 	if (sbdsp_wdsp(iobase, SB_DSP_RDMA) == 0) {
    252  1.17   brezak 		(void)sbdsp_wdsp(iobase, 0);
    253  1.17   brezak 		(void)sbdsp_wdsp(iobase, 0);
    254   1.1      cgd 	}
    255   1.1      cgd }
    256   1.6  mycroft #endif
    257   1.1      cgd 
    258  1.17   brezak /*
    259  1.17   brezak  * Attach hardware to driver, attach hardware driver to audio
    260  1.17   brezak  * pseudo-device driver .
    261  1.17   brezak  */
    262   1.1      cgd void
    263   1.1      cgd sbattach(parent, self, aux)
    264   1.1      cgd 	struct device *parent, *self;
    265   1.1      cgd 	void *aux;
    266   1.1      cgd {
    267  1.26   brezak 	register struct sbdsp_softc *sc = (struct sbdsp_softc *)self;
    268   1.1      cgd 	struct isa_attach_args *ia = (struct isa_attach_args *)aux;
    269  1.28  mycroft 	register int iobase = ia->ia_iobase;
    270  1.26   brezak 	int err;
    271  1.26   brezak 
    272  1.29  mycroft 	sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_AUDIO,
    273  1.26   brezak 				       sbdsp_intr, sc);
    274   1.6  mycroft 
    275  1.26   brezak 	sbdsp_attach(sc);
    276  1.20   brezak 
    277  1.20   brezak 	sprintf(sb_device.version, "%d.%d",
    278  1.26   brezak 		SBVER_MAJOR(sc->sc_model),
    279  1.26   brezak 		SBVER_MINOR(sc->sc_model));
    280   1.1      cgd 
    281  1.26   brezak 	if ((err = audio_hardware_attach(&sb_hw_if, sc)) != 0)
    282  1.26   brezak 		printf("sb: could not attach to audio pseudo-device driver (%d)\n", err);
    283   1.1      cgd }
    284   1.1      cgd 
    285   1.1      cgd /*
    286  1.17   brezak  * Various routines to interface to higher level audio driver
    287   1.1      cgd  */
    288   1.1      cgd 
    289   1.1      cgd int
    290  1.17   brezak sbopen(dev, flags)
    291  1.17   brezak     dev_t dev;
    292  1.17   brezak     int flags;
    293   1.1      cgd {
    294  1.26   brezak     struct sbdsp_softc *sc;
    295  1.17   brezak     int unit = AUDIOUNIT(dev);
    296  1.17   brezak 
    297  1.17   brezak     if (unit >= sbcd.cd_ndevs)
    298  1.17   brezak 	return ENODEV;
    299  1.17   brezak 
    300  1.17   brezak     sc = sbcd.cd_devs[unit];
    301  1.17   brezak     if (!sc)
    302  1.17   brezak 	return ENXIO;
    303  1.17   brezak 
    304  1.26   brezak     return sbdsp_open(sc, dev, flags);
    305   1.1      cgd }
    306   1.1      cgd 
    307   1.1      cgd int
    308  1.17   brezak sb_getdev(addr, retp)
    309  1.22  mycroft 	void *addr;
    310  1.17   brezak 	struct audio_device *retp;
    311   1.1      cgd {
    312  1.17   brezak 	*retp = sb_device;
    313   1.6  mycroft 	return 0;
    314   1.1      cgd }
    315