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