sb.c revision 1.45 1 /* $NetBSD: sb.c,v 1.45 1997/04/29 21:01:39 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 <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/pio.h>
48
49 #include <sys/audioio.h>
50 #include <dev/audio_if.h>
51 #include <dev/mulaw.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 sb_softc {
61 struct device sc_dev; /* base device */
62 struct isadev sc_id; /* ISA device */
63 void *sc_ih; /* interrupt vectoring */
64
65 struct sbdsp_softc sc_sbdsp;
66 };
67
68 struct cfdriver sb_cd = {
69 NULL, "sb", DV_DULL
70 };
71
72 struct audio_device sb_device = {
73 "SoundBlaster",
74 "x",
75 "sb"
76 };
77
78 int sbopen __P((dev_t, int));
79 int sb_getdev __P((void *, struct audio_device *));
80
81 /*
82 * Define our interface to the higher level audio driver.
83 */
84
85 struct audio_hw_if sb_hw_if = {
86 sbopen,
87 sbdsp_close,
88 NULL,
89 sbdsp_query_encoding,
90 sbdsp_set_out_params,
91 sbdsp_set_in_params,
92 sbdsp_round_blocksize,
93 sbdsp_set_out_port,
94 sbdsp_get_out_port,
95 sbdsp_set_in_port,
96 sbdsp_get_in_port,
97 sbdsp_commit_settings,
98 mulaw_expand,
99 mulaw_compress,
100 sbdsp_dma_output,
101 sbdsp_dma_input,
102 sbdsp_haltdma,
103 sbdsp_haltdma,
104 sbdsp_contdma,
105 sbdsp_contdma,
106 sbdsp_speaker_ctl,
107 sb_getdev,
108 sbdsp_setfd,
109 sbdsp_mixer_set_port,
110 sbdsp_mixer_get_port,
111 sbdsp_mixer_query_devinfo,
112 0, /* not full-duplex */
113 0
114 };
115
116 /*
117 * Probe / attach routines.
118 */
119
120
121 int
122 sbmatch(sc)
123 struct sbdsp_softc *sc;
124 {
125 static u_char drq_conf[8] = {
126 0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
127 };
128
129 static u_char irq_conf[11] = {
130 -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
131 };
132
133 if (sbdsp_probe(sc) == 0)
134 return 0;
135
136 /*
137 * Cannot auto-discover DMA channel.
138 */
139 if (ISSBPROCLASS(sc)) {
140 if (!SBP_DRQ_VALID(sc->sc_drq8)) {
141 printf("%s: configured dma chan %d invalid\n",
142 sc->sc_dev.dv_xname, sc->sc_drq8);
143 return 0;
144 }
145 } else {
146 if (!SB_DRQ_VALID(sc->sc_drq8)) {
147 printf("%s: configured dma chan %d invalid\n",
148 sc->sc_dev.dv_xname, sc->sc_drq8);
149 return 0;
150 }
151 }
152
153 if (ISSB16CLASS(sc)) {
154 if (sc->sc_drq16 == -1)
155 sc->sc_drq16 = sc->sc_drq8;
156 if (!SB16_DRQ_VALID(sc->sc_drq16)) {
157 printf("%s: configured dma chan %d invalid\n",
158 sc->sc_dev.dv_xname, sc->sc_drq16);
159 return 0;
160 }
161 } else
162 sc->sc_drq16 = sc->sc_drq8;
163
164 #ifdef NEWCONFIG
165 /*
166 * If the IRQ wasn't compiled in, auto-detect it.
167 */
168 if (sc->sc_irq == IRQUNK) {
169 sc->sc_irq = isa_discoverintr(sbforceintr, sc);
170 sbdsp_reset(sc);
171 if (ISSBPROCLASS(sc)) {
172 if (!SBP_IRQ_VALID(sc->sc_irq)) {
173 printf("%s: couldn't auto-detect interrupt\n",
174 sc->sc_dev.dv_xname);
175 return 0;
176 }
177 }
178 else {
179 if (!SB_IRQ_VALID(sc->sc_irq)) {
180 printf("%s: couldn't auto-detect interrupt\n");
181 sc->sc_dev.dv_xname);
182 return 0;
183 }
184 }
185 } else
186 #endif
187 if (ISSBPROCLASS(sc)) {
188 if (!SBP_IRQ_VALID(sc->sc_irq)) {
189 printf("%s: configured irq %d invalid\n",
190 sc->sc_dev.dv_xname, sc->sc_irq);
191 return 0;
192 }
193 } else {
194 if (!SB_IRQ_VALID(sc->sc_irq)) {
195 printf("%s: configured irq %d invalid\n",
196 sc->sc_dev.dv_xname, sc->sc_irq);
197 return 0;
198 }
199 }
200
201 if (ISSB16CLASS(sc)) {
202 #if 0
203 printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
204 sbdsp_mix_read(sc, SBP_SET_DRQ));
205 printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
206 drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
207 #endif
208 sbdsp_mix_write(sc, SBP_SET_DRQ,
209 drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
210 #if 0
211 printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
212 sbdsp_mix_read(sc, SBP_SET_DRQ));
213 #endif
214
215 #if 0
216 printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
217 sbdsp_mix_read(sc, SBP_SET_IRQ));
218 printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
219 irq_conf[sc->sc_irq]);
220 #endif
221 sbdsp_mix_write(sc, SBP_SET_IRQ,
222 irq_conf[sc->sc_irq]);
223 #if 0
224 printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
225 sbdsp_mix_read(sc, SBP_SET_IRQ));
226 #endif
227 }
228
229 return 1;
230 }
231
232
233 void
234 sbattach(sc)
235 struct sbdsp_softc *sc;
236 {
237 int error;
238
239 sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, IST_EDGE,
240 IPL_AUDIO, sbdsp_intr, sc);
241
242 sbdsp_attach(sc);
243
244 if ((error = audio_hardware_attach(&sb_hw_if, sc)) != 0)
245 printf("%s: could not attach to audio device driver (%d)\n",
246 sc->sc_dev.dv_xname, error);
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 sbopen(dev, flags)
283 dev_t dev;
284 int flags;
285 {
286 struct sbdsp_softc *sc;
287 int unit = AUDIOUNIT(dev);
288
289 if (unit >= sb_cd.cd_ndevs)
290 return ENODEV;
291
292 sc = sb_cd.cd_devs[unit];
293 if (!sc)
294 return ENXIO;
295
296 return sbdsp_open(sc, dev, flags);
297 }
298
299 int
300 sb_getdev(addr, retp)
301 void *addr;
302 struct audio_device *retp;
303 {
304 struct sbdsp_softc *sc = addr;
305
306 if (sc->sc_model & MODEL_JAZZ16)
307 strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
308 else
309 strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
310 sprintf(retp->version, "%d.%02d",
311 SBVER_MAJOR(sc->sc_model),
312 SBVER_MINOR(sc->sc_model));
313 strncpy(retp->config, "sb", sizeof(retp->config));
314
315 return 0;
316 }
317