sb.c revision 1.60 1 /* $NetBSD: sb.c,v 1.60 1998/08/07 00:00:59 augustss 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 sbdsp_dma_init_output,
107 sbdsp_dma_init_input,
108 sbdsp_dma_output,
109 sbdsp_dma_input,
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 };
124
125 /*
126 * Probe / attach routines.
127 */
128
129
130 int
131 sbmatch(sc)
132 struct sbdsp_softc *sc;
133 {
134 static u_char drq_conf[8] = {
135 0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
136 };
137
138 static u_char irq_conf[11] = {
139 -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
140 };
141
142 if (sbdsp_probe(sc) == 0)
143 return 0;
144
145 /*
146 * Cannot auto-discover DMA channel.
147 */
148 if (ISSBPROCLASS(sc)) {
149 if (!SBP_DRQ_VALID(sc->sc_drq8)) {
150 printf("%s: configured dma chan %d invalid\n",
151 sc->sc_dev.dv_xname, sc->sc_drq8);
152 return 0;
153 }
154 } else {
155 if (!SB_DRQ_VALID(sc->sc_drq8)) {
156 printf("%s: configured dma chan %d invalid\n",
157 sc->sc_dev.dv_xname, sc->sc_drq8);
158 return 0;
159 }
160 }
161
162 if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
163 /*
164 * XXX Some ViBRA16 cards seem to have two 8 bit DMA
165 * channels. I've no clue how to use them, so ignore
166 * one of them for now. -- augustss (at) netbsd.org
167 */
168 sc->sc_drq16 = -1;
169
170 if (ISSB16CLASS(sc)) {
171 if (sc->sc_drq16 == -1)
172 sc->sc_drq16 = sc->sc_drq8;
173 if (!SB16_DRQ_VALID(sc->sc_drq16)) {
174 printf("%s: configured dma chan %d invalid\n",
175 sc->sc_dev.dv_xname, sc->sc_drq16);
176 return 0;
177 }
178 } else
179 sc->sc_drq16 = sc->sc_drq8;
180
181 #ifdef NEWCONFIG
182 /*
183 * If the IRQ wasn't compiled in, auto-detect it.
184 */
185 if (sc->sc_irq == IRQUNK) {
186 sc->sc_irq = isa_discoverintr(sbforceintr, sc);
187 sbdsp_reset(sc);
188 if (ISSBPROCLASS(sc)) {
189 if (!SBP_IRQ_VALID(sc->sc_irq)) {
190 printf("%s: couldn't auto-detect interrupt\n",
191 sc->sc_dev.dv_xname);
192 return 0;
193 }
194 }
195 else {
196 if (!SB_IRQ_VALID(sc->sc_irq)) {
197 printf("%s: couldn't auto-detect interrupt\n",
198 sc->sc_dev.dv_xname);
199 return 0;
200 }
201 }
202 } else
203 #endif
204 if (ISSBPROCLASS(sc)) {
205 if (!SBP_IRQ_VALID(sc->sc_irq)) {
206 printf("%s: configured irq %d invalid\n",
207 sc->sc_dev.dv_xname, sc->sc_irq);
208 return 0;
209 }
210 } else {
211 if (!SB_IRQ_VALID(sc->sc_irq)) {
212 printf("%s: configured irq %d invalid\n",
213 sc->sc_dev.dv_xname, sc->sc_irq);
214 return 0;
215 }
216 }
217
218 if (ISSB16CLASS(sc)) {
219 int w, r;
220 #if 0
221 printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
222 sbdsp_mix_read(sc, SBP_SET_DRQ));
223 printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
224 drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
225 #endif
226 w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
227 sbdsp_mix_write(sc, SBP_SET_DRQ, w);
228 r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
229 if (r != w) {
230 printf("%s: setting drq 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 drq conf %02x\n", sc->sc_dev.dv_xname,
236 sbdsp_mix_read(sc, SBP_SET_DRQ));
237 #endif
238
239 #if 0
240 printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
241 sbdsp_mix_read(sc, SBP_SET_IRQ));
242 printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
243 irq_conf[sc->sc_irq]);
244 #endif
245 w = irq_conf[sc->sc_irq];
246 sbdsp_mix_write(sc, SBP_SET_IRQ, w);
247 r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
248 if (r != w) {
249 printf("%s: setting irq mask %02x failed, got %02x\n",
250 sc->sc_dev.dv_xname, w, r);
251 return 0;
252 }
253 #if 0
254 printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
255 sbdsp_mix_read(sc, SBP_SET_IRQ));
256 #endif
257 }
258
259 return 1;
260 }
261
262
263 void
264 sbattach(sc)
265 struct sbdsp_softc *sc;
266 {
267 #if NMIDI > 0
268 struct midi_hw_if *mhw = &sb_midi_hw_if;
269 #else
270 struct midi_hw_if *mhw = 0;
271 #endif
272
273 sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, sc->sc_ist,
274 IPL_AUDIO, sbdsp_intr, sc);
275
276 sbdsp_attach(sc);
277
278 #if NMIDI > 0
279 sc->sc_hasmpu = 0;
280 if (ISSB16CLASS(sc) && sc->sc_mpu_sc.iobase != 0) {
281 sc->sc_mpu_sc.iot = sc->sc_iot;
282 if (mpu401_find(&sc->sc_mpu_sc)) {
283 sc->sc_hasmpu = 1;
284 mhw = &sb_mpu401_hw_if;
285 }
286 }
287 #endif
288
289 audio_attach_mi(&sb_hw_if, mhw, sc, &sc->sc_dev);
290 }
291
292 /*
293 * Various routines to interface to higher level audio driver
294 */
295
296 int
297 sb_getdev(addr, retp)
298 void *addr;
299 struct audio_device *retp;
300 {
301 struct sbdsp_softc *sc = addr;
302 static char *names[] = SB_NAMES;
303 char *config;
304
305 if (sc->sc_model == SB_JAZZ)
306 strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
307 else
308 strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
309 sprintf(retp->version, "%d.%02d",
310 SBVER_MAJOR(sc->sc_version),
311 SBVER_MINOR(sc->sc_version));
312 if (0 <= sc->sc_model && sc->sc_model < sizeof names / sizeof names[0])
313 config = names[sc->sc_model];
314 else
315 config = "??";
316 strncpy(retp->config, config, sizeof(retp->config));
317
318 return 0;
319 }
320
321 #if NMIDI > 0
322
323 #define SBMPU(a) (&((struct sbdsp_softc *)addr)->sc_mpu_sc)
324
325 int
326 sb_mpu401_open(addr, flags, iintr, ointr, arg)
327 void *addr;
328 int flags;
329 void (*iintr)__P((void *, int));
330 void (*ointr)__P((void *));
331 void *arg;
332 {
333 return mpu401_open(SBMPU(addr), flags, iintr, ointr, arg);
334 }
335
336 int
337 sb_mpu401_output(addr, d)
338 void *addr;
339 int d;
340 {
341 return mpu401_output(SBMPU(addr), d);
342 }
343
344 void
345 sb_mpu401_close(addr)
346 void *addr;
347 {
348 mpu401_close(SBMPU(addr));
349 }
350
351
352
353 void
354 sb_mpu401_getinfo(addr, mi)
355 void *addr;
356 struct midi_info *mi;
357 {
358 mi->name = "SB MPU-401 UART";
359 mi->props = 0;
360 }
361 #endif
362
363