sb.c revision 1.17 1 1.17 brezak /* $NetBSD: sb.c,v 1.17 1995/02/21 02:28:06 brezak Exp $ */
2 1.10 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1991-1993 Regents of the University of California.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the Computer Systems
18 1.1 cgd * Engineering Group at Lawrence Berkeley Laboratory.
19 1.1 cgd * 4. Neither the name of the University nor of the Laboratory may be used
20 1.1 cgd * to endorse or promote products derived from this software without
21 1.1 cgd * specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.17 brezak *
35 1.17 brezak * $Id: sb.c,v 1.17 1995/02/21 02:28:06 brezak Exp $
36 1.1 cgd */
37 1.1 cgd
38 1.1 cgd #include <sys/param.h>
39 1.1 cgd #include <sys/systm.h>
40 1.1 cgd #include <sys/errno.h>
41 1.1 cgd #include <sys/ioctl.h>
42 1.1 cgd #include <sys/syslog.h>
43 1.6 mycroft #include <sys/device.h>
44 1.17 brezak #include <sys/proc.h>
45 1.1 cgd
46 1.1 cgd #include <machine/cpu.h>
47 1.1 cgd #include <machine/pio.h>
48 1.1 cgd
49 1.17 brezak #include <sys/audioio.h>
50 1.17 brezak #include <dev/audio_if.h>
51 1.17 brezak
52 1.6 mycroft #include <i386/isa/isavar.h>
53 1.8 mycroft #include <i386/isa/dmavar.h>
54 1.17 brezak #include <i386/isa/icu.h>
55 1.1 cgd
56 1.17 brezak #include <i386/isa/sbdspvar.h>
57 1.17 brezak #include <i386/isa/sbreg.h>
58 1.17 brezak
59 1.17 brezak #define DEBUG /*XXX*/
60 1.17 brezak #ifdef DEBUG
61 1.17 brezak #define DPRINTF(x) if (sbdebug) printf x
62 1.17 brezak int sbdebug = 0;
63 1.17 brezak #else
64 1.17 brezak #define DPRINTF(x)
65 1.17 brezak #endif
66 1.1 cgd
67 1.1 cgd struct sb_softc {
68 1.17 brezak struct device sc_dev; /* base device */
69 1.17 brezak struct isadev sc_id; /* ISA device */
70 1.17 brezak struct intrhand sc_ih; /* interrupt vectoring */
71 1.17 brezak
72 1.17 brezak struct sbdsp_softc sc_sbdsp;
73 1.1 cgd };
74 1.1 cgd
75 1.17 brezak int sbprobe();
76 1.17 brezak void sbattach __P((struct device *, struct device *, void *));
77 1.1 cgd
78 1.17 brezak struct cfdriver sbcd = {
79 1.17 brezak NULL, "sb", sbprobe, sbattach, DV_DULL, sizeof(struct sb_softc)
80 1.17 brezak };
81 1.1 cgd
82 1.17 brezak struct audio_device sb_device = {
83 1.17 brezak "SoundBlaster",
84 1.17 brezak "x",
85 1.17 brezak "sb"
86 1.17 brezak };
87 1.1 cgd
88 1.17 brezak int sbopen __P((dev_t, int));
89 1.1 cgd
90 1.17 brezak int sbprobe();
91 1.17 brezak void sbattach();
92 1.1 cgd
93 1.17 brezak int sb_getdev __P((caddr_t, struct audio_device *));
94 1.1 cgd
95 1.17 brezak /*
96 1.17 brezak * Define our interface to the higher level audio driver.
97 1.17 brezak */
98 1.17 brezak
99 1.17 brezak struct audio_hw_if sb_hw_if = {
100 1.17 brezak sbopen,
101 1.17 brezak sbdsp_close,
102 1.17 brezak NULL,
103 1.17 brezak sbdsp_set_in_sr,
104 1.17 brezak sbdsp_get_in_sr,
105 1.17 brezak sbdsp_set_out_sr,
106 1.17 brezak sbdsp_get_out_sr,
107 1.17 brezak sbdsp_query_encoding,
108 1.17 brezak sbdsp_set_encoding,
109 1.17 brezak sbdsp_get_encoding,
110 1.17 brezak sbdsp_set_precision,
111 1.17 brezak sbdsp_get_precision,
112 1.17 brezak sbdsp_set_channels,
113 1.17 brezak sbdsp_get_channels,
114 1.17 brezak sbdsp_round_blocksize,
115 1.17 brezak sbdsp_set_out_port,
116 1.17 brezak sbdsp_get_out_port,
117 1.17 brezak sbdsp_set_in_port,
118 1.17 brezak sbdsp_get_in_port,
119 1.17 brezak sbdsp_commit_settings,
120 1.17 brezak sbdsp_get_silence,
121 1.17 brezak sbdsp_expand,
122 1.17 brezak sbdsp_compress,
123 1.17 brezak sbdsp_dma_output,
124 1.17 brezak sbdsp_dma_input,
125 1.17 brezak sbdsp_haltdma,
126 1.17 brezak sbdsp_haltdma,
127 1.17 brezak sbdsp_contdma,
128 1.17 brezak sbdsp_contdma,
129 1.17 brezak sbdsp_speaker_ctl,
130 1.17 brezak sb_getdev,
131 1.17 brezak sbdsp_setfd,
132 1.17 brezak sbdsp_mixer_set_port,
133 1.17 brezak sbdsp_mixer_get_port,
134 1.17 brezak sbdsp_mixer_query_devinfo,
135 1.17 brezak 0, /* not full-duplex */
136 1.17 brezak 0
137 1.6 mycroft };
138 1.1 cgd
139 1.17 brezak /*
140 1.17 brezak * Probe / attach routines.
141 1.17 brezak */
142 1.17 brezak
143 1.17 brezak /*
144 1.17 brezak * Probe for the soundblaster hardware.
145 1.17 brezak */
146 1.1 cgd int
147 1.17 brezak sbprobe(parent, self, aux)
148 1.17 brezak struct device *parent, *self;
149 1.17 brezak void *aux;
150 1.1 cgd {
151 1.17 brezak register struct sb_softc *sc = (void *)self;
152 1.6 mycroft register struct isa_attach_args *ia = aux;
153 1.17 brezak register u_short iobase = ia->ia_iobase;
154 1.1 cgd
155 1.6 mycroft if (!SB_BASE_VALID(ia->ia_iobase)) {
156 1.6 mycroft printf("sb: configured iobase %d invalid\n", ia->ia_iobase);
157 1.6 mycroft return 0;
158 1.1 cgd }
159 1.17 brezak sc->sc_sbdsp.sc_iobase = iobase;
160 1.17 brezak if (sbdsp_probe(&sc->sc_sbdsp) == 0) {
161 1.17 brezak DPRINTF(("sb: sbdsp probe failed\n"));
162 1.6 mycroft return 0;
163 1.1 cgd }
164 1.17 brezak
165 1.1 cgd /*
166 1.1 cgd * Cannot auto-discover DMA channel.
167 1.1 cgd */
168 1.17 brezak if (ISSBPROCLASS(&sc->sc_sbdsp)) {
169 1.17 brezak if (!SBP_DRQ_VALID(ia->ia_drq)) {
170 1.17 brezak printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
171 1.17 brezak return 0;
172 1.17 brezak }
173 1.17 brezak }
174 1.17 brezak else {
175 1.17 brezak if (!SB_DRQ_VALID(ia->ia_drq)) {
176 1.17 brezak printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
177 1.17 brezak return 0;
178 1.17 brezak }
179 1.1 cgd }
180 1.17 brezak
181 1.6 mycroft #ifdef NEWCONFIG
182 1.1 cgd /*
183 1.1 cgd * If the IRQ wasn't compiled in, auto-detect it.
184 1.1 cgd */
185 1.1 cgd if (ia->ia_irq == IRQUNK) {
186 1.1 cgd ia->ia_irq = isa_discoverintr(sbforceintr, aux);
187 1.17 brezak sbdsp_reset(&sc->sc_sbdsp);
188 1.17 brezak if (ISSBPROCLASS(&sc->sc_sbdsp)) {
189 1.17 brezak if (!SBP_IRQ_VALID(ia->ia_irq)) {
190 1.17 brezak printf("sb: couldn't auto-detect interrupt");
191 1.17 brezak return 0;
192 1.17 brezak }
193 1.17 brezak }
194 1.17 brezak else {
195 1.17 brezak if (!SB_IRQ_VALID(ia->ia_irq)) {
196 1.17 brezak printf("sb: couldn't auto-detect interrupt");
197 1.17 brezak return 0;
198 1.17 brezak }
199 1.17 brezak }
200 1.17 brezak } else
201 1.17 brezak #endif
202 1.17 brezak if (ISSBPROCLASS(&sc->sc_sbdsp)) {
203 1.17 brezak if (!SBP_IRQ_VALID(ia->ia_irq)) {
204 1.17 brezak int irq = ffs(ia->ia_irq) - 1;
205 1.17 brezak printf("sb: configured irq %d invalid\n", irq);
206 1.17 brezak return 0;
207 1.17 brezak }
208 1.17 brezak }
209 1.17 brezak else {
210 1.1 cgd if (!SB_IRQ_VALID(ia->ia_irq)) {
211 1.17 brezak int irq = ffs(ia->ia_irq) - 1;
212 1.17 brezak printf("sb: configured irq %d invalid\n", irq);
213 1.6 mycroft return 0;
214 1.1 cgd }
215 1.1 cgd }
216 1.17 brezak
217 1.17 brezak sc->sc_sbdsp.sc_irq = ffs(ia->ia_irq) - 1;
218 1.17 brezak sc->sc_sbdsp.sc_drq = ia->ia_drq;
219 1.17 brezak
220 1.17 brezak if (ISSBPROCLASS(&sc->sc_sbdsp))
221 1.17 brezak ia->ia_iosize = SBP_NPORT;
222 1.17 brezak else
223 1.17 brezak ia->ia_iosize = SB_NPORT;
224 1.6 mycroft return 1;
225 1.1 cgd }
226 1.1 cgd
227 1.6 mycroft #ifdef NEWCONFIG
228 1.1 cgd void
229 1.6 mycroft sbforceintr(aux)
230 1.6 mycroft void *aux;
231 1.1 cgd {
232 1.1 cgd static char dmabuf;
233 1.6 mycroft struct isa_attach_args *ia = aux;
234 1.17 brezak u_short iobase = ia->ia_iobase;
235 1.6 mycroft
236 1.1 cgd /*
237 1.1 cgd * Set up a DMA read of one byte.
238 1.1 cgd * XXX Note that at this point we haven't called
239 1.1 cgd * at_setup_dmachan(). This is okay because it just
240 1.1 cgd * allocates a buffer in case it needs to make a copy,
241 1.1 cgd * and it won't need to make a copy for a 1 byte buffer.
242 1.1 cgd * (I think that calling at_setup_dmachan() should be optional;
243 1.1 cgd * if you don't call it, it will be called the first time
244 1.1 cgd * it is needed (and you pay the latency). Also, you might
245 1.1 cgd * never need the buffer anyway.)
246 1.1 cgd */
247 1.15 mycroft at_dma(B_READ, &dmabuf, 1, ia->ia_drq);
248 1.17 brezak if (sbdsp_wdsp(iobase, SB_DSP_RDMA) == 0) {
249 1.17 brezak (void)sbdsp_wdsp(iobase, 0);
250 1.17 brezak (void)sbdsp_wdsp(iobase, 0);
251 1.1 cgd }
252 1.1 cgd }
253 1.6 mycroft #endif
254 1.1 cgd
255 1.17 brezak /*
256 1.17 brezak * Attach hardware to driver, attach hardware driver to audio
257 1.17 brezak * pseudo-device driver .
258 1.17 brezak */
259 1.1 cgd void
260 1.1 cgd sbattach(parent, self, aux)
261 1.1 cgd struct device *parent, *self;
262 1.1 cgd void *aux;
263 1.1 cgd {
264 1.1 cgd register struct sb_softc *sc = (struct sb_softc *)self;
265 1.1 cgd struct isa_attach_args *ia = (struct isa_attach_args *)aux;
266 1.17 brezak register u_short iobase = ia->ia_iobase;
267 1.6 mycroft
268 1.6 mycroft #ifdef NEWCONFIG
269 1.1 cgd isa_establish(&sc->sc_id, &sc->sc_dev);
270 1.7 mycroft #endif
271 1.17 brezak sc->sc_ih.ih_fun = sbdsp_intr;
272 1.17 brezak sc->sc_ih.ih_arg = &sc->sc_sbdsp;
273 1.7 mycroft sc->sc_ih.ih_level = IPL_BIO;
274 1.16 mycroft intr_establish(ia->ia_irq, IST_EDGE, &sc->sc_ih);
275 1.1 cgd
276 1.17 brezak sbdsp_attach(&sc->sc_sbdsp);
277 1.1 cgd
278 1.17 brezak if (audio_hardware_attach(&sb_hw_if, (caddr_t)&sc->sc_sbdsp) != 0)
279 1.17 brezak printf("sb: could not attach to audio pseudo-device driver\n");
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd /*
283 1.17 brezak * Various routines to interface to higher level audio driver
284 1.1 cgd */
285 1.1 cgd
286 1.1 cgd int
287 1.17 brezak sbopen(dev, flags)
288 1.17 brezak dev_t dev;
289 1.17 brezak int flags;
290 1.1 cgd {
291 1.17 brezak struct sb_softc *sc;
292 1.17 brezak int unit = AUDIOUNIT(dev);
293 1.17 brezak
294 1.17 brezak if (unit >= sbcd.cd_ndevs)
295 1.17 brezak return ENODEV;
296 1.17 brezak
297 1.17 brezak sc = sbcd.cd_devs[unit];
298 1.17 brezak if (!sc)
299 1.17 brezak return ENXIO;
300 1.17 brezak
301 1.17 brezak return sbdsp_open(&sc->sc_sbdsp, dev, flags);
302 1.1 cgd }
303 1.1 cgd
304 1.1 cgd int
305 1.17 brezak sb_getdev(addr, retp)
306 1.17 brezak caddr_t addr;
307 1.17 brezak struct audio_device *retp;
308 1.1 cgd {
309 1.17 brezak *retp = sb_device;
310 1.6 mycroft return 0;
311 1.1 cgd }
312