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