sb.c revision 1.84 1 /* $NetBSD: sb.c,v 1.84 2006/04/14 19:56:40 christos 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/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: sb.c,v 1.84 2006/04/14 19:56:40 christos Exp $");
39
40 #include "midi.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/syslog.h>
47 #include <sys/device.h>
48 #include <sys/proc.h>
49
50 #include <machine/cpu.h>
51 #include <machine/intr.h>
52 #include <machine/bus.h>
53
54 #include <sys/audioio.h>
55 #include <dev/audio_if.h>
56 #include <dev/midi_if.h>
57
58 #include <dev/midi_if.h>
59
60 #include <dev/isa/isavar.h>
61 #include <dev/isa/isadmavar.h>
62
63 #include <dev/isa/sbreg.h>
64 #include <dev/isa/sbvar.h>
65 #include <dev/isa/sbdspvar.h>
66
67 #if NMPU > 0
68 const struct midi_hw_if sb_midi_hw_if = {
69 sbdsp_midi_open,
70 sbdsp_midi_close,
71 sbdsp_midi_output,
72 sbdsp_midi_getinfo,
73 0, /* ioctl */
74 };
75 #endif
76
77 int sb_getdev(void *, struct audio_device *);
78
79 /*
80 * Define our interface to the higher level audio driver.
81 */
82
83 const struct audio_hw_if sb_hw_if = {
84 sbdsp_open,
85 sbdsp_close,
86 0,
87 sbdsp_query_encoding,
88 sbdsp_set_params,
89 sbdsp_round_blocksize,
90 0,
91 0,
92 0,
93 0,
94 0,
95 sbdsp_halt_output,
96 sbdsp_halt_input,
97 sbdsp_speaker_ctl,
98 sb_getdev,
99 0,
100 sbdsp_mixer_set_port,
101 sbdsp_mixer_get_port,
102 sbdsp_mixer_query_devinfo,
103 sb_malloc,
104 sb_free,
105 sb_round_buffersize,
106 sb_mappage,
107 sbdsp_get_props,
108 sbdsp_trigger_output,
109 sbdsp_trigger_input,
110 0,
111 };
112
113 /*
114 * Probe / attach routines.
115 */
116
117
118 int
119 sbmatch(struct sbdsp_softc *sc)
120 {
121 static const u_char drq_conf[8] = {
122 0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
123 };
124
125 static const u_char irq_conf[11] = {
126 -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
127 };
128
129 if (sbdsp_probe(sc) == 0)
130 return 0;
131
132 /*
133 * Cannot auto-discover DMA channel.
134 */
135 if (ISSBPROCLASS(sc)) {
136 if (!SBP_DRQ_VALID(sc->sc_drq8)) {
137 printf("%s: configured DMA chan %d invalid\n",
138 sc->sc_dev.dv_xname, sc->sc_drq8);
139 return 0;
140 }
141 } else {
142 if (!SB_DRQ_VALID(sc->sc_drq8)) {
143 printf("%s: configured DMA chan %d invalid\n",
144 sc->sc_dev.dv_xname, sc->sc_drq8);
145 return 0;
146 }
147 }
148
149 if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
150 /*
151 * XXX Some ViBRA16 cards seem to have two 8 bit DMA
152 * channels. I've no clue how to use them, so ignore
153 * one of them for now. -- augustss (at) NetBSD.org
154 */
155 sc->sc_drq16 = -1;
156
157 if (ISSB16CLASS(sc)) {
158 if (sc->sc_drq16 == -1)
159 sc->sc_drq16 = sc->sc_drq8;
160 if (!SB16_DRQ_VALID(sc->sc_drq16)) {
161 printf("%s: configured DMA chan %d invalid\n",
162 sc->sc_dev.dv_xname, sc->sc_drq16);
163 return 0;
164 }
165 } else
166 sc->sc_drq16 = sc->sc_drq8;
167
168 if (ISSBPROCLASS(sc)) {
169 if (!SBP_IRQ_VALID(sc->sc_irq)) {
170 printf("%s: configured irq %d invalid\n",
171 sc->sc_dev.dv_xname, sc->sc_irq);
172 return 0;
173 }
174 } else {
175 if (!SB_IRQ_VALID(sc->sc_irq)) {
176 printf("%s: configured irq %d invalid\n",
177 sc->sc_dev.dv_xname, sc->sc_irq);
178 return 0;
179 }
180 }
181
182 if (ISSB16CLASS(sc) && !(sc->sc_quirks & SB_QUIRK_NO_INIT_DRQ)) {
183 int w, r;
184 if (sc->sc_irq >= __arraycount(irq_conf)) {
185 printf("%s: Cannot handle irq %d\n",
186 sc->sc_dev.dv_xname, sc->sc_irq);
187 return 0;
188 }
189 if (sc->sc_drq16 >= __arraycount(drq_conf)) {
190 printf("%s: Cannot handle drq16 %d\n",
191 sc->sc_dev.dv_xname, sc->sc_drq16);
192 return 0;
193 }
194 if (sc->sc_drq8 >= __arraycount(drq_conf)) {
195 printf("%s: Cannot handle drq8 %d\n",
196 sc->sc_dev.dv_xname, sc->sc_drq8);
197 return 0;
198 }
199 #if 0
200 printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
201 sbdsp_mix_read(sc, SBP_SET_DRQ));
202 printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
203 drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
204 #endif
205 w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
206 sbdsp_mix_write(sc, SBP_SET_DRQ, w);
207 r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
208 if (r != w) {
209 printf("%s: setting drq mask %02x failed, got %02x\n",
210 sc->sc_dev.dv_xname, w, r);
211 return 0;
212 }
213 #if 0
214 printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
215 sbdsp_mix_read(sc, SBP_SET_DRQ));
216 #endif
217
218 #if 0
219 printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
220 sbdsp_mix_read(sc, SBP_SET_IRQ));
221 printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
222 irq_conf[sc->sc_irq]);
223 #endif
224 w = irq_conf[sc->sc_irq];
225 sbdsp_mix_write(sc, SBP_SET_IRQ, w);
226 r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
227 if (r != w) {
228 printf("%s: setting irq mask %02x failed, got %02x\n",
229 sc->sc_dev.dv_xname, w, r);
230 return 0;
231 }
232 #if 0
233 printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
234 sbdsp_mix_read(sc, SBP_SET_IRQ));
235 #endif
236 }
237
238 return 1;
239 }
240
241
242 void
243 sbattach(struct sbdsp_softc *sc)
244 {
245 struct audio_attach_args arg;
246
247 sbdsp_attach(sc);
248
249 audio_attach_mi(&sb_hw_if, sc, &sc->sc_dev);
250
251 #if NMPU > 0
252 switch(sc->sc_hasmpu) {
253 default:
254 case SBMPU_NONE: /* no mpu */
255 break;
256 case SBMPU_INTERNAL: /* try to attach midi directly */
257 midi_attach_mi(&sb_midi_hw_if, sc, &sc->sc_dev);
258 break;
259 case SBMPU_EXTERNAL: /* search for mpu */
260 arg.type = AUDIODEV_TYPE_MPU;
261 arg.hwif = 0;
262 arg.hdl = 0;
263 sc->sc_mpudev = config_found(&sc->sc_dev, &arg, audioprint);
264 break;
265 }
266 #endif
267 if (sc->sc_model >= SB_20) {
268 arg.type = AUDIODEV_TYPE_OPL;
269 arg.hwif = 0;
270 arg.hdl = 0;
271 (void)config_found(&sc->sc_dev, &arg, audioprint);
272 }
273 }
274
275 /*
276 * Various routines to interface to higher level audio driver
277 */
278
279 int
280 sb_getdev(void *addr, struct audio_device *retp)
281 {
282 static const char * const names[] = SB_NAMES;
283 struct sbdsp_softc *sc;
284 const char *config;
285
286 sc = addr;
287 if (sc->sc_model == SB_JAZZ)
288 strlcpy(retp->name, "MV Jazz16", sizeof(retp->name));
289 else
290 strlcpy(retp->name, "SoundBlaster", sizeof(retp->name));
291 snprintf(retp->version, sizeof(retp->version), "%d.%02d",
292 SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version));
293 if (0 <= sc->sc_model && sc->sc_model < sizeof names / sizeof names[0])
294 config = names[sc->sc_model];
295 else
296 config = "??";
297 strlcpy(retp->config, config, sizeof(retp->config));
298
299 return 0;
300 }
301