Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.52
      1  1.52  augustss /*	$NetBSD: sb.c,v 1.52 1997/07/28 20:56:20 augustss 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.36   mycroft #include <machine/intr.h>
     47  1.49   thorpej #include <machine/bus.h>
     48   1.1       cgd #include <machine/pio.h>
     49   1.1       cgd 
     50  1.17    brezak #include <sys/audioio.h>
     51  1.17    brezak #include <dev/audio_if.h>
     52  1.17    brezak 
     53  1.23       cgd #include <dev/isa/isavar.h>
     54  1.23       cgd #include <dev/isa/isadmavar.h>
     55   1.1       cgd 
     56  1.39  christos #include <dev/isa/sbreg.h>
     57  1.39  christos #include <dev/isa/sbvar.h>
     58  1.24       cgd #include <dev/isa/sbdspvar.h>
     59   1.1       cgd 
     60  1.33   thorpej struct cfdriver sb_cd = {
     61  1.33   thorpej 	NULL, "sb", DV_DULL
     62  1.33   thorpej };
     63  1.33   thorpej 
     64  1.33   thorpej struct audio_device sb_device = {
     65  1.33   thorpej 	"SoundBlaster",
     66  1.33   thorpej 	"x",
     67  1.33   thorpej 	"sb"
     68  1.17    brezak };
     69   1.1       cgd 
     70  1.17    brezak int	sbopen __P((dev_t, int));
     71  1.22   mycroft int	sb_getdev __P((void *, struct audio_device *));
     72   1.1       cgd 
     73  1.17    brezak /*
     74  1.17    brezak  * Define our interface to the higher level audio driver.
     75  1.17    brezak  */
     76  1.17    brezak 
     77  1.17    brezak struct audio_hw_if sb_hw_if = {
     78  1.17    brezak 	sbopen,
     79  1.17    brezak 	sbdsp_close,
     80  1.51  augustss 	0,
     81  1.17    brezak 	sbdsp_query_encoding,
     82  1.46  augustss 	sbdsp_set_params,
     83  1.17    brezak 	sbdsp_round_blocksize,
     84  1.17    brezak 	sbdsp_set_out_port,
     85  1.17    brezak 	sbdsp_get_out_port,
     86  1.17    brezak 	sbdsp_set_in_port,
     87  1.17    brezak 	sbdsp_get_in_port,
     88  1.51  augustss 	0,
     89  1.50  augustss 	sbdsp_dma_init_output,
     90  1.50  augustss 	sbdsp_dma_init_input,
     91  1.17    brezak 	sbdsp_dma_output,
     92  1.17    brezak 	sbdsp_dma_input,
     93  1.17    brezak 	sbdsp_haltdma,
     94  1.17    brezak 	sbdsp_haltdma,
     95  1.17    brezak 	sbdsp_contdma,
     96  1.17    brezak 	sbdsp_contdma,
     97  1.17    brezak 	sbdsp_speaker_ctl,
     98  1.17    brezak 	sb_getdev,
     99  1.51  augustss 	0,
    100  1.17    brezak 	sbdsp_mixer_set_port,
    101  1.17    brezak 	sbdsp_mixer_get_port,
    102  1.17    brezak 	sbdsp_mixer_query_devinfo,
    103  1.50  augustss 	sb_malloc,
    104  1.50  augustss 	sb_free,
    105  1.50  augustss 	sb_round,
    106  1.52  augustss         sb_mappage,
    107  1.52  augustss 	AUDIO_PROP_MMAP
    108   1.6   mycroft };
    109   1.1       cgd 
    110  1.17    brezak /*
    111  1.17    brezak  * Probe / attach routines.
    112  1.17    brezak  */
    113  1.17    brezak 
    114  1.39  christos 
    115   1.1       cgd int
    116  1.39  christos sbmatch(sc)
    117  1.39  christos 	struct sbdsp_softc *sc;
    118   1.1       cgd {
    119  1.43   mycroft 	static u_char drq_conf[8] = {
    120  1.43   mycroft 		0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
    121  1.28   mycroft 	};
    122  1.39  christos 
    123  1.19    brezak 	static u_char irq_conf[11] = {
    124  1.28   mycroft 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    125  1.19    brezak 	};
    126   1.1       cgd 
    127  1.44   mycroft 	if (sbdsp_probe(sc) == 0)
    128   1.6   mycroft 		return 0;
    129  1.43   mycroft 
    130   1.1       cgd 	/*
    131   1.1       cgd 	 * Cannot auto-discover DMA channel.
    132   1.1       cgd 	 */
    133  1.26    brezak 	if (ISSBPROCLASS(sc)) {
    134  1.43   mycroft 		if (!SBP_DRQ_VALID(sc->sc_drq8)) {
    135  1.43   mycroft 			printf("%s: configured dma chan %d invalid\n",
    136  1.43   mycroft 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    137  1.43   mycroft 			return 0;
    138  1.43   mycroft 		}
    139  1.43   mycroft 	} else {
    140  1.43   mycroft 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
    141  1.39  christos 			printf("%s: configured dma chan %d invalid\n",
    142  1.43   mycroft 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    143  1.17    brezak 			return 0;
    144  1.17    brezak 		}
    145  1.17    brezak 	}
    146  1.49   thorpej 
    147  1.49   thorpej 	/*
    148  1.49   thorpej 	 * XXX THIS IS WRONG!  My ViBRA16S PnP
    149  1.49   thorpej 	 * XXX has 1 drqs, but this resets it to use drq8 everywhere!
    150  1.49   thorpej 	 * XXX --thorpej (at) netbsd.org
    151  1.49   thorpej 	 */
    152  1.43   mycroft 	if (ISSB16CLASS(sc)) {
    153  1.43   mycroft 		if (sc->sc_drq16 == -1)
    154  1.43   mycroft 			sc->sc_drq16 = sc->sc_drq8;
    155  1.43   mycroft 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
    156  1.39  christos 			printf("%s: configured dma chan %d invalid\n",
    157  1.43   mycroft 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    158  1.17    brezak 			return 0;
    159  1.17    brezak 		}
    160  1.43   mycroft 	} else
    161  1.43   mycroft 		sc->sc_drq16 = sc->sc_drq8;
    162  1.17    brezak 
    163   1.6   mycroft #ifdef NEWCONFIG
    164   1.1       cgd 	/*
    165   1.1       cgd 	 * If the IRQ wasn't compiled in, auto-detect it.
    166   1.1       cgd 	 */
    167  1.39  christos 	if (sc->sc_irq == IRQUNK) {
    168  1.39  christos 		sc->sc_irq = isa_discoverintr(sbforceintr, sc);
    169  1.26    brezak 		sbdsp_reset(sc);
    170  1.26    brezak 		if (ISSBPROCLASS(sc)) {
    171  1.39  christos 			if (!SBP_IRQ_VALID(sc->sc_irq)) {
    172  1.39  christos 				printf("%s: couldn't auto-detect interrupt\n",
    173  1.39  christos 					sc->sc_dev.dv_xname);
    174  1.17    brezak 				return 0;
    175  1.17    brezak 			}
    176  1.17    brezak 		}
    177  1.17    brezak 		else {
    178  1.39  christos 			if (!SB_IRQ_VALID(sc->sc_irq)) {
    179  1.39  christos 				printf("%s: couldn't auto-detect interrupt\n");
    180  1.39  christos 					sc->sc_dev.dv_xname);
    181  1.17    brezak 				return 0;
    182  1.17    brezak 			}
    183  1.17    brezak 		}
    184  1.17    brezak 	} else
    185  1.17    brezak #endif
    186  1.26    brezak 	if (ISSBPROCLASS(sc)) {
    187  1.39  christos 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
    188  1.39  christos 			printf("%s: configured irq %d invalid\n",
    189  1.39  christos 			    sc->sc_dev.dv_xname, sc->sc_irq);
    190  1.17    brezak 			return 0;
    191  1.19    brezak 		}
    192  1.43   mycroft 	} else {
    193  1.39  christos 		if (!SB_IRQ_VALID(sc->sc_irq)) {
    194  1.39  christos 			printf("%s: configured irq %d invalid\n",
    195  1.39  christos 			    sc->sc_dev.dv_xname, sc->sc_irq);
    196   1.6   mycroft 			return 0;
    197   1.1       cgd 		}
    198   1.1       cgd 	}
    199  1.43   mycroft 
    200  1.43   mycroft 	if (ISSB16CLASS(sc)) {
    201  1.47  augustss 		int w, r;
    202  1.43   mycroft #if 0
    203  1.43   mycroft 		printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
    204  1.43   mycroft 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    205  1.43   mycroft 		printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
    206  1.43   mycroft 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    207  1.43   mycroft #endif
    208  1.47  augustss 		w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
    209  1.47  augustss 		sbdsp_mix_write(sc, SBP_SET_DRQ, w);
    210  1.47  augustss 		r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
    211  1.47  augustss 		if (r != w) {
    212  1.47  augustss 			printf("%s: setting drq mask %02x failed, got %02x\n",
    213  1.47  augustss 			    sc->sc_dev.dv_xname, w, r);
    214  1.47  augustss 			return 0;
    215  1.47  augustss 		}
    216  1.43   mycroft #if 0
    217  1.43   mycroft 		printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
    218  1.43   mycroft 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    219  1.43   mycroft #endif
    220  1.43   mycroft 
    221  1.43   mycroft #if 0
    222  1.43   mycroft 		printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
    223  1.43   mycroft 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    224  1.43   mycroft 		printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
    225  1.43   mycroft 		    irq_conf[sc->sc_irq]);
    226  1.43   mycroft #endif
    227  1.47  augustss 		w = irq_conf[sc->sc_irq];
    228  1.47  augustss 		sbdsp_mix_write(sc, SBP_SET_IRQ, w);
    229  1.47  augustss 		r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
    230  1.47  augustss 		if (r != w) {
    231  1.47  augustss 			printf("%s: setting irq mask %02x failed, got %02x\n",
    232  1.47  augustss 			    sc->sc_dev.dv_xname, w, r);
    233  1.47  augustss 			return 0;
    234  1.47  augustss 		}
    235  1.43   mycroft #if 0
    236  1.43   mycroft 		printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
    237  1.43   mycroft 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    238  1.43   mycroft #endif
    239  1.43   mycroft 	}
    240  1.43   mycroft 
    241  1.39  christos 	return 1;
    242  1.39  christos }
    243  1.39  christos 
    244  1.39  christos 
    245  1.39  christos void
    246  1.39  christos sbattach(sc)
    247  1.39  christos 	struct sbdsp_softc *sc;
    248  1.39  christos {
    249  1.39  christos 	int error;
    250  1.17    brezak 
    251  1.39  christos 	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, IST_EDGE,
    252  1.39  christos 	    IPL_AUDIO, sbdsp_intr, sc);
    253  1.39  christos 
    254  1.39  christos 	sbdsp_attach(sc);
    255  1.39  christos 
    256  1.39  christos 	if ((error = audio_hardware_attach(&sb_hw_if, sc)) != 0)
    257  1.39  christos 		printf("%s: could not attach to audio device driver (%d)\n",
    258  1.39  christos 		    sc->sc_dev.dv_xname, error);
    259   1.1       cgd }
    260   1.1       cgd 
    261   1.6   mycroft #ifdef NEWCONFIG
    262   1.1       cgd void
    263   1.6   mycroft sbforceintr(aux)
    264   1.6   mycroft 	void *aux;
    265   1.1       cgd {
    266   1.1       cgd 	static char dmabuf;
    267  1.39  christos 	struct sbdsp_softc *sc = aux;
    268   1.6   mycroft 
    269   1.1       cgd 	/*
    270   1.1       cgd 	 * Set up a DMA read of one byte.
    271   1.1       cgd 	 * XXX Note that at this point we haven't called
    272   1.1       cgd 	 * at_setup_dmachan().  This is okay because it just
    273   1.1       cgd 	 * allocates a buffer in case it needs to make a copy,
    274   1.1       cgd 	 * and it won't need to make a copy for a 1 byte buffer.
    275   1.1       cgd 	 * (I think that calling at_setup_dmachan() should be optional;
    276   1.1       cgd 	 * if you don't call it, it will be called the first time
    277   1.1       cgd 	 * it is needed (and you pay the latency).  Also, you might
    278   1.1       cgd 	 * never need the buffer anyway.)
    279   1.1       cgd 	 */
    280  1.43   mycroft 	at_dma(DMAMODE_READ, &dmabuf, 1, sc->sc_drq8);
    281  1.39  christos 	if (sbdsp_wdsp(sc, SB_DSP_RDMA) == 0) {
    282  1.39  christos 		(void)sbdsp_wdsp(sc, 0);
    283  1.39  christos 		(void)sbdsp_wdsp(sc, 0);
    284   1.1       cgd 	}
    285   1.1       cgd }
    286   1.6   mycroft #endif
    287   1.1       cgd 
    288   1.1       cgd 
    289   1.1       cgd /*
    290  1.17    brezak  * Various routines to interface to higher level audio driver
    291   1.1       cgd  */
    292   1.1       cgd 
    293   1.1       cgd int
    294  1.17    brezak sbopen(dev, flags)
    295  1.17    brezak     dev_t dev;
    296  1.17    brezak     int flags;
    297   1.1       cgd {
    298  1.26    brezak     struct sbdsp_softc *sc;
    299  1.17    brezak     int unit = AUDIOUNIT(dev);
    300  1.17    brezak 
    301  1.33   thorpej     if (unit >= sb_cd.cd_ndevs)
    302  1.17    brezak 	return ENODEV;
    303  1.17    brezak 
    304  1.33   thorpej     sc = sb_cd.cd_devs[unit];
    305  1.17    brezak     if (!sc)
    306  1.17    brezak 	return ENXIO;
    307  1.17    brezak 
    308  1.26    brezak     return sbdsp_open(sc, dev, flags);
    309   1.1       cgd }
    310   1.1       cgd 
    311   1.1       cgd int
    312  1.17    brezak sb_getdev(addr, retp)
    313  1.22   mycroft 	void *addr;
    314  1.17    brezak 	struct audio_device *retp;
    315   1.1       cgd {
    316  1.32       jtk 	struct sbdsp_softc *sc = addr;
    317  1.32       jtk 
    318  1.48  augustss 	if (sc->sc_model == SB_JAZZ)
    319  1.32       jtk 		strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
    320  1.32       jtk 	else
    321  1.32       jtk 		strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
    322  1.40     mikel 	sprintf(retp->version, "%d.%02d",
    323  1.48  augustss 		SBVER_MAJOR(sc->sc_version),
    324  1.48  augustss 		SBVER_MINOR(sc->sc_version));
    325  1.32       jtk 	strncpy(retp->config, "sb", sizeof(retp->config));
    326  1.32       jtk 
    327   1.6   mycroft 	return 0;
    328   1.1       cgd }
    329