sbdsp.c revision 1.81 1 /* $NetBSD: sbdsp.c,v 1.81 1998/08/07 00:01:00 augustss 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 /*
38 * SoundBlaster Pro code provided by John Kohl, based on lots of
39 * information he gleaned from Steve Haehnichen <steve (at) vigra.com>'s
40 * SBlast driver for 386BSD and DOS driver code from Daniel Sachs
41 * <sachs (at) meibm15.cen.uiuc.edu>.
42 * Lots of rewrites by Lennart Augustsson <augustss (at) cs.chalmers.se>
43 * with information from SB "Hardware Programming Guide" and the
44 * Linux drivers.
45 */
46
47 #include "midi.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/errno.h>
52 #include <sys/ioctl.h>
53 #include <sys/syslog.h>
54 #include <sys/device.h>
55 #include <sys/proc.h>
56 #include <sys/buf.h>
57 #include <vm/vm.h>
58
59 #include <machine/cpu.h>
60 #include <machine/intr.h>
61 #include <machine/bus.h>
62
63 #include <sys/audioio.h>
64 #include <dev/audio_if.h>
65 #include <dev/mulaw.h>
66 #include <dev/auconv.h>
67
68 #include <dev/isa/isavar.h>
69 #include <dev/isa/isadmavar.h>
70
71 #include <dev/isa/sbreg.h>
72 #include <dev/isa/sbdspvar.h>
73
74
75 #ifdef AUDIO_DEBUG
76 #define DPRINTF(x) if (sbdspdebug) printf x
77 #define DPRINTFN(n,x) if (sbdspdebug >= (n)) printf x
78 int sbdspdebug = 0;
79 #else
80 #define DPRINTF(x)
81 #define DPRINTFN(n,x)
82 #endif
83
84 #ifndef SBDSP_NPOLL
85 #define SBDSP_NPOLL 3000
86 #endif
87
88 struct {
89 int wdsp;
90 int rdsp;
91 int wmidi;
92 } sberr;
93
94 /*
95 * Time constant routines follow. See SBK, section 12.
96 * Although they don't come out and say it (in the docs),
97 * the card clearly uses a 1MHz countdown timer, as the
98 * low-speed formula (p. 12-4) is:
99 * tc = 256 - 10^6 / sr
100 * In high-speed mode, the constant is the upper byte of a 16-bit counter,
101 * and a 256MHz clock is used:
102 * tc = 65536 - 256 * 10^ 6 / sr
103 * Since we can only use the upper byte of the HS TC, the two formulae
104 * are equivalent. (Why didn't they say so?) E.g.,
105 * (65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
106 *
107 * The crossover point (from low- to high-speed modes) is different
108 * for the SBPRO and SB20. The table on p. 12-5 gives the following data:
109 *
110 * SBPRO SB20
111 * ----- --------
112 * input ls min 4 KHz 4 KHz
113 * input ls max 23 KHz 13 KHz
114 * input hs max 44.1 KHz 15 KHz
115 * output ls min 4 KHz 4 KHz
116 * output ls max 23 KHz 23 KHz
117 * output hs max 44.1 KHz 44.1 KHz
118 */
119 /* XXX Should we round the tc?
120 #define SB_RATE_TO_TC(x) (((65536 - 256 * 1000000 / (x)) + 128) >> 8)
121 */
122 #define SB_RATE_TO_TC(x) (256 - 1000000 / (x))
123 #define SB_TC_TO_RATE(tc) (1000000 / (256 - (tc)))
124
125 struct sbmode {
126 short model;
127 u_char channels;
128 u_char precision;
129 u_short lowrate, highrate;
130 u_char cmd;
131 u_char cmdchan;
132 };
133 static struct sbmode sbpmodes[] = {
134 { SB_1, 1, 8, 4000, 22727, SB_DSP_WDMA },
135 { SB_20, 1, 8, 4000, 22727, SB_DSP_WDMA_LOOP },
136 { SB_2x, 1, 8, 4000, 22727, SB_DSP_WDMA_LOOP },
137 { SB_2x, 1, 8, 22727, 45454, SB_DSP_HS_OUTPUT },
138 { SB_PRO, 1, 8, 4000, 22727, SB_DSP_WDMA_LOOP },
139 { SB_PRO, 1, 8, 22727, 45454, SB_DSP_HS_OUTPUT },
140 { SB_PRO, 2, 8, 11025, 22727, SB_DSP_HS_OUTPUT },
141 /* Yes, we write the record mode to set 16-bit playback mode. weird, huh? */
142 { SB_JAZZ, 1, 8, 4000, 22727, SB_DSP_WDMA_LOOP, SB_DSP_RECORD_MONO },
143 { SB_JAZZ, 1, 8, 22727, 45454, SB_DSP_HS_OUTPUT, SB_DSP_RECORD_MONO },
144 { SB_JAZZ, 2, 8, 11025, 22727, SB_DSP_HS_OUTPUT, SB_DSP_RECORD_STEREO },
145 { SB_JAZZ, 1, 16, 4000, 22727, SB_DSP_WDMA_LOOP, JAZZ16_RECORD_MONO },
146 { SB_JAZZ, 1, 16, 22727, 45454, SB_DSP_HS_OUTPUT, JAZZ16_RECORD_MONO },
147 { SB_JAZZ, 2, 16, 11025, 22727, SB_DSP_HS_OUTPUT, JAZZ16_RECORD_STEREO },
148 { SB_16, 1, 8, 5000, 45000, SB_DSP16_WDMA_8 },
149 { SB_16, 2, 8, 5000, 45000, SB_DSP16_WDMA_8 },
150 #define PLAY16 15 /* must be the index of the next entry in the table */
151 { SB_16, 1, 16, 5000, 45000, SB_DSP16_WDMA_16 },
152 { SB_16, 2, 16, 5000, 45000, SB_DSP16_WDMA_16 },
153 { -1 }
154 };
155 static struct sbmode sbrmodes[] = {
156 { SB_1, 1, 8, 4000, 12987, SB_DSP_RDMA },
157 { SB_20, 1, 8, 4000, 12987, SB_DSP_RDMA_LOOP },
158 { SB_2x, 1, 8, 4000, 12987, SB_DSP_RDMA_LOOP },
159 { SB_2x, 1, 8, 12987, 14925, SB_DSP_HS_INPUT },
160 { SB_PRO, 1, 8, 4000, 22727, SB_DSP_RDMA_LOOP, SB_DSP_RECORD_MONO },
161 { SB_PRO, 1, 8, 22727, 45454, SB_DSP_HS_INPUT, SB_DSP_RECORD_MONO },
162 { SB_PRO, 2, 8, 11025, 22727, SB_DSP_HS_INPUT, SB_DSP_RECORD_STEREO },
163 { SB_JAZZ, 1, 8, 4000, 22727, SB_DSP_RDMA_LOOP, SB_DSP_RECORD_MONO },
164 { SB_JAZZ, 1, 8, 22727, 45454, SB_DSP_HS_INPUT, SB_DSP_RECORD_MONO },
165 { SB_JAZZ, 2, 8, 11025, 22727, SB_DSP_HS_INPUT, SB_DSP_RECORD_STEREO },
166 { SB_JAZZ, 1, 16, 4000, 22727, SB_DSP_RDMA_LOOP, JAZZ16_RECORD_MONO },
167 { SB_JAZZ, 1, 16, 22727, 45454, SB_DSP_HS_INPUT, JAZZ16_RECORD_MONO },
168 { SB_JAZZ, 2, 16, 11025, 22727, SB_DSP_HS_INPUT, JAZZ16_RECORD_STEREO },
169 { SB_16, 1, 8, 5000, 45000, SB_DSP16_RDMA_8 },
170 { SB_16, 2, 8, 5000, 45000, SB_DSP16_RDMA_8 },
171 { SB_16, 1, 16, 5000, 45000, SB_DSP16_RDMA_16 },
172 { SB_16, 2, 16, 5000, 45000, SB_DSP16_RDMA_16 },
173 { -1 }
174 };
175
176 void sbversion __P((struct sbdsp_softc *));
177 void sbdsp_jazz16_probe __P((struct sbdsp_softc *));
178 void sbdsp_set_mixer_gain __P((struct sbdsp_softc *sc, int port));
179 void sbdsp_to __P((void *));
180 void sbdsp_pause __P((struct sbdsp_softc *));
181 int sbdsp_set_timeconst __P((struct sbdsp_softc *, int));
182 int sbdsp16_set_rate __P((struct sbdsp_softc *, int, int));
183 int sbdsp_set_in_ports __P((struct sbdsp_softc *, int));
184 void sbdsp_set_ifilter __P((void *, int));
185 int sbdsp_get_ifilter __P((void *));
186
187 static int sbdsp_dma_setup_input __P((struct sbdsp_softc *sc));
188 static int sbdsp_dma_setup_output __P((struct sbdsp_softc *sc));
189 static int sbdsp_adjust __P((int, int));
190
191 void sbdsp_midi_intr __P((struct sbdsp_softc *));
192
193 #ifdef AUDIO_DEBUG
194 void sb_printsc __P((struct sbdsp_softc *));
195
196 void
197 sb_printsc(sc)
198 struct sbdsp_softc *sc;
199 {
200 int i;
201
202 printf("open %d dmachan %d/%d %d/%d iobase 0x%x irq %d\n",
203 (int)sc->sc_open, sc->sc_i.run, sc->sc_o.run,
204 sc->sc_drq8, sc->sc_drq16,
205 sc->sc_iobase, sc->sc_irq);
206 printf("irate %d itc %x orate %d otc %x\n",
207 sc->sc_i.rate, sc->sc_i.tc,
208 sc->sc_o.rate, sc->sc_o.tc);
209 printf("spkron %u nintr %lu\n",
210 sc->spkr_state, sc->sc_interrupts);
211 printf("intr8 %p arg8 %p\n",
212 sc->sc_intr8, sc->sc_arg16);
213 printf("intr16 %p arg16 %p\n",
214 sc->sc_intr8, sc->sc_arg16);
215 printf("gain:");
216 for (i = 0; i < SB_NDEVS; i++)
217 printf(" %u,%u", sc->gain[i][SB_LEFT], sc->gain[i][SB_RIGHT]);
218 printf("\n");
219 }
220 #endif /* AUDIO_DEBUG */
221
222 /*
223 * Probe / attach routines.
224 */
225
226 /*
227 * Probe for the soundblaster hardware.
228 */
229 int
230 sbdsp_probe(sc)
231 struct sbdsp_softc *sc;
232 {
233
234 if (sbdsp_reset(sc) < 0) {
235 DPRINTF(("sbdsp: couldn't reset card\n"));
236 return 0;
237 }
238 /* if flags set, go and probe the jazz16 stuff */
239 if (sc->sc_dev.dv_cfdata->cf_flags & 1)
240 sbdsp_jazz16_probe(sc);
241 else
242 sbversion(sc);
243 if (sc->sc_model == SB_UNK) {
244 /* Unknown SB model found. */
245 DPRINTF(("sbdsp: unknown SB model found\n"));
246 return 0;
247 }
248 return 1;
249 }
250
251 /*
252 * Try add-on stuff for Jazz16.
253 */
254 void
255 sbdsp_jazz16_probe(sc)
256 struct sbdsp_softc *sc;
257 {
258 static u_char jazz16_irq_conf[16] = {
259 -1, -1, 0x02, 0x03,
260 -1, 0x01, -1, 0x04,
261 -1, 0x02, 0x05, -1,
262 -1, -1, -1, 0x06};
263 static u_char jazz16_drq_conf[8] = {
264 -1, 0x01, -1, 0x02,
265 -1, 0x03, -1, 0x04};
266
267 bus_space_tag_t iot = sc->sc_iot;
268 bus_space_handle_t ioh;
269
270 sbversion(sc);
271
272 DPRINTF(("jazz16 probe\n"));
273
274 if (bus_space_map(iot, JAZZ16_CONFIG_PORT, 1, 0, &ioh)) {
275 DPRINTF(("bus map failed\n"));
276 return;
277 }
278
279 if (jazz16_drq_conf[sc->sc_drq8] == (u_char)-1 ||
280 jazz16_irq_conf[sc->sc_irq] == (u_char)-1) {
281 DPRINTF(("drq/irq check failed\n"));
282 goto done; /* give up, we can't do it. */
283 }
284
285 bus_space_write_1(iot, ioh, 0, JAZZ16_WAKEUP);
286 delay(10000); /* delay 10 ms */
287 bus_space_write_1(iot, ioh, 0, JAZZ16_SETBASE);
288 bus_space_write_1(iot, ioh, 0, sc->sc_iobase & 0x70);
289
290 if (sbdsp_reset(sc) < 0) {
291 DPRINTF(("sbdsp_reset check failed\n"));
292 goto done; /* XXX? what else could we do? */
293 }
294
295 if (sbdsp_wdsp(sc, JAZZ16_READ_VER)) {
296 DPRINTF(("read16 setup failed\n"));
297 goto done;
298 }
299
300 if (sbdsp_rdsp(sc) != JAZZ16_VER_JAZZ) {
301 DPRINTF(("read16 failed\n"));
302 goto done;
303 }
304
305 /* XXX set both 8 & 16-bit drq to same channel, it works fine. */
306 sc->sc_drq16 = sc->sc_drq8;
307 if (sbdsp_wdsp(sc, JAZZ16_SET_DMAINTR) ||
308 sbdsp_wdsp(sc, (jazz16_drq_conf[sc->sc_drq16] << 4) |
309 jazz16_drq_conf[sc->sc_drq8]) ||
310 sbdsp_wdsp(sc, jazz16_irq_conf[sc->sc_irq])) {
311 DPRINTF(("sbdsp: can't write jazz16 probe stuff\n"));
312 } else {
313 DPRINTF(("jazz16 detected!\n"));
314 sc->sc_model = SB_JAZZ;
315 sc->sc_mixer_model = SBM_CT1345; /* XXX really? */
316 }
317
318 done:
319 bus_space_unmap(iot, ioh, 1);
320 }
321
322 /*
323 * Attach hardware to driver, attach hardware driver to audio
324 * pseudo-device driver .
325 */
326 void
327 sbdsp_attach(sc)
328 struct sbdsp_softc *sc;
329 {
330 struct audio_params pparams, rparams;
331 int i;
332 u_int v;
333
334 /*
335 * Create our DMA maps.
336 */
337 if (sc->sc_drq8 != -1) {
338 if (isa_dmamap_create(sc->sc_ic, sc->sc_drq8,
339 MAX_ISADMA, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
340 printf("%s: can't create map for drq %d\n",
341 sc->sc_dev.dv_xname, sc->sc_drq8);
342 return;
343 }
344 }
345 if (sc->sc_drq16 != -1 && sc->sc_drq16 != sc->sc_drq8) {
346 if (isa_dmamap_create(sc->sc_ic, sc->sc_drq16,
347 MAX_ISADMA, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
348 printf("%s: can't create map for drq %d\n",
349 sc->sc_dev.dv_xname, sc->sc_drq16);
350 return;
351 }
352 }
353
354 pparams = audio_default;
355 rparams = audio_default;
356 sbdsp_set_params(sc, AUMODE_RECORD|AUMODE_PLAY, 0, &pparams, &rparams);
357
358 sbdsp_set_in_ports(sc, 1 << SB_MIC_VOL);
359
360 if (sc->sc_mixer_model != SBM_NONE) {
361 /* Reset the mixer.*/
362 sbdsp_mix_write(sc, SBP_MIX_RESET, SBP_MIX_RESET);
363 /* And set our own default values */
364 for (i = 0; i < SB_NDEVS; i++) {
365 switch(i) {
366 case SB_MIC_VOL:
367 case SB_LINE_IN_VOL:
368 v = 0;
369 break;
370 case SB_BASS:
371 case SB_TREBLE:
372 v = SB_ADJUST_GAIN(sc, AUDIO_MAX_GAIN/2);
373 break;
374 case SB_CD_IN_MUTE:
375 case SB_MIC_IN_MUTE:
376 case SB_LINE_IN_MUTE:
377 case SB_MIDI_IN_MUTE:
378 case SB_CD_SWAP:
379 case SB_MIC_SWAP:
380 case SB_LINE_SWAP:
381 case SB_MIDI_SWAP:
382 case SB_CD_OUT_MUTE:
383 case SB_MIC_OUT_MUTE:
384 case SB_LINE_OUT_MUTE:
385 v = 0;
386 break;
387 default:
388 v = SB_ADJUST_GAIN(sc, AUDIO_MAX_GAIN / 2);
389 break;
390 }
391 sc->gain[i][SB_LEFT] = sc->gain[i][SB_RIGHT] = v;
392 sbdsp_set_mixer_gain(sc, i);
393 }
394 sc->in_filter = 0; /* no filters turned on, please */
395 }
396
397 printf(": dsp v%d.%02d%s\n",
398 SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version),
399 sc->sc_model == SB_JAZZ ? ": <Jazz16>" : "");
400
401 sc->sc_fullduplex = ISSB16CLASS(sc) &&
402 sc->sc_drq8 != -1 && sc->sc_drq16 != -1 &&
403 sc->sc_drq8 != sc->sc_drq16;
404 }
405
406 void
407 sbdsp_mix_write(sc, mixerport, val)
408 struct sbdsp_softc *sc;
409 int mixerport;
410 int val;
411 {
412 bus_space_tag_t iot = sc->sc_iot;
413 bus_space_handle_t ioh = sc->sc_ioh;
414 int s;
415
416 s = splaudio();
417 bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
418 delay(20);
419 bus_space_write_1(iot, ioh, SBP_MIXER_DATA, val);
420 delay(30);
421 splx(s);
422 }
423
424 int
425 sbdsp_mix_read(sc, mixerport)
426 struct sbdsp_softc *sc;
427 int mixerport;
428 {
429 bus_space_tag_t iot = sc->sc_iot;
430 bus_space_handle_t ioh = sc->sc_ioh;
431 int val;
432 int s;
433
434 s = splaudio();
435 bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
436 delay(20);
437 val = bus_space_read_1(iot, ioh, SBP_MIXER_DATA);
438 delay(30);
439 splx(s);
440 return val;
441 }
442
443 /*
444 * Various routines to interface to higher level audio driver
445 */
446
447 int
448 sbdsp_query_encoding(addr, fp)
449 void *addr;
450 struct audio_encoding *fp;
451 {
452 struct sbdsp_softc *sc = addr;
453 int emul;
454
455 emul = ISSB16CLASS(sc) ? 0 : AUDIO_ENCODINGFLAG_EMULATED;
456
457 switch (fp->index) {
458 case 0:
459 strcpy(fp->name, AudioEulinear);
460 fp->encoding = AUDIO_ENCODING_ULINEAR;
461 fp->precision = 8;
462 fp->flags = 0;
463 return 0;
464 case 1:
465 strcpy(fp->name, AudioEmulaw);
466 fp->encoding = AUDIO_ENCODING_ULAW;
467 fp->precision = 8;
468 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
469 return 0;
470 case 2:
471 strcpy(fp->name, AudioEalaw);
472 fp->encoding = AUDIO_ENCODING_ALAW;
473 fp->precision = 8;
474 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
475 return 0;
476 case 3:
477 strcpy(fp->name, AudioEslinear);
478 fp->encoding = AUDIO_ENCODING_SLINEAR;
479 fp->precision = 8;
480 fp->flags = emul;
481 return 0;
482 }
483 if (!ISSB16CLASS(sc) && sc->sc_model != SB_JAZZ)
484 return EINVAL;
485
486 switch(fp->index) {
487 case 4:
488 strcpy(fp->name, AudioEslinear_le);
489 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
490 fp->precision = 16;
491 fp->flags = 0;
492 return 0;
493 case 5:
494 strcpy(fp->name, AudioEulinear_le);
495 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
496 fp->precision = 16;
497 fp->flags = emul;
498 return 0;
499 case 6:
500 strcpy(fp->name, AudioEslinear_be);
501 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
502 fp->precision = 16;
503 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
504 return 0;
505 case 7:
506 strcpy(fp->name, AudioEulinear_be);
507 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
508 fp->precision = 16;
509 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
510 return 0;
511 default:
512 return EINVAL;
513 }
514 return 0;
515 }
516
517 int
518 sbdsp_set_params(addr, setmode, usemode, play, rec)
519 void *addr;
520 int setmode, usemode;
521 struct audio_params *play, *rec;
522 {
523 struct sbdsp_softc *sc = addr;
524 struct sbmode *m;
525 u_int rate, tc, bmode;
526 void (*swcode) __P((void *, u_char *buf, int cnt));
527 int factor;
528 int model;
529 int chan;
530 struct audio_params *p;
531 int mode;
532
533 if (sc->sc_open == SB_OPEN_MIDI)
534 return EBUSY;
535
536 model = sc->sc_model;
537 if (model > SB_16)
538 model = SB_16; /* later models work like SB16 */
539
540 /* Set first record info, then play info */
541 for(mode = AUMODE_RECORD; mode != -1;
542 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
543 if ((setmode & mode) == 0)
544 continue;
545
546 p = mode == AUMODE_PLAY ? play : rec;
547 /* Locate proper commands */
548 for(m = mode == AUMODE_PLAY ? sbpmodes : sbrmodes;
549 m->model != -1; m++) {
550 if (model == m->model &&
551 p->channels == m->channels &&
552 p->precision == m->precision &&
553 p->sample_rate >= m->lowrate &&
554 p->sample_rate < m->highrate)
555 break;
556 }
557 if (m->model == -1)
558 return EINVAL;
559 rate = p->sample_rate;
560 swcode = 0;
561 factor = 1;
562 tc = 1;
563 bmode = -1;
564 if (model == SB_16) {
565 switch (p->encoding) {
566 case AUDIO_ENCODING_SLINEAR_BE:
567 if (p->precision == 16)
568 swcode = swap_bytes;
569 /* fall into */
570 case AUDIO_ENCODING_SLINEAR_LE:
571 bmode = SB_BMODE_SIGNED;
572 break;
573 case AUDIO_ENCODING_ULINEAR_BE:
574 if (p->precision == 16)
575 swcode = swap_bytes;
576 /* fall into */
577 case AUDIO_ENCODING_ULINEAR_LE:
578 bmode = SB_BMODE_UNSIGNED;
579 break;
580 case AUDIO_ENCODING_ULAW:
581 if (mode == AUMODE_PLAY) {
582 swcode = mulaw_to_ulinear16;
583 factor = 2;
584 m = &sbpmodes[PLAY16];
585 } else
586 swcode = ulinear8_to_mulaw;
587 bmode = SB_BMODE_UNSIGNED;
588 break;
589 case AUDIO_ENCODING_ALAW:
590 if (mode == AUMODE_PLAY) {
591 swcode = alaw_to_ulinear16;
592 factor = 2;
593 m = &sbpmodes[PLAY16];
594 } else
595 swcode = ulinear8_to_alaw;
596 bmode = SB_BMODE_UNSIGNED;
597 break;
598 default:
599 return EINVAL;
600 }
601 if (p->channels == 2)
602 bmode |= SB_BMODE_STEREO;
603 } else if (m->model == SB_JAZZ && m->precision == 16) {
604 switch (p->encoding) {
605 case AUDIO_ENCODING_SLINEAR_LE:
606 break;
607 case AUDIO_ENCODING_ULINEAR_LE:
608 swcode = change_sign16;
609 break;
610 case AUDIO_ENCODING_SLINEAR_BE:
611 swcode = swap_bytes;
612 break;
613 case AUDIO_ENCODING_ULINEAR_BE:
614 swcode = mode == AUMODE_PLAY ?
615 swap_bytes_change_sign16 : change_sign16_swap_bytes;
616 break;
617 case AUDIO_ENCODING_ULAW:
618 swcode = mode == AUMODE_PLAY ?
619 mulaw_to_ulinear8 : ulinear8_to_mulaw;
620 break;
621 case AUDIO_ENCODING_ALAW:
622 swcode = mode == AUMODE_PLAY ?
623 alaw_to_ulinear8 : ulinear8_to_alaw;
624 break;
625 default:
626 return EINVAL;
627 }
628 tc = SB_RATE_TO_TC(p->sample_rate * p->channels);
629 p->sample_rate = SB_TC_TO_RATE(tc) / p->channels;
630 } else {
631 switch (p->encoding) {
632 case AUDIO_ENCODING_SLINEAR_BE:
633 case AUDIO_ENCODING_SLINEAR_LE:
634 swcode = change_sign8;
635 break;
636 case AUDIO_ENCODING_ULINEAR_BE:
637 case AUDIO_ENCODING_ULINEAR_LE:
638 break;
639 case AUDIO_ENCODING_ULAW:
640 swcode = mode == AUMODE_PLAY ?
641 mulaw_to_ulinear8 : ulinear8_to_mulaw;
642 break;
643 case AUDIO_ENCODING_ALAW:
644 swcode = mode == AUMODE_PLAY ?
645 alaw_to_ulinear8 : ulinear8_to_alaw;
646 break;
647 default:
648 return EINVAL;
649 }
650 tc = SB_RATE_TO_TC(p->sample_rate * p->channels);
651 p->sample_rate = SB_TC_TO_RATE(tc) / p->channels;
652 }
653
654 chan = m->precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
655 if (mode == AUMODE_PLAY) {
656 sc->sc_o.rate = rate;
657 sc->sc_o.tc = tc;
658 sc->sc_o.modep = m;
659 sc->sc_o.bmode = bmode;
660 sc->sc_o.dmachan = chan;
661 } else {
662 sc->sc_i.rate = rate;
663 sc->sc_i.tc = tc;
664 sc->sc_i.modep = m;
665 sc->sc_i.bmode = bmode;
666 sc->sc_i.dmachan = chan;
667 }
668
669 p->sw_code = swcode;
670 p->factor = factor;
671 DPRINTF(("sbdsp_set_params: model=%d, mode=%d, rate=%ld, prec=%d, chan=%d, enc=%d -> tc=%02x, cmd=%02x, bmode=%02x, cmdchan=%02x, swcode=%p, factor=%d\n",
672 sc->sc_model, mode, p->sample_rate, p->precision, p->channels,
673 p->encoding, tc, m->cmd, bmode, m->cmdchan, swcode, factor));
674
675 }
676 /*
677 * XXX
678 * Should wait for chip to be idle.
679 */
680 sc->sc_i.run = SB_NOTRUNNING;
681 sc->sc_o.run = SB_NOTRUNNING;
682
683 if (sc->sc_fullduplex &&
684 (usemode & (AUMODE_PLAY | AUMODE_RECORD)) == (AUMODE_PLAY | AUMODE_RECORD) &&
685 sc->sc_i.dmachan == sc->sc_o.dmachan) {
686 DPRINTF(("sbdsp_set_params: fd=%d, usemode=%d, idma=%d, odma=%d\n", sc->sc_fullduplex, usemode, sc->sc_i.dmachan, sc->sc_o.dmachan));
687 if (sc->sc_o.dmachan == sc->sc_drq8) {
688 /* Use 16 bit DMA for playing by expanding the samples. */
689 play->sw_code = linear8_to_linear16;
690 play->factor = 2;
691 sc->sc_o.modep = &sbpmodes[PLAY16];
692 sc->sc_o.dmachan = sc->sc_drq16;
693 } else {
694 return EINVAL;
695 }
696 }
697 DPRINTF(("sbdsp_set_params ichan=%d, ochan=%d\n",
698 sc->sc_i.dmachan, sc->sc_o.dmachan));
699
700 return 0;
701 }
702
703 void
704 sbdsp_set_ifilter(addr, which)
705 void *addr;
706 int which;
707 {
708 struct sbdsp_softc *sc = addr;
709 int mixval;
710
711 mixval = sbdsp_mix_read(sc, SBP_INFILTER) & ~SBP_IFILTER_MASK;
712 switch (which) {
713 case 0:
714 mixval |= SBP_FILTER_OFF;
715 break;
716 case SB_TREBLE:
717 mixval |= SBP_FILTER_ON | SBP_IFILTER_HIGH;
718 break;
719 case SB_BASS:
720 mixval |= SBP_FILTER_ON | SBP_IFILTER_LOW;
721 break;
722 default:
723 return;
724 }
725 sc->in_filter = mixval & SBP_IFILTER_MASK;
726 sbdsp_mix_write(sc, SBP_INFILTER, mixval);
727 }
728
729 int
730 sbdsp_get_ifilter(addr)
731 void *addr;
732 {
733 struct sbdsp_softc *sc = addr;
734
735 sc->in_filter =
736 sbdsp_mix_read(sc, SBP_INFILTER) & SBP_IFILTER_MASK;
737 switch (sc->in_filter) {
738 case SBP_FILTER_ON|SBP_IFILTER_HIGH:
739 return SB_TREBLE;
740 case SBP_FILTER_ON|SBP_IFILTER_LOW:
741 return SB_BASS;
742 default:
743 return 0;
744 }
745 }
746
747 int
748 sbdsp_set_in_ports(sc, mask)
749 struct sbdsp_softc *sc;
750 int mask;
751 {
752 int bitsl, bitsr;
753 int sbport;
754
755 if (sc->sc_open == SB_OPEN_MIDI)
756 return EBUSY;
757
758 DPRINTF(("sbdsp_set_in_ports: model=%d, mask=%x\n",
759 sc->sc_mixer_model, mask));
760
761 switch(sc->sc_mixer_model) {
762 case SBM_NONE:
763 return EINVAL;
764 case SBM_CT1335:
765 if (mask != (1 << SB_MIC_VOL))
766 return EINVAL;
767 break;
768 case SBM_CT1345:
769 switch (mask) {
770 case 1 << SB_MIC_VOL:
771 sbport = SBP_FROM_MIC;
772 break;
773 case 1 << SB_LINE_IN_VOL:
774 sbport = SBP_FROM_LINE;
775 break;
776 case 1 << SB_CD_VOL:
777 sbport = SBP_FROM_CD;
778 break;
779 default:
780 return (EINVAL);
781 }
782 sbdsp_mix_write(sc, SBP_RECORD_SOURCE, sbport | sc->in_filter);
783 break;
784 case SBM_CT1XX5:
785 case SBM_CT1745:
786 if (mask & ~((1<<SB_MIDI_VOL) | (1<<SB_LINE_IN_VOL) |
787 (1<<SB_CD_VOL) | (1<<SB_MIC_VOL)))
788 return EINVAL;
789 bitsr = 0;
790 if (mask & (1<<SB_MIDI_VOL)) bitsr |= SBP_MIDI_SRC_R;
791 if (mask & (1<<SB_LINE_IN_VOL)) bitsr |= SBP_LINE_SRC_R;
792 if (mask & (1<<SB_CD_VOL)) bitsr |= SBP_CD_SRC_R;
793 bitsl = SB_SRC_R_TO_L(bitsr);
794 if (mask & (1<<SB_MIC_VOL)) {
795 bitsl |= SBP_MIC_SRC;
796 bitsr |= SBP_MIC_SRC;
797 }
798 sbdsp_mix_write(sc, SBP_RECORD_SOURCE_L, bitsl);
799 sbdsp_mix_write(sc, SBP_RECORD_SOURCE_R, bitsr);
800 break;
801 }
802 sc->in_mask = mask;
803
804 return 0;
805 }
806
807 int
808 sbdsp_speaker_ctl(addr, newstate)
809 void *addr;
810 int newstate;
811 {
812 struct sbdsp_softc *sc = addr;
813
814 if (sc->sc_open == SB_OPEN_MIDI)
815 return EBUSY;
816
817 if ((newstate == SPKR_ON) &&
818 (sc->spkr_state == SPKR_OFF)) {
819 sbdsp_spkron(sc);
820 sc->spkr_state = SPKR_ON;
821 }
822 if ((newstate == SPKR_OFF) &&
823 (sc->spkr_state == SPKR_ON)) {
824 sbdsp_spkroff(sc);
825 sc->spkr_state = SPKR_OFF;
826 }
827 return 0;
828 }
829
830 int
831 sbdsp_round_blocksize(addr, blk)
832 void *addr;
833 int blk;
834 {
835 return blk & -4; /* round to biggest sample size */
836 }
837
838 int
839 sbdsp_open(addr, flags)
840 void *addr;
841 int flags;
842 {
843 struct sbdsp_softc *sc = addr;
844
845 DPRINTF(("sbdsp_open: sc=%p\n", sc));
846
847 if (sc->sc_open != SB_CLOSED)
848 return EBUSY;
849 if (sbdsp_reset(sc) != 0)
850 return EIO;
851
852 sc->sc_open = SB_OPEN_AUDIO;
853 sc->sc_openflags = flags;
854 sc->sc_intrm = 0;
855 if (ISSBPRO(sc) &&
856 sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0) {
857 DPRINTF(("sbdsp_open: can't set mono mode\n"));
858 /* we'll readjust when it's time for DMA. */
859 }
860
861 /*
862 * Leave most things as they were; users must change things if
863 * the previous process didn't leave it they way they wanted.
864 * Looked at another way, it's easy to set up a configuration
865 * in one program and leave it for another to inherit.
866 */
867 DPRINTF(("sbdsp_open: opened\n"));
868
869 return 0;
870 }
871
872 void
873 sbdsp_close(addr)
874 void *addr;
875 {
876 struct sbdsp_softc *sc = addr;
877
878 DPRINTF(("sbdsp_close: sc=%p\n", sc));
879
880 sc->sc_open = SB_CLOSED;
881 sbdsp_spkroff(sc);
882 sc->spkr_state = SPKR_OFF;
883 sc->sc_intr8 = 0;
884 sc->sc_intr16 = 0;
885 sc->sc_intrm = 0;
886 sbdsp_haltdma(sc);
887
888 DPRINTF(("sbdsp_close: closed\n"));
889 }
890
891 /*
892 * Lower-level routines
893 */
894
895 /*
896 * Reset the card.
897 * Return non-zero if the card isn't detected.
898 */
899 int
900 sbdsp_reset(sc)
901 struct sbdsp_softc *sc;
902 {
903 bus_space_tag_t iot = sc->sc_iot;
904 bus_space_handle_t ioh = sc->sc_ioh;
905
906 sc->sc_intr8 = 0;
907 sc->sc_intr16 = 0;
908 if (sc->sc_i.run != SB_NOTRUNNING) {
909 isa_dmaabort(sc->sc_ic, sc->sc_i.dmachan);
910 sc->sc_i.run = SB_NOTRUNNING;
911 }
912 if (sc->sc_o.run != SB_NOTRUNNING) {
913 isa_dmaabort(sc->sc_ic, sc->sc_o.dmachan);
914 sc->sc_o.run = SB_NOTRUNNING;
915 }
916
917 /*
918 * See SBK, section 11.3.
919 * We pulse a reset signal into the card.
920 * Gee, what a brilliant hardware design.
921 */
922 bus_space_write_1(iot, ioh, SBP_DSP_RESET, 1);
923 delay(10);
924 bus_space_write_1(iot, ioh, SBP_DSP_RESET, 0);
925 delay(30);
926 if (sbdsp_rdsp(sc) != SB_MAGIC)
927 return -1;
928
929 return 0;
930 }
931
932 /*
933 * Write a byte to the dsp.
934 * We are at the mercy of the card as we use a
935 * polling loop and wait until it can take the byte.
936 */
937 int
938 sbdsp_wdsp(sc, v)
939 struct sbdsp_softc *sc;
940 int v;
941 {
942 bus_space_tag_t iot = sc->sc_iot;
943 bus_space_handle_t ioh = sc->sc_ioh;
944 int i;
945 u_char x;
946
947 for (i = SBDSP_NPOLL; --i >= 0; ) {
948 x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
949 delay(10);
950 if ((x & SB_DSP_BUSY) == 0) {
951 bus_space_write_1(iot, ioh, SBP_DSP_WRITE, v);
952 delay(10);
953 return 0;
954 }
955 }
956 ++sberr.wdsp;
957 return -1;
958 }
959
960 /*
961 * Read a byte from the DSP, using polling.
962 */
963 int
964 sbdsp_rdsp(sc)
965 struct sbdsp_softc *sc;
966 {
967 bus_space_tag_t iot = sc->sc_iot;
968 bus_space_handle_t ioh = sc->sc_ioh;
969 int i;
970 u_char x;
971
972 for (i = SBDSP_NPOLL; --i >= 0; ) {
973 x = bus_space_read_1(iot, ioh, SBP_DSP_RSTAT);
974 delay(10);
975 if (x & SB_DSP_READY) {
976 x = bus_space_read_1(iot, ioh, SBP_DSP_READ);
977 delay(10);
978 return x;
979 }
980 }
981 ++sberr.rdsp;
982 return -1;
983 }
984
985 /*
986 * Doing certain things (like toggling the speaker) make
987 * the SB hardware go away for a while, so pause a little.
988 */
989 void
990 sbdsp_to(arg)
991 void *arg;
992 {
993 wakeup(arg);
994 }
995
996 void
997 sbdsp_pause(sc)
998 struct sbdsp_softc *sc;
999 {
1000 extern int hz;
1001
1002 timeout(sbdsp_to, sbdsp_to, hz/8);
1003 (void)tsleep(sbdsp_to, PWAIT, "sbpause", 0);
1004 }
1005
1006 /*
1007 * Turn on the speaker. The SBK documention says this operation
1008 * can take up to 1/10 of a second. Higher level layers should
1009 * probably let the task sleep for this amount of time after
1010 * calling here. Otherwise, things might not work (because
1011 * sbdsp_wdsp() and sbdsp_rdsp() will probably timeout.)
1012 *
1013 * These engineers had their heads up their ass when
1014 * they designed this card.
1015 */
1016 void
1017 sbdsp_spkron(sc)
1018 struct sbdsp_softc *sc;
1019 {
1020 (void)sbdsp_wdsp(sc, SB_DSP_SPKR_ON);
1021 sbdsp_pause(sc);
1022 }
1023
1024 /*
1025 * Turn off the speaker; see comment above.
1026 */
1027 void
1028 sbdsp_spkroff(sc)
1029 struct sbdsp_softc *sc;
1030 {
1031 (void)sbdsp_wdsp(sc, SB_DSP_SPKR_OFF);
1032 sbdsp_pause(sc);
1033 }
1034
1035 /*
1036 * Read the version number out of the card.
1037 * Store version information in the softc.
1038 */
1039 void
1040 sbversion(sc)
1041 struct sbdsp_softc *sc;
1042 {
1043 int v;
1044
1045 sc->sc_model = SB_UNK;
1046 sc->sc_version = 0;
1047 if (sbdsp_wdsp(sc, SB_DSP_VERSION) < 0)
1048 return;
1049 v = sbdsp_rdsp(sc) << 8;
1050 v |= sbdsp_rdsp(sc);
1051 if (v < 0)
1052 return;
1053 sc->sc_version = v;
1054 switch(SBVER_MAJOR(v)) {
1055 case 1:
1056 sc->sc_mixer_model = SBM_NONE;
1057 sc->sc_model = SB_1;
1058 break;
1059 case 2:
1060 /* Some SB2 have a mixer, some don't. */
1061 sbdsp_mix_write(sc, SBP_1335_MASTER_VOL, 0x04);
1062 sbdsp_mix_write(sc, SBP_1335_MIDI_VOL, 0x06);
1063 /* Check if we can read back the mixer values. */
1064 if ((sbdsp_mix_read(sc, SBP_1335_MASTER_VOL) & 0x0e) == 0x04 &&
1065 (sbdsp_mix_read(sc, SBP_1335_MIDI_VOL) & 0x0e) == 0x06)
1066 sc->sc_mixer_model = SBM_CT1335;
1067 else
1068 sc->sc_mixer_model = SBM_NONE;
1069 if (SBVER_MINOR(v) == 0)
1070 sc->sc_model = SB_20;
1071 else
1072 sc->sc_model = SB_2x;
1073 break;
1074 case 3:
1075 sc->sc_mixer_model = SBM_CT1345;
1076 sc->sc_model = SB_PRO;
1077 break;
1078 case 4:
1079 #if 0
1080 /* XXX This does not work */
1081 /* Most SB16 have a tone controls, but some don't. */
1082 sbdsp_mix_write(sc, SB16P_TREBLE_L, 0x80);
1083 /* Check if we can read back the mixer value. */
1084 if ((sbdsp_mix_read(sc, SB16P_TREBLE_L) & 0xf0) == 0x80)
1085 sc->sc_mixer_model = SBM_CT1745;
1086 else
1087 sc->sc_mixer_model = SBM_CT1XX5;
1088 #else
1089 sc->sc_mixer_model = SBM_CT1745;
1090 #endif
1091 #if 0
1092 /* XXX figure out a good way of determining the model */
1093 /* XXX what about SB_32 */
1094 if (SBVER_MINOR(v) == 16)
1095 sc->sc_model = SB_64;
1096 else
1097 #endif
1098 sc->sc_model = SB_16;
1099 break;
1100 }
1101 }
1102
1103 /*
1104 * Halt a DMA in progress.
1105 */
1106 int
1107 sbdsp_haltdma(addr)
1108 void *addr;
1109 {
1110 struct sbdsp_softc *sc = addr;
1111
1112 DPRINTF(("sbdsp_haltdma: sc=%p\n", sc));
1113
1114 sbdsp_reset(sc);
1115 return 0;
1116 }
1117
1118 int
1119 sbdsp_set_timeconst(sc, tc)
1120 struct sbdsp_softc *sc;
1121 int tc;
1122 {
1123 DPRINTF(("sbdsp_set_timeconst: sc=%p tc=%d\n", sc, tc));
1124
1125 if (sbdsp_wdsp(sc, SB_DSP_TIMECONST) < 0 ||
1126 sbdsp_wdsp(sc, tc) < 0)
1127 return EIO;
1128
1129 return 0;
1130 }
1131
1132 int
1133 sbdsp16_set_rate(sc, cmd, rate)
1134 struct sbdsp_softc *sc;
1135 int cmd, rate;
1136 {
1137 DPRINTF(("sbdsp16_set_rate: sc=%p cmd=0x%02x rate=%d\n", sc, cmd, rate));
1138
1139 if (sbdsp_wdsp(sc, cmd) < 0 ||
1140 sbdsp_wdsp(sc, rate >> 8) < 0 ||
1141 sbdsp_wdsp(sc, rate) < 0)
1142 return EIO;
1143 return 0;
1144 }
1145
1146 int
1147 sbdsp_dma_init_input(addr, buf, cc)
1148 void *addr;
1149 void *buf;
1150 int cc;
1151 {
1152 struct sbdsp_softc *sc = addr;
1153
1154 if (sc->sc_model == SB_1)
1155 return 0;
1156 sc->sc_i.run = SB_DMARUNNING;
1157 DPRINTF(("sbdsp: dma start loop input addr=%p cc=%d chan=%d\n",
1158 buf, cc, sc->sc_i.dmachan));
1159 isa_dmastart(sc->sc_ic, sc->sc_i.dmachan, buf,
1160 cc, NULL, DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1161 return 0;
1162 }
1163
1164 static int
1165 sbdsp_dma_setup_input(sc)
1166 struct sbdsp_softc *sc;
1167 {
1168 int stereo = sc->sc_i.modep->channels == 2;
1169 int filter;
1170
1171 /* Initialize the PCM */
1172 if (ISSBPRO(sc)) {
1173 if (sbdsp_wdsp(sc, sc->sc_i.modep->cmdchan) < 0)
1174 return 0;
1175 filter = stereo ? SBP_FILTER_OFF : sc->in_filter;
1176 sbdsp_mix_write(sc, SBP_INFILTER,
1177 (sbdsp_mix_read(sc, SBP_INFILTER) &
1178 ~SBP_IFILTER_MASK) | filter);
1179 }
1180
1181 if (ISSB16CLASS(sc)) {
1182 if (sbdsp16_set_rate(sc, SB_DSP16_INPUTRATE,
1183 sc->sc_i.rate)) {
1184 DPRINTF(("sbdsp_dma_setup_input: rate=%d set failed\n",
1185 sc->sc_i.rate));
1186 return 0;
1187 }
1188 } else {
1189 if (sbdsp_set_timeconst(sc, sc->sc_i.tc)) {
1190 DPRINTF(("sbdsp_dma_setup_input: tc=%d set failed\n",
1191 sc->sc_i.rate));
1192 return 0;
1193 }
1194 }
1195 return 1;
1196 }
1197
1198 int
1199 sbdsp_dma_input(addr, p, cc, intr, arg)
1200 void *addr;
1201 void *p;
1202 int cc;
1203 void (*intr) __P((void *));
1204 void *arg;
1205 {
1206 struct sbdsp_softc *sc = addr;
1207
1208 DPRINTFN(2, ("sbdsp_dma_input: sc=%p buf=%p cc=%d intr=%p(%p)\n",
1209 addr, p, cc, intr, arg));
1210 #ifdef DIAGNOSTIC
1211 if (sc->sc_i.modep->channels == 2 && (cc & 1)) {
1212 DPRINTF(("stereo record odd bytes (%d)\n", cc));
1213 return EIO;
1214 }
1215 #endif
1216
1217 if (sc->sc_i.modep->precision == 8) {
1218 #ifdef DIAGNOSTIC
1219 if (sc->sc_i.dmachan != sc->sc_drq8) {
1220 printf("sbdsp_dma_input: prec=%d bad chan %d\n",
1221 sc->sc_i.modep->precision, sc->sc_i.dmachan);
1222 return EIO;
1223 }
1224 #endif
1225 sc->sc_intr8 = intr;
1226 sc->sc_arg8 = arg;
1227 } else {
1228 #ifdef DIAGNOSTIC
1229 if (sc->sc_i.dmachan != sc->sc_drq16) {
1230 printf("sbdsp_dma_input: prec=%d bad chan %d\n",
1231 sc->sc_i.modep->precision, sc->sc_i.dmachan);
1232 return EIO;
1233 }
1234 #endif
1235 sc->sc_intr16 = intr;
1236 sc->sc_arg16 = arg;
1237 }
1238
1239 switch(sc->sc_i.run) {
1240 case SB_NOTRUNNING:
1241 /* Non-looping mode, not initialized */
1242 sc->sc_i.run = SB_RUNNING;
1243 if (!sbdsp_dma_setup_input(sc))
1244 goto giveup;
1245 /* fall into */
1246 case SB_RUNNING:
1247 /* Non-looping mode, start DMA */
1248 DPRINTFN(3,("sbdsp_dma_input: dmastart buf=%p cc=%d chan=%d\n",
1249 p, cc, sc->sc_i.dmachan));
1250 isa_dmastart(sc->sc_ic, sc->sc_i.dmachan, p,
1251 cc, NULL, DMAMODE_READ, BUS_DMA_NOWAIT);
1252
1253 /* Start PCM in non-looping mode */
1254 if ((sc->sc_model == SB_JAZZ && sc->sc_i.dmachan > 3) ||
1255 (sc->sc_model != SB_JAZZ && sc->sc_i.modep->precision == 16))
1256 cc >>= 1;
1257 --cc;
1258 if (sbdsp_wdsp(sc, sc->sc_i.modep->cmd) < 0 ||
1259 sbdsp_wdsp(sc, cc) < 0 ||
1260 sbdsp_wdsp(sc, cc >> 8) < 0) {
1261 DPRINTF(("sbdsp_dma_input: SB1 DMA start failed\n"));
1262 goto giveup;
1263 }
1264 break;
1265 case SB_DMARUNNING:
1266 /* Looping mode, not initialized */
1267 sc->sc_i.run = SB_PCMRUNNING;
1268 if (!sbdsp_dma_setup_input(sc))
1269 goto giveup;
1270 if ((sc->sc_model == SB_JAZZ && sc->sc_i.dmachan > 3) ||
1271 (sc->sc_model != SB_JAZZ && sc->sc_i.modep->precision == 16))
1272 cc >>= 1;
1273 --cc;
1274 /* Initialize looping PCM */
1275 if (ISSB16CLASS(sc)) {
1276 DPRINTFN(3, ("sbdsp16 input command cmd=0x%02x bmode=0x%02x cc=%d\n",
1277 sc->sc_i.modep->cmd, sc->sc_i.bmode, cc));
1278 if (sbdsp_wdsp(sc, sc->sc_i.modep->cmd) < 0 ||
1279 sbdsp_wdsp(sc, sc->sc_i.bmode) < 0 ||
1280 sbdsp_wdsp(sc, cc) < 0 ||
1281 sbdsp_wdsp(sc, cc >> 8) < 0) {
1282 DPRINTF(("sbdsp_dma_input: SB16 DMA start failed\n"));
1283 DPRINTF(("sbdsp16 input command cmd=0x%02x bmode=0x%02x cc=%d\n",
1284 sc->sc_i.modep->cmd, sc->sc_i.bmode, cc));
1285 goto giveup;
1286 }
1287 } else {
1288 DPRINTF(("sbdsp_dma_input: set blocksize=%d\n", cc));
1289 if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
1290 sbdsp_wdsp(sc, cc) < 0 ||
1291 sbdsp_wdsp(sc, cc >> 8) < 0) {
1292 DPRINTF(("sbdsp_dma_input: SB2 DMA blocksize failed\n"));
1293 goto giveup;
1294 }
1295 if (sbdsp_wdsp(sc, sc->sc_i.modep->cmd) < 0) {
1296 DPRINTF(("sbdsp_dma_input: SB2 DMA start failed\n"));
1297 goto giveup;
1298 }
1299 }
1300 break;
1301 case SB_PCMRUNNING:
1302 /* Looping mode, nothing to do */
1303 break;
1304 }
1305 return 0;
1306
1307 giveup:
1308 sbdsp_reset(sc);
1309 return EIO;
1310 }
1311
1312 int
1313 sbdsp_dma_init_output(addr, buf, cc)
1314 void *addr;
1315 void *buf;
1316 int cc;
1317 {
1318 struct sbdsp_softc *sc = addr;
1319
1320 if (sc->sc_model == SB_1)
1321 return 0;
1322 sc->sc_o.run = SB_DMARUNNING;
1323 DPRINTF(("sbdsp: dma start loop output buf=%p cc=%d chan=%d\n",
1324 buf, cc, sc->sc_o.dmachan));
1325 isa_dmastart(sc->sc_ic, sc->sc_o.dmachan, buf,
1326 cc, NULL, DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1327 return 0;
1328 }
1329
1330 static int
1331 sbdsp_dma_setup_output(sc)
1332 struct sbdsp_softc *sc;
1333 {
1334 int stereo = sc->sc_o.modep->channels == 2;
1335 int cmd;
1336
1337 if (ISSBPRO(sc)) {
1338 /* make sure we re-set stereo mixer bit when we start output. */
1339 sbdsp_mix_write(sc, SBP_STEREO,
1340 (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
1341 (stereo ? SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
1342 cmd = sc->sc_o.modep->cmdchan;
1343 if (cmd && sbdsp_wdsp(sc, cmd) < 0)
1344 return 0;
1345 }
1346
1347 if (ISSB16CLASS(sc)) {
1348 if (sbdsp16_set_rate(sc, SB_DSP16_OUTPUTRATE,
1349 sc->sc_o.rate)) {
1350 DPRINTF(("sbdsp_dma_setup_output: rate=%d set failed\n",
1351 sc->sc_o.rate));
1352 return 0;
1353 }
1354 } else {
1355 if (sbdsp_set_timeconst(sc, sc->sc_o.tc)) {
1356 DPRINTF(("sbdsp_dma_setup_output: tc=%d set failed\n",
1357 sc->sc_o.rate));
1358 return 0;
1359 }
1360 }
1361 return 1;
1362 }
1363
1364 int
1365 sbdsp_dma_output(addr, p, cc, intr, arg)
1366 void *addr;
1367 void *p;
1368 int cc;
1369 void (*intr) __P((void *));
1370 void *arg;
1371 {
1372 struct sbdsp_softc *sc = addr;
1373
1374 DPRINTFN(2, ("sbdsp_dma_output: sc=%p buf=%p cc=%d intr=%p(%p)\n",
1375 addr, p, cc, intr, arg));
1376 #ifdef DIAGNOSTIC
1377 if (sc->sc_o.modep->channels == 2 && (cc & 1)) {
1378 DPRINTF(("stereo playback odd bytes (%d)\n", cc));
1379 return EIO;
1380 }
1381 #endif
1382
1383 if (sc->sc_o.modep->precision == 8) {
1384 #ifdef DIAGNOSTIC
1385 if (sc->sc_o.dmachan != sc->sc_drq8) {
1386 printf("sbdsp_dma_output: prec=%d bad chan %d\n",
1387 sc->sc_o.modep->precision, sc->sc_o.dmachan);
1388 return EIO;
1389 }
1390 #endif
1391 sc->sc_intr8 = intr;
1392 sc->sc_arg8 = arg;
1393 } else {
1394 #ifdef DIAGNOSTIC
1395 if (sc->sc_o.dmachan != sc->sc_drq16) {
1396 printf("sbdsp_dma_output: prec=%d bad chan %d\n",
1397 sc->sc_o.modep->precision, sc->sc_o.dmachan);
1398 return EIO;
1399 }
1400 #endif
1401 sc->sc_intr16 = intr;
1402 sc->sc_arg16 = arg;
1403 }
1404
1405 switch(sc->sc_o.run) {
1406 case SB_NOTRUNNING:
1407 /* Non-looping mode, not initialized */
1408 sc->sc_o.run = SB_RUNNING;
1409 if (!sbdsp_dma_setup_output(sc))
1410 goto giveup;
1411 /* fall into */
1412 case SB_RUNNING:
1413 /* Non-looping mode, initialized. Start DMA and PCM */
1414 DPRINTFN(3, ("sbdsp: start dma out addr=%p, cc=%d, chan=%d\n",
1415 p, cc, sc->sc_o.dmachan));
1416 isa_dmastart(sc->sc_ic, sc->sc_o.dmachan, p,
1417 cc, NULL, DMAMODE_WRITE, BUS_DMA_NOWAIT);
1418 if ((sc->sc_model == SB_JAZZ && sc->sc_o.dmachan > 3) ||
1419 (sc->sc_model != SB_JAZZ && sc->sc_o.modep->precision == 16))
1420 cc >>= 1;
1421 --cc;
1422 if (sbdsp_wdsp(sc, sc->sc_o.modep->cmd) < 0 ||
1423 sbdsp_wdsp(sc, cc) < 0 ||
1424 sbdsp_wdsp(sc, cc >> 8) < 0) {
1425 DPRINTF(("sbdsp_dma_output: SB1 DMA start failed\n"));
1426 goto giveup;
1427 }
1428 break;
1429 case SB_DMARUNNING:
1430 /* Looping mode, not initialized */
1431 sc->sc_o.run = SB_PCMRUNNING;
1432 if (!sbdsp_dma_setup_output(sc))
1433 goto giveup;
1434 if ((sc->sc_model == SB_JAZZ && sc->sc_o.dmachan > 3) ||
1435 (sc->sc_model != SB_JAZZ && sc->sc_o.modep->precision == 16))
1436 cc >>= 1;
1437 --cc;
1438 /* Initialize looping PCM */
1439 if (ISSB16CLASS(sc)) {
1440 DPRINTF(("sbdsp_dma_output: SB16 cmd=0x%02x bmode=0x%02x cc=%d\n",
1441 sc->sc_o.modep->cmd,sc->sc_o.bmode, cc));
1442 if (sbdsp_wdsp(sc, sc->sc_o.modep->cmd) < 0 ||
1443 sbdsp_wdsp(sc, sc->sc_o.bmode) < 0 ||
1444 sbdsp_wdsp(sc, cc) < 0 ||
1445 sbdsp_wdsp(sc, cc >> 8) < 0) {
1446 DPRINTF(("sbdsp_dma_output: SB16 DMA start failed\n"));
1447 goto giveup;
1448 }
1449 } else {
1450 DPRINTF(("sbdsp_dma_output: set blocksize=%d\n", cc));
1451 if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
1452 sbdsp_wdsp(sc, cc) < 0 ||
1453 sbdsp_wdsp(sc, cc >> 8) < 0) {
1454 DPRINTF(("sbdsp_dma_output: SB2 DMA blocksize failed\n"));
1455 goto giveup;
1456 }
1457 if (sbdsp_wdsp(sc, sc->sc_o.modep->cmd) < 0) {
1458 DPRINTF(("sbdsp_dma_output: SB2 DMA start failed\n"));
1459 goto giveup;
1460 }
1461 }
1462 break;
1463 case SB_PCMRUNNING:
1464 /* Looping mode, nothing to do */
1465 break;
1466 }
1467 return 0;
1468
1469 giveup:
1470 sbdsp_reset(sc);
1471 return EIO;
1472 }
1473
1474 /*
1475 * Only the DSP unit on the sound blaster generates interrupts.
1476 * There are three cases of interrupt: reception of a midi byte
1477 * (when mode is enabled), completion of dma transmission, or
1478 * completion of a dma reception.
1479 *
1480 * If there is interrupt sharing or a spurious interrupt occurs
1481 * there is no way to distinguish this on an SB2. So if you have
1482 * an SB2 and experience problems, buy an SB16 (it's only $40).
1483 */
1484 int
1485 sbdsp_intr(arg)
1486 void *arg;
1487 {
1488 struct sbdsp_softc *sc = arg;
1489 int loop = sc->sc_model != SB_1;
1490 u_char irq;
1491
1492 DPRINTFN(2, ("sbdsp_intr: intr8=%p, intr16=%p\n",
1493 sc->sc_intr8, sc->sc_intr16));
1494 if (ISSB16CLASS(sc)) {
1495 irq = sbdsp_mix_read(sc, SBP_IRQ_STATUS);
1496 if ((irq & (SBP_IRQ_DMA8 | SBP_IRQ_DMA16 | SBP_IRQ_MPU401)) == 0) {
1497 DPRINTF(("sbdsp_intr: Spurious interrupt 0x%x\n", irq));
1498 return 0;
1499 }
1500 } else {
1501 if (!loop && !isa_dmafinished(sc->sc_ic, sc->sc_drq8))
1502 return 0;
1503 irq = SBP_IRQ_DMA8;
1504 }
1505 sc->sc_interrupts++;
1506 delay(10); /* XXX why? */
1507 if (sc->sc_intr8 == 0 && sc->sc_intr16 == 0 && sc->sc_intrm == 0) {
1508 DPRINTF(("sbdsp_intr: Unexpected interrupt 0x%x\n", irq));
1509 /* XXX return 0;*/ /* Did not expect an interrupt */
1510 }
1511
1512 /* clear interrupt */
1513 if (irq & SBP_IRQ_DMA8) {
1514 bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_IRQACK8);
1515 #if NMIDI > 0
1516 if (sc->sc_open == SB_OPEN_MIDI)
1517 sbdsp_midi_intr(sc);
1518 else
1519 #endif
1520 {
1521 if (!loop)
1522 isa_dmadone(sc->sc_ic, sc->sc_drq8);
1523 if (sc->sc_intr8)
1524 sc->sc_intr8(sc->sc_arg8);
1525 }
1526 }
1527 if (irq & SBP_IRQ_DMA16) {
1528 bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_IRQACK16);
1529 if (sc->sc_intr16)
1530 sc->sc_intr16(sc->sc_arg16);
1531 }
1532 #if NMIDI > 0
1533 if ((irq & SBP_IRQ_MPU401) & sc->sc_hasmpu) {
1534 mpu401_intr(&sc->sc_mpu_sc);
1535 }
1536 #endif
1537 return 1;
1538 }
1539
1540 /* Like val & mask, but make sure the result is correctly rounded. */
1541 #define MAXVAL 256
1542 static int
1543 sbdsp_adjust(val, mask)
1544 int val, mask;
1545 {
1546 val += (MAXVAL - mask) >> 1;
1547 if (val >= MAXVAL)
1548 val = MAXVAL-1;
1549 return val & mask;
1550 }
1551
1552 void
1553 sbdsp_set_mixer_gain(sc, port)
1554 struct sbdsp_softc *sc;
1555 int port;
1556 {
1557 int src, gain;
1558
1559 switch(sc->sc_mixer_model) {
1560 case SBM_NONE:
1561 return;
1562 case SBM_CT1335:
1563 gain = SB_1335_GAIN(sc->gain[port][SB_LEFT]);
1564 switch(port) {
1565 case SB_MASTER_VOL:
1566 src = SBP_1335_MASTER_VOL;
1567 break;
1568 case SB_MIDI_VOL:
1569 src = SBP_1335_MIDI_VOL;
1570 break;
1571 case SB_CD_VOL:
1572 src = SBP_1335_CD_VOL;
1573 break;
1574 case SB_VOICE_VOL:
1575 src = SBP_1335_VOICE_VOL;
1576 gain = SB_1335_MASTER_GAIN(sc->gain[port][SB_LEFT]);
1577 break;
1578 default:
1579 return;
1580 }
1581 sbdsp_mix_write(sc, src, gain);
1582 break;
1583 case SBM_CT1345:
1584 gain = SB_STEREO_GAIN(sc->gain[port][SB_LEFT],
1585 sc->gain[port][SB_RIGHT]);
1586 switch (port) {
1587 case SB_MIC_VOL:
1588 src = SBP_MIC_VOL;
1589 gain = SB_MIC_GAIN(sc->gain[port][SB_LEFT]);
1590 break;
1591 case SB_MASTER_VOL:
1592 src = SBP_MASTER_VOL;
1593 break;
1594 case SB_LINE_IN_VOL:
1595 src = SBP_LINE_VOL;
1596 break;
1597 case SB_VOICE_VOL:
1598 src = SBP_VOICE_VOL;
1599 break;
1600 case SB_MIDI_VOL:
1601 src = SBP_MIDI_VOL;
1602 break;
1603 case SB_CD_VOL:
1604 src = SBP_CD_VOL;
1605 break;
1606 default:
1607 return;
1608 }
1609 sbdsp_mix_write(sc, src, gain);
1610 break;
1611 case SBM_CT1XX5:
1612 case SBM_CT1745:
1613 switch (port) {
1614 case SB_MIC_VOL:
1615 src = SB16P_MIC_L;
1616 break;
1617 case SB_MASTER_VOL:
1618 src = SB16P_MASTER_L;
1619 break;
1620 case SB_LINE_IN_VOL:
1621 src = SB16P_LINE_L;
1622 break;
1623 case SB_VOICE_VOL:
1624 src = SB16P_VOICE_L;
1625 break;
1626 case SB_MIDI_VOL:
1627 src = SB16P_MIDI_L;
1628 break;
1629 case SB_CD_VOL:
1630 src = SB16P_CD_L;
1631 break;
1632 case SB_INPUT_GAIN:
1633 src = SB16P_INPUT_GAIN_L;
1634 break;
1635 case SB_OUTPUT_GAIN:
1636 src = SB16P_OUTPUT_GAIN_L;
1637 break;
1638 case SB_TREBLE:
1639 src = SB16P_TREBLE_L;
1640 break;
1641 case SB_BASS:
1642 src = SB16P_BASS_L;
1643 break;
1644 case SB_PCSPEAKER:
1645 sbdsp_mix_write(sc, SB16P_PCSPEAKER, sc->gain[port][SB_LEFT]);
1646 return;
1647 default:
1648 return;
1649 }
1650 sbdsp_mix_write(sc, src, sc->gain[port][SB_LEFT]);
1651 sbdsp_mix_write(sc, SB16P_L_TO_R(src), sc->gain[port][SB_RIGHT]);
1652 break;
1653 }
1654 }
1655
1656 int
1657 sbdsp_mixer_set_port(addr, cp)
1658 void *addr;
1659 mixer_ctrl_t *cp;
1660 {
1661 struct sbdsp_softc *sc = addr;
1662 int lgain, rgain;
1663 int mask, bits;
1664 int lmask, rmask, lbits, rbits;
1665 int mute, swap;
1666
1667 if (sc->sc_open == SB_OPEN_MIDI)
1668 return EBUSY;
1669
1670 DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev,
1671 cp->un.value.num_channels));
1672
1673 if (sc->sc_mixer_model == SBM_NONE)
1674 return EINVAL;
1675
1676 switch (cp->dev) {
1677 case SB_TREBLE:
1678 case SB_BASS:
1679 if (sc->sc_mixer_model == SBM_CT1345 ||
1680 sc->sc_mixer_model == SBM_CT1XX5) {
1681 if (cp->type != AUDIO_MIXER_ENUM)
1682 return EINVAL;
1683 switch (cp->dev) {
1684 case SB_TREBLE:
1685 sbdsp_set_ifilter(addr, cp->un.ord ? SB_TREBLE : 0);
1686 return 0;
1687 case SB_BASS:
1688 sbdsp_set_ifilter(addr, cp->un.ord ? SB_BASS : 0);
1689 return 0;
1690 }
1691 }
1692 case SB_PCSPEAKER:
1693 case SB_INPUT_GAIN:
1694 case SB_OUTPUT_GAIN:
1695 if (!ISSBM1745(sc))
1696 return EINVAL;
1697 case SB_MIC_VOL:
1698 case SB_LINE_IN_VOL:
1699 if (sc->sc_mixer_model == SBM_CT1335)
1700 return EINVAL;
1701 case SB_VOICE_VOL:
1702 case SB_MIDI_VOL:
1703 case SB_CD_VOL:
1704 case SB_MASTER_VOL:
1705 if (cp->type != AUDIO_MIXER_VALUE)
1706 return EINVAL;
1707
1708 /*
1709 * All the mixer ports are stereo except for the microphone.
1710 * If we get a single-channel gain value passed in, then we
1711 * duplicate it to both left and right channels.
1712 */
1713
1714 switch (cp->dev) {
1715 case SB_MIC_VOL:
1716 if (cp->un.value.num_channels != 1)
1717 return EINVAL;
1718
1719 lgain = rgain = SB_ADJUST_MIC_GAIN(sc,
1720 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1721 break;
1722 case SB_PCSPEAKER:
1723 if (cp->un.value.num_channels != 1)
1724 return EINVAL;
1725 /* fall into */
1726 case SB_INPUT_GAIN:
1727 case SB_OUTPUT_GAIN:
1728 lgain = rgain = SB_ADJUST_2_GAIN(sc,
1729 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1730 break;
1731 default:
1732 switch (cp->un.value.num_channels) {
1733 case 1:
1734 lgain = rgain = SB_ADJUST_GAIN(sc,
1735 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1736 break;
1737 case 2:
1738 if (sc->sc_mixer_model == SBM_CT1335)
1739 return EINVAL;
1740 lgain = SB_ADJUST_GAIN(sc,
1741 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
1742 rgain = SB_ADJUST_GAIN(sc,
1743 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
1744 break;
1745 default:
1746 return EINVAL;
1747 }
1748 break;
1749 }
1750 sc->gain[cp->dev][SB_LEFT] = lgain;
1751 sc->gain[cp->dev][SB_RIGHT] = rgain;
1752
1753 sbdsp_set_mixer_gain(sc, cp->dev);
1754 break;
1755
1756 case SB_RECORD_SOURCE:
1757 if (ISSBM1745(sc)) {
1758 if (cp->type != AUDIO_MIXER_SET)
1759 return EINVAL;
1760 return sbdsp_set_in_ports(sc, cp->un.mask);
1761 } else {
1762 if (cp->type != AUDIO_MIXER_ENUM)
1763 return EINVAL;
1764 sc->in_port = cp->un.ord;
1765 return sbdsp_set_in_ports(sc, 1 << cp->un.ord);
1766 }
1767 break;
1768
1769 case SB_AGC:
1770 if (!ISSBM1745(sc) || cp->type != AUDIO_MIXER_ENUM)
1771 return EINVAL;
1772 sbdsp_mix_write(sc, SB16P_AGC, cp->un.ord & 1);
1773 break;
1774
1775 case SB_CD_OUT_MUTE:
1776 mask = SB16P_SW_CD;
1777 goto omute;
1778 case SB_MIC_OUT_MUTE:
1779 mask = SB16P_SW_MIC;
1780 goto omute;
1781 case SB_LINE_OUT_MUTE:
1782 mask = SB16P_SW_LINE;
1783 omute:
1784 if (cp->type != AUDIO_MIXER_ENUM)
1785 return EINVAL;
1786 bits = sbdsp_mix_read(sc, SB16P_OSWITCH);
1787 sc->gain[cp->dev][SB_LR] = cp->un.ord != 0;
1788 if (cp->un.ord)
1789 bits = bits & ~mask;
1790 else
1791 bits = bits | mask;
1792 sbdsp_mix_write(sc, SB16P_OSWITCH, bits);
1793 break;
1794
1795 case SB_MIC_IN_MUTE:
1796 case SB_MIC_SWAP:
1797 lmask = rmask = SB16P_SW_MIC;
1798 goto imute;
1799 case SB_CD_IN_MUTE:
1800 case SB_CD_SWAP:
1801 lmask = SB16P_SW_CD_L;
1802 rmask = SB16P_SW_CD_R;
1803 goto imute;
1804 case SB_LINE_IN_MUTE:
1805 case SB_LINE_SWAP:
1806 lmask = SB16P_SW_LINE_L;
1807 rmask = SB16P_SW_LINE_R;
1808 goto imute;
1809 case SB_MIDI_IN_MUTE:
1810 case SB_MIDI_SWAP:
1811 lmask = SB16P_SW_MIDI_L;
1812 rmask = SB16P_SW_MIDI_R;
1813 imute:
1814 if (cp->type != AUDIO_MIXER_ENUM)
1815 return EINVAL;
1816 mask = lmask | rmask;
1817 lbits = sbdsp_mix_read(sc, SB16P_ISWITCH_L) & ~mask;
1818 rbits = sbdsp_mix_read(sc, SB16P_ISWITCH_R) & ~mask;
1819 sc->gain[cp->dev][SB_LR] = cp->un.ord != 0;
1820 if (SB_IS_IN_MUTE(cp->dev)) {
1821 mute = cp->dev;
1822 swap = mute - SB_CD_IN_MUTE + SB_CD_SWAP;
1823 } else {
1824 swap = cp->dev;
1825 mute = swap + SB_CD_IN_MUTE - SB_CD_SWAP;
1826 }
1827 if (sc->gain[swap][SB_LR]) {
1828 mask = lmask;
1829 lmask = rmask;
1830 rmask = mask;
1831 }
1832 if (!sc->gain[mute][SB_LR]) {
1833 lbits = lbits | lmask;
1834 rbits = rbits | rmask;
1835 }
1836 sbdsp_mix_write(sc, SB16P_ISWITCH_L, lbits);
1837 sbdsp_mix_write(sc, SB16P_ISWITCH_L, rbits);
1838 break;
1839
1840 default:
1841 return EINVAL;
1842 }
1843
1844 return 0;
1845 }
1846
1847 int
1848 sbdsp_mixer_get_port(addr, cp)
1849 void *addr;
1850 mixer_ctrl_t *cp;
1851 {
1852 struct sbdsp_softc *sc = addr;
1853
1854 if (sc->sc_open == SB_OPEN_MIDI)
1855 return EBUSY;
1856
1857 DPRINTF(("sbdsp_mixer_get_port: port=%d\n", cp->dev));
1858
1859 if (sc->sc_mixer_model == SBM_NONE)
1860 return EINVAL;
1861
1862 switch (cp->dev) {
1863 case SB_TREBLE:
1864 case SB_BASS:
1865 if (sc->sc_mixer_model == SBM_CT1345 ||
1866 sc->sc_mixer_model == SBM_CT1XX5) {
1867 switch (cp->dev) {
1868 case SB_TREBLE:
1869 cp->un.ord = sbdsp_get_ifilter(addr) == SB_TREBLE;
1870 return 0;
1871 case SB_BASS:
1872 cp->un.ord = sbdsp_get_ifilter(addr) == SB_BASS;
1873 return 0;
1874 }
1875 }
1876 case SB_PCSPEAKER:
1877 case SB_INPUT_GAIN:
1878 case SB_OUTPUT_GAIN:
1879 if (!ISSBM1745(sc))
1880 return EINVAL;
1881 case SB_MIC_VOL:
1882 case SB_LINE_IN_VOL:
1883 if (sc->sc_mixer_model == SBM_CT1335)
1884 return EINVAL;
1885 case SB_VOICE_VOL:
1886 case SB_MIDI_VOL:
1887 case SB_CD_VOL:
1888 case SB_MASTER_VOL:
1889 switch (cp->dev) {
1890 case SB_MIC_VOL:
1891 case SB_PCSPEAKER:
1892 if (cp->un.value.num_channels != 1)
1893 return EINVAL;
1894 /* fall into */
1895 default:
1896 switch (cp->un.value.num_channels) {
1897 case 1:
1898 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1899 sc->gain[cp->dev][SB_LEFT];
1900 break;
1901 case 2:
1902 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1903 sc->gain[cp->dev][SB_LEFT];
1904 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1905 sc->gain[cp->dev][SB_RIGHT];
1906 break;
1907 default:
1908 return EINVAL;
1909 }
1910 break;
1911 }
1912 break;
1913
1914 case SB_RECORD_SOURCE:
1915 if (ISSBM1745(sc))
1916 cp->un.mask = sc->in_mask;
1917 else
1918 cp->un.ord = sc->in_port;
1919 break;
1920
1921 case SB_AGC:
1922 if (!ISSBM1745(sc))
1923 return EINVAL;
1924 cp->un.ord = sbdsp_mix_read(sc, SB16P_AGC);
1925 break;
1926
1927 case SB_CD_IN_MUTE:
1928 case SB_MIC_IN_MUTE:
1929 case SB_LINE_IN_MUTE:
1930 case SB_MIDI_IN_MUTE:
1931 case SB_CD_SWAP:
1932 case SB_MIC_SWAP:
1933 case SB_LINE_SWAP:
1934 case SB_MIDI_SWAP:
1935 case SB_CD_OUT_MUTE:
1936 case SB_MIC_OUT_MUTE:
1937 case SB_LINE_OUT_MUTE:
1938 cp->un.ord = sc->gain[cp->dev][SB_LR];
1939 break;
1940
1941 default:
1942 return EINVAL;
1943 }
1944
1945 return 0;
1946 }
1947
1948 int
1949 sbdsp_mixer_query_devinfo(addr, dip)
1950 void *addr;
1951 mixer_devinfo_t *dip;
1952 {
1953 struct sbdsp_softc *sc = addr;
1954 int chan, class, is1745;
1955
1956 DPRINTF(("sbdsp_mixer_query_devinfo: model=%d index=%d\n",
1957 sc->sc_mixer_model, dip->index));
1958
1959 if (sc->sc_mixer_model == SBM_NONE)
1960 return ENXIO;
1961
1962 chan = sc->sc_mixer_model == SBM_CT1335 ? 1 : 2;
1963 is1745 = ISSBM1745(sc);
1964 class = is1745 ? SB_INPUT_CLASS : SB_OUTPUT_CLASS;
1965
1966 switch (dip->index) {
1967 case SB_MASTER_VOL:
1968 dip->type = AUDIO_MIXER_VALUE;
1969 dip->mixer_class = SB_OUTPUT_CLASS;
1970 dip->prev = dip->next = AUDIO_MIXER_LAST;
1971 strcpy(dip->label.name, AudioNmaster);
1972 dip->un.v.num_channels = chan;
1973 strcpy(dip->un.v.units.name, AudioNvolume);
1974 return 0;
1975 case SB_MIDI_VOL:
1976 dip->type = AUDIO_MIXER_VALUE;
1977 dip->mixer_class = class;
1978 dip->prev = AUDIO_MIXER_LAST;
1979 dip->next = is1745 ? SB_MIDI_IN_MUTE : AUDIO_MIXER_LAST;
1980 strcpy(dip->label.name, AudioNfmsynth);
1981 dip->un.v.num_channels = chan;
1982 strcpy(dip->un.v.units.name, AudioNvolume);
1983 return 0;
1984 case SB_CD_VOL:
1985 dip->type = AUDIO_MIXER_VALUE;
1986 dip->mixer_class = class;
1987 dip->prev = AUDIO_MIXER_LAST;
1988 dip->next = is1745 ? SB_CD_IN_MUTE : AUDIO_MIXER_LAST;
1989 strcpy(dip->label.name, AudioNcd);
1990 dip->un.v.num_channels = chan;
1991 strcpy(dip->un.v.units.name, AudioNvolume);
1992 return 0;
1993 case SB_VOICE_VOL:
1994 dip->type = AUDIO_MIXER_VALUE;
1995 dip->mixer_class = class;
1996 dip->prev = AUDIO_MIXER_LAST;
1997 dip->next = AUDIO_MIXER_LAST;
1998 strcpy(dip->label.name, AudioNdac);
1999 dip->un.v.num_channels = chan;
2000 strcpy(dip->un.v.units.name, AudioNvolume);
2001 return 0;
2002 case SB_OUTPUT_CLASS:
2003 dip->type = AUDIO_MIXER_CLASS;
2004 dip->mixer_class = SB_OUTPUT_CLASS;
2005 dip->next = dip->prev = AUDIO_MIXER_LAST;
2006 strcpy(dip->label.name, AudioCoutputs);
2007 return 0;
2008 }
2009
2010 if (sc->sc_mixer_model == SBM_CT1335)
2011 return ENXIO;
2012
2013 switch (dip->index) {
2014 case SB_MIC_VOL:
2015 dip->type = AUDIO_MIXER_VALUE;
2016 dip->mixer_class = class;
2017 dip->prev = AUDIO_MIXER_LAST;
2018 dip->next = is1745 ? SB_MIC_IN_MUTE : AUDIO_MIXER_LAST;
2019 strcpy(dip->label.name, AudioNmicrophone);
2020 dip->un.v.num_channels = 1;
2021 strcpy(dip->un.v.units.name, AudioNvolume);
2022 return 0;
2023
2024 case SB_LINE_IN_VOL:
2025 dip->type = AUDIO_MIXER_VALUE;
2026 dip->mixer_class = class;
2027 dip->prev = AUDIO_MIXER_LAST;
2028 dip->next = is1745 ? SB_LINE_IN_MUTE : AUDIO_MIXER_LAST;
2029 strcpy(dip->label.name, AudioNline);
2030 dip->un.v.num_channels = 2;
2031 strcpy(dip->un.v.units.name, AudioNvolume);
2032 return 0;
2033
2034 case SB_RECORD_SOURCE:
2035 dip->mixer_class = SB_RECORD_CLASS;
2036 dip->prev = dip->next = AUDIO_MIXER_LAST;
2037 strcpy(dip->label.name, AudioNsource);
2038 if (ISSBM1745(sc)) {
2039 dip->type = AUDIO_MIXER_SET;
2040 dip->un.s.num_mem = 4;
2041 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
2042 dip->un.s.member[0].mask = 1 << SB_MIC_VOL;
2043 strcpy(dip->un.s.member[1].label.name, AudioNcd);
2044 dip->un.s.member[1].mask = 1 << SB_CD_VOL;
2045 strcpy(dip->un.s.member[2].label.name, AudioNline);
2046 dip->un.s.member[2].mask = 1 << SB_LINE_IN_VOL;
2047 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
2048 dip->un.s.member[3].mask = 1 << SB_MIDI_VOL;
2049 } else {
2050 dip->type = AUDIO_MIXER_ENUM;
2051 dip->un.e.num_mem = 3;
2052 strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
2053 dip->un.e.member[0].ord = SB_MIC_VOL;
2054 strcpy(dip->un.e.member[1].label.name, AudioNcd);
2055 dip->un.e.member[1].ord = SB_CD_VOL;
2056 strcpy(dip->un.e.member[2].label.name, AudioNline);
2057 dip->un.e.member[2].ord = SB_LINE_IN_VOL;
2058 }
2059 return 0;
2060
2061 case SB_BASS:
2062 dip->prev = dip->next = AUDIO_MIXER_LAST;
2063 strcpy(dip->label.name, AudioNbass);
2064 if (sc->sc_mixer_model == SBM_CT1745) {
2065 dip->type = AUDIO_MIXER_VALUE;
2066 dip->mixer_class = SB_EQUALIZATION_CLASS;
2067 dip->un.v.num_channels = 2;
2068 strcpy(dip->un.v.units.name, AudioNbass);
2069 } else {
2070 dip->type = AUDIO_MIXER_ENUM;
2071 dip->mixer_class = SB_INPUT_CLASS;
2072 dip->un.e.num_mem = 2;
2073 strcpy(dip->un.e.member[0].label.name, AudioNoff);
2074 dip->un.e.member[0].ord = 0;
2075 strcpy(dip->un.e.member[1].label.name, AudioNon);
2076 dip->un.e.member[1].ord = 1;
2077 }
2078 return 0;
2079
2080 case SB_TREBLE:
2081 dip->prev = dip->next = AUDIO_MIXER_LAST;
2082 strcpy(dip->label.name, AudioNtreble);
2083 if (sc->sc_mixer_model == SBM_CT1745) {
2084 dip->type = AUDIO_MIXER_VALUE;
2085 dip->mixer_class = SB_EQUALIZATION_CLASS;
2086 dip->un.v.num_channels = 2;
2087 strcpy(dip->un.v.units.name, AudioNtreble);
2088 } else {
2089 dip->type = AUDIO_MIXER_ENUM;
2090 dip->mixer_class = SB_INPUT_CLASS;
2091 dip->un.e.num_mem = 2;
2092 strcpy(dip->un.e.member[0].label.name, AudioNoff);
2093 dip->un.e.member[0].ord = 0;
2094 strcpy(dip->un.e.member[1].label.name, AudioNon);
2095 dip->un.e.member[1].ord = 1;
2096 }
2097 return 0;
2098
2099 case SB_RECORD_CLASS: /* record source class */
2100 dip->type = AUDIO_MIXER_CLASS;
2101 dip->mixer_class = SB_RECORD_CLASS;
2102 dip->next = dip->prev = AUDIO_MIXER_LAST;
2103 strcpy(dip->label.name, AudioCrecord);
2104 return 0;
2105
2106 case SB_INPUT_CLASS:
2107 dip->type = AUDIO_MIXER_CLASS;
2108 dip->mixer_class = SB_INPUT_CLASS;
2109 dip->next = dip->prev = AUDIO_MIXER_LAST;
2110 strcpy(dip->label.name, AudioCinputs);
2111 return 0;
2112
2113 }
2114
2115 if (sc->sc_mixer_model == SBM_CT1345)
2116 return ENXIO;
2117
2118 switch(dip->index) {
2119 case SB_PCSPEAKER:
2120 dip->type = AUDIO_MIXER_VALUE;
2121 dip->mixer_class = SB_INPUT_CLASS;
2122 dip->prev = dip->next = AUDIO_MIXER_LAST;
2123 strcpy(dip->label.name, "pc_speaker");
2124 dip->un.v.num_channels = 1;
2125 strcpy(dip->un.v.units.name, AudioNvolume);
2126 return 0;
2127
2128 case SB_INPUT_GAIN:
2129 dip->type = AUDIO_MIXER_VALUE;
2130 dip->mixer_class = SB_INPUT_CLASS;
2131 dip->prev = dip->next = AUDIO_MIXER_LAST;
2132 strcpy(dip->label.name, AudioNinput);
2133 dip->un.v.num_channels = 2;
2134 strcpy(dip->un.v.units.name, AudioNvolume);
2135 return 0;
2136
2137 case SB_OUTPUT_GAIN:
2138 dip->type = AUDIO_MIXER_VALUE;
2139 dip->mixer_class = SB_OUTPUT_CLASS;
2140 dip->prev = dip->next = AUDIO_MIXER_LAST;
2141 strcpy(dip->label.name, AudioNoutput);
2142 dip->un.v.num_channels = 2;
2143 strcpy(dip->un.v.units.name, AudioNvolume);
2144 return 0;
2145
2146 case SB_AGC:
2147 dip->type = AUDIO_MIXER_ENUM;
2148 dip->mixer_class = SB_INPUT_CLASS;
2149 dip->prev = dip->next = AUDIO_MIXER_LAST;
2150 strcpy(dip->label.name, "agc");
2151 dip->un.e.num_mem = 2;
2152 strcpy(dip->un.e.member[0].label.name, AudioNoff);
2153 dip->un.e.member[0].ord = 0;
2154 strcpy(dip->un.e.member[1].label.name, AudioNon);
2155 dip->un.e.member[1].ord = 1;
2156 return 0;
2157
2158 case SB_EQUALIZATION_CLASS:
2159 dip->type = AUDIO_MIXER_CLASS;
2160 dip->mixer_class = SB_EQUALIZATION_CLASS;
2161 dip->next = dip->prev = AUDIO_MIXER_LAST;
2162 strcpy(dip->label.name, AudioCequalization);
2163 return 0;
2164
2165 case SB_CD_IN_MUTE:
2166 dip->prev = SB_CD_VOL;
2167 dip->next = SB_CD_SWAP;
2168 dip->mixer_class = SB_INPUT_CLASS;
2169 goto mute;
2170
2171 case SB_MIC_IN_MUTE:
2172 dip->prev = SB_MIC_VOL;
2173 dip->next = SB_MIC_SWAP;
2174 dip->mixer_class = SB_INPUT_CLASS;
2175 goto mute;
2176
2177 case SB_LINE_IN_MUTE:
2178 dip->prev = SB_LINE_IN_VOL;
2179 dip->next = SB_LINE_SWAP;
2180 dip->mixer_class = SB_INPUT_CLASS;
2181 goto mute;
2182
2183 case SB_MIDI_IN_MUTE:
2184 dip->prev = SB_MIDI_VOL;
2185 dip->next = SB_MIDI_SWAP;
2186 dip->mixer_class = SB_INPUT_CLASS;
2187 goto mute;
2188
2189 case SB_CD_SWAP:
2190 dip->prev = SB_CD_IN_MUTE;
2191 dip->next = SB_CD_OUT_MUTE;
2192 goto swap;
2193
2194 case SB_MIC_SWAP:
2195 dip->prev = SB_MIC_IN_MUTE;
2196 dip->next = SB_MIC_OUT_MUTE;
2197 goto swap;
2198
2199 case SB_LINE_SWAP:
2200 dip->prev = SB_LINE_IN_MUTE;
2201 dip->next = SB_LINE_OUT_MUTE;
2202 goto swap;
2203
2204 case SB_MIDI_SWAP:
2205 dip->prev = SB_MIDI_IN_MUTE;
2206 dip->next = AUDIO_MIXER_LAST;
2207 swap:
2208 dip->mixer_class = SB_INPUT_CLASS;
2209 strcpy(dip->label.name, AudioNswap);
2210 goto mute1;
2211
2212 case SB_CD_OUT_MUTE:
2213 dip->prev = SB_CD_SWAP;
2214 dip->next = AUDIO_MIXER_LAST;
2215 dip->mixer_class = SB_OUTPUT_CLASS;
2216 goto mute;
2217
2218 case SB_MIC_OUT_MUTE:
2219 dip->prev = SB_MIC_SWAP;
2220 dip->next = AUDIO_MIXER_LAST;
2221 dip->mixer_class = SB_OUTPUT_CLASS;
2222 goto mute;
2223
2224 case SB_LINE_OUT_MUTE:
2225 dip->prev = SB_LINE_SWAP;
2226 dip->next = AUDIO_MIXER_LAST;
2227 dip->mixer_class = SB_OUTPUT_CLASS;
2228 mute:
2229 strcpy(dip->label.name, AudioNmute);
2230 mute1:
2231 dip->type = AUDIO_MIXER_ENUM;
2232 dip->un.e.num_mem = 2;
2233 strcpy(dip->un.e.member[0].label.name, AudioNoff);
2234 dip->un.e.member[0].ord = 0;
2235 strcpy(dip->un.e.member[1].label.name, AudioNon);
2236 dip->un.e.member[1].ord = 1;
2237 return 0;
2238
2239 }
2240
2241 return ENXIO;
2242 }
2243
2244 void *
2245 sb_malloc(addr, size, pool, flags)
2246 void *addr;
2247 unsigned long size;
2248 int pool;
2249 int flags;
2250 {
2251 struct sbdsp_softc *sc = addr;
2252
2253 return isa_malloc(sc->sc_ic, 4, size, pool, flags);
2254 }
2255
2256 void
2257 sb_free(addr, ptr, pool)
2258 void *addr;
2259 void *ptr;
2260 int pool;
2261 {
2262 isa_free(ptr, pool);
2263 }
2264
2265 unsigned long
2266 sb_round(addr, size)
2267 void *addr;
2268 unsigned long size;
2269 {
2270 if (size > MAX_ISADMA)
2271 size = MAX_ISADMA;
2272 return size;
2273 }
2274
2275 int
2276 sb_mappage(addr, mem, off, prot)
2277 void *addr;
2278 void *mem;
2279 int off;
2280 int prot;
2281 {
2282 return isa_mappage(mem, off, prot);
2283 }
2284
2285 int
2286 sbdsp_get_props(addr)
2287 void *addr;
2288 {
2289 struct sbdsp_softc *sc = addr;
2290 return AUDIO_PROP_MMAP |
2291 (sc->sc_fullduplex ? AUDIO_PROP_FULLDUPLEX : 0);
2292 }
2293
2294 #if NMIDI > 0
2295 /*
2296 * MIDI related routines.
2297 */
2298
2299 int
2300 sbdsp_midi_open(addr, flags, iintr, ointr, arg)
2301 void *addr;
2302 int flags;
2303 void (*iintr)__P((void *, int));
2304 void (*ointr)__P((void *));
2305 void *arg;
2306 {
2307 struct sbdsp_softc *sc = addr;
2308
2309 DPRINTF(("sbdsp_midi_open: sc=%p\n", sc));
2310
2311 if (sc->sc_open != SB_CLOSED)
2312 return EBUSY;
2313 if (sbdsp_reset(sc) != 0)
2314 return EIO;
2315
2316 if (sc->sc_model >= SB_20)
2317 if (sbdsp_wdsp(sc, SB_MIDI_UART_INTR)) /* enter UART mode */
2318 return EIO;
2319 sc->sc_open = SB_OPEN_MIDI;
2320 sc->sc_openflags = flags;
2321 sc->sc_intrm = iintr;
2322 sc->sc_argm = arg;
2323 return 0;
2324 }
2325
2326 void
2327 sbdsp_midi_close(addr)
2328 void *addr;
2329 {
2330 struct sbdsp_softc *sc = addr;
2331
2332 DPRINTF(("sbdsp_midi_close: sc=%p\n", sc));
2333
2334 if (sc->sc_model >= SB_20)
2335 sbdsp_reset(sc); /* exit UART mode */
2336 sc->sc_open = SB_CLOSED;
2337 sc->sc_intrm = 0;
2338 }
2339
2340 int
2341 sbdsp_midi_output(addr, d)
2342 void *addr;
2343 int d;
2344 {
2345 struct sbdsp_softc *sc = addr;
2346
2347 if (sc->sc_model < SB_20 && sbdsp_wdsp(sc, SB_MIDI_WRITE))
2348 return EIO;
2349 if (sbdsp_wdsp(sc, d))
2350 return EIO;
2351 return 0;
2352 }
2353
2354 void
2355 sbdsp_midi_getinfo(addr, mi)
2356 void *addr;
2357 struct midi_info *mi;
2358 {
2359 struct sbdsp_softc *sc = addr;
2360
2361 mi->name = sc->sc_model < SB_20 ? "SB MIDI cmd" : "SB MIDI UART";
2362 mi->props = MIDI_PROP_CAN_INPUT;
2363 }
2364
2365 void
2366 sbdsp_midi_intr(sc)
2367 struct sbdsp_softc *sc;
2368 {
2369 sc->sc_intrm(sc->sc_argm, sbdsp_rdsp(sc));
2370 }
2371
2372 #endif
2373
2374