sb.c revision 1.61 1 /* $NetBSD: sb.c,v 1.61 1998/08/10 00:20:39 mycroft 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 "midi.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/ioctl.h>
43 #include <sys/syslog.h>
44 #include <sys/device.h>
45 #include <sys/proc.h>
46
47 #include <machine/cpu.h>
48 #include <machine/intr.h>
49 #include <machine/bus.h>
50
51 #include <sys/audioio.h>
52 #include <dev/audio_if.h>
53
54 #include <dev/isa/isavar.h>
55 #include <dev/isa/isadmavar.h>
56
57 #include <dev/isa/sbreg.h>
58 #include <dev/isa/sbvar.h>
59 #include <dev/isa/sbdspvar.h>
60
61 #if NMIDI > 0
62 int sb_mpu401_open __P((void *, int,
63 void (*iintr)__P((void *, int)),
64 void (*ointr)__P((void *)), void *arg));
65 void sb_mpu401_close __P((void *));
66 int sb_mpu401_output __P((void *, int));
67 void sb_mpu401_getinfo __P((void *, struct midi_info *));
68
69 struct midi_hw_if sb_midi_hw_if = {
70 sbdsp_midi_open,
71 sbdsp_midi_close,
72 sbdsp_midi_output,
73 sbdsp_midi_getinfo,
74 0, /* ioctl */
75 };
76
77 struct midi_hw_if sb_mpu401_hw_if = {
78 sb_mpu401_open,
79 sb_mpu401_close,
80 sb_mpu401_output,
81 sb_mpu401_getinfo,
82 0, /* ioctl */
83 };
84 #endif
85
86 struct audio_device sb_device = {
87 "SoundBlaster",
88 "x",
89 "sb"
90 };
91
92 int sb_getdev __P((void *, struct audio_device *));
93
94 /*
95 * Define our interface to the higher level audio driver.
96 */
97
98 struct audio_hw_if sb_hw_if = {
99 sbdsp_open,
100 sbdsp_close,
101 0,
102 sbdsp_query_encoding,
103 sbdsp_set_params,
104 sbdsp_round_blocksize,
105 0,
106 0,
107 0,
108 0,
109 0,
110 sbdsp_haltdma,
111 sbdsp_haltdma,
112 sbdsp_speaker_ctl,
113 sb_getdev,
114 0,
115 sbdsp_mixer_set_port,
116 sbdsp_mixer_get_port,
117 sbdsp_mixer_query_devinfo,
118 sb_malloc,
119 sb_free,
120 sb_round,
121 sb_mappage,
122 sbdsp_get_props,
123 sbdsp_trigger_output,
124 sbdsp_trigger_input,
125 };
126
127 /*
128 * Probe / attach routines.
129 */
130
131
132 int
133 sbmatch(sc)
134 struct sbdsp_softc *sc;
135 {
136 static u_char drq_conf[8] = {
137 0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
138 };
139
140 static u_char irq_conf[11] = {
141 -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
142 };
143
144 if (sbdsp_probe(sc) == 0)
145 return 0;
146
147 /*
148 * Cannot auto-discover DMA channel.
149 */
150 if (ISSBPROCLASS(sc)) {
151 if (!SBP_DRQ_VALID(sc->sc_drq8)) {
152 printf("%s: configured dma chan %d invalid\n",
153 sc->sc_dev.dv_xname, sc->sc_drq8);
154 return 0;
155 }
156 } else {
157 if (!SB_DRQ_VALID(sc->sc_drq8)) {
158 printf("%s: configured dma chan %d invalid\n",
159 sc->sc_dev.dv_xname, sc->sc_drq8);
160 return 0;
161 }
162 }
163
164 if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
165 /*
166 * XXX Some ViBRA16 cards seem to have two 8 bit DMA
167 * channels. I've no clue how to use them, so ignore
168 * one of them for now. -- augustss (at) netbsd.org
169 */
170 sc->sc_drq16 = -1;
171
172 if (ISSB16CLASS(sc)) {
173 if (sc->sc_drq16 == -1)
174 sc->sc_drq16 = sc->sc_drq8;
175 if (!SB16_DRQ_VALID(sc->sc_drq16)) {
176 printf("%s: configured dma chan %d invalid\n",
177 sc->sc_dev.dv_xname, sc->sc_drq16);
178 return 0;
179 }
180 } else
181 sc->sc_drq16 = sc->sc_drq8;
182
183 #ifdef NEWCONFIG
184 /*
185 * If the IRQ wasn't compiled in, auto-detect it.
186 */
187 if (sc->sc_irq == IRQUNK) {
188 sc->sc_irq = isa_discoverintr(sbforceintr, sc);
189 sbdsp_reset(sc);
190 if (ISSBPROCLASS(sc)) {
191 if (!SBP_IRQ_VALID(sc->sc_irq)) {
192 printf("%s: couldn't auto-detect interrupt\n",
193 sc->sc_dev.dv_xname);
194 return 0;
195 }
196 }
197 else {
198 if (!SB_IRQ_VALID(sc->sc_irq)) {
199 printf("%s: couldn't auto-detect interrupt\n",
200 sc->sc_dev.dv_xname);
201 return 0;
202 }
203 }
204 } else
205 #endif
206 if (ISSBPROCLASS(sc)) {
207 if (!SBP_IRQ_VALID(sc->sc_irq)) {
208 printf("%s: configured irq %d invalid\n",
209 sc->sc_dev.dv_xname, sc->sc_irq);
210 return 0;
211 }
212 } else {
213 if (!SB_IRQ_VALID(sc->sc_irq)) {
214 printf("%s: configured irq %d invalid\n",
215 sc->sc_dev.dv_xname, sc->sc_irq);
216 return 0;
217 }
218 }
219
220 if (ISSB16CLASS(sc)) {
221 int w, r;
222 #if 0
223 printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
224 sbdsp_mix_read(sc, SBP_SET_DRQ));
225 printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
226 drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
227 #endif
228 w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
229 sbdsp_mix_write(sc, SBP_SET_DRQ, w);
230 r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
231 if (r != w) {
232 printf("%s: setting drq mask %02x failed, got %02x\n",
233 sc->sc_dev.dv_xname, w, r);
234 return 0;
235 }
236 #if 0
237 printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
238 sbdsp_mix_read(sc, SBP_SET_DRQ));
239 #endif
240
241 #if 0
242 printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
243 sbdsp_mix_read(sc, SBP_SET_IRQ));
244 printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
245 irq_conf[sc->sc_irq]);
246 #endif
247 w = irq_conf[sc->sc_irq];
248 sbdsp_mix_write(sc, SBP_SET_IRQ, w);
249 r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
250 if (r != w) {
251 printf("%s: setting irq mask %02x failed, got %02x\n",
252 sc->sc_dev.dv_xname, w, r);
253 return 0;
254 }
255 #if 0
256 printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
257 sbdsp_mix_read(sc, SBP_SET_IRQ));
258 #endif
259 }
260
261 return 1;
262 }
263
264
265 void
266 sbattach(sc)
267 struct sbdsp_softc *sc;
268 {
269 #if NMIDI > 0
270 struct midi_hw_if *mhw = &sb_midi_hw_if;
271 #else
272 struct midi_hw_if *mhw = 0;
273 #endif
274
275 sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, sc->sc_ist,
276 IPL_AUDIO, sbdsp_intr, sc);
277
278 sbdsp_attach(sc);
279
280 #if NMIDI > 0
281 sc->sc_hasmpu = 0;
282 if (ISSB16CLASS(sc) && sc->sc_mpu_sc.iobase != 0) {
283 sc->sc_mpu_sc.iot = sc->sc_iot;
284 if (mpu401_find(&sc->sc_mpu_sc)) {
285 sc->sc_hasmpu = 1;
286 mhw = &sb_mpu401_hw_if;
287 }
288 }
289 #endif
290
291 audio_attach_mi(&sb_hw_if, mhw, sc, &sc->sc_dev);
292 }
293
294 /*
295 * Various routines to interface to higher level audio driver
296 */
297
298 int
299 sb_getdev(addr, retp)
300 void *addr;
301 struct audio_device *retp;
302 {
303 struct sbdsp_softc *sc = addr;
304 static char *names[] = SB_NAMES;
305 char *config;
306
307 if (sc->sc_model == SB_JAZZ)
308 strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
309 else
310 strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
311 sprintf(retp->version, "%d.%02d",
312 SBVER_MAJOR(sc->sc_version),
313 SBVER_MINOR(sc->sc_version));
314 if (0 <= sc->sc_model && sc->sc_model < sizeof names / sizeof names[0])
315 config = names[sc->sc_model];
316 else
317 config = "??";
318 strncpy(retp->config, config, sizeof(retp->config));
319
320 return 0;
321 }
322
323 #if NMIDI > 0
324
325 #define SBMPU(a) (&((struct sbdsp_softc *)addr)->sc_mpu_sc)
326
327 int
328 sb_mpu401_open(addr, flags, iintr, ointr, arg)
329 void *addr;
330 int flags;
331 void (*iintr)__P((void *, int));
332 void (*ointr)__P((void *));
333 void *arg;
334 {
335 return mpu401_open(SBMPU(addr), flags, iintr, ointr, arg);
336 }
337
338 int
339 sb_mpu401_output(addr, d)
340 void *addr;
341 int d;
342 {
343 return mpu401_output(SBMPU(addr), d);
344 }
345
346 void
347 sb_mpu401_close(addr)
348 void *addr;
349 {
350 mpu401_close(SBMPU(addr));
351 }
352
353
354
355 void
356 sb_mpu401_getinfo(addr, mi)
357 void *addr;
358 struct midi_info *mi;
359 {
360 mi->name = "SB MPU-401 UART";
361 mi->props = 0;
362 }
363 #endif
364
365