sbdsp.c revision 1.77 1 /* $NetBSD: sbdsp.c,v 1.77 1997/12/02 13:17:39 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 <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 /* XXX what about SB_32 */
1075 if (SBVER_MINOR(v) == 16)
1076 sc->sc_model = SB_64;
1077 else
1078 sc->sc_model = SB_16;
1079 break;
1080 }
1081 }
1082
1083 /*
1084 * Halt a DMA in progress.
1085 */
1086 int
1087 sbdsp_haltdma(addr)
1088 void *addr;
1089 {
1090 struct sbdsp_softc *sc = addr;
1091
1092 DPRINTF(("sbdsp_haltdma: sc=%p\n", sc));
1093
1094 sbdsp_reset(sc);
1095 return 0;
1096 }
1097
1098 int
1099 sbdsp_set_timeconst(sc, tc)
1100 struct sbdsp_softc *sc;
1101 int tc;
1102 {
1103 DPRINTF(("sbdsp_set_timeconst: sc=%p tc=%d\n", sc, tc));
1104
1105 if (sbdsp_wdsp(sc, SB_DSP_TIMECONST) < 0 ||
1106 sbdsp_wdsp(sc, tc) < 0)
1107 return EIO;
1108
1109 return 0;
1110 }
1111
1112 int
1113 sbdsp16_set_rate(sc, cmd, rate)
1114 struct sbdsp_softc *sc;
1115 int cmd, rate;
1116 {
1117 DPRINTF(("sbdsp16_set_rate: sc=%p cmd=0x%02x rate=%d\n", sc, cmd, rate));
1118
1119 if (sbdsp_wdsp(sc, cmd) < 0 ||
1120 sbdsp_wdsp(sc, rate >> 8) < 0 ||
1121 sbdsp_wdsp(sc, rate) < 0)
1122 return EIO;
1123 return 0;
1124 }
1125
1126 int
1127 sbdsp_dma_init_input(addr, buf, cc)
1128 void *addr;
1129 void *buf;
1130 int cc;
1131 {
1132 struct sbdsp_softc *sc = addr;
1133
1134 if (sc->sc_model == SB_1)
1135 return 0;
1136 sc->sc_i.run = SB_DMARUNNING;
1137 DPRINTF(("sbdsp: dma start loop input addr=%p cc=%d chan=%d\n",
1138 buf, cc, sc->sc_i.dmachan));
1139 isa_dmastart(sc->sc_isa, sc->sc_i.dmachan, buf,
1140 cc, NULL, DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1141 return 0;
1142 }
1143
1144 static int
1145 sbdsp_dma_setup_input(sc)
1146 struct sbdsp_softc *sc;
1147 {
1148 int stereo = sc->sc_i.modep->channels == 2;
1149 int filter;
1150
1151 /* Initialize the PCM */
1152 if (ISSBPRO(sc)) {
1153 if (sbdsp_wdsp(sc, sc->sc_i.modep->cmdchan) < 0)
1154 return 0;
1155 filter = stereo ? SBP_FILTER_OFF : sc->in_filter;
1156 sbdsp_mix_write(sc, SBP_INFILTER,
1157 (sbdsp_mix_read(sc, SBP_INFILTER) &
1158 ~SBP_IFILTER_MASK) | filter);
1159 }
1160
1161 if (ISSB16CLASS(sc)) {
1162 if (sbdsp16_set_rate(sc, SB_DSP16_INPUTRATE,
1163 sc->sc_i.rate)) {
1164 DPRINTF(("sbdsp_dma_setup_input: rate=%d set failed\n",
1165 sc->sc_i.rate));
1166 return 0;
1167 }
1168 } else {
1169 if (sbdsp_set_timeconst(sc, sc->sc_i.tc)) {
1170 DPRINTF(("sbdsp_dma_setup_input: tc=%d set failed\n",
1171 sc->sc_i.rate));
1172 return 0;
1173 }
1174 }
1175 return 1;
1176 }
1177
1178 int
1179 sbdsp_dma_input(addr, p, cc, intr, arg)
1180 void *addr;
1181 void *p;
1182 int cc;
1183 void (*intr) __P((void *));
1184 void *arg;
1185 {
1186 struct sbdsp_softc *sc = addr;
1187
1188 #ifdef AUDIO_DEBUG
1189 if (sbdspdebug > 1)
1190 printf("sbdsp_dma_input: sc=%p buf=%p cc=%d intr=%p(%p)\n",
1191 addr, p, cc, intr, arg);
1192 #endif
1193 #ifdef DIAGNOSTIC
1194 if (sc->sc_i.modep->channels == 2 && (cc & 1)) {
1195 DPRINTF(("stereo record odd bytes (%d)\n", cc));
1196 return EIO;
1197 }
1198 #endif
1199
1200 if (sc->sc_i.modep->precision == 8) {
1201 #ifdef DIAGNOSTIC
1202 if (sc->sc_i.dmachan != sc->sc_drq8) {
1203 printf("sbdsp_dma_input: prec=%d bad chan %d\n",
1204 sc->sc_i.modep->precision, sc->sc_i.dmachan);
1205 return EIO;
1206 }
1207 #endif
1208 sc->sc_intr8 = intr;
1209 sc->sc_arg8 = arg;
1210 } else {
1211 #ifdef DIAGNOSTIC
1212 if (sc->sc_i.dmachan != sc->sc_drq16) {
1213 printf("sbdsp_dma_input: prec=%d bad chan %d\n",
1214 sc->sc_i.modep->precision, sc->sc_i.dmachan);
1215 return EIO;
1216 }
1217 #endif
1218 sc->sc_intr16 = intr;
1219 sc->sc_arg16 = arg;
1220 }
1221
1222 switch(sc->sc_i.run) {
1223 case SB_NOTRUNNING:
1224 /* Non-looping mode, not initialized */
1225 sc->sc_i.run = SB_RUNNING;
1226 if (!sbdsp_dma_setup_input(sc))
1227 goto giveup;
1228 /* fall into */
1229 case SB_RUNNING:
1230 /* Non-looping mode, start DMA */
1231 #ifdef AUDIO_DEBUG
1232 if (sbdspdebug > 2)
1233 printf("sbdsp_dma_input: dmastart buf=%p cc=%d chan=%d\n",
1234 p, cc, sc->sc_i.dmachan);
1235 #endif
1236 isa_dmastart(sc->sc_isa, sc->sc_i.dmachan, p,
1237 cc, NULL, DMAMODE_READ, BUS_DMA_NOWAIT);
1238
1239 /* Start PCM in non-looping mode */
1240 if ((sc->sc_model == SB_JAZZ && sc->sc_i.dmachan > 3) ||
1241 (sc->sc_model != SB_JAZZ && sc->sc_i.modep->precision == 16))
1242 cc >>= 1;
1243 --cc;
1244 if (sbdsp_wdsp(sc, sc->sc_i.modep->cmd) < 0 ||
1245 sbdsp_wdsp(sc, cc) < 0 ||
1246 sbdsp_wdsp(sc, cc >> 8) < 0) {
1247 DPRINTF(("sbdsp_dma_input: SB1 DMA start failed\n"));
1248 goto giveup;
1249 }
1250 break;
1251 case SB_DMARUNNING:
1252 /* Looping mode, not initialized */
1253 sc->sc_i.run = SB_PCMRUNNING;
1254 if (!sbdsp_dma_setup_input(sc))
1255 goto giveup;
1256 if ((sc->sc_model == SB_JAZZ && sc->sc_i.dmachan > 3) ||
1257 (sc->sc_model != SB_JAZZ && sc->sc_i.modep->precision == 16))
1258 cc >>= 1;
1259 --cc;
1260 /* Initialize looping PCM */
1261 if (ISSB16CLASS(sc)) {
1262 #ifdef AUDIO_DEBUG
1263 if (sbdspdebug > 2)
1264 printf("sbdsp16 input command cmd=0x%02x bmode=0x%02x cc=%d\n",
1265 sc->sc_i.modep->cmd, sc->sc_i.bmode, cc);
1266 #endif
1267 if (sbdsp_wdsp(sc, sc->sc_i.modep->cmd) < 0 ||
1268 sbdsp_wdsp(sc, sc->sc_i.bmode) < 0 ||
1269 sbdsp_wdsp(sc, cc) < 0 ||
1270 sbdsp_wdsp(sc, cc >> 8) < 0) {
1271 DPRINTF(("sbdsp_dma_input: SB16 DMA start failed\n"));
1272 DPRINTF(("sbdsp16 input command cmd=0x%02x bmode=0x%02x cc=%d\n",
1273 sc->sc_i.modep->cmd, sc->sc_i.bmode, cc));
1274 goto giveup;
1275 }
1276 } else {
1277 DPRINTF(("sbdsp_dma_input: set blocksize=%d\n", cc));
1278 if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
1279 sbdsp_wdsp(sc, cc) < 0 ||
1280 sbdsp_wdsp(sc, cc >> 8) < 0) {
1281 DPRINTF(("sbdsp_dma_input: SB2 DMA blocksize failed\n"));
1282 goto giveup;
1283 }
1284 if (sbdsp_wdsp(sc, sc->sc_i.modep->cmd) < 0) {
1285 DPRINTF(("sbdsp_dma_input: SB2 DMA start failed\n"));
1286 goto giveup;
1287 }
1288 }
1289 break;
1290 case SB_PCMRUNNING:
1291 /* Looping mode, nothing to do */
1292 break;
1293 }
1294 return 0;
1295
1296 giveup:
1297 sbdsp_reset(sc);
1298 return EIO;
1299 }
1300
1301 int
1302 sbdsp_dma_init_output(addr, buf, cc)
1303 void *addr;
1304 void *buf;
1305 int cc;
1306 {
1307 struct sbdsp_softc *sc = addr;
1308
1309 if (sc->sc_model == SB_1)
1310 return 0;
1311 sc->sc_o.run = SB_DMARUNNING;
1312 DPRINTF(("sbdsp: dma start loop output buf=%p cc=%d chan=%d\n",
1313 buf, cc, sc->sc_o.dmachan));
1314 isa_dmastart(sc->sc_isa, sc->sc_o.dmachan, buf,
1315 cc, NULL, DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
1316 return 0;
1317 }
1318
1319 static int
1320 sbdsp_dma_setup_output(sc)
1321 struct sbdsp_softc *sc;
1322 {
1323 int stereo = sc->sc_o.modep->channels == 2;
1324 int cmd;
1325
1326 if (ISSBPRO(sc)) {
1327 /* make sure we re-set stereo mixer bit when we start output. */
1328 sbdsp_mix_write(sc, SBP_STEREO,
1329 (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
1330 (stereo ? SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
1331 cmd = sc->sc_o.modep->cmdchan;
1332 if (cmd && sbdsp_wdsp(sc, cmd) < 0)
1333 return 0;
1334 }
1335
1336 if (ISSB16CLASS(sc)) {
1337 if (sbdsp16_set_rate(sc, SB_DSP16_OUTPUTRATE,
1338 sc->sc_o.rate)) {
1339 DPRINTF(("sbdsp_dma_setup_output: rate=%d set failed\n",
1340 sc->sc_o.rate));
1341 return 0;
1342 }
1343 } else {
1344 if (sbdsp_set_timeconst(sc, sc->sc_o.tc)) {
1345 DPRINTF(("sbdsp_dma_setup_output: tc=%d set failed\n",
1346 sc->sc_o.rate));
1347 return 0;
1348 }
1349 }
1350 return 1;
1351 }
1352
1353 int
1354 sbdsp_dma_output(addr, p, cc, intr, arg)
1355 void *addr;
1356 void *p;
1357 int cc;
1358 void (*intr) __P((void *));
1359 void *arg;
1360 {
1361 struct sbdsp_softc *sc = addr;
1362
1363 #ifdef AUDIO_DEBUG
1364 if (sbdspdebug > 1)
1365 printf("sbdsp_dma_output: sc=%p buf=%p cc=%d intr=%p(%p)\n", addr, p, cc, intr, arg);
1366 #endif
1367 #ifdef DIAGNOSTIC
1368 if (sc->sc_o.modep->channels == 2 && (cc & 1)) {
1369 DPRINTF(("stereo playback odd bytes (%d)\n", cc));
1370 return EIO;
1371 }
1372 #endif
1373
1374 if (sc->sc_o.modep->precision == 8) {
1375 #ifdef DIAGNOSTIC
1376 if (sc->sc_o.dmachan != sc->sc_drq8) {
1377 printf("sbdsp_dma_output: prec=%d bad chan %d\n",
1378 sc->sc_o.modep->precision, sc->sc_o.dmachan);
1379 return EIO;
1380 }
1381 #endif
1382 sc->sc_intr8 = intr;
1383 sc->sc_arg8 = arg;
1384 } else {
1385 #ifdef DIAGNOSTIC
1386 if (sc->sc_o.dmachan != sc->sc_drq16) {
1387 printf("sbdsp_dma_output: prec=%d bad chan %d\n",
1388 sc->sc_o.modep->precision, sc->sc_o.dmachan);
1389 return EIO;
1390 }
1391 #endif
1392 sc->sc_intr16 = intr;
1393 sc->sc_arg16 = arg;
1394 }
1395
1396 switch(sc->sc_o.run) {
1397 case SB_NOTRUNNING:
1398 /* Non-looping mode, not initialized */
1399 sc->sc_o.run = SB_RUNNING;
1400 if (!sbdsp_dma_setup_output(sc))
1401 goto giveup;
1402 /* fall into */
1403 case SB_RUNNING:
1404 /* Non-looping mode, initialized. Start DMA and PCM */
1405 #ifdef AUDIO_DEBUG
1406 if (sbdspdebug > 2)
1407 printf("sbdsp: start dma out addr=%p, cc=%d, chan=%d\n",
1408 p, cc, sc->sc_o.dmachan);
1409 #endif
1410 isa_dmastart(sc->sc_isa, sc->sc_o.dmachan, p,
1411 cc, NULL, DMAMODE_WRITE, BUS_DMA_NOWAIT);
1412 if ((sc->sc_model == SB_JAZZ && sc->sc_o.dmachan > 3) ||
1413 (sc->sc_model != SB_JAZZ && sc->sc_o.modep->precision == 16))
1414 cc >>= 1;
1415 --cc;
1416 if (sbdsp_wdsp(sc, sc->sc_o.modep->cmd) < 0 ||
1417 sbdsp_wdsp(sc, cc) < 0 ||
1418 sbdsp_wdsp(sc, cc >> 8) < 0) {
1419 DPRINTF(("sbdsp_dma_output: SB1 DMA start failed\n"));
1420 goto giveup;
1421 }
1422 break;
1423 case SB_DMARUNNING:
1424 /* Looping mode, not initialized */
1425 sc->sc_o.run = SB_PCMRUNNING;
1426 if (!sbdsp_dma_setup_output(sc))
1427 goto giveup;
1428 if ((sc->sc_model == SB_JAZZ && sc->sc_o.dmachan > 3) ||
1429 (sc->sc_model != SB_JAZZ && sc->sc_o.modep->precision == 16))
1430 cc >>= 1;
1431 --cc;
1432 /* Initialize looping PCM */
1433 if (ISSB16CLASS(sc)) {
1434 DPRINTF(("sbdsp_dma_output: SB16 cmd=0x%02x bmode=0x%02x cc=%d\n",
1435 sc->sc_o.modep->cmd,sc->sc_o.bmode, cc));
1436 if (sbdsp_wdsp(sc, sc->sc_o.modep->cmd) < 0 ||
1437 sbdsp_wdsp(sc, sc->sc_o.bmode) < 0 ||
1438 sbdsp_wdsp(sc, cc) < 0 ||
1439 sbdsp_wdsp(sc, cc >> 8) < 0) {
1440 DPRINTF(("sbdsp_dma_output: SB16 DMA start failed\n"));
1441 goto giveup;
1442 }
1443 } else {
1444 DPRINTF(("sbdsp_dma_output: set blocksize=%d\n", cc));
1445 if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
1446 sbdsp_wdsp(sc, cc) < 0 ||
1447 sbdsp_wdsp(sc, cc >> 8) < 0) {
1448 DPRINTF(("sbdsp_dma_output: SB2 DMA blocksize failed\n"));
1449 goto giveup;
1450 }
1451 if (sbdsp_wdsp(sc, sc->sc_o.modep->cmd) < 0) {
1452 DPRINTF(("sbdsp_dma_output: SB2 DMA start failed\n"));
1453 goto giveup;
1454 }
1455 }
1456 break;
1457 case SB_PCMRUNNING:
1458 /* Looping mode, nothing to do */
1459 break;
1460 }
1461 return 0;
1462
1463 giveup:
1464 sbdsp_reset(sc);
1465 return EIO;
1466 }
1467
1468 /*
1469 * Only the DSP unit on the sound blaster generates interrupts.
1470 * There are three cases of interrupt: reception of a midi byte
1471 * (when mode is enabled), completion of dma transmission, or
1472 * completion of a dma reception.
1473 *
1474 * If there is interrupt sharing or a spurious interrupt occurs
1475 * there is no way to distinguish this on an SB2. So if you have
1476 * an SB2 and experience problems, buy an SB16 (it's only $40).
1477 */
1478 int
1479 sbdsp_intr(arg)
1480 void *arg;
1481 {
1482 struct sbdsp_softc *sc = arg;
1483 int loop = sc->sc_model != SB_1;
1484 u_char irq;
1485
1486 #ifdef AUDIO_DEBUG
1487 if (sbdspdebug > 1)
1488 printf("sbdsp_intr: intr8=%p, intr16=%p\n",
1489 sc->sc_intr8, sc->sc_intr16);
1490 #endif
1491 if (ISSB16CLASS(sc)) {
1492 irq = sbdsp_mix_read(sc, SBP_IRQ_STATUS);
1493 if ((irq & (SBP_IRQ_DMA8 | SBP_IRQ_DMA16)) == 0) {
1494 DPRINTF(("sbdsp_intr: Spurious interrupt 0x%x\n", irq));
1495 return 0;
1496 }
1497 } else {
1498 if (!loop && !isa_dmafinished(sc->sc_isa, sc->sc_drq8))
1499 return 0;
1500 irq = SBP_IRQ_DMA8;
1501 }
1502 sc->sc_interrupts++;
1503 delay(10); /* XXX why? */
1504 #if 0
1505 if (sc->sc_mintr != 0) {
1506 x = sbdsp_rdsp(sc);
1507 (*sc->sc_mintr)(sc->sc_arg, x);
1508 } else
1509 #endif
1510 if (sc->sc_intr8 == 0 && sc->sc_intr16 == 0) {
1511 DPRINTF(("sbdsp_intr: Unexpected interrupt 0x%x\n", irq));
1512 /* XXX return 0;*/ /* Did not expect an interrupt */
1513 }
1514
1515 /* clear interrupt */
1516 if (irq & SBP_IRQ_DMA8) {
1517 bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_IRQACK8);
1518 if (!loop)
1519 isa_dmadone(sc->sc_isa, sc->sc_drq8);
1520 if (sc->sc_intr8)
1521 (*sc->sc_intr8)(sc->sc_arg8);
1522 }
1523 if (irq & SBP_IRQ_DMA16) {
1524 bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_IRQACK16);
1525 if (sc->sc_intr16)
1526 (*sc->sc_intr16)(sc->sc_arg16);
1527 }
1528 return 1;
1529 }
1530
1531 #if 0
1532 /*
1533 * Enter midi uart mode and arrange for read interrupts
1534 * to vector to `intr'. This puts the card in a mode
1535 * which allows only midi I/O; the card must be reset
1536 * to leave this mode. Unfortunately, the card does not
1537 * use transmit interrupts, so bytes must be output
1538 * using polling. To keep the polling overhead to a
1539 * minimum, output should be driven off a timer.
1540 * This is a little tricky since only 320us separate
1541 * consecutive midi bytes.
1542 */
1543 void
1544 sbdsp_set_midi_mode(sc, intr, arg)
1545 struct sbdsp_softc *sc;
1546 void (*intr)();
1547 void *arg;
1548 {
1549
1550 sbdsp_wdsp(sc, SB_MIDI_UART_INTR);
1551 sc->sc_mintr = intr;
1552 sc->sc_intr = 0;
1553 sc->sc_arg = arg;
1554 }
1555
1556 /*
1557 * Write a byte to the midi port, when in midi uart mode.
1558 */
1559 void
1560 sbdsp_midi_output(sc, v)
1561 struct sbdsp_softc *sc;
1562 int v;
1563 {
1564
1565 if (sbdsp_wdsp(sc, v) < 0)
1566 ++sberr.wmidi;
1567 }
1568 #endif
1569
1570 /* Mask a value 0-255, but round it first */
1571 #define MAXVAL 256
1572 static int
1573 sbdsp_adjust(val, mask)
1574 int val, mask;
1575 {
1576 val += (MAXVAL - mask) >> 1;
1577 if (val >= MAXVAL)
1578 val = MAXVAL-1;
1579 return val & mask;
1580 }
1581
1582 void
1583 sbdsp_set_mixer_gain(sc, port)
1584 struct sbdsp_softc *sc;
1585 int port;
1586 {
1587 int src, gain;
1588
1589 switch(sc->sc_mixer_model) {
1590 case SBM_NONE:
1591 return;
1592 case SBM_CT1335:
1593 gain = SB_1335_GAIN(sc->gain[port][SB_LEFT]);
1594 switch(port) {
1595 case SB_MASTER_VOL:
1596 src = SBP_1335_MASTER_VOL;
1597 break;
1598 case SB_MIDI_VOL:
1599 src = SBP_1335_MIDI_VOL;
1600 break;
1601 case SB_CD_VOL:
1602 src = SBP_1335_CD_VOL;
1603 break;
1604 case SB_VOICE_VOL:
1605 src = SBP_1335_VOICE_VOL;
1606 gain = SB_1335_MASTER_GAIN(sc->gain[port][SB_LEFT]);
1607 break;
1608 default:
1609 return;
1610 }
1611 sbdsp_mix_write(sc, src, gain);
1612 break;
1613 case SBM_CT1345:
1614 gain = SB_STEREO_GAIN(sc->gain[port][SB_LEFT],
1615 sc->gain[port][SB_RIGHT]);
1616 switch (port) {
1617 case SB_MIC_VOL:
1618 src = SBP_MIC_VOL;
1619 gain = SB_MIC_GAIN(sc->gain[port][SB_LEFT]);
1620 break;
1621 case SB_MASTER_VOL:
1622 src = SBP_MASTER_VOL;
1623 break;
1624 case SB_LINE_IN_VOL:
1625 src = SBP_LINE_VOL;
1626 break;
1627 case SB_VOICE_VOL:
1628 src = SBP_VOICE_VOL;
1629 break;
1630 case SB_MIDI_VOL:
1631 src = SBP_MIDI_VOL;
1632 break;
1633 case SB_CD_VOL:
1634 src = SBP_CD_VOL;
1635 break;
1636 default:
1637 return;
1638 }
1639 sbdsp_mix_write(sc, src, gain);
1640 break;
1641 case SBM_CT1XX5:
1642 case SBM_CT1745:
1643 switch (port) {
1644 case SB_MIC_VOL:
1645 src = SB16P_MIC_L;
1646 break;
1647 case SB_MASTER_VOL:
1648 src = SB16P_MASTER_L;
1649 break;
1650 case SB_LINE_IN_VOL:
1651 src = SB16P_LINE_L;
1652 break;
1653 case SB_VOICE_VOL:
1654 src = SB16P_VOICE_L;
1655 break;
1656 case SB_MIDI_VOL:
1657 src = SB16P_MIDI_L;
1658 break;
1659 case SB_CD_VOL:
1660 src = SB16P_CD_L;
1661 break;
1662 case SB_INPUT_GAIN:
1663 src = SB16P_INPUT_GAIN_L;
1664 break;
1665 case SB_OUTPUT_GAIN:
1666 src = SB16P_OUTPUT_GAIN_L;
1667 break;
1668 case SB_TREBLE:
1669 src = SB16P_TREBLE_L;
1670 break;
1671 case SB_BASS:
1672 src = SB16P_BASS_L;
1673 break;
1674 case SB_PCSPEAKER:
1675 sbdsp_mix_write(sc, SB16P_PCSPEAKER, sc->gain[port][SB_LEFT]);
1676 return;
1677 default:
1678 return;
1679 }
1680 sbdsp_mix_write(sc, src, sc->gain[port][SB_LEFT]);
1681 sbdsp_mix_write(sc, SB16P_L_TO_R(src), sc->gain[port][SB_RIGHT]);
1682 break;
1683 }
1684 }
1685
1686 int
1687 sbdsp_mixer_set_port(addr, cp)
1688 void *addr;
1689 mixer_ctrl_t *cp;
1690 {
1691 struct sbdsp_softc *sc = addr;
1692 int lgain, rgain;
1693 int mask, bits;
1694 int lmask, rmask, lbits, rbits;
1695 int mute, swap;
1696
1697 DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev,
1698 cp->un.value.num_channels));
1699
1700 if (sc->sc_mixer_model == SBM_NONE)
1701 return EINVAL;
1702
1703 switch (cp->dev) {
1704 case SB_TREBLE:
1705 case SB_BASS:
1706 if (sc->sc_mixer_model == SBM_CT1345 ||
1707 sc->sc_mixer_model == SBM_CT1XX5) {
1708 if (cp->type != AUDIO_MIXER_ENUM)
1709 return EINVAL;
1710 switch (cp->dev) {
1711 case SB_TREBLE:
1712 sbdsp_set_ifilter(addr, cp->un.ord ? SB_TREBLE : 0);
1713 return 0;
1714 case SB_BASS:
1715 sbdsp_set_ifilter(addr, cp->un.ord ? SB_BASS : 0);
1716 return 0;
1717 }
1718 }
1719 case SB_PCSPEAKER:
1720 case SB_INPUT_GAIN:
1721 case SB_OUTPUT_GAIN:
1722 if (!ISSBM1745(sc))
1723 return EINVAL;
1724 case SB_MIC_VOL:
1725 case SB_LINE_IN_VOL:
1726 if (sc->sc_mixer_model == SBM_CT1335)
1727 return EINVAL;
1728 case SB_VOICE_VOL:
1729 case SB_MIDI_VOL:
1730 case SB_CD_VOL:
1731 case SB_MASTER_VOL:
1732 if (cp->type != AUDIO_MIXER_VALUE)
1733 return EINVAL;
1734
1735 /*
1736 * All the mixer ports are stereo except for the microphone.
1737 * If we get a single-channel gain value passed in, then we
1738 * duplicate it to both left and right channels.
1739 */
1740
1741 switch (cp->dev) {
1742 case SB_MIC_VOL:
1743 if (cp->un.value.num_channels != 1)
1744 return EINVAL;
1745
1746 lgain = rgain = SB_ADJUST_MIC_GAIN(sc,
1747 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1748 break;
1749 case SB_PCSPEAKER:
1750 if (cp->un.value.num_channels != 1)
1751 return EINVAL;
1752 /* fall into */
1753 case SB_INPUT_GAIN:
1754 case SB_OUTPUT_GAIN:
1755 lgain = rgain = SB_ADJUST_2_GAIN(sc,
1756 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1757 break;
1758 default:
1759 switch (cp->un.value.num_channels) {
1760 case 1:
1761 lgain = rgain = SB_ADJUST_GAIN(sc,
1762 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1763 break;
1764 case 2:
1765 if (sc->sc_mixer_model == SBM_CT1335)
1766 return EINVAL;
1767 lgain = SB_ADJUST_GAIN(sc,
1768 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
1769 rgain = SB_ADJUST_GAIN(sc,
1770 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
1771 break;
1772 default:
1773 return EINVAL;
1774 }
1775 break;
1776 }
1777 sc->gain[cp->dev][SB_LEFT] = lgain;
1778 sc->gain[cp->dev][SB_RIGHT] = rgain;
1779
1780 sbdsp_set_mixer_gain(sc, cp->dev);
1781 break;
1782
1783 case SB_RECORD_SOURCE:
1784 if (ISSBM1745(sc)) {
1785 if (cp->type != AUDIO_MIXER_SET)
1786 return EINVAL;
1787 return sbdsp_set_in_ports(sc, cp->un.mask);
1788 } else {
1789 if (cp->type != AUDIO_MIXER_ENUM)
1790 return EINVAL;
1791 sc->in_port = cp->un.ord;
1792 return sbdsp_set_in_ports(sc, 1 << cp->un.ord);
1793 }
1794 break;
1795
1796 case SB_AGC:
1797 if (!ISSBM1745(sc) || cp->type != AUDIO_MIXER_ENUM)
1798 return EINVAL;
1799 sbdsp_mix_write(sc, SB16P_AGC, cp->un.ord & 1);
1800 break;
1801
1802 case SB_CD_OUT_MUTE:
1803 mask = SB16P_SW_CD;
1804 goto omute;
1805 case SB_MIC_OUT_MUTE:
1806 mask = SB16P_SW_MIC;
1807 goto omute;
1808 case SB_LINE_OUT_MUTE:
1809 mask = SB16P_SW_LINE;
1810 omute:
1811 if (cp->type != AUDIO_MIXER_ENUM)
1812 return EINVAL;
1813 bits = sbdsp_mix_read(sc, SB16P_OSWITCH);
1814 sc->gain[cp->dev][SB_LR] = cp->un.ord != 0;
1815 if (cp->un.ord)
1816 bits = bits & ~mask;
1817 else
1818 bits = bits | mask;
1819 sbdsp_mix_write(sc, SB16P_OSWITCH, bits);
1820 break;
1821
1822 case SB_MIC_IN_MUTE:
1823 case SB_MIC_SWAP:
1824 lmask = rmask = SB16P_SW_MIC;
1825 goto imute;
1826 case SB_CD_IN_MUTE:
1827 case SB_CD_SWAP:
1828 lmask = SB16P_SW_CD_L;
1829 rmask = SB16P_SW_CD_R;
1830 goto imute;
1831 case SB_LINE_IN_MUTE:
1832 case SB_LINE_SWAP:
1833 lmask = SB16P_SW_LINE_L;
1834 rmask = SB16P_SW_LINE_R;
1835 goto imute;
1836 case SB_MIDI_IN_MUTE:
1837 case SB_MIDI_SWAP:
1838 lmask = SB16P_SW_MIDI_L;
1839 rmask = SB16P_SW_MIDI_R;
1840 imute:
1841 if (cp->type != AUDIO_MIXER_ENUM)
1842 return EINVAL;
1843 mask = lmask | rmask;
1844 lbits = sbdsp_mix_read(sc, SB16P_ISWITCH_L) & ~mask;
1845 rbits = sbdsp_mix_read(sc, SB16P_ISWITCH_R) & ~mask;
1846 sc->gain[cp->dev][SB_LR] = cp->un.ord != 0;
1847 if (SB_IS_IN_MUTE(cp->dev)) {
1848 mute = cp->dev;
1849 swap = mute - SB_CD_IN_MUTE + SB_CD_SWAP;
1850 } else {
1851 swap = cp->dev;
1852 mute = swap + SB_CD_IN_MUTE - SB_CD_SWAP;
1853 }
1854 if (sc->gain[swap][SB_LR]) {
1855 mask = lmask;
1856 lmask = rmask;
1857 rmask = mask;
1858 }
1859 if (!sc->gain[mute][SB_LR]) {
1860 lbits = lbits | lmask;
1861 rbits = rbits | rmask;
1862 }
1863 sbdsp_mix_write(sc, SB16P_ISWITCH_L, lbits);
1864 sbdsp_mix_write(sc, SB16P_ISWITCH_L, rbits);
1865 break;
1866
1867 default:
1868 return EINVAL;
1869 }
1870
1871 return 0;
1872 }
1873
1874 int
1875 sbdsp_mixer_get_port(addr, cp)
1876 void *addr;
1877 mixer_ctrl_t *cp;
1878 {
1879 struct sbdsp_softc *sc = addr;
1880
1881 DPRINTF(("sbdsp_mixer_get_port: port=%d\n", cp->dev));
1882
1883 if (sc->sc_mixer_model == SBM_NONE)
1884 return EINVAL;
1885
1886 switch (cp->dev) {
1887 case SB_TREBLE:
1888 case SB_BASS:
1889 if (sc->sc_mixer_model == SBM_CT1345 ||
1890 sc->sc_mixer_model == SBM_CT1XX5) {
1891 switch (cp->dev) {
1892 case SB_TREBLE:
1893 cp->un.ord = sbdsp_get_ifilter(addr) == SB_TREBLE;
1894 return 0;
1895 case SB_BASS:
1896 cp->un.ord = sbdsp_get_ifilter(addr) == SB_BASS;
1897 return 0;
1898 }
1899 }
1900 case SB_PCSPEAKER:
1901 case SB_INPUT_GAIN:
1902 case SB_OUTPUT_GAIN:
1903 if (!ISSBM1745(sc))
1904 return EINVAL;
1905 case SB_MIC_VOL:
1906 case SB_LINE_IN_VOL:
1907 if (sc->sc_mixer_model == SBM_CT1335)
1908 return EINVAL;
1909 case SB_VOICE_VOL:
1910 case SB_MIDI_VOL:
1911 case SB_CD_VOL:
1912 case SB_MASTER_VOL:
1913 switch (cp->dev) {
1914 case SB_MIC_VOL:
1915 case SB_PCSPEAKER:
1916 if (cp->un.value.num_channels != 1)
1917 return EINVAL;
1918 /* fall into */
1919 default:
1920 switch (cp->un.value.num_channels) {
1921 case 1:
1922 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1923 sc->gain[cp->dev][SB_LEFT];
1924 break;
1925 case 2:
1926 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1927 sc->gain[cp->dev][SB_LEFT];
1928 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1929 sc->gain[cp->dev][SB_RIGHT];
1930 break;
1931 default:
1932 return EINVAL;
1933 }
1934 break;
1935 }
1936 break;
1937
1938 case SB_RECORD_SOURCE:
1939 if (ISSBM1745(sc))
1940 cp->un.mask = sc->in_mask;
1941 else
1942 cp->un.ord = sc->in_port;
1943 break;
1944
1945 case SB_AGC:
1946 if (!ISSBM1745(sc))
1947 return EINVAL;
1948 cp->un.ord = sbdsp_mix_read(sc, SB16P_AGC);
1949 break;
1950
1951 case SB_CD_IN_MUTE:
1952 case SB_MIC_IN_MUTE:
1953 case SB_LINE_IN_MUTE:
1954 case SB_MIDI_IN_MUTE:
1955 case SB_CD_SWAP:
1956 case SB_MIC_SWAP:
1957 case SB_LINE_SWAP:
1958 case SB_MIDI_SWAP:
1959 case SB_CD_OUT_MUTE:
1960 case SB_MIC_OUT_MUTE:
1961 case SB_LINE_OUT_MUTE:
1962 cp->un.ord = sc->gain[cp->dev][SB_LR];
1963 break;
1964
1965 default:
1966 return EINVAL;
1967 }
1968
1969 return 0;
1970 }
1971
1972 int
1973 sbdsp_mixer_query_devinfo(addr, dip)
1974 void *addr;
1975 mixer_devinfo_t *dip;
1976 {
1977 struct sbdsp_softc *sc = addr;
1978 int chan, class, is1745;
1979
1980 DPRINTF(("sbdsp_mixer_query_devinfo: model=%d index=%d\n",
1981 sc->sc_mixer_model, dip->index));
1982
1983 if (sc->sc_mixer_model == SBM_NONE)
1984 return ENXIO;
1985
1986 chan = sc->sc_mixer_model == SBM_CT1335 ? 1 : 2;
1987 is1745 = ISSBM1745(sc);
1988 class = is1745 ? SB_INPUT_CLASS : SB_OUTPUT_CLASS;
1989
1990 switch (dip->index) {
1991 case SB_MASTER_VOL:
1992 dip->type = AUDIO_MIXER_VALUE;
1993 dip->mixer_class = SB_OUTPUT_CLASS;
1994 dip->prev = dip->next = AUDIO_MIXER_LAST;
1995 strcpy(dip->label.name, AudioNmaster);
1996 dip->un.v.num_channels = chan;
1997 strcpy(dip->un.v.units.name, AudioNvolume);
1998 return 0;
1999 case SB_MIDI_VOL:
2000 dip->type = AUDIO_MIXER_VALUE;
2001 dip->mixer_class = class;
2002 dip->prev = AUDIO_MIXER_LAST;
2003 dip->next = is1745 ? SB_MIDI_IN_MUTE : AUDIO_MIXER_LAST;
2004 strcpy(dip->label.name, AudioNfmsynth);
2005 dip->un.v.num_channels = chan;
2006 strcpy(dip->un.v.units.name, AudioNvolume);
2007 return 0;
2008 case SB_CD_VOL:
2009 dip->type = AUDIO_MIXER_VALUE;
2010 dip->mixer_class = class;
2011 dip->prev = AUDIO_MIXER_LAST;
2012 dip->next = is1745 ? SB_CD_IN_MUTE : AUDIO_MIXER_LAST;
2013 strcpy(dip->label.name, AudioNcd);
2014 dip->un.v.num_channels = chan;
2015 strcpy(dip->un.v.units.name, AudioNvolume);
2016 return 0;
2017 case SB_VOICE_VOL:
2018 dip->type = AUDIO_MIXER_VALUE;
2019 dip->mixer_class = class;
2020 dip->prev = AUDIO_MIXER_LAST;
2021 dip->next = AUDIO_MIXER_LAST;
2022 strcpy(dip->label.name, AudioNdac);
2023 dip->un.v.num_channels = chan;
2024 strcpy(dip->un.v.units.name, AudioNvolume);
2025 return 0;
2026 case SB_OUTPUT_CLASS:
2027 dip->type = AUDIO_MIXER_CLASS;
2028 dip->mixer_class = SB_OUTPUT_CLASS;
2029 dip->next = dip->prev = AUDIO_MIXER_LAST;
2030 strcpy(dip->label.name, AudioCoutputs);
2031 return 0;
2032 }
2033
2034 if (sc->sc_mixer_model == SBM_CT1335)
2035 return ENXIO;
2036
2037 switch (dip->index) {
2038 case SB_MIC_VOL:
2039 dip->type = AUDIO_MIXER_VALUE;
2040 dip->mixer_class = class;
2041 dip->prev = AUDIO_MIXER_LAST;
2042 dip->next = is1745 ? SB_MIC_IN_MUTE : AUDIO_MIXER_LAST;
2043 strcpy(dip->label.name, AudioNmicrophone);
2044 dip->un.v.num_channels = 1;
2045 strcpy(dip->un.v.units.name, AudioNvolume);
2046 return 0;
2047
2048 case SB_LINE_IN_VOL:
2049 dip->type = AUDIO_MIXER_VALUE;
2050 dip->mixer_class = class;
2051 dip->prev = AUDIO_MIXER_LAST;
2052 dip->next = is1745 ? SB_LINE_IN_MUTE : AUDIO_MIXER_LAST;
2053 strcpy(dip->label.name, AudioNline);
2054 dip->un.v.num_channels = 2;
2055 strcpy(dip->un.v.units.name, AudioNvolume);
2056 return 0;
2057
2058 case SB_RECORD_SOURCE:
2059 dip->mixer_class = SB_RECORD_CLASS;
2060 dip->prev = dip->next = AUDIO_MIXER_LAST;
2061 strcpy(dip->label.name, AudioNsource);
2062 if (ISSBM1745(sc)) {
2063 dip->type = AUDIO_MIXER_SET;
2064 dip->un.s.num_mem = 4;
2065 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
2066 dip->un.s.member[0].mask = 1 << SB_MIC_VOL;
2067 strcpy(dip->un.s.member[1].label.name, AudioNcd);
2068 dip->un.s.member[1].mask = 1 << SB_CD_VOL;
2069 strcpy(dip->un.s.member[2].label.name, AudioNline);
2070 dip->un.s.member[2].mask = 1 << SB_LINE_IN_VOL;
2071 strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
2072 dip->un.s.member[3].mask = 1 << SB_MIDI_VOL;
2073 } else {
2074 dip->type = AUDIO_MIXER_ENUM;
2075 dip->un.e.num_mem = 3;
2076 strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
2077 dip->un.e.member[0].ord = SB_MIC_VOL;
2078 strcpy(dip->un.e.member[1].label.name, AudioNcd);
2079 dip->un.e.member[1].ord = SB_CD_VOL;
2080 strcpy(dip->un.e.member[2].label.name, AudioNline);
2081 dip->un.e.member[2].ord = SB_LINE_IN_VOL;
2082 }
2083 return 0;
2084
2085 case SB_BASS:
2086 dip->prev = dip->next = AUDIO_MIXER_LAST;
2087 strcpy(dip->label.name, AudioNbass);
2088 if (sc->sc_mixer_model == SBM_CT1745) {
2089 dip->type = AUDIO_MIXER_VALUE;
2090 dip->mixer_class = SB_EQUALIZATION_CLASS;
2091 dip->un.v.num_channels = 2;
2092 strcpy(dip->un.v.units.name, AudioNbass);
2093 } else {
2094 dip->type = AUDIO_MIXER_ENUM;
2095 dip->mixer_class = SB_INPUT_CLASS;
2096 dip->un.e.num_mem = 2;
2097 strcpy(dip->un.e.member[0].label.name, AudioNoff);
2098 dip->un.e.member[0].ord = 0;
2099 strcpy(dip->un.e.member[1].label.name, AudioNon);
2100 dip->un.e.member[1].ord = 1;
2101 }
2102 return 0;
2103
2104 case SB_TREBLE:
2105 dip->prev = dip->next = AUDIO_MIXER_LAST;
2106 strcpy(dip->label.name, AudioNtreble);
2107 if (sc->sc_mixer_model == SBM_CT1745) {
2108 dip->type = AUDIO_MIXER_VALUE;
2109 dip->mixer_class = SB_EQUALIZATION_CLASS;
2110 dip->un.v.num_channels = 2;
2111 strcpy(dip->un.v.units.name, AudioNtreble);
2112 } else {
2113 dip->type = AUDIO_MIXER_ENUM;
2114 dip->mixer_class = SB_INPUT_CLASS;
2115 dip->un.e.num_mem = 2;
2116 strcpy(dip->un.e.member[0].label.name, AudioNoff);
2117 dip->un.e.member[0].ord = 0;
2118 strcpy(dip->un.e.member[1].label.name, AudioNon);
2119 dip->un.e.member[1].ord = 1;
2120 }
2121 return 0;
2122
2123 case SB_RECORD_CLASS: /* record source class */
2124 dip->type = AUDIO_MIXER_CLASS;
2125 dip->mixer_class = SB_RECORD_CLASS;
2126 dip->next = dip->prev = AUDIO_MIXER_LAST;
2127 strcpy(dip->label.name, AudioCrecord);
2128 return 0;
2129
2130 case SB_INPUT_CLASS:
2131 dip->type = AUDIO_MIXER_CLASS;
2132 dip->mixer_class = SB_INPUT_CLASS;
2133 dip->next = dip->prev = AUDIO_MIXER_LAST;
2134 strcpy(dip->label.name, AudioCinputs);
2135 return 0;
2136
2137 }
2138
2139 if (sc->sc_mixer_model == SBM_CT1345)
2140 return ENXIO;
2141
2142 switch(dip->index) {
2143 case SB_PCSPEAKER:
2144 dip->type = AUDIO_MIXER_VALUE;
2145 dip->mixer_class = SB_INPUT_CLASS;
2146 dip->prev = dip->next = AUDIO_MIXER_LAST;
2147 strcpy(dip->label.name, "pc_speaker");
2148 dip->un.v.num_channels = 1;
2149 strcpy(dip->un.v.units.name, AudioNvolume);
2150 return 0;
2151
2152 case SB_INPUT_GAIN:
2153 dip->type = AUDIO_MIXER_VALUE;
2154 dip->mixer_class = SB_INPUT_CLASS;
2155 dip->prev = dip->next = AUDIO_MIXER_LAST;
2156 strcpy(dip->label.name, AudioNinput);
2157 dip->un.v.num_channels = 2;
2158 strcpy(dip->un.v.units.name, AudioNvolume);
2159 return 0;
2160
2161 case SB_OUTPUT_GAIN:
2162 dip->type = AUDIO_MIXER_VALUE;
2163 dip->mixer_class = SB_OUTPUT_CLASS;
2164 dip->prev = dip->next = AUDIO_MIXER_LAST;
2165 strcpy(dip->label.name, AudioNoutput);
2166 dip->un.v.num_channels = 2;
2167 strcpy(dip->un.v.units.name, AudioNvolume);
2168 return 0;
2169
2170 case SB_AGC:
2171 dip->type = AUDIO_MIXER_ENUM;
2172 dip->mixer_class = SB_INPUT_CLASS;
2173 dip->prev = dip->next = AUDIO_MIXER_LAST;
2174 strcpy(dip->label.name, "AGC");
2175 dip->un.e.num_mem = 2;
2176 strcpy(dip->un.e.member[0].label.name, AudioNoff);
2177 dip->un.e.member[0].ord = 0;
2178 strcpy(dip->un.e.member[1].label.name, AudioNon);
2179 dip->un.e.member[1].ord = 1;
2180 return 0;
2181
2182 case SB_EQUALIZATION_CLASS:
2183 dip->type = AUDIO_MIXER_CLASS;
2184 dip->mixer_class = SB_EQUALIZATION_CLASS;
2185 dip->next = dip->prev = AUDIO_MIXER_LAST;
2186 strcpy(dip->label.name, AudioCequalization);
2187 return 0;
2188
2189 case SB_CD_IN_MUTE:
2190 dip->prev = SB_CD_VOL;
2191 dip->next = SB_CD_SWAP;
2192 dip->mixer_class = SB_INPUT_CLASS;
2193 goto mute;
2194
2195 case SB_MIC_IN_MUTE:
2196 dip->prev = SB_MIC_VOL;
2197 dip->next = SB_MIC_SWAP;
2198 dip->mixer_class = SB_INPUT_CLASS;
2199 goto mute;
2200
2201 case SB_LINE_IN_MUTE:
2202 dip->prev = SB_LINE_IN_VOL;
2203 dip->next = SB_LINE_SWAP;
2204 dip->mixer_class = SB_INPUT_CLASS;
2205 goto mute;
2206
2207 case SB_MIDI_IN_MUTE:
2208 dip->prev = SB_MIDI_VOL;
2209 dip->next = SB_MIDI_SWAP;
2210 dip->mixer_class = SB_INPUT_CLASS;
2211 goto mute;
2212
2213 case SB_CD_SWAP:
2214 dip->prev = SB_CD_IN_MUTE;
2215 dip->next = SB_CD_OUT_MUTE;
2216 goto swap;
2217
2218 case SB_MIC_SWAP:
2219 dip->prev = SB_MIC_IN_MUTE;
2220 dip->next = SB_MIC_OUT_MUTE;
2221 goto swap;
2222
2223 case SB_LINE_SWAP:
2224 dip->prev = SB_LINE_IN_MUTE;
2225 dip->next = SB_LINE_OUT_MUTE;
2226 goto swap;
2227
2228 case SB_MIDI_SWAP:
2229 dip->prev = SB_MIDI_IN_MUTE;
2230 dip->next = AUDIO_MIXER_LAST;
2231 swap:
2232 dip->mixer_class = SB_INPUT_CLASS;
2233 strcpy(dip->label.name, AudioNswap);
2234 goto mute1;
2235
2236 case SB_CD_OUT_MUTE:
2237 dip->prev = SB_CD_SWAP;
2238 dip->next = AUDIO_MIXER_LAST;
2239 dip->mixer_class = SB_OUTPUT_CLASS;
2240 goto mute;
2241
2242 case SB_MIC_OUT_MUTE:
2243 dip->prev = SB_MIC_SWAP;
2244 dip->next = AUDIO_MIXER_LAST;
2245 dip->mixer_class = SB_OUTPUT_CLASS;
2246 goto mute;
2247
2248 case SB_LINE_OUT_MUTE:
2249 dip->prev = SB_LINE_SWAP;
2250 dip->next = AUDIO_MIXER_LAST;
2251 dip->mixer_class = SB_OUTPUT_CLASS;
2252 mute:
2253 strcpy(dip->label.name, AudioNmute);
2254 mute1:
2255 dip->type = AUDIO_MIXER_ENUM;
2256 dip->un.e.num_mem = 2;
2257 strcpy(dip->un.e.member[0].label.name, AudioNoff);
2258 dip->un.e.member[0].ord = 0;
2259 strcpy(dip->un.e.member[1].label.name, AudioNon);
2260 dip->un.e.member[1].ord = 1;
2261 return 0;
2262
2263 }
2264
2265 return ENXIO;
2266 }
2267
2268 void *
2269 sb_malloc(addr, size, pool, flags)
2270 void *addr;
2271 unsigned long size;
2272 int pool;
2273 int flags;
2274 {
2275 struct sbdsp_softc *sc = addr;
2276
2277 return isa_malloc(sc->sc_isa, 4, size, pool, flags);
2278 }
2279
2280 void
2281 sb_free(addr, ptr, pool)
2282 void *addr;
2283 void *ptr;
2284 int pool;
2285 {
2286 isa_free(ptr, pool);
2287 }
2288
2289 unsigned long
2290 sb_round(addr, size)
2291 void *addr;
2292 unsigned long size;
2293 {
2294 if (size > MAX_ISADMA)
2295 size = MAX_ISADMA;
2296 return size;
2297 }
2298
2299 int
2300 sb_mappage(addr, mem, off, prot)
2301 void *addr;
2302 void *mem;
2303 int off;
2304 int prot;
2305 {
2306 return isa_mappage(mem, off, prot);
2307 }
2308
2309 int
2310 sbdsp_get_props(addr)
2311 void *addr;
2312 {
2313 struct sbdsp_softc *sc = addr;
2314 return AUDIO_PROP_MMAP |
2315 (sc->sc_fullduplex ? AUDIO_PROP_FULLDUPLEX : 0);
2316 }
2317