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