sb.c revision 1.16 1 1.16 mycroft /* $NetBSD: sb.c,v 1.16 1995/01/03 01:31:01 mycroft 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.1 cgd */
35 1.1 cgd
36 1.1 cgd #include <sys/param.h>
37 1.1 cgd #include <sys/systm.h>
38 1.1 cgd #include <sys/errno.h>
39 1.1 cgd #include <sys/ioctl.h>
40 1.1 cgd #include <sys/syslog.h>
41 1.6 mycroft #include <sys/device.h>
42 1.15 mycroft #include <sys/buf.h>
43 1.1 cgd
44 1.1 cgd #include <machine/cpu.h>
45 1.1 cgd #include <machine/pio.h>
46 1.1 cgd
47 1.6 mycroft #include <i386/isa/isavar.h>
48 1.8 mycroft #include <i386/isa/dmavar.h>
49 1.1 cgd
50 1.1 cgd #include "sbreg.h"
51 1.1 cgd
52 1.1 cgd /*
53 1.1 cgd * Software state, per SoundBlaster card.
54 1.1 cgd * The soundblaster has multiple functionality, which we must demultiplex.
55 1.1 cgd * One approach is to have one major device number for the soundblaster card,
56 1.1 cgd * and use different minor numbers to indicate which hardware function
57 1.1 cgd * we want. This would make for one large driver. Instead our approach
58 1.1 cgd * is to partition the design into a set of drivers that share an underlying
59 1.1 cgd * piece of hardware. Most things are hard to share, for example, the audio
60 1.1 cgd * and midi ports. For audio, we might want to mix two processes' signals,
61 1.1 cgd * and for midi we might want to merge streams (this is hard due to
62 1.1 cgd * running status). Moreover, we should be able to re-use the high-level
63 1.1 cgd * modules with other kinds of hardware. In this module, we only handle the
64 1.1 cgd * most basic communications with the sb card.
65 1.1 cgd */
66 1.1 cgd struct sb_softc {
67 1.1 cgd struct device sc_dev; /* base device */
68 1.1 cgd struct isadev sc_id; /* ISA device */
69 1.7 mycroft struct intrhand sc_ih; /* interrupt vectoring */
70 1.6 mycroft
71 1.14 mycroft int sc_open; /* reference count of open calls */
72 1.14 mycroft int sc_dmachan; /* dma channel */
73 1.14 mycroft int sc_locked; /* true when doing HS DMA */
74 1.14 mycroft int sc_iobase; /* I/O port base address */
75 1.14 mycroft int sc_adacmode; /* low/high speed mode indicator */
76 1.1 cgd #define SB_ADAC_LS 0
77 1.1 cgd #define SB_ADAC_HS 1
78 1.14 mycroft int sc_adactc; /* current adac time constant */
79 1.1 cgd u_long sc_interrupts; /* number of interrupts taken */
80 1.1 cgd void (*sc_intr)(void*); /* dma completion intr handler */
81 1.1 cgd void (*sc_mintr)(void*, int);/* midi input intr handler */
82 1.1 cgd void *sc_arg; /* arg for sc_intr() */
83 1.1 cgd };
84 1.1 cgd
85 1.6 mycroft int sbreset __P((struct sb_softc *));
86 1.6 mycroft void sb_spkron __P((struct sb_softc *));
87 1.6 mycroft void sb_spkroff __P((struct sb_softc *));
88 1.1 cgd
89 1.14 mycroft static int wdsp(int iobase, int v);
90 1.14 mycroft static int rdsp(int iobase);
91 1.1 cgd
92 1.6 mycroft #define splsb splhigh /* XXX */
93 1.6 mycroft struct sb_softc *sb_softc; /* XXX */
94 1.1 cgd
95 1.1 cgd #ifndef NEWCONFIG
96 1.1 cgd #define at_dma(flags, ptr, cc, chan) isa_dmastart(flags, ptr, cc, chan)
97 1.1 cgd #endif
98 1.1 cgd
99 1.1 cgd struct {
100 1.1 cgd int wdsp;
101 1.1 cgd int rdsp;
102 1.1 cgd int wmidi;
103 1.1 cgd } sberr;
104 1.1 cgd
105 1.11 mycroft int sbprobe __P((struct device *, void *, void *));
106 1.1 cgd #ifdef NEWCONFIG
107 1.1 cgd void sbforceintr(void *);
108 1.6 mycroft #endif
109 1.11 mycroft void sbattach __P((struct device *, struct device *, void *));
110 1.11 mycroft int sbintr __P((struct sb_softc *));
111 1.1 cgd
112 1.6 mycroft struct cfdriver sbcd = {
113 1.6 mycroft NULL, "sb", sbprobe, sbattach, DV_DULL, sizeof(struct sb_softc)
114 1.6 mycroft };
115 1.1 cgd
116 1.1 cgd int
117 1.11 mycroft sbprobe(parent, match, aux)
118 1.11 mycroft struct device *parent;
119 1.11 mycroft void *match, *aux;
120 1.1 cgd {
121 1.11 mycroft register struct sb_softc *sc = match;
122 1.6 mycroft register struct isa_attach_args *ia = aux;
123 1.14 mycroft register int iobase = ia->ia_iobase;
124 1.1 cgd
125 1.6 mycroft if (!SB_BASE_VALID(ia->ia_iobase)) {
126 1.6 mycroft printf("sb: configured iobase %d invalid\n", ia->ia_iobase);
127 1.6 mycroft return 0;
128 1.1 cgd }
129 1.6 mycroft sc->sc_iobase = iobase;
130 1.6 mycroft if (sbreset(sc) < 0) {
131 1.1 cgd printf("sb: couldn't reset card\n");
132 1.6 mycroft return 0;
133 1.1 cgd }
134 1.1 cgd /*
135 1.1 cgd * Cannot auto-discover DMA channel.
136 1.1 cgd */
137 1.1 cgd if (!SB_DRQ_VALID(ia->ia_drq)) {
138 1.1 cgd printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
139 1.6 mycroft return 0;
140 1.1 cgd }
141 1.6 mycroft #ifdef NEWCONFIG
142 1.1 cgd /*
143 1.1 cgd * If the IRQ wasn't compiled in, auto-detect it.
144 1.1 cgd */
145 1.1 cgd if (ia->ia_irq == IRQUNK) {
146 1.1 cgd ia->ia_irq = isa_discoverintr(sbforceintr, aux);
147 1.6 mycroft sbreset(iobase);
148 1.1 cgd if (!SB_IRQ_VALID(ia->ia_irq)) {
149 1.1 cgd printf("sb: couldn't auto-detect interrupt");
150 1.6 mycroft return 0;
151 1.1 cgd }
152 1.6 mycroft } else
153 1.6 mycroft #endif
154 1.6 mycroft if (!SB_IRQ_VALID(ia->ia_irq)) {
155 1.12 mycroft int irq = ia->ia_irq;
156 1.1 cgd printf("sb: configured irq %d invalid\n", irq);
157 1.6 mycroft return 0;
158 1.1 cgd }
159 1.6 mycroft ia->ia_iosize = SB_NPORT;
160 1.6 mycroft return 1;
161 1.1 cgd }
162 1.1 cgd
163 1.6 mycroft #ifdef NEWCONFIG
164 1.1 cgd void
165 1.6 mycroft sbforceintr(aux)
166 1.6 mycroft void *aux;
167 1.1 cgd {
168 1.1 cgd static char dmabuf;
169 1.6 mycroft struct isa_attach_args *ia = aux;
170 1.14 mycroft int iobase = ia->ia_iobase;
171 1.6 mycroft
172 1.1 cgd /*
173 1.1 cgd * Set up a DMA read of one byte.
174 1.1 cgd * XXX Note that at this point we haven't called
175 1.1 cgd * at_setup_dmachan(). This is okay because it just
176 1.1 cgd * allocates a buffer in case it needs to make a copy,
177 1.1 cgd * and it won't need to make a copy for a 1 byte buffer.
178 1.1 cgd * (I think that calling at_setup_dmachan() should be optional;
179 1.1 cgd * if you don't call it, it will be called the first time
180 1.1 cgd * it is needed (and you pay the latency). Also, you might
181 1.1 cgd * never need the buffer anyway.)
182 1.1 cgd */
183 1.15 mycroft at_dma(B_READ, &dmabuf, 1, ia->ia_drq);
184 1.6 mycroft if (wdsp(iobase, SB_DSP_RDMA) == 0) {
185 1.6 mycroft (void)wdsp(iobase, 0);
186 1.6 mycroft (void)wdsp(iobase, 0);
187 1.1 cgd }
188 1.1 cgd }
189 1.6 mycroft #endif
190 1.1 cgd
191 1.1 cgd void
192 1.1 cgd sbattach(parent, self, aux)
193 1.1 cgd struct device *parent, *self;
194 1.1 cgd void *aux;
195 1.1 cgd {
196 1.1 cgd register struct sb_softc *sc = (struct sb_softc *)self;
197 1.1 cgd struct isa_attach_args *ia = (struct isa_attach_args *)aux;
198 1.14 mycroft register int iobase = ia->ia_iobase;
199 1.1 cgd register int vers;
200 1.1 cgd
201 1.1 cgd /* XXX */
202 1.1 cgd sb_softc = sc;
203 1.1 cgd
204 1.6 mycroft sc->sc_iobase = iobase;
205 1.1 cgd sc->sc_dmachan = ia->ia_drq;
206 1.1 cgd sc->sc_locked = 0;
207 1.6 mycroft
208 1.6 mycroft #ifdef NEWCONFIG
209 1.1 cgd isa_establish(&sc->sc_id, &sc->sc_dev);
210 1.7 mycroft #endif
211 1.1 cgd sc->sc_ih.ih_fun = sbintr;
212 1.7 mycroft sc->sc_ih.ih_arg = sc;
213 1.7 mycroft sc->sc_ih.ih_level = IPL_BIO;
214 1.16 mycroft intr_establish(ia->ia_irq, IST_EDGE, &sc->sc_ih);
215 1.1 cgd
216 1.7 mycroft #ifdef NEWCONFIG
217 1.1 cgd /*
218 1.1 cgd * We limit DMA transfers to a page, and use the generic DMA handling
219 1.1 cgd * code in isa.c. This code can end up copying a buffer, but since
220 1.1 cgd * the audio driver uses relative small buffers this isn't likely.
221 1.1 cgd *
222 1.1 cgd * This allocation scheme means that the maximum transfer is limited
223 1.1 cgd * by the page size (rather than 64k). This is reasonable. For 4K
224 1.1 cgd * pages, the transfer time at 48KHz is 4096 / 48000 = 85ms. This
225 1.1 cgd * is plenty long enough to amortize any fixed time overhead.
226 1.1 cgd */
227 1.1 cgd at_setup_dmachan(sc->sc_dmachan, NBPG);
228 1.1 cgd #endif
229 1.1 cgd
230 1.6 mycroft vers = sbversion(sc);
231 1.6 mycroft printf(": dsp v%d.%d\n", vers >> 8, vers & 0xff);
232 1.1 cgd }
233 1.1 cgd
234 1.6 mycroft #define SBUNIT(x) (minor(x) & 0xf)
235 1.1 cgd
236 1.1 cgd struct sb_softc *
237 1.1 cgd sbopen()
238 1.1 cgd {
239 1.6 mycroft /* XXXX */
240 1.1 cgd struct sb_softc *sc = sb_softc;
241 1.1 cgd
242 1.1 cgd if (sc == 0)
243 1.1 cgd return 0;
244 1.1 cgd
245 1.6 mycroft if (sc->sc_open == 0 && sbreset(sc) == 0) {
246 1.1 cgd sc->sc_open = 1;
247 1.1 cgd sc->sc_mintr = 0;
248 1.1 cgd sc->sc_intr = 0;
249 1.6 mycroft return sc;
250 1.1 cgd }
251 1.6 mycroft return 0;
252 1.1 cgd }
253 1.1 cgd
254 1.1 cgd void
255 1.6 mycroft sbclose(sc)
256 1.6 mycroft struct sb_softc *sc;
257 1.1 cgd {
258 1.6 mycroft
259 1.1 cgd sc->sc_open = 0;
260 1.1 cgd sb_spkroff(sc);
261 1.1 cgd sc->sc_intr = 0;
262 1.1 cgd sc->sc_mintr = 0;
263 1.1 cgd /* XXX this will turn off any dma */
264 1.6 mycroft sbreset(sc);
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd /*
268 1.1 cgd * Write a byte to the dsp.
269 1.1 cgd * XXX We are at the mercy of the card as we use a
270 1.1 cgd * polling loop and wait until it can take the byte.
271 1.1 cgd */
272 1.1 cgd static int
273 1.14 mycroft wdsp(int iobase, int v)
274 1.1 cgd {
275 1.1 cgd register int i;
276 1.1 cgd
277 1.1 cgd for (i = 100; --i >= 0; ) {
278 1.15 mycroft register u_char x;
279 1.15 mycroft x = inb(iobase + SBP_DSP_WSTAT);
280 1.15 mycroft delay(20);
281 1.15 mycroft if ((x & SB_DSP_BUSY) != 0)
282 1.1 cgd continue;
283 1.6 mycroft outb(iobase + SBP_DSP_WRITE, v);
284 1.15 mycroft delay(20);
285 1.6 mycroft return 0;
286 1.1 cgd }
287 1.1 cgd ++sberr.wdsp;
288 1.6 mycroft return -1;
289 1.1 cgd }
290 1.1 cgd
291 1.1 cgd /*
292 1.1 cgd * Read a byte from the DSP, using polling.
293 1.1 cgd */
294 1.1 cgd int
295 1.14 mycroft rdsp(int iobase)
296 1.1 cgd {
297 1.1 cgd register int i;
298 1.1 cgd
299 1.1 cgd for (i = 100; --i >= 0; ) {
300 1.15 mycroft register u_char x;
301 1.15 mycroft x = inb(iobase + SBP_DSP_RSTAT);
302 1.15 mycroft delay(20);
303 1.15 mycroft if ((x & SB_DSP_READY) == 0)
304 1.1 cgd continue;
305 1.15 mycroft x = inb(iobase + SBP_DSP_READ);
306 1.15 mycroft delay(20);
307 1.15 mycroft return x;
308 1.1 cgd }
309 1.1 cgd ++sberr.rdsp;
310 1.6 mycroft return -1;
311 1.1 cgd }
312 1.1 cgd
313 1.1 cgd /*
314 1.1 cgd * Reset the card.
315 1.1 cgd * Return non-zero if the card isn't detected.
316 1.1 cgd */
317 1.1 cgd int
318 1.6 mycroft sbreset(sc)
319 1.6 mycroft struct sb_softc *sc;
320 1.1 cgd {
321 1.14 mycroft register int iobase = sc->sc_iobase;
322 1.1 cgd register int i;
323 1.6 mycroft
324 1.1 cgd /*
325 1.1 cgd * See SBK, section 11.3.
326 1.1 cgd * We pulse a reset signal into the card.
327 1.1 cgd * Gee, what a brilliant hardware design.
328 1.1 cgd */
329 1.6 mycroft outb(iobase + SBP_DSP_RESET, 1);
330 1.15 mycroft delay(20);
331 1.6 mycroft outb(iobase + SBP_DSP_RESET, 0);
332 1.15 mycroft delay(20);
333 1.6 mycroft if (rdsp(iobase) != SB_MAGIC)
334 1.6 mycroft return -1;
335 1.6 mycroft return 0;
336 1.1 cgd }
337 1.1 cgd
338 1.1 cgd /*
339 1.1 cgd * Turn on the speaker. The SBK documention says this operation
340 1.1 cgd * can take up to 1/10 of a second. Higher level layers should
341 1.1 cgd * probably let the task sleep for this amount of time after
342 1.1 cgd * calling here. Otherwise, things might not work (because
343 1.1 cgd * wdsp() and rdsp() will probably timeout.)
344 1.1 cgd *
345 1.1 cgd * These engineers had their heads up their ass when
346 1.1 cgd * they designed this card.
347 1.1 cgd */
348 1.1 cgd void
349 1.6 mycroft sb_spkron(sc)
350 1.6 mycroft struct sb_softc *sc;
351 1.1 cgd {
352 1.6 mycroft
353 1.6 mycroft (void)wdsp(sc->sc_iobase, SB_DSP_SPKR_ON);
354 1.6 mycroft /* XXX bogus */
355 1.5 mycroft delay(1000);
356 1.1 cgd }
357 1.1 cgd
358 1.1 cgd /*
359 1.1 cgd * Turn off the speaker; see comment above.
360 1.1 cgd */
361 1.1 cgd void
362 1.6 mycroft sb_spkroff(sc)
363 1.6 mycroft struct sb_softc *sc;
364 1.1 cgd {
365 1.6 mycroft
366 1.6 mycroft (void)wdsp(sc->sc_iobase, SB_DSP_SPKR_OFF);
367 1.1 cgd }
368 1.1 cgd
369 1.1 cgd /*
370 1.1 cgd * Read the version number out of the card. Return major code
371 1.1 cgd * in high byte, and minor code in low byte.
372 1.1 cgd */
373 1.1 cgd int
374 1.6 mycroft sbversion(sc)
375 1.6 mycroft struct sb_softc *sc;
376 1.1 cgd {
377 1.14 mycroft register int iobase = sc->sc_iobase;
378 1.1 cgd int v;
379 1.1 cgd
380 1.6 mycroft if (wdsp(iobase, SB_DSP_VERSION) < 0)
381 1.6 mycroft return 0;
382 1.6 mycroft v = rdsp(iobase) << 8;
383 1.6 mycroft v |= rdsp(iobase);
384 1.1 cgd return ((v >= 0) ? v : 0);
385 1.1 cgd }
386 1.1 cgd
387 1.1 cgd /*
388 1.1 cgd * Halt a DMA in progress. A low-speed transfer can be
389 1.1 cgd * resumed with sb_contdma().
390 1.1 cgd */
391 1.1 cgd void
392 1.6 mycroft sb_haltdma(sc)
393 1.6 mycroft struct sb_softc *sc;
394 1.1 cgd {
395 1.6 mycroft
396 1.1 cgd if (sc->sc_locked)
397 1.6 mycroft sbreset(sc);
398 1.1 cgd else
399 1.6 mycroft (void)wdsp(sc->sc_iobase, SB_DSP_HALT);
400 1.1 cgd }
401 1.1 cgd
402 1.1 cgd void
403 1.6 mycroft sb_contdma(sc)
404 1.6 mycroft struct sb_softc *sc;
405 1.1 cgd {
406 1.6 mycroft
407 1.6 mycroft (void)wdsp(sc->sc_iobase, SB_DSP_CONT);
408 1.1 cgd }
409 1.1 cgd
410 1.1 cgd /*
411 1.1 cgd * Time constant routines follow. See SBK, section 12.
412 1.1 cgd * Although they don't come out and say it (in the docs),
413 1.1 cgd * the card clearly uses a 1MHz countdown timer, as the
414 1.1 cgd * low-speed formula (p. 12-4) is:
415 1.1 cgd * tc = 256 - 10^6 / sr
416 1.1 cgd * In high-speed mode, the constant is the upper byte of a 16-bit counter,
417 1.1 cgd * and a 256MHz clock is used:
418 1.1 cgd * tc = 65536 - 256 * 10^ 6 / sr
419 1.1 cgd * Since we can only use the upper byte of the HS TC, the two formulae
420 1.1 cgd * are equivalent. (Why didn't they say so?) E.g.,
421 1.1 cgd * (65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
422 1.1 cgd *
423 1.1 cgd * The crossover point (from low- to high-speed modes) is different
424 1.1 cgd * for the SBPRO and SB20. The table on p. 12-5 gives the following data:
425 1.1 cgd *
426 1.1 cgd * SBPRO SB20
427 1.1 cgd * ----- --------
428 1.1 cgd * input ls min 4 KHz 4 HJz
429 1.1 cgd * input ls max 23 KHz 13 KHz
430 1.1 cgd * input hs max 44.1 KHz 15 KHz
431 1.1 cgd * output ls min 4 KHz 4 KHz
432 1.1 cgd * output ls max 23 KHz 23 KHz
433 1.1 cgd * output hs max 44.1 KHz 44.1 KHz
434 1.1 cgd */
435 1.1 cgd #define SB_LS_MIN 0x06 /* 4000 Hz */
436 1.1 cgd #ifdef SBPRO
437 1.1 cgd #define SB_ADC_LS_MAX 0xd4 /* 22727 Hz */
438 1.1 cgd #define SB_ADC_HS_MAX 0xe9 /* 43478 Hz */
439 1.1 cgd #else
440 1.1 cgd #define SB_ADC_LS_MAX 0xb3 /* 12987 Hz */
441 1.1 cgd #define SB_ADC_HS_MAX 0xbd /* 14925 Hz */
442 1.1 cgd #endif
443 1.1 cgd #define SB_DAC_LS_MAX 0xd4 /* 22727 Hz */
444 1.1 cgd #define SB_DAC_HS_MAX 0xe9 /* 43478 Hz */
445 1.1 cgd
446 1.1 cgd /*
447 1.1 cgd * Convert a linear sampling rate into the DAC time constant.
448 1.1 cgd * Set *mode to indicate the high/low-speed DMA operation.
449 1.1 cgd * Because of limitations of the card, not all rates are possible.
450 1.1 cgd * We return the time constant of the closest possible rate.
451 1.1 cgd * The sampling rate limits are different for the DAC and ADC,
452 1.1 cgd * so isdac indicates output, and !isdac indicates input.
453 1.1 cgd */
454 1.1 cgd int
455 1.6 mycroft sb_srtotc(sr, mode, isdac)
456 1.6 mycroft int sr;
457 1.6 mycroft int *mode;
458 1.6 mycroft int isdac;
459 1.1 cgd {
460 1.1 cgd register int tc = 256 - 1000000 / sr;
461 1.1 cgd
462 1.1 cgd if (tc < SB_LS_MIN) {
463 1.1 cgd tc = SB_LS_MIN;
464 1.1 cgd *mode = SB_ADAC_LS;
465 1.1 cgd } else if (isdac) {
466 1.1 cgd if (tc < SB_DAC_LS_MAX)
467 1.1 cgd *mode = SB_ADAC_LS;
468 1.1 cgd else {
469 1.1 cgd *mode = SB_ADAC_HS;
470 1.1 cgd if (tc > SB_DAC_HS_MAX)
471 1.1 cgd tc = SB_DAC_HS_MAX;
472 1.1 cgd }
473 1.1 cgd } else {
474 1.1 cgd if (tc < SB_ADC_LS_MAX)
475 1.1 cgd *mode = SB_ADAC_LS;
476 1.1 cgd else {
477 1.1 cgd *mode = SB_ADAC_HS;
478 1.1 cgd if (tc > SB_ADC_HS_MAX)
479 1.1 cgd tc = SB_ADC_HS_MAX;
480 1.1 cgd }
481 1.1 cgd }
482 1.6 mycroft return tc;
483 1.1 cgd }
484 1.1 cgd
485 1.1 cgd /*
486 1.1 cgd * Convert a DAC time constant to a sampling rate.
487 1.1 cgd * See SBK, section 12.
488 1.1 cgd */
489 1.1 cgd int
490 1.6 mycroft sb_tctosr(tc)
491 1.6 mycroft int tc;
492 1.1 cgd {
493 1.1 cgd return (1000000 / (256 - tc));
494 1.1 cgd }
495 1.1 cgd
496 1.1 cgd int
497 1.6 mycroft sb_set_sr(sc, sr, isdac)
498 1.6 mycroft register struct sb_softc *sc;
499 1.6 mycroft u_long *sr;
500 1.6 mycroft int isdac;
501 1.1 cgd {
502 1.1 cgd register int tc;
503 1.1 cgd int mode;
504 1.1 cgd
505 1.1 cgd tc = sb_srtotc(*sr, &mode, isdac);
506 1.6 mycroft if (wdsp(sc->sc_iobase, SB_DSP_TIMECONST) < 0 ||
507 1.6 mycroft wdsp(sc->sc_iobase, tc) < 0)
508 1.6 mycroft return -1;
509 1.1 cgd
510 1.1 cgd *sr = sb_tctosr(tc);
511 1.1 cgd sc->sc_adacmode = mode;
512 1.1 cgd sc->sc_adactc = tc;
513 1.1 cgd
514 1.6 mycroft return 0;
515 1.1 cgd }
516 1.1 cgd
517 1.1 cgd int
518 1.6 mycroft sb_round_sr(sr, isdac)
519 1.6 mycroft u_long sr;
520 1.6 mycroft int isdac;
521 1.1 cgd {
522 1.1 cgd int mode, tc;
523 1.1 cgd
524 1.1 cgd tc = sb_srtotc(sr, &mode, isdac);
525 1.6 mycroft return sb_tctosr(tc);
526 1.1 cgd }
527 1.1 cgd
528 1.1 cgd int
529 1.6 mycroft sb_dma_input(sc, p, cc, intr, arg)
530 1.6 mycroft struct sb_softc *sc;
531 1.6 mycroft void *p;
532 1.6 mycroft int cc;
533 1.6 mycroft void (*intr)();
534 1.6 mycroft void *arg;
535 1.1 cgd {
536 1.14 mycroft register int iobase;
537 1.1 cgd
538 1.15 mycroft at_dma(B_READ, p, cc, sc->sc_dmachan);
539 1.1 cgd sc->sc_intr = intr;
540 1.1 cgd sc->sc_arg = arg;
541 1.6 mycroft iobase = sc->sc_iobase;
542 1.1 cgd --cc;
543 1.1 cgd if (sc->sc_adacmode == SB_ADAC_LS) {
544 1.6 mycroft if (wdsp(iobase, SB_DSP_RDMA) < 0 ||
545 1.6 mycroft wdsp(iobase, cc) < 0 ||
546 1.6 mycroft wdsp(iobase, cc >> 8) < 0) {
547 1.6 mycroft sbreset(sc);
548 1.6 mycroft return EIO;
549 1.1 cgd }
550 1.1 cgd } else {
551 1.6 mycroft if (wdsp(iobase, SB_DSP_BLOCKSIZE) < 0 ||
552 1.6 mycroft wdsp(iobase, cc) < 0 ||
553 1.6 mycroft wdsp(iobase, cc >> 8) < 0 ||
554 1.6 mycroft wdsp(iobase, SB_DSP_HS_INPUT) < 0) {
555 1.6 mycroft sbreset(sc);
556 1.6 mycroft return EIO;
557 1.1 cgd }
558 1.1 cgd sc->sc_locked = 1;
559 1.1 cgd }
560 1.6 mycroft return 0;
561 1.1 cgd }
562 1.1 cgd
563 1.1 cgd int
564 1.6 mycroft sb_dma_output(sc, p, cc, intr, arg)
565 1.6 mycroft struct sb_softc *sc;
566 1.6 mycroft void *p;
567 1.6 mycroft int cc;
568 1.6 mycroft void (*intr)();
569 1.6 mycroft void *arg;
570 1.1 cgd {
571 1.14 mycroft register int iobase;
572 1.1 cgd
573 1.15 mycroft at_dma(B_WRITE, p, cc, sc->sc_dmachan);
574 1.1 cgd sc->sc_intr = intr;
575 1.1 cgd sc->sc_arg = arg;
576 1.6 mycroft iobase = sc->sc_iobase;
577 1.1 cgd --cc;
578 1.1 cgd if (sc->sc_adacmode == SB_ADAC_LS) {
579 1.6 mycroft if (wdsp(iobase, SB_DSP_WDMA) < 0 ||
580 1.6 mycroft wdsp(iobase, cc) < 0 ||
581 1.6 mycroft wdsp(iobase, cc >> 8) < 0) {
582 1.6 mycroft sbreset(sc);
583 1.6 mycroft return EIO;
584 1.1 cgd }
585 1.1 cgd } else {
586 1.6 mycroft if (wdsp(iobase, SB_DSP_BLOCKSIZE) < 0 ||
587 1.6 mycroft wdsp(iobase, cc) < 0 ||
588 1.6 mycroft wdsp(iobase, cc >> 8) < 0 ||
589 1.6 mycroft wdsp(iobase, SB_DSP_HS_OUTPUT) < 0) {
590 1.6 mycroft sbreset(sc);
591 1.6 mycroft return EIO;
592 1.1 cgd }
593 1.1 cgd sc->sc_locked = 1;
594 1.1 cgd }
595 1.6 mycroft return 0;
596 1.1 cgd }
597 1.1 cgd
598 1.1 cgd /*
599 1.1 cgd * Only the DSP unit on the sound blaster generates interrupts.
600 1.1 cgd * There are three cases of interrupt: reception of a midi byte
601 1.1 cgd * (when mode is enabled), completion of dma transmission, or
602 1.1 cgd * completion of a dma reception. The three modes are mutually
603 1.1 cgd * exclusive so we know a priori which event has occurred.
604 1.1 cgd */
605 1.1 cgd int
606 1.7 mycroft sbintr(sc)
607 1.7 mycroft register struct sb_softc *sc;
608 1.1 cgd {
609 1.1 cgd
610 1.1 cgd sc->sc_locked = 0;
611 1.1 cgd /* clear interrupt */
612 1.6 mycroft inb(sc->sc_iobase + SBP_DSP_RSTAT);
613 1.15 mycroft delay(20);
614 1.1 cgd if (sc->sc_mintr != 0) {
615 1.6 mycroft int c = rdsp(sc->sc_iobase);
616 1.1 cgd (*sc->sc_mintr)(sc->sc_arg, c);
617 1.6 mycroft } else if (sc->sc_intr != 0)
618 1.3 deraadt (*sc->sc_intr)(sc->sc_arg);
619 1.6 mycroft else
620 1.6 mycroft return 0;
621 1.6 mycroft return 1;
622 1.1 cgd }
623 1.1 cgd
624 1.1 cgd /*
625 1.1 cgd * Enter midi uart mode and arrange for read interrupts
626 1.1 cgd * to vector to `intr'. This puts the card in a mode
627 1.1 cgd * which allows only midi I/O; the card must be reset
628 1.1 cgd * to leave this mode. Unfortunately, the card does not
629 1.1 cgd * use transmit interrupts, so bytes must be output
630 1.1 cgd * using polling. To keep the polling overhead to a
631 1.1 cgd * minimum, output should be driven off a timer.
632 1.1 cgd * This is a little tricky since only 320us separate
633 1.1 cgd * consecutive midi bytes.
634 1.1 cgd */
635 1.1 cgd void
636 1.6 mycroft sb_set_midi_mode(sc, intr, arg)
637 1.6 mycroft struct sb_softc *sc;
638 1.6 mycroft void (*intr)();
639 1.6 mycroft void *arg;
640 1.1 cgd {
641 1.6 mycroft
642 1.6 mycroft wdsp(sc->sc_iobase, SB_MIDI_UART_INTR);
643 1.1 cgd sc->sc_mintr = intr;
644 1.1 cgd sc->sc_intr = 0;
645 1.1 cgd sc->sc_arg = arg;
646 1.1 cgd }
647 1.1 cgd
648 1.1 cgd /*
649 1.1 cgd * Write a byte to the midi port, when in midi uart mode.
650 1.1 cgd */
651 1.1 cgd void
652 1.6 mycroft sb_midi_output(sc, v)
653 1.6 mycroft struct sb_softc *sc;
654 1.6 mycroft int v;
655 1.1 cgd {
656 1.6 mycroft
657 1.6 mycroft if (wdsp(sc->sc_iobase, v) < 0)
658 1.1 cgd ++sberr.wmidi;
659 1.1 cgd }
660