sbdsp.c revision 1.2 1 /* $NetBSD: sbdsp.c,v 1.2 1995/03/08 18:27:38 brezak 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 * $Id: sbdsp.c,v 1.2 1995/03/08 18:27:38 brezak Exp $
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 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/ioctl.h>
48 #include <sys/syslog.h>
49 #include <sys/device.h>
50 #include <sys/proc.h>
51 #include <sys/buf.h>
52 #include <vm/vm.h>
53
54 #include <machine/cpu.h>
55 #include <machine/pio.h>
56
57 #include <sys/audioio.h>
58 #include <dev/audio_if.h>
59
60 #include <i386/isa/isavar.h>
61 #include <i386/isa/dmavar.h>
62 #include <i386/isa/icu.h>
63
64 #include <i386/isa/sbreg.h>
65 #include <i386/isa/sbdspvar.h>
66
67 #ifdef DEBUG
68 extern void Dprintf __P((const char *, ...));
69
70 #define DPRINTF(x) if (sbdspdebug) printf x
71 int sbdspdebug = 0;
72 #else
73 #define DPRINTF(x)
74 #endif
75
76 #ifndef NEWCONFIG
77 #define at_dma(flags, ptr, cc, chan) isa_dmastart(flags, ptr, cc, chan)
78 #endif
79
80 #ifndef SBDSP_NPOLL
81 #define SBDSP_NPOLL 3000
82 #endif
83
84 struct {
85 int wdsp;
86 int rdsp;
87 int wmidi;
88 } sberr;
89
90 extern struct audio_device sb_device;
91
92 #ifdef DEBUG
93 void
94 sb_printsc(struct sbdsp_softc *sc)
95 {
96 int i;
97
98 printf("open %d dmachan %d iobase %x locked %d\n", sc->sc_open, sc->sc_drq,
99 sc->sc_iobase, sc->sc_locked);
100 printf("hispeed %d irate %d orate %d encoding %x\n",
101 sc->sc_adacmode, sc->sc_irate, sc->sc_orate, sc->encoding);
102 printf("outport %d inport %d spkron %d nintr %d\n",
103 sc->out_port, sc->in_port, sc->spkr_state, sc->sc_interrupts);
104 printf("tc %x chans %x scintr %x arg %x\n", sc->sc_adactc, sc->sc_chans,
105 sc->sc_intr, sc->sc_arg);
106 printf("gain: ");
107 for (i = 0; i < SB_NDEVS; i++)
108 printf("%d ", sc->gain[i]);
109 printf("\n");
110 }
111 #endif
112
113 /*
114 * Probe / attach routines.
115 */
116
117 /*
118 * Probe for the soundblaster hardware.
119 */
120 int
121 sbdsp_probe(sc)
122 struct sbdsp_softc *sc;
123 {
124 register u_short iobase = sc->sc_iobase;
125
126 if (sbdsp_reset(sc) < 0) {
127 DPRINTF(("sbdsp: couldn't reset card\n"));
128 return 0;
129 }
130 sc->sc_model = sbversion(sc);
131
132 return 1;
133 }
134
135 /*
136 * Attach hardware to driver, attach hardware driver to audio
137 * pseudo-device driver .
138 */
139 void
140 sbdsp_attach(sc)
141 struct sbdsp_softc *sc;
142 {
143 register u_short iobase = sc->sc_iobase;
144
145 sc->sc_locked = 0;
146
147 #ifdef NEWCONFIG
148 /*
149 * We limit DMA transfers to a page, and use the generic DMA handling
150 * code in isa.c. This code can end up copying a buffer, but since
151 * the audio driver uses relative small buffers this isn't likely.
152 *
153 * This allocation scheme means that the maximum transfer is limited
154 * by the page size (rather than 64k). This is reasonable. For 4K
155 * pages, the transfer time at 48KHz is 4096 / 48000 = 85ms. This
156 * is plenty long enough to amortize any fixed time overhead.
157 */
158 at_setup_dmachan(sc->sc_drq, NBPG);
159 #endif
160
161 /* Set defaults */
162 if (ISSBPROCLASS(sc))
163 sc->sc_irate = sc->sc_orate = 45454;
164 else
165 sc->sc_irate = sc->sc_orate = 14925;
166 sc->sc_chans = 1;
167 sc->encoding = AUDIO_ENCODING_LINEAR;
168
169 (void) sbdsp_set_in_sr_real((caddr_t)sc, sc->sc_irate);
170 (void) sbdsp_set_out_sr_real((caddr_t)sc, sc->sc_orate);
171
172 (void) sbdsp_set_in_port((caddr_t)sc, SB_MIC_PORT);
173 (void) sbdsp_set_out_port((caddr_t)sc, SB_SPEAKER);
174
175 if (ISSBPROCLASS(sc)) {
176 int i;
177
178 /* set mixer to default levels, by sending a mixer
179 reset command. */
180 sbdsp_mix_write(sc, SBP_MIX_RESET, SBP_MIX_RESET);
181 /* then some adjustments :) */
182 sbdsp_mix_write(sc, SBP_CD_VOL,
183 sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
184 sbdsp_mix_write(sc, SBP_DAC_VOL,
185 sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
186 sbdsp_mix_write(sc, SBP_MASTER_VOL,
187 sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
188 sbdsp_mix_write(sc, SBP_LINE_VOL,
189 sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL));
190 for (i = 0; i < SB_NDEVS; i++)
191 sc->gain[i] = sbdsp_stereo_vol(SBP_MAXVOL, SBP_MAXVOL);
192 }
193 printf(": dsp v%d.%d\n",
194 SBVER_MAJOR(sc->sc_model), SBVER_MINOR(sc->sc_model));
195 sprintf(sb_device.version, "%d.%d",
196 SBVER_MAJOR(sc->sc_model), SBVER_MINOR(sc->sc_model));
197 }
198
199 /*
200 * Various routines to interface to higher level audio driver
201 */
202
203 void
204 sbdsp_mix_write(sc, mixerport, val)
205 struct sbdsp_softc *sc;
206 int mixerport;
207 int val;
208 {
209 int iobase = sc->sc_iobase;
210 outb(iobase + SBP_MIXER_ADDR, mixerport);
211 delay(10);
212 outb(iobase + SBP_MIXER_DATA, val);
213 delay(30);
214 }
215
216 int
217 sbdsp_mix_read(sc, mixerport)
218 struct sbdsp_softc *sc;
219 int mixerport;
220 {
221 int iobase = sc->sc_iobase;
222 outb(iobase + SBP_MIXER_ADDR, mixerport);
223 delay(10);
224 return inb(iobase + SBP_MIXER_DATA);
225 }
226
227 int
228 sbdsp_set_in_sr(addr, sr)
229 caddr_t addr;
230 u_long sr;
231 {
232 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
233
234 sc->sc_irate = sr;
235
236 return 0;
237 }
238
239 int
240 sbdsp_set_in_sr_real(addr, sr)
241 caddr_t addr;
242 u_long sr;
243 {
244 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
245 int rval;
246
247 if (rval = sbdsp_set_sr(sc, &sr, SB_INPUT_RATE))
248 return rval;
249 sc->sc_irate = sr;
250 sc->sc_dmain_inprogress = 0; /* do it again on next DMA out */
251 sc->sc_dmaout_inprogress = 0;
252 return(0);
253 }
254
255 u_long
256 sbdsp_get_in_sr(addr)
257 caddr_t addr;
258 {
259 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
260
261 return(sc->sc_irate);
262 }
263
264 int
265 sbdsp_set_out_sr(addr, sr)
266 caddr_t addr;
267 u_long sr;
268 {
269 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
270
271 sc->sc_orate = sr;
272 return(0);
273 }
274
275 int
276 sbdsp_set_out_sr_real(addr, sr)
277 caddr_t addr;
278 u_long sr;
279 {
280 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
281 int rval;
282
283 if (rval = sbdsp_set_sr(sc, &sr, SB_OUTPUT_RATE))
284 return rval;
285 sc->sc_orate = sr;
286 sc->sc_dmain_inprogress = 0; /* do it again on next DMA out */
287 return(0);
288 }
289
290 u_long
291 sbdsp_get_out_sr(addr)
292 caddr_t addr;
293 {
294 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
295
296 return(sc->sc_orate);
297 }
298
299 int
300 sbdsp_query_encoding(addr, fp)
301 caddr_t addr;
302 struct audio_encoding *fp;
303 {
304 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
305
306 switch (fp->index) {
307 case 0:
308 strcpy(fp->name, "MU-Law");
309 fp->format_id = AUDIO_ENCODING_ULAW;
310 break;
311 case 2:
312 strcpy(fp->name, "pcm16");
313 fp->format_id = AUDIO_ENCODING_PCM16;
314 break;
315 default:
316 return(EINVAL);
317 /*NOTREACHED*/
318 }
319 return (0);
320 }
321
322 int
323 sbdsp_set_encoding(addr, enc)
324 caddr_t addr;
325 u_int enc;
326 {
327 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
328
329 switch(enc){
330 case AUDIO_ENCODING_ULAW:
331 sc->encoding = AUDIO_ENCODING_ULAW;
332 break;
333 case AUDIO_ENCODING_LINEAR:
334 sc->encoding = AUDIO_ENCODING_LINEAR;
335 break;
336 default:
337 return (EINVAL);
338 }
339 return (0);
340 }
341
342 int
343 sbdsp_get_encoding(addr)
344 caddr_t addr;
345 {
346 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
347
348 return(sc->encoding);
349 }
350
351 int
352 sbdsp_set_precision(addr, prec)
353 caddr_t addr;
354 u_int prec;
355 {
356
357 if (prec != 8)
358 return(EINVAL);
359 return(0);
360 }
361
362 int
363 sbdsp_get_precision(addr)
364 caddr_t addr;
365 {
366 return(8);
367 }
368
369 int
370 sbdsp_set_channels(addr, chans)
371 caddr_t addr;
372 int chans;
373 {
374 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
375 int rval;
376
377 if (ISSBPROCLASS(sc)) {
378 if (chans != 1 && chans != 2)
379 return(EINVAL);
380
381 sc->sc_chans = chans;
382 if (rval = sbdsp_set_in_sr_real(addr, sc->sc_irate))
383 return rval;
384 sbdsp_mix_write(sc, SBP_STEREO,
385 (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
386 (chans == 2 ? SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
387 /* recording channels needs to be done right when we start
388 DMA recording. Just record number of channels for now
389 and set stereo when ready. */
390 }
391 else {
392 if (chans != 1)
393 return(EINVAL);
394 sc->sc_chans = 1;
395 }
396
397 return(0);
398 }
399
400 int
401 sbdsp_get_channels(addr)
402 caddr_t addr;
403 {
404 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
405
406 #if 0
407 /* recording stereo may frob the mixer output */
408 if (ISSBPROCLASS(sc)) {
409 if ((sbdsp_mix_read(sc, SBP_STEREO) & SBP_PLAYMODE_MASK) == SBP_PLAYMODE_STEREO) {
410 sc->sc_chans = 2;
411 }
412 else {
413 sc->sc_chans = 1;
414 }
415 }
416 else {
417 sc->sc_chans = 1;
418 }
419 #endif
420
421 return(sc->sc_chans);
422 }
423
424 int
425 sbdsp_set_out_port(addr, port)
426 caddr_t addr;
427 int port;
428 {
429 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
430
431 sc->out_port = port; /* Just record it */
432
433 return(0);
434 }
435
436 int
437 sbdsp_get_out_port(addr)
438 caddr_t addr;
439 {
440 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
441
442 return(sc->out_port);
443 }
444
445
446 int
447 sbdsp_set_in_port(addr, port)
448 caddr_t addr;
449 int port;
450 {
451 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
452 int mixport, sbport;
453
454 switch (port) {
455 case SB_MIC_PORT:
456 sbport = SBP_FROM_MIC;
457 mixport = SBP_MIC_VOL;
458 break;
459 }
460
461 if (ISSBPROCLASS(sc)) {
462 switch (port) {
463 case SB_LINE_IN_PORT:
464 sbport = SBP_FROM_LINE;
465 mixport = SBP_LINE_VOL;
466 break;
467 case SB_CD_PORT:
468 sbport = SBP_FROM_CD;
469 mixport = SBP_CD_VOL;
470 break;
471 case SB_DAC_PORT:
472 case SB_FM_PORT:
473 default:
474 return(EINVAL);
475 /*NOTREACHED*/
476 }
477 }
478 else {
479 return(EINVAL);
480 /*NOTREACHED*/
481 }
482
483 sc->in_port = port; /* Just record it */
484
485 if (ISSBPROCLASS(sc)) {
486 /* record from that port */
487 sbdsp_mix_write(sc, SBP_RECORD_SOURCE,
488 SBP_RECORD_FROM(sbport, SBP_FILTER_OFF,
489 SBP_FILTER_HIGH));
490 /* fetch gain from that port */
491 sc->gain[port] = sbdsp_mix_read(sc, mixport);
492 }
493
494 return(0);
495 }
496
497 int
498 sbdsp_get_in_port(addr)
499 caddr_t addr;
500 {
501 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
502
503 return(sc->in_port);
504 }
505
506
507 int
508 sbdsp_speaker_ctl(addr, newstate)
509 caddr_t addr;
510 int newstate;
511 {
512 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
513
514 if ((newstate == SPKR_ON) &&
515 (sc->spkr_state == SPKR_OFF)) {
516 sbdsp_spkron(sc);
517 sc->spkr_state = SPKR_ON;
518 }
519 if ((newstate == SPKR_OFF) &&
520 (sc->spkr_state == SPKR_ON)) {
521 sbdsp_spkroff(sc);
522 sc->spkr_state = SPKR_OFF;
523 }
524 return(0);
525 }
526
527 int
528 sbdsp_round_blocksize(addr, blk)
529 caddr_t addr;
530 int blk;
531 {
532 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
533
534 sc->sc_last_hsr_size = sc->sc_last_hsw_size = 0;
535
536 /* Higher speeds need bigger blocks to avoid popping and silence gaps. */
537 if ((sc->sc_orate > 8000 || sc->sc_irate > 8000) &&
538 (blk > NBPG/2 || blk < NBPG/4))
539 blk = NBPG/2;
540 /* don't try to DMA too much at once, though. */
541 if (blk > NBPG) blk = NBPG;
542 if (sc->sc_chans == 2)
543 return (blk & ~1); /* must be even to preserve stereo separation */
544 else
545 return(blk); /* Anything goes :-) */
546 }
547
548 int
549 sbdsp_commit_settings(addr)
550 caddr_t addr;
551 {
552 /* due to potentially unfortunate ordering in the above layers,
553 re-do a few sets which may be important--input gains
554 (adjust the proper channels), number of input channels (hit the
555 record rate and set mode) */
556
557 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
558
559 if (ISSBPROCLASS(sc)) {
560 #ifdef not_anymore
561 sbdsp_set_in_gain_real(addr, sc->in_gain, sc->in_balance);
562 sbdsp_set_out_gain_real(addr, sc->out_gain, sc->out_balance);
563 #endif
564 sbdsp_set_out_sr_real(addr, sc->sc_orate);
565 sbdsp_set_in_sr_real(addr, sc->sc_irate);
566 }
567 sc->sc_last_hsw_size = sc->sc_last_hsr_size = 0;
568 return(0);
569 }
570
571
572 int
573 sbdsp_open(sc, dev, flags)
574 register struct sbdsp_softc *sc;
575 dev_t dev;
576 int flags;
577 {
578 DPRINTF(("sbdsp_open: sc=0x%x\n", sc));
579
580 if (sc->sc_open != 0 || sbdsp_reset(sc) != 0)
581 return ENXIO;
582
583 sc->sc_open = 1;
584 sc->sc_mintr = 0;
585 sc->sc_intr = 0;
586 sc->sc_arg = 0;
587 sc->sc_locked = 0;
588 if (ISSBPROCLASS(sc) &&
589 sbdsp_wdsp(sc->sc_iobase, SB_DSP_RECORD_MONO) < 0) {
590 DPRINTF(("sbdsp_open: can't set mono mode\n"));
591 /* we'll readjust when it's time for DMA. */
592 }
593 sc->sc_dmain_inprogress = 0;
594 sc->sc_dmaout_inprogress = 0;
595
596 /*
597 * Leave most things as they were; users must change things if
598 * the previous process didn't leave it they way they wanted.
599 * Looked at another way, it's easy to set up a configuration
600 * in one program and leave it for another to inherit.
601 */
602 DPRINTF(("sbdsp_open: opened\n"));
603
604 return 0;
605 }
606
607 void
608 sbdsp_close(addr)
609 caddr_t addr;
610 {
611 struct sbdsp_softc *sc = (struct sbdsp_softc *) addr;
612
613 DPRINTF(("sbdsp_close: sc=0x%x\n", sc));
614
615 sc->sc_open = 0;
616 sbdsp_spkroff(sc);
617 sc->spkr_state = SPKR_OFF;
618 sc->sc_intr = 0;
619 sc->sc_mintr = 0;
620 /* XXX this will turn off any dma */
621 sbdsp_reset(sc);
622
623 DPRINTF(("sbdsp_close: closed\n"));
624 }
625
626 /*
627 * Lower-level routines
628 */
629
630 /*
631 * Reset the card.
632 * Return non-zero if the card isn't detected.
633 */
634 int
635 sbdsp_reset(sc)
636 register struct sbdsp_softc *sc;
637 {
638 register u_short iobase = sc->sc_iobase;
639
640 /*
641 * erase any memory of last transfer size.
642 */
643 sc->sc_last_hsr_size = sc->sc_last_hsw_size = 0;
644 /*
645 * See SBK, section 11.3.
646 * We pulse a reset signal into the card.
647 * Gee, what a brilliant hardware design.
648 */
649 outb(iobase + SBP_DSP_RESET, 1);
650 delay(3);
651 outb(iobase + SBP_DSP_RESET, 0);
652 if (sbdsp_rdsp(iobase) != SB_MAGIC)
653 return -1;
654 return 0;
655 }
656
657 /*
658 * Write a byte to the dsp.
659 * XXX We are at the mercy of the card as we use a
660 * polling loop and wait until it can take the byte.
661 */
662 int
663 sbdsp_wdsp(u_short iobase, int v)
664 {
665 register int i;
666
667 for (i = SBDSP_NPOLL; --i >= 0; ) {
668 if ((inb(iobase + SBP_DSP_WSTAT) & SB_DSP_BUSY) != 0) {
669 delay(10); continue;
670 }
671 outb(iobase + SBP_DSP_WRITE, v);
672 return 0;
673 }
674 ++sberr.wdsp;
675 return -1;
676 }
677
678 /*
679 * Read a byte from the DSP, using polling.
680 */
681 int
682 sbdsp_rdsp(u_short iobase)
683 {
684 register int i;
685
686 for (i = SBDSP_NPOLL; --i >= 0; ) {
687 if ((inb(iobase + SBP_DSP_RSTAT) & SB_DSP_READY) == 0)
688 continue;
689 return inb(iobase + SBP_DSP_READ);
690 }
691 ++sberr.rdsp;
692 return -1;
693 }
694
695 /*
696 * Doing certain things (like toggling the speaker) make
697 * the SB hardware go away for a while, so pause a little.
698 */
699 void
700 sbdsp_to(arg)
701 void *arg;
702 {
703 wakeup(arg);
704 }
705
706 void
707 sbdsp_pause(sc)
708 struct sbdsp_softc *sc;
709 {
710 extern int hz;
711
712 timeout(sbdsp_to, sbdsp_to, hz/8);
713 (void)tsleep((caddr_t)sbdsp_to, PWAIT, "sbpause", 0);
714 }
715
716 /*
717 * Turn on the speaker. The SBK documention says this operation
718 * can take up to 1/10 of a second. Higher level layers should
719 * probably let the task sleep for this amount of time after
720 * calling here. Otherwise, things might not work (because
721 * sbdsp_wdsp() and sbdsp_rdsp() will probably timeout.)
722 *
723 * These engineers had their heads up their ass when
724 * they designed this card.
725 */
726 void
727 sbdsp_spkron(sc)
728 struct sbdsp_softc *sc;
729 {
730 (void)sbdsp_wdsp(sc->sc_iobase, SB_DSP_SPKR_ON);
731 sbdsp_pause(sc);
732 }
733
734 /*
735 * Turn off the speaker; see comment above.
736 */
737 void
738 sbdsp_spkroff(sc)
739 struct sbdsp_softc *sc;
740 {
741 (void)sbdsp_wdsp(sc->sc_iobase, SB_DSP_SPKR_OFF);
742 sbdsp_pause(sc);
743 }
744
745 /*
746 * Read the version number out of the card. Return major code
747 * in high byte, and minor code in low byte.
748 */
749 short
750 sbversion(sc)
751 struct sbdsp_softc *sc;
752 {
753 register u_short iobase = sc->sc_iobase;
754 short v;
755
756 if (sbdsp_wdsp(iobase, SB_DSP_VERSION) < 0)
757 return 0;
758 v = sbdsp_rdsp(iobase) << 8;
759 v |= sbdsp_rdsp(iobase);
760 return ((v >= 0) ? v : 0);
761 }
762
763 /*
764 * Halt a DMA in progress. A low-speed transfer can be
765 * resumed with sbdsp_contdma().
766 */
767 int
768 sbdsp_haltdma(addr)
769 caddr_t addr;
770 {
771 register struct sbdsp_softc *sc = (struct sbdsp_softc*) addr;
772
773 DPRINTF(("sbdsp_haltdma: sc=0x%x\n", sc));
774
775 if (sc->sc_locked)
776 sbdsp_reset(sc);
777 else
778 (void)sbdsp_wdsp(sc->sc_iobase, SB_DSP_HALT);
779
780 isa_dmaabort(sc->sc_drq);
781 sc->dmaaddr = 0;
782 sc->dmacnt = 0;
783 sc->sc_locked = 0;
784 sc->dmaflags = 0;
785 sc->sc_dmain_inprogress = sc->sc_dmaout_inprogress = 0;
786 return(0);
787 }
788
789 int
790 sbdsp_contdma(addr)
791 caddr_t addr;
792 {
793 register struct sbdsp_softc *sc = (struct sbdsp_softc*) addr;
794
795 DPRINTF(("sbdsp_contdma: sc=0x%x\n", sc));
796
797 /* XXX how do we reinitialize the DMA controller state? do we care? */
798 (void)sbdsp_wdsp(sc->sc_iobase, SB_DSP_CONT);
799 return(0);
800 }
801
802 /*
803 * Time constant routines follow. See SBK, section 12.
804 * Although they don't come out and say it (in the docs),
805 * the card clearly uses a 1MHz countdown timer, as the
806 * low-speed formula (p. 12-4) is:
807 * tc = 256 - 10^6 / sr
808 * In high-speed mode, the constant is the upper byte of a 16-bit counter,
809 * and a 256MHz clock is used:
810 * tc = 65536 - 256 * 10^ 6 / sr
811 * Since we can only use the upper byte of the HS TC, the two formulae
812 * are equivalent. (Why didn't they say so?) E.g.,
813 * (65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
814 *
815 * The crossover point (from low- to high-speed modes) is different
816 * for the SBPRO and SB20. The table on p. 12-5 gives the following data:
817 *
818 * SBPRO SB20
819 * ----- --------
820 * input ls min 4 KHz 4 KHz
821 * input ls max 23 KHz 13 KHz
822 * input hs max 44.1 KHz 15 KHz
823 * output ls min 4 KHz 4 KHz
824 * output ls max 23 KHz 23 KHz
825 * output hs max 44.1 KHz 44.1 KHz
826 */
827 #define SB_LS_MIN 0x06 /* 4000 Hz */
828 #define SBPRO_ADC_LS_MAX 0xd4 /* 22727 Hz */
829 #define SBPRO_ADC_HS_MAX 0xea /* 45454 Hz */
830 #define SBCLA_ADC_LS_MAX 0xb3 /* 12987 Hz */
831 #define SBCLA_ADC_HS_MAX 0xbd /* 14925 Hz */
832 #define SB_DAC_LS_MAX 0xd4 /* 22727 Hz */
833 #define SB_DAC_HS_MAX 0xea /* 45454 Hz */
834
835 /*
836 * Convert a linear sampling rate into the DAC time constant.
837 * Set *mode to indicate the high/low-speed DMA operation.
838 * Because of limitations of the card, not all rates are possible.
839 * We return the time constant of the closest possible rate.
840 * The sampling rate limits are different for the DAC and ADC,
841 * so isdac indicates output, and !isdac indicates input.
842 */
843 int
844 sbdsp_srtotc(sc, sr, mode, isdac)
845 register struct sbdsp_softc *sc;
846 int sr;
847 int *mode;
848 int isdac;
849 {
850 int adc_ls_max, adc_hs_max;
851 register int tc;
852
853 if (sr == 0) {
854 *mode = SB_ADAC_LS;
855 return SB_LS_MIN;
856 }
857 tc = 256 - 1000000 / sr;
858
859 /* XXX use better rounding--compare distance to nearest tc on both
860 sides of requested speed */
861 if (ISSBPROCLASS(sc)) {
862 adc_ls_max = SBPRO_ADC_LS_MAX;
863 adc_hs_max = SBPRO_ADC_HS_MAX;
864 }
865 else {
866 adc_ls_max = SBCLA_ADC_LS_MAX;
867 adc_hs_max = SBCLA_ADC_HS_MAX;
868 }
869
870 if (tc < SB_LS_MIN) {
871 tc = SB_LS_MIN;
872 *mode = SB_ADAC_LS;
873 } else if (isdac) {
874 if (tc <= SB_DAC_LS_MAX)
875 *mode = SB_ADAC_LS;
876 else {
877 *mode = SB_ADAC_HS;
878 if (tc > SB_DAC_HS_MAX)
879 tc = SB_DAC_HS_MAX;
880 }
881 } else {
882 if (tc <= adc_ls_max)
883 *mode = SB_ADAC_LS;
884 else {
885 *mode = SB_ADAC_HS;
886 if (tc > adc_hs_max)
887 tc = adc_hs_max;
888 }
889 }
890 return tc;
891 }
892
893 /*
894 * Convert a DAC time constant to a sampling rate.
895 * See SBK, section 12.
896 */
897 int
898 sbdsp_tctosr(sc, tc)
899 register struct sbdsp_softc *sc;
900 int tc;
901 {
902 int adc;
903
904 if (ISSBPROCLASS(sc))
905 adc = SBPRO_ADC_HS_MAX;
906 else
907 adc = SBCLA_ADC_HS_MAX;
908
909 if (tc > adc)
910 tc = adc;
911
912 return (1000000 / (256 - tc));
913 }
914
915 int
916 sbdsp_set_sr(sc, srp, isdac)
917 register struct sbdsp_softc *sc;
918 u_long *srp;
919 int isdac;
920 {
921 register int tc;
922 int mode;
923 int sr = *srp;
924 register u_short iobase;
925
926 /*
927 * A SBPro in stereo mode uses time constants at double the
928 * actual rate.
929 */
930 if (ISSBPRO(sc) && sc->sc_chans == 2) {
931 if (sr > 22727)
932 sr = 22727; /* Can't bounce it...order of
933 operations may yield bogus
934 sr here. */
935 sr *= 2;
936 }
937 else if (!ISSBPROCLASS(sc) && sc->sc_chans != 1)
938 return EINVAL;
939
940 tc = sbdsp_srtotc(sc, sr, &mode, isdac);
941 DPRINTF(("sbdsp_set_sr: sc=0x%x sr=%d mode=0x%x\n", sc, sr, mode));
942
943 iobase = sc->sc_iobase;
944 if (sbdsp_wdsp(iobase, SB_DSP_TIMECONST) < 0 ||
945 sbdsp_wdsp(iobase, tc) < 0)
946 return EIO;
947
948 sr = sbdsp_tctosr(sc, tc);
949 if (ISSBPRO(sc) && sc->sc_chans == 2)
950 *srp = sr / 2;
951 else
952 *srp = sr;
953
954 sc->sc_adacmode = mode;
955 sc->sc_adactc = tc;
956 return 0;
957 }
958
959 int
960 sbdsp_dma_input(addr, p, cc, intr, arg)
961 caddr_t addr;
962 void *p;
963 int cc;
964 void (*intr)();
965 void *arg;
966 {
967 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
968 register u_short iobase;
969 u_int phys;
970
971 #ifdef DEBUG
972 if (sbdspdebug > 1)
973 Dprintf("sbdsp_dma_input: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
974 #endif
975 if (sc->sc_chans == 2 && (cc & 1)) {
976 DPRINTF(("sbdsp_dma_input: stereo input, odd bytecnt\n"));
977 return EIO;
978 }
979 iobase = sc->sc_iobase;
980 if (ISSBPROCLASS(sc) && !sc->sc_dmain_inprogress) {
981 if (sc->sc_chans == 2) {
982 if (sbdsp_wdsp(iobase, SB_DSP_RECORD_STEREO) < 0)
983 goto badmode;
984 sbdsp_mix_write(sc, SBP_STEREO,
985 sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK);
986 sbdsp_mix_write(sc, SBP_INFILTER,
987 sbdsp_mix_read(sc, SBP_INFILTER) | SBP_FILTER_OFF);
988 }
989 else {
990 if (sbdsp_wdsp(iobase, SB_DSP_RECORD_MONO) < 0)
991 goto badmode;
992 sbdsp_mix_write(sc, SBP_STEREO,
993 sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK);
994 sbdsp_mix_write(sc, SBP_INFILTER,
995 sc->sc_irate <= 8000 ?
996 sbdsp_mix_read(sc, SBP_INFILTER) & ~SBP_FILTER_MASK :
997 sbdsp_mix_read(sc, SBP_INFILTER) | SBP_FILTER_OFF);
998 }
999 sc->sc_dmain_inprogress = 1;
1000 sc->sc_last_hsr_size = 0; /* restarting */
1001 }
1002 sc->sc_dmaout_inprogress = 0;
1003
1004 at_dma(B_READ, p, cc, sc->sc_drq);
1005 sc->sc_intr = intr;
1006 sc->sc_arg = arg;
1007 sc->dmaflags = B_READ;
1008 sc->dmaaddr = p;
1009 sc->dmacnt = --cc; /* DMA controller is strange...? */
1010 if (sc->sc_adacmode == SB_ADAC_LS) {
1011 if (sbdsp_wdsp(iobase, SB_DSP_RDMA) < 0 ||
1012 sbdsp_wdsp(iobase, cc) < 0 ||
1013 sbdsp_wdsp(iobase, cc >> 8) < 0) {
1014 goto giveup;
1015 }
1016 }
1017 else {
1018 if (cc != sc->sc_last_hsr_size) {
1019 if (sbdsp_wdsp(iobase, SB_DSP_BLOCKSIZE) < 0 ||
1020 sbdsp_wdsp(iobase, cc) < 0 ||
1021 sbdsp_wdsp(iobase, cc >> 8) < 0)
1022 goto giveup;
1023 }
1024 if (sbdsp_wdsp(iobase, SB_DSP_HS_INPUT) < 0)
1025 goto giveup;
1026 sc->sc_last_hsr_size = cc;
1027 sc->sc_locked = 1;
1028 }
1029 return 0;
1030
1031 giveup:
1032 isa_dmaabort(sc->sc_drq);
1033 sbdsp_reset(sc);
1034 sc->sc_intr = 0;
1035 sc->sc_arg = 0;
1036 return EIO;
1037 badmode:
1038 DPRINTF(("sbdsp_dma_input: can't set %s mode\n",
1039 sc->sc_chans == 2 ? "stereo" : "mono"));
1040 return EIO;
1041 }
1042
1043 int
1044 sbdsp_dma_output(addr, p, cc, intr, arg)
1045 caddr_t addr;
1046 void *p;
1047 int cc;
1048 void (*intr)();
1049 void *arg;
1050 {
1051 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
1052 register u_short iobase;
1053
1054 #ifdef DEBUG
1055 if (sbdspdebug > 1)
1056 Dprintf("sbdsp_dma_output: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
1057 #endif
1058 if (sc->sc_chans == 2 && cc & 1) {
1059 DPRINTF(("stereo playback odd bytes (%d)\n", cc));
1060 return EIO;
1061 }
1062
1063 if (ISSBPROCLASS(sc) && !sc->sc_dmaout_inprogress) {
1064 /* make sure we re-set stereo mixer bit when we start
1065 output. */
1066 sbdsp_mix_write(sc, SBP_STEREO,
1067 (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
1068 (sc->sc_chans == 2 ?
1069 SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
1070 sc->sc_dmaout_inprogress = 1;
1071 sc->sc_last_hsw_size = 0; /* restarting */
1072 }
1073 sc->sc_dmain_inprogress = 0;
1074 at_dma(B_WRITE, p, cc, sc->sc_drq);
1075 sc->sc_intr = intr;
1076 sc->sc_arg = arg;
1077 sc->dmaflags = B_WRITE;
1078 sc->dmaaddr = p;
1079 sc->dmacnt = --cc; /* a vagary of how DMA works, apparently. */
1080
1081 iobase = sc->sc_iobase;
1082 if (sc->sc_adacmode == SB_ADAC_LS) {
1083 if (sbdsp_wdsp(iobase, SB_DSP_WDMA) < 0 ||
1084 sbdsp_wdsp(iobase, cc) < 0 ||
1085 sbdsp_wdsp(iobase, cc >> 8) < 0) {
1086 DPRINTF(("sbdsp_dma_output: LS DMA start failed\n"));
1087 goto giveup;
1088 }
1089 }
1090 else {
1091 if (cc != sc->sc_last_hsw_size) {
1092 if (sbdsp_wdsp(iobase, SB_DSP_BLOCKSIZE) < 0) {
1093 /* sometimes fails initial startup?? */
1094 delay(100);
1095 if (sbdsp_wdsp(iobase, SB_DSP_BLOCKSIZE) < 0) {
1096 DPRINTF(("sbdsp_dma_output: BLOCKSIZE failed\n"));
1097 goto giveup;
1098 }
1099 }
1100 if (sbdsp_wdsp(iobase, cc) < 0 ||
1101 sbdsp_wdsp(iobase, cc >> 8) < 0) {
1102 DPRINTF(("sbdsp_dma_output: HS DMA start failed\n"));
1103 goto giveup;
1104 }
1105 sc->sc_last_hsw_size = cc;
1106 }
1107 if (sbdsp_wdsp(iobase, SB_DSP_HS_OUTPUT) < 0) {
1108 delay(100);
1109 if (sbdsp_wdsp(iobase, SB_DSP_HS_OUTPUT) < 0) {
1110 DPRINTF(("sbdsp_dma_output: HS DMA restart failed\n"));
1111 goto giveup;
1112 }
1113 }
1114 sc->sc_locked = 1;
1115 }
1116
1117 return 0;
1118
1119 giveup:
1120 isa_dmaabort(sc->sc_drq);
1121 sbdsp_reset(sc);
1122 sc->sc_intr = 0;
1123 sc->sc_arg = 0;
1124 return EIO;
1125 }
1126
1127 /*
1128 * Only the DSP unit on the sound blaster generates interrupts.
1129 * There are three cases of interrupt: reception of a midi byte
1130 * (when mode is enabled), completion of dma transmission, or
1131 * completion of a dma reception. The three modes are mutually
1132 * exclusive so we know a priori which event has occurred.
1133 */
1134 int
1135 sbdsp_intr(sc)
1136 register struct sbdsp_softc *sc;
1137 {
1138
1139 #ifdef DEBUG
1140 if (sbdspdebug > 1)
1141 Dprintf("sbdsp_intr: intr=0x%x\n", sc->sc_intr);
1142 #endif
1143 sc->sc_interrupts++;
1144 sc->sc_locked = 0;
1145 /* clear interrupt */
1146 inb(sc->sc_iobase + SBP_DSP_RSTAT);
1147 #if 0
1148 if (sc->sc_mintr != 0) {
1149 int c = sbdsp_rdsp(sc->sc_iobase);
1150 (*sc->sc_mintr)(sc->sc_arg, c);
1151 } else
1152 #endif
1153 if (sc->sc_intr != 0) {
1154 /*
1155 * The SBPro used to develop and test this driver often
1156 * generated dma underruns--it interrupted to signal
1157 * completion of the DMA input recording block, but the
1158 * ISA DMA controller didn't think the channel was
1159 * finished. Maybe this is just a bus speed issue, I dunno,
1160 * but it seems strange and leads to channel-flipping with stereo
1161 * recording. Sigh.
1162 */
1163 isa_dmadone(sc->dmaflags, sc->dmaaddr, sc->dmacnt,
1164 sc->sc_drq);
1165 sc->dmaflags = 0;
1166 sc->dmaaddr = 0;
1167 sc->dmacnt = 0;
1168 (*sc->sc_intr)(sc->sc_arg);
1169 }
1170 else
1171 return 0;
1172 return 1;
1173 }
1174
1175 #if 0
1176 /*
1177 * Enter midi uart mode and arrange for read interrupts
1178 * to vector to `intr'. This puts the card in a mode
1179 * which allows only midi I/O; the card must be reset
1180 * to leave this mode. Unfortunately, the card does not
1181 * use transmit interrupts, so bytes must be output
1182 * using polling. To keep the polling overhead to a
1183 * minimum, output should be driven off a timer.
1184 * This is a little tricky since only 320us separate
1185 * consecutive midi bytes.
1186 */
1187 void
1188 sbdsp_set_midi_mode(sc, intr, arg)
1189 struct sbdsp_softc *sc;
1190 void (*intr)();
1191 void *arg;
1192 {
1193
1194 sbdsp_wdsp(sc->sc_iobase, SB_MIDI_UART_INTR);
1195 sc->sc_mintr = intr;
1196 sc->sc_intr = 0;
1197 sc->sc_arg = arg;
1198 }
1199
1200 /*
1201 * Write a byte to the midi port, when in midi uart mode.
1202 */
1203 void
1204 sbdsp_midi_output(sc, v)
1205 struct sbdsp_softc *sc;
1206 int v;
1207 {
1208
1209 if (sbdsp_wdsp(sc->sc_iobase, v) < 0)
1210 ++sberr.wmidi;
1211 }
1212 #endif
1213
1214 u_int
1215 sbdsp_get_silence(enc)
1216 int enc;
1217 {
1218 #define ULAW_SILENCE 0x7f
1219 #define LINEAR_SILENCE 0
1220 u_int auzero;
1221
1222 switch (enc) {
1223 case AUDIO_ENCODING_ULAW:
1224 auzero = ULAW_SILENCE;
1225 break;
1226 case AUDIO_ENCODING_PCM16:
1227 default:
1228 auzero = LINEAR_SILENCE;
1229 break;
1230 }
1231
1232 return(auzero);
1233 }
1234
1235 static u_char mulawtolin[256] = {
1236 128, 4, 8, 12, 16, 20, 24, 28,
1237 32, 36, 40, 44, 48, 52, 56, 60,
1238 64, 66, 68, 70, 72, 74, 76, 78,
1239 80, 82, 84, 86, 88, 90, 92, 94,
1240 96, 97, 98, 99, 100, 101, 102, 103,
1241 104, 105, 106, 107, 108, 109, 110, 111,
1242 112, 112, 113, 113, 114, 114, 115, 115,
1243 116, 116, 117, 117, 118, 118, 119, 119,
1244 120, 120, 120, 121, 121, 121, 121, 122,
1245 122, 122, 122, 123, 123, 123, 123, 124,
1246 124, 124, 124, 124, 125, 125, 125, 125,
1247 125, 125, 125, 125, 126, 126, 126, 126,
1248 126, 126, 126, 126, 126, 126, 126, 126,
1249 127, 127, 127, 127, 127, 127, 127, 127,
1250 127, 127, 127, 127, 127, 127, 127, 127,
1251 127, 127, 127, 127, 127, 127, 127, 127,
1252 255, 251, 247, 243, 239, 235, 231, 227,
1253 223, 219, 215, 211, 207, 203, 199, 195,
1254 191, 189, 187, 185, 183, 181, 179, 177,
1255 175, 173, 171, 169, 167, 165, 163, 161,
1256 159, 158, 157, 156, 155, 154, 153, 152,
1257 151, 150, 149, 148, 147, 146, 145, 144,
1258 143, 143, 142, 142, 141, 141, 140, 140,
1259 139, 139, 138, 138, 137, 137, 136, 136,
1260 135, 135, 135, 134, 134, 134, 134, 133,
1261 133, 133, 133, 132, 132, 132, 132, 131,
1262 131, 131, 131, 131, 130, 130, 130, 130,
1263 130, 130, 130, 130, 129, 129, 129, 129,
1264 129, 129, 129, 129, 129, 129, 129, 129,
1265 128, 128, 128, 128, 128, 128, 128, 128,
1266 128, 128, 128, 128, 128, 128, 128, 128,
1267 128, 128, 128, 128, 128, 128, 128, 128,
1268 };
1269
1270 static u_char lintomulaw[256] = {
1271 0, 0, 0, 0, 0, 1, 1, 1,
1272 1, 2, 2, 2, 2, 3, 3, 3,
1273 3, 4, 4, 4, 4, 5, 5, 5,
1274 5, 6, 6, 6, 6, 7, 7, 7,
1275 7, 8, 8, 8, 8, 9, 9, 9,
1276 9, 10, 10, 10, 10, 11, 11, 11,
1277 11, 12, 12, 12, 12, 13, 13, 13,
1278 13, 14, 14, 14, 14, 15, 15, 15,
1279 15, 16, 16, 17, 17, 18, 18, 19,
1280 19, 20, 20, 21, 21, 22, 22, 23,
1281 23, 24, 24, 25, 25, 26, 26, 27,
1282 27, 28, 28, 29, 29, 30, 30, 31,
1283 31, 32, 33, 34, 35, 36, 37, 38,
1284 39, 40, 41, 42, 43, 44, 45, 46,
1285 47, 48, 50, 52, 54, 56, 58, 60,
1286 62, 65, 69, 73, 77, 83, 91, 103,
1287 255, 231, 219, 211, 205, 201, 197, 193,
1288 190, 188, 186, 184, 182, 180, 178, 176,
1289 175, 174, 173, 172, 171, 170, 169, 168,
1290 167, 166, 165, 164, 163, 162, 161, 160,
1291 159, 159, 158, 158, 157, 157, 156, 156,
1292 155, 155, 154, 154, 153, 153, 152, 152,
1293 151, 151, 150, 150, 149, 149, 148, 148,
1294 147, 147, 146, 146, 145, 145, 144, 144,
1295 143, 143, 143, 143, 142, 142, 142, 142,
1296 141, 141, 141, 141, 140, 140, 140, 140,
1297 139, 139, 139, 139, 138, 138, 138, 138,
1298 137, 137, 137, 137, 136, 136, 136, 136,
1299 135, 135, 135, 135, 134, 134, 134, 134,
1300 133, 133, 133, 133, 132, 132, 132, 132,
1301 131, 131, 131, 131, 130, 130, 130, 130,
1302 129, 129, 129, 129, 128, 128, 128, 128,
1303 };
1304
1305 void
1306 sbdsp_compress(e, p, cc)
1307 int e;
1308 u_char *p;
1309 int cc;
1310 {
1311 u_char *tab;
1312
1313 switch (e) {
1314 case AUDIO_ENCODING_ULAW:
1315 tab = lintomulaw;
1316 break;
1317 default:
1318 return;
1319 }
1320
1321 while (--cc >= 0) {
1322 *p = tab[*p];
1323 ++p;
1324 }
1325 }
1326
1327 void
1328 sbdsp_expand(e, p, cc)
1329 int e;
1330 u_char *p;
1331 int cc;
1332 {
1333 u_char *tab;
1334
1335 switch (e) {
1336 case AUDIO_ENCODING_ULAW:
1337 tab = mulawtolin;
1338 break;
1339 default:
1340 return;
1341 }
1342
1343 while (--cc >= 0) {
1344 *p = tab[*p];
1345 ++p;
1346 }
1347 }
1348
1349 int
1350 sbdsp_setfd(addr, flag)
1351 caddr_t addr;
1352 int flag;
1353 {
1354 /* Can't do full-duplex */
1355 return(ENOTTY);
1356 }
1357
1358 int
1359 sbdsp_mixer_set_port(addr, cp)
1360 caddr_t addr;
1361 mixer_ctrl_t *cp;
1362 {
1363 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
1364 int error = 0;
1365 int src, gain;
1366 int left, right;
1367
1368 DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev, cp->un.value.num_channels));
1369
1370 /*
1371 * Everything is a value except for SBPro special OUTPUT_MODE and
1372 * RECORD_SOURCE
1373 */
1374 if (cp->type != AUDIO_MIXER_VALUE) {
1375 if (!ISSBPROCLASS(sc) || (cp->dev != SB_OUTPUT_MODE &&
1376 cp->dev != SB_RECORD_SOURCE))
1377 return EINVAL;
1378 }
1379 else {
1380 /*
1381 * All the mixer ports are stereo except for the microphone.
1382 * If we get a single-channel gain value passed in, then we
1383 * duplicate it to both left and right channels.
1384 */
1385 if (cp->un.value.num_channels == 2) {
1386 left = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1387 right = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1388 }
1389 else
1390 left = right = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
1391 }
1392
1393 if (ISSBPROCLASS(sc)) {
1394 /* The _PORT things are all signal inputs to the mixer.
1395 * Here we are tweaking their mixing level.
1396 *
1397 * We can also tweak the output stage volume (MASTER_VOL)
1398 */
1399 gain = sbdsp_stereo_vol(SBP_AGAIN_TO_SBGAIN(left),
1400 SBP_AGAIN_TO_SBGAIN(right));
1401 switch(cp->dev) {
1402 case SB_MIC_PORT:
1403 src = SBP_MIC_VOL;
1404 if (cp->un.value.num_channels != 1)
1405 error = EINVAL;
1406 else
1407 /* handle funny microphone gain */
1408 gain = SBP_AGAIN_TO_MICGAIN(left);
1409 break;
1410 case SB_LINE_IN_PORT:
1411 src = SBP_LINE_VOL;
1412 break;
1413 case SB_DAC_PORT:
1414 src = SBP_DAC_VOL;
1415 break;
1416 case SB_FM_PORT:
1417 src = SBP_FM_VOL;
1418 break;
1419 case SB_CD_PORT:
1420 src = SBP_CD_VOL;
1421 break;
1422 case SB_SPEAKER:
1423 cp->dev = SB_MASTER_VOL;
1424 case SB_MASTER_VOL:
1425 src = SBP_MASTER_VOL;
1426 break;
1427 #if 0
1428 case SB_OUTPUT_MODE:
1429 if (cp->type == AUDIO_MIXER_ENUM)
1430 return sbdsp_set_channels(addr, cp->un.ord);
1431 /* fall through...carefully! */
1432 #endif
1433 case SB_RECORD_SOURCE:
1434 if (cp->type == AUDIO_MIXER_ENUM)
1435 return sbdsp_set_in_port(addr, cp->un.ord);
1436 /* else fall through: bad input */
1437 case SB_TREBLE:
1438 case SB_BASS:
1439 default:
1440 error = EINVAL;
1441 break;
1442 }
1443 if (!error)
1444 sbdsp_mix_write(sc, src, gain);
1445 }
1446 else if (cp->dev != SB_MIC_PORT &&
1447 cp->dev != SB_SPEAKER)
1448 error = EINVAL;
1449
1450 if (!error)
1451 sc->gain[cp->dev] = gain;
1452
1453 return(error);
1454 }
1455
1456 int
1457 sbdsp_mixer_get_port(addr, cp)
1458 caddr_t addr;
1459 mixer_ctrl_t *cp;
1460 {
1461 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
1462 int error = 0;
1463 int done = 0;
1464
1465 DPRINTF(("sbdsp_mixer_get_port: port=%d", cp->dev));
1466
1467 if (ISSBPROCLASS(sc))
1468 switch(cp->dev) {
1469 case SB_MIC_PORT:
1470 if (cp->un.value.num_channels == 1) {
1471 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1472 SBP_MICGAIN_TO_AGAIN(sc->gain[cp->dev]);
1473 return 0;
1474 }
1475 else
1476 return EINVAL;
1477 break;
1478 case SB_LINE_IN_PORT:
1479 case SB_DAC_PORT:
1480 case SB_FM_PORT:
1481 case SB_CD_PORT:
1482 case SB_MASTER_VOL:
1483 break;
1484 case SB_SPEAKER:
1485 cp->dev = SB_MASTER_VOL;
1486 break;
1487 default:
1488 error = EINVAL;
1489 break;
1490 }
1491 else {
1492 if (cp->un.value.num_channels != 1) /* no stereo on SB classic */
1493 error = EINVAL;
1494 else
1495 switch(cp->dev) {
1496 case SB_MIC_PORT:
1497 break;
1498 case SB_SPEAKER:
1499 break;
1500 default:
1501 error = EINVAL;
1502 break;
1503 }
1504 }
1505 if (error == 0) {
1506 if (cp->un.value.num_channels == 1) {
1507 cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1508 SBP_SBGAIN_TO_AGAIN(sc->gain[cp->dev]);
1509 }
1510 else if (cp->un.value.num_channels == 2) {
1511 cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1512 SBP_LEFTGAIN(sc->gain[cp->dev]);
1513 cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1514 SBP_RIGHTGAIN(sc->gain[cp->dev]);
1515 } else
1516 return EINVAL;
1517 }
1518 return(error);
1519 }
1520
1521 int
1522 sbdsp_mixer_query_devinfo(addr, dip)
1523 caddr_t addr;
1524 register mixer_devinfo_t *dip;
1525 {
1526 register struct sbdsp_softc *sc = (struct sbdsp_softc *)addr;
1527 int done = 0;
1528
1529 DPRINTF(("sbdsp_mixer_query_devinfo: index=%d\n", dip->index));
1530
1531 switch (dip->index) {
1532 case SB_MIC_PORT:
1533 dip->type = AUDIO_MIXER_VALUE;
1534 dip->mixer_class = SB_INPUT_CLASS;
1535 dip->prev = AUDIO_MIXER_LAST;
1536 dip->next = AUDIO_MIXER_LAST;
1537 strcpy(dip->label.name, AudioNmicrophone);
1538 dip->un.v.num_channels = 1;
1539 strcpy(dip->un.v.units.name, AudioNvolume);
1540 done = 1;
1541 break;
1542 case SB_SPEAKER:
1543 dip->type = AUDIO_MIXER_VALUE;
1544 dip->mixer_class = SB_OUTPUT_CLASS;
1545 dip->prev = AUDIO_MIXER_LAST;
1546 dip->next = AUDIO_MIXER_LAST;
1547 strcpy(dip->label.name, AudioNspeaker);
1548 dip->un.v.num_channels = 1;
1549 strcpy(dip->un.v.units.name, AudioNvolume);
1550 done = 1;
1551 break;
1552 case SB_INPUT_CLASS:
1553 dip->type = AUDIO_MIXER_CLASS;
1554 dip->mixer_class = SB_INPUT_CLASS;
1555 dip->next = dip->prev = AUDIO_MIXER_LAST;
1556 strcpy(dip->label.name, AudioCInputs);
1557 done = 1;
1558 break;
1559 case SB_OUTPUT_CLASS:
1560 dip->type = AUDIO_MIXER_CLASS;
1561 dip->mixer_class = SB_OUTPUT_CLASS;
1562 dip->next = dip->prev = AUDIO_MIXER_LAST;
1563 strcpy(dip->label.name, AudioCOutputs);
1564 done = 1;
1565 break;
1566 }
1567
1568 if (!done) {
1569 if (ISSBPROCLASS(sc))
1570 switch(dip->index) {
1571 case SB_LINE_IN_PORT:
1572 dip->type = AUDIO_MIXER_VALUE;
1573 dip->mixer_class = SB_INPUT_CLASS;
1574 dip->prev = AUDIO_MIXER_LAST;
1575 dip->next = AUDIO_MIXER_LAST;
1576 strcpy(dip->label.name, AudioNline);
1577 dip->un.v.num_channels = 2;
1578 strcpy(dip->un.v.units.name, AudioNvolume);
1579 break;
1580 case SB_DAC_PORT:
1581 dip->type = AUDIO_MIXER_VALUE;
1582 dip->mixer_class = SB_INPUT_CLASS;
1583 dip->prev = AUDIO_MIXER_LAST;
1584 dip->next = AUDIO_MIXER_LAST;
1585 strcpy(dip->label.name, AudioNdac);
1586 dip->un.v.num_channels = 2;
1587 strcpy(dip->un.v.units.name, AudioNvolume);
1588 break;
1589 case SB_CD_PORT:
1590 dip->type = AUDIO_MIXER_VALUE;
1591 dip->mixer_class = SB_INPUT_CLASS;
1592 dip->prev = AUDIO_MIXER_LAST;
1593 dip->next = AUDIO_MIXER_LAST;
1594 strcpy(dip->label.name, AudioNcd);
1595 dip->un.v.num_channels = 2;
1596 strcpy(dip->un.v.units.name, AudioNvolume);
1597 break;
1598 case SB_FM_PORT:
1599 dip->type = AUDIO_MIXER_VALUE;
1600 dip->mixer_class = SB_INPUT_CLASS;
1601 dip->prev = AUDIO_MIXER_LAST;
1602 dip->next = AUDIO_MIXER_LAST;
1603 strcpy(dip->label.name, "fmsynth"); /* XXX move to audioio.h */
1604 dip->un.v.num_channels = 2;
1605 strcpy(dip->un.v.units.name, AudioNvolume);
1606 break;
1607 case SB_MASTER_VOL:
1608 dip->type = AUDIO_MIXER_VALUE;
1609 dip->mixer_class = SB_OUTPUT_CLASS;
1610 dip->prev = AUDIO_MIXER_LAST;
1611 dip->next = SB_OUTPUT_MODE;
1612 strcpy(dip->label.name, AudioNvolume);
1613 dip->un.v.num_channels = 2;
1614 strcpy(dip->un.v.units.name, AudioNvolume);
1615 break;
1616 #if 0
1617 case SB_OUTPUT_MODE:
1618 dip->mixer_class = SB_OUTPUT_CLASS;
1619 dip->type = AUDIO_MIXER_ENUM;
1620 dip->prev = SB_MASTER_VOL;
1621 dip->next = AUDIO_MIXER_LAST;
1622 strcpy(dip->label.name, AudioNmode);
1623 dip->un.e.num_mem = 2;
1624 strcpy(dip->un.e.member[0].label.name, AudioNmono);
1625 dip->un.e.member[0].ord = 1; /* nchans */
1626 strcpy(dip->un.e.member[1].label.name, AudioNstereo);
1627 dip->un.e.member[1].ord = 2; /* nchans */
1628 break;
1629 #endif
1630 case SB_RECORD_SOURCE:
1631 dip->mixer_class = SB_RECORD_CLASS;
1632 dip->type = AUDIO_MIXER_ENUM;
1633 dip->prev = AUDIO_MIXER_LAST;
1634 dip->next = AUDIO_MIXER_LAST;
1635 strcpy(dip->label.name, AudioNsource);
1636 dip->un.e.num_mem = 3;
1637 strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
1638 dip->un.e.member[0].ord = SB_MIC_PORT;
1639 strcpy(dip->un.e.member[1].label.name, AudioNcd);
1640 dip->un.e.member[1].ord = SB_CD_PORT;
1641 strcpy(dip->un.e.member[2].label.name, AudioNline);
1642 dip->un.e.member[2].ord = SB_LINE_IN_PORT;
1643 break;
1644 case SB_BASS:
1645 case SB_TREBLE:
1646 default:
1647 return ENXIO;
1648 /*NOTREACHED*/
1649 }
1650 else
1651 return ENXIO;
1652 }
1653
1654 DPRINTF(("AUDIO_MIXER_DEVINFO: name=%s\n", dip->label.name));
1655
1656 return 0;
1657 }
1658