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