sb.c revision 1.18 1 /* $NetBSD: sb.c,v 1.18 1995/02/28 21:47:42 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.18 1995/02/28 21:47:42 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 printf("sb: configured irq %d invalid\n", ia->ia_irq);
205 return 0;
206 }
207 }
208 else {
209 if (!SB_IRQ_VALID(ia->ia_irq)) {
210 printf("sb: configured irq %d invalid\n", ia->ia_irq);
211 return 0;
212 }
213 }
214
215 sc->sc_sbdsp.sc_irq = ia->ia_irq;
216 sc->sc_sbdsp.sc_drq = ia->ia_drq;
217
218 if (ISSBPROCLASS(&sc->sc_sbdsp))
219 ia->ia_iosize = SBP_NPORT;
220 else
221 ia->ia_iosize = SB_NPORT;
222 return 1;
223 }
224
225 #ifdef NEWCONFIG
226 void
227 sbforceintr(aux)
228 void *aux;
229 {
230 static char dmabuf;
231 struct isa_attach_args *ia = aux;
232 u_short iobase = ia->ia_iobase;
233
234 /*
235 * Set up a DMA read of one byte.
236 * XXX Note that at this point we haven't called
237 * at_setup_dmachan(). This is okay because it just
238 * allocates a buffer in case it needs to make a copy,
239 * and it won't need to make a copy for a 1 byte buffer.
240 * (I think that calling at_setup_dmachan() should be optional;
241 * if you don't call it, it will be called the first time
242 * it is needed (and you pay the latency). Also, you might
243 * never need the buffer anyway.)
244 */
245 at_dma(B_READ, &dmabuf, 1, ia->ia_drq);
246 if (sbdsp_wdsp(iobase, SB_DSP_RDMA) == 0) {
247 (void)sbdsp_wdsp(iobase, 0);
248 (void)sbdsp_wdsp(iobase, 0);
249 }
250 }
251 #endif
252
253 /*
254 * Attach hardware to driver, attach hardware driver to audio
255 * pseudo-device driver .
256 */
257 void
258 sbattach(parent, self, aux)
259 struct device *parent, *self;
260 void *aux;
261 {
262 register struct sb_softc *sc = (struct sb_softc *)self;
263 struct isa_attach_args *ia = (struct isa_attach_args *)aux;
264 register u_short iobase = ia->ia_iobase;
265
266 #ifdef NEWCONFIG
267 isa_establish(&sc->sc_id, &sc->sc_dev);
268 #endif
269 sc->sc_ih.ih_fun = sbdsp_intr;
270 sc->sc_ih.ih_arg = &sc->sc_sbdsp;
271 sc->sc_ih.ih_level = IPL_BIO;
272 intr_establish(ia->ia_irq, IST_EDGE, &sc->sc_ih);
273
274 sbdsp_attach(&sc->sc_sbdsp);
275
276 if (audio_hardware_attach(&sb_hw_if, (caddr_t)&sc->sc_sbdsp) != 0)
277 printf("sb: could not attach to audio pseudo-device driver\n");
278 }
279
280 /*
281 * Various routines to interface to higher level audio driver
282 */
283
284 int
285 sbopen(dev, flags)
286 dev_t dev;
287 int flags;
288 {
289 struct sb_softc *sc;
290 int unit = AUDIOUNIT(dev);
291
292 if (unit >= sbcd.cd_ndevs)
293 return ENODEV;
294
295 sc = sbcd.cd_devs[unit];
296 if (!sc)
297 return ENXIO;
298
299 return sbdsp_open(&sc->sc_sbdsp, dev, flags);
300 }
301
302 int
303 sb_getdev(addr, retp)
304 caddr_t addr;
305 struct audio_device *retp;
306 {
307 *retp = sb_device;
308 return 0;
309 }
310