sb.c revision 1.35 1 /* $NetBSD: sb.c,v 1.35 1996/04/29 20:03:29 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/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/pio.h>
47
48 #include <sys/audioio.h>
49 #include <dev/audio_if.h>
50 #include <dev/mulaw.h>
51
52 #include <dev/isa/isavar.h>
53 #include <dev/isa/isadmavar.h>
54
55 #include <dev/isa/sbdspvar.h>
56 #include <dev/isa/sbreg.h>
57
58 #ifdef AUDIO_DEBUG
59 extern void Dprintf __P((const char *, ...));
60 #define DPRINTF(x) if (sbdebug) Dprintf x
61 int sbdebug = 0;
62 #else
63 #define DPRINTF(x)
64 #endif
65
66 struct sb_softc {
67 struct device sc_dev; /* base device */
68 struct isadev sc_id; /* ISA device */
69 void *sc_ih; /* interrupt vectoring */
70
71 struct sbdsp_softc sc_sbdsp;
72 };
73
74 int sbprobe __P((struct device *, void *, void *));
75 void sbattach __P((struct device *, struct device *, void *));
76
77 struct cfattach sb_ca = {
78 sizeof(struct sbdsp_softc), sbprobe, sbattach
79 };
80
81 struct cfdriver sb_cd = {
82 NULL, "sb", DV_DULL
83 };
84
85 struct audio_device sb_device = {
86 "SoundBlaster",
87 "x",
88 "sb"
89 };
90
91 int sbopen __P((dev_t, int));
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 sbopen,
100 sbdsp_close,
101 NULL,
102 sbdsp_set_in_sr,
103 sbdsp_get_in_sr,
104 sbdsp_set_out_sr,
105 sbdsp_get_out_sr,
106 sbdsp_query_encoding,
107 sbdsp_set_encoding,
108 sbdsp_get_encoding,
109 sbdsp_set_precision,
110 sbdsp_get_precision,
111 sbdsp_set_channels,
112 sbdsp_get_channels,
113 sbdsp_round_blocksize,
114 sbdsp_set_out_port,
115 sbdsp_get_out_port,
116 sbdsp_set_in_port,
117 sbdsp_get_in_port,
118 sbdsp_commit_settings,
119 sbdsp_get_silence,
120 mulaw_expand,
121 mulaw_compress,
122 sbdsp_dma_output,
123 sbdsp_dma_input,
124 sbdsp_haltdma,
125 sbdsp_haltdma,
126 sbdsp_contdma,
127 sbdsp_contdma,
128 sbdsp_speaker_ctl,
129 sb_getdev,
130 sbdsp_setfd,
131 sbdsp_mixer_set_port,
132 sbdsp_mixer_get_port,
133 sbdsp_mixer_query_devinfo,
134 0, /* not full-duplex */
135 0
136 };
137
138 /*
139 * Probe / attach routines.
140 */
141
142 /*
143 * Probe for the soundblaster hardware.
144 */
145 int
146 sbprobe(parent, match, aux)
147 struct device *parent;
148 void *match, *aux;
149 {
150 register struct sbdsp_softc *sc = match;
151 register struct isa_attach_args *ia = aux;
152 register int iobase = ia->ia_iobase;
153 static u_char drq_conf[4] = {
154 0x01, 0x02, -1, 0x08
155 };
156 static u_char irq_conf[11] = {
157 -1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
158 };
159
160 if (!SB_BASE_VALID(ia->ia_iobase)) {
161 printf("sb: configured iobase %d invalid\n", ia->ia_iobase);
162 return 0;
163 }
164 sc->sc_iobase = iobase;
165 sc->sc_irq = ia->ia_irq;
166 sc->sc_drq = ia->ia_drq;
167
168 if (sbdsp_probe(sc) == 0) {
169 DPRINTF(("sb: sbdsp probe failed\n"));
170 return 0;
171 }
172
173 /*
174 * Cannot auto-discover DMA channel.
175 */
176 if (ISSBPROCLASS(sc)) {
177 if (!SBP_DRQ_VALID(ia->ia_drq)) {
178 printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
179 return 0;
180 }
181 if (ISSB16CLASS(sc))
182 sbdsp_mix_write(sc, SBP_SET_DRQ, drq_conf[ia->ia_drq]);
183 }
184 else {
185 if (!SB_DRQ_VALID(ia->ia_drq)) {
186 printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
187 return 0;
188 }
189 }
190
191 #ifdef NEWCONFIG
192 /*
193 * If the IRQ wasn't compiled in, auto-detect it.
194 */
195 if (ia->ia_irq == IRQUNK) {
196 ia->ia_irq = isa_discoverintr(sbforceintr, aux);
197 sbdsp_reset(sc);
198 if (ISSBPROCLASS(sc)) {
199 if (!SBP_IRQ_VALID(ia->ia_irq)) {
200 printf("sb: couldn't auto-detect interrupt");
201 return 0;
202 }
203 }
204 else {
205 if (!SB_IRQ_VALID(ia->ia_irq)) {
206 printf("sb: couldn't auto-detect interrupt");
207 return 0;
208 }
209 }
210 } else
211 #endif
212 if (ISSBPROCLASS(sc)) {
213 if (!SBP_IRQ_VALID(ia->ia_irq)) {
214 printf("sb: configured irq %d invalid\n", ia->ia_irq);
215 return 0;
216 }
217 if (ISSB16CLASS(sc))
218 sbdsp_mix_write(sc, SBP_SET_IRQ, irq_conf[ia->ia_irq]);
219 }
220 else {
221 if (!SB_IRQ_VALID(ia->ia_irq)) {
222 printf("sb: configured irq %d invalid\n", ia->ia_irq);
223 return 0;
224 }
225 }
226
227 if (ISSBPROCLASS(sc))
228 ia->ia_iosize = SBP_NPORT;
229 else
230 ia->ia_iosize = SB_NPORT;
231 return 1;
232 }
233
234 #ifdef NEWCONFIG
235 void
236 sbforceintr(aux)
237 void *aux;
238 {
239 static char dmabuf;
240 struct isa_attach_args *ia = aux;
241 int iobase = ia->ia_iobase;
242
243 /*
244 * Set up a DMA read of one byte.
245 * XXX Note that at this point we haven't called
246 * at_setup_dmachan(). This is okay because it just
247 * allocates a buffer in case it needs to make a copy,
248 * and it won't need to make a copy for a 1 byte buffer.
249 * (I think that calling at_setup_dmachan() should be optional;
250 * if you don't call it, it will be called the first time
251 * it is needed (and you pay the latency). Also, you might
252 * never need the buffer anyway.)
253 */
254 at_dma(DMAMODE_READ, &dmabuf, 1, ia->ia_drq);
255 if (sbdsp_wdsp(iobase, SB_DSP_RDMA) == 0) {
256 (void)sbdsp_wdsp(iobase, 0);
257 (void)sbdsp_wdsp(iobase, 0);
258 }
259 }
260 #endif
261
262 /*
263 * Attach hardware to driver, attach hardware driver to audio
264 * pseudo-device driver .
265 */
266 void
267 sbattach(parent, self, aux)
268 struct device *parent, *self;
269 void *aux;
270 {
271 register struct sbdsp_softc *sc = (struct sbdsp_softc *)self;
272 struct isa_attach_args *ia = (struct isa_attach_args *)aux;
273 int err;
274
275 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
276 IPL_AUDIO, sbdsp_intr, sc);
277
278 sbdsp_attach(sc);
279
280 if ((err = audio_hardware_attach(&sb_hw_if, sc)) != 0)
281 printf("sb: could not attach to audio pseudo-device driver (%d)\n", err);
282 }
283
284 /*
285 * Various routines to interface to higher level audio driver
286 */
287
288 int
289 sbopen(dev, flags)
290 dev_t dev;
291 int flags;
292 {
293 struct sbdsp_softc *sc;
294 int unit = AUDIOUNIT(dev);
295
296 if (unit >= sb_cd.cd_ndevs)
297 return ENODEV;
298
299 sc = sb_cd.cd_devs[unit];
300 if (!sc)
301 return ENXIO;
302
303 return sbdsp_open(sc, dev, flags);
304 }
305
306 int
307 sb_getdev(addr, retp)
308 void *addr;
309 struct audio_device *retp;
310 {
311 struct sbdsp_softc *sc = addr;
312
313 if (sc->sc_model & MODEL_JAZZ16)
314 strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
315 else
316 strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
317 sprintf(retp->version, "%d.%d",
318 SBVER_MAJOR(sc->sc_model),
319 SBVER_MINOR(sc->sc_model));
320 strncpy(retp->config, "sb", sizeof(retp->config));
321
322 return 0;
323 }
324