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