sb.c revision 1.17 1 /* $NetBSD: sb.c,v 1.17 1995/02/21 02:28:06 brezak 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 * $Id: sb.c,v 1.17 1995/02/21 02:28:06 brezak Exp $
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/ioctl.h>
42 #include <sys/syslog.h>
43 #include <sys/device.h>
44 #include <sys/proc.h>
45
46 #include <machine/cpu.h>
47 #include <machine/pio.h>
48
49 #include <sys/audioio.h>
50 #include <dev/audio_if.h>
51
52 #include <i386/isa/isavar.h>
53 #include <i386/isa/dmavar.h>
54 #include <i386/isa/icu.h>
55
56 #include <i386/isa/sbdspvar.h>
57 #include <i386/isa/sbreg.h>
58
59 #define DEBUG /*XXX*/
60 #ifdef DEBUG
61 #define DPRINTF(x) if (sbdebug) printf x
62 int sbdebug = 0;
63 #else
64 #define DPRINTF(x)
65 #endif
66
67 struct sb_softc {
68 struct device sc_dev; /* base device */
69 struct isadev sc_id; /* ISA device */
70 struct intrhand sc_ih; /* interrupt vectoring */
71
72 struct sbdsp_softc sc_sbdsp;
73 };
74
75 int sbprobe();
76 void sbattach __P((struct device *, struct device *, void *));
77
78 struct cfdriver sbcd = {
79 NULL, "sb", sbprobe, sbattach, DV_DULL, sizeof(struct sb_softc)
80 };
81
82 struct audio_device sb_device = {
83 "SoundBlaster",
84 "x",
85 "sb"
86 };
87
88 int sbopen __P((dev_t, int));
89
90 int sbprobe();
91 void sbattach();
92
93 int sb_getdev __P((caddr_t, struct audio_device *));
94
95 /*
96 * Define our interface to the higher level audio driver.
97 */
98
99 struct audio_hw_if sb_hw_if = {
100 sbopen,
101 sbdsp_close,
102 NULL,
103 sbdsp_set_in_sr,
104 sbdsp_get_in_sr,
105 sbdsp_set_out_sr,
106 sbdsp_get_out_sr,
107 sbdsp_query_encoding,
108 sbdsp_set_encoding,
109 sbdsp_get_encoding,
110 sbdsp_set_precision,
111 sbdsp_get_precision,
112 sbdsp_set_channels,
113 sbdsp_get_channels,
114 sbdsp_round_blocksize,
115 sbdsp_set_out_port,
116 sbdsp_get_out_port,
117 sbdsp_set_in_port,
118 sbdsp_get_in_port,
119 sbdsp_commit_settings,
120 sbdsp_get_silence,
121 sbdsp_expand,
122 sbdsp_compress,
123 sbdsp_dma_output,
124 sbdsp_dma_input,
125 sbdsp_haltdma,
126 sbdsp_haltdma,
127 sbdsp_contdma,
128 sbdsp_contdma,
129 sbdsp_speaker_ctl,
130 sb_getdev,
131 sbdsp_setfd,
132 sbdsp_mixer_set_port,
133 sbdsp_mixer_get_port,
134 sbdsp_mixer_query_devinfo,
135 0, /* not full-duplex */
136 0
137 };
138
139 /*
140 * Probe / attach routines.
141 */
142
143 /*
144 * Probe for the soundblaster hardware.
145 */
146 int
147 sbprobe(parent, self, aux)
148 struct device *parent, *self;
149 void *aux;
150 {
151 register struct sb_softc *sc = (void *)self;
152 register struct isa_attach_args *ia = aux;
153 register u_short iobase = ia->ia_iobase;
154
155 if (!SB_BASE_VALID(ia->ia_iobase)) {
156 printf("sb: configured iobase %d invalid\n", ia->ia_iobase);
157 return 0;
158 }
159 sc->sc_sbdsp.sc_iobase = iobase;
160 if (sbdsp_probe(&sc->sc_sbdsp) == 0) {
161 DPRINTF(("sb: sbdsp probe failed\n"));
162 return 0;
163 }
164
165 /*
166 * Cannot auto-discover DMA channel.
167 */
168 if (ISSBPROCLASS(&sc->sc_sbdsp)) {
169 if (!SBP_DRQ_VALID(ia->ia_drq)) {
170 printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
171 return 0;
172 }
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->sc_sbdsp);
188 if (ISSBPROCLASS(&sc->sc_sbdsp)) {
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->sc_sbdsp)) {
203 if (!SBP_IRQ_VALID(ia->ia_irq)) {
204 int irq = ffs(ia->ia_irq) - 1;
205 printf("sb: configured irq %d invalid\n", irq);
206 return 0;
207 }
208 }
209 else {
210 if (!SB_IRQ_VALID(ia->ia_irq)) {
211 int irq = ffs(ia->ia_irq) - 1;
212 printf("sb: configured irq %d invalid\n", irq);
213 return 0;
214 }
215 }
216
217 sc->sc_sbdsp.sc_irq = ffs(ia->ia_irq) - 1;
218 sc->sc_sbdsp.sc_drq = ia->ia_drq;
219
220 if (ISSBPROCLASS(&sc->sc_sbdsp))
221 ia->ia_iosize = SBP_NPORT;
222 else
223 ia->ia_iosize = SB_NPORT;
224 return 1;
225 }
226
227 #ifdef NEWCONFIG
228 void
229 sbforceintr(aux)
230 void *aux;
231 {
232 static char dmabuf;
233 struct isa_attach_args *ia = aux;
234 u_short iobase = ia->ia_iobase;
235
236 /*
237 * Set up a DMA read of one byte.
238 * XXX Note that at this point we haven't called
239 * at_setup_dmachan(). This is okay because it just
240 * allocates a buffer in case it needs to make a copy,
241 * and it won't need to make a copy for a 1 byte buffer.
242 * (I think that calling at_setup_dmachan() should be optional;
243 * if you don't call it, it will be called the first time
244 * it is needed (and you pay the latency). Also, you might
245 * never need the buffer anyway.)
246 */
247 at_dma(B_READ, &dmabuf, 1, ia->ia_drq);
248 if (sbdsp_wdsp(iobase, SB_DSP_RDMA) == 0) {
249 (void)sbdsp_wdsp(iobase, 0);
250 (void)sbdsp_wdsp(iobase, 0);
251 }
252 }
253 #endif
254
255 /*
256 * Attach hardware to driver, attach hardware driver to audio
257 * pseudo-device driver .
258 */
259 void
260 sbattach(parent, self, aux)
261 struct device *parent, *self;
262 void *aux;
263 {
264 register struct sb_softc *sc = (struct sb_softc *)self;
265 struct isa_attach_args *ia = (struct isa_attach_args *)aux;
266 register u_short iobase = ia->ia_iobase;
267
268 #ifdef NEWCONFIG
269 isa_establish(&sc->sc_id, &sc->sc_dev);
270 #endif
271 sc->sc_ih.ih_fun = sbdsp_intr;
272 sc->sc_ih.ih_arg = &sc->sc_sbdsp;
273 sc->sc_ih.ih_level = IPL_BIO;
274 intr_establish(ia->ia_irq, IST_EDGE, &sc->sc_ih);
275
276 sbdsp_attach(&sc->sc_sbdsp);
277
278 if (audio_hardware_attach(&sb_hw_if, (caddr_t)&sc->sc_sbdsp) != 0)
279 printf("sb: could not attach to audio pseudo-device driver\n");
280 }
281
282 /*
283 * Various routines to interface to higher level audio driver
284 */
285
286 int
287 sbopen(dev, flags)
288 dev_t dev;
289 int flags;
290 {
291 struct sb_softc *sc;
292 int unit = AUDIOUNIT(dev);
293
294 if (unit >= sbcd.cd_ndevs)
295 return ENODEV;
296
297 sc = sbcd.cd_devs[unit];
298 if (!sc)
299 return ENXIO;
300
301 return sbdsp_open(&sc->sc_sbdsp, dev, flags);
302 }
303
304 int
305 sb_getdev(addr, retp)
306 caddr_t addr;
307 struct audio_device *retp;
308 {
309 *retp = sb_device;
310 return 0;
311 }
312