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