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