sb.c revision 1.29 1 /* $NetBSD: sb.c,v 1.29 1995/12/24 02:31:49 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();
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
89 int sbprobe();
90 void sbattach();
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 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, self, aux)
147 struct device *parent, *self;
148 void *aux;
149 {
150 register struct sbdsp_softc *sc = (void *)self;
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 if (sbdsp_probe(sc) == 0) {
166 DPRINTF(("sb: sbdsp probe failed\n"));
167 return 0;
168 }
169
170 /*
171 * Cannot auto-discover DMA channel.
172 */
173 if (ISSBPROCLASS(sc)) {
174 if (!SBP_DRQ_VALID(ia->ia_drq)) {
175 printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
176 return 0;
177 }
178 if (ISSB16CLASS(sc))
179 sbdsp_mix_write(sc, SBP_SET_DRQ, drq_conf[ia->ia_drq]);
180 }
181 else {
182 if (!SB_DRQ_VALID(ia->ia_drq)) {
183 printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
184 return 0;
185 }
186 }
187
188 #ifdef NEWCONFIG
189 /*
190 * If the IRQ wasn't compiled in, auto-detect it.
191 */
192 if (ia->ia_irq == IRQUNK) {
193 ia->ia_irq = isa_discoverintr(sbforceintr, aux);
194 sbdsp_reset(sc);
195 if (ISSBPROCLASS(sc)) {
196 if (!SBP_IRQ_VALID(ia->ia_irq)) {
197 printf("sb: couldn't auto-detect interrupt");
198 return 0;
199 }
200 }
201 else {
202 if (!SB_IRQ_VALID(ia->ia_irq)) {
203 printf("sb: couldn't auto-detect interrupt");
204 return 0;
205 }
206 }
207 } else
208 #endif
209 if (ISSBPROCLASS(sc)) {
210 if (!SBP_IRQ_VALID(ia->ia_irq)) {
211 printf("sb: configured irq %d invalid\n", ia->ia_irq);
212 return 0;
213 }
214 if (ISSB16CLASS(sc))
215 sbdsp_mix_write(sc, SBP_SET_IRQ, irq_conf[ia->ia_irq]);
216 }
217 else {
218 if (!SB_IRQ_VALID(ia->ia_irq)) {
219 printf("sb: configured irq %d invalid\n", ia->ia_irq);
220 return 0;
221 }
222 }
223
224 sc->sc_irq = ia->ia_irq;
225 sc->sc_drq = ia->ia_drq;
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(B_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 register int iobase = ia->ia_iobase;
274 int err;
275
276 sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_AUDIO,
277 sbdsp_intr, sc);
278
279 sbdsp_attach(sc);
280
281 sprintf(sb_device.version, "%d.%d",
282 SBVER_MAJOR(sc->sc_model),
283 SBVER_MINOR(sc->sc_model));
284
285 if ((err = audio_hardware_attach(&sb_hw_if, sc)) != 0)
286 printf("sb: could not attach to audio pseudo-device driver (%d)\n", err);
287 }
288
289 /*
290 * Various routines to interface to higher level audio driver
291 */
292
293 int
294 sbopen(dev, flags)
295 dev_t dev;
296 int flags;
297 {
298 struct sbdsp_softc *sc;
299 int unit = AUDIOUNIT(dev);
300
301 if (unit >= sbcd.cd_ndevs)
302 return ENODEV;
303
304 sc = sbcd.cd_devs[unit];
305 if (!sc)
306 return ENXIO;
307
308 return sbdsp_open(sc, dev, flags);
309 }
310
311 int
312 sb_getdev(addr, retp)
313 void *addr;
314 struct audio_device *retp;
315 {
316 *retp = sb_device;
317 return 0;
318 }
319