xref: /netbsd-src/sys/dev/isa/sbdsp.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: sbdsp.c,v 1.58 1997/06/13 19:21:59 augustss Exp $	*/
2 
3 /*
4  * Copyright (c) 1991-1993 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the Computer Systems
18  *	Engineering Group at Lawrence Berkeley Laboratory.
19  * 4. Neither the name of the University nor of the Laboratory may be used
20  *    to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /*
38  * SoundBlaster Pro code provided by John Kohl, based on lots of
39  * information he gleaned from Steve Haehnichen <steve@vigra.com>'s
40  * SBlast driver for 386BSD and DOS driver code from Daniel Sachs
41  * <sachs@meibm15.cen.uiuc.edu>.
42  * Lots of rewrites by Lennart Augustsson <augustss@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 
65 #include <dev/isa/isavar.h>
66 #include <dev/isa/isadmavar.h>
67 
68 #include <dev/isa/sbreg.h>
69 #include <dev/isa/sbdspvar.h>
70 
71 #ifdef AUDIO_DEBUG
72 extern void Dprintf __P((const char *, ...));
73 #define DPRINTF(x)	if (sbdspdebug) Dprintf 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  { SB_16,   1, 16,  5000, 45000, SB_DSP16_WDMA_16 },
146  { SB_16,   2, 16,  5000, 45000, SB_DSP16_WDMA_16 },
147  { -1 }
148 };
149 static struct sbmode sbrmodes[] = {
150  { SB_1,    1,  8,  4000, 12987, SB_DSP_RDMA      },
151  { SB_20,   1,  8,  4000, 12987, SB_DSP_RDMA_LOOP },
152  { SB_2x,   1,  8,  4000, 12987, SB_DSP_RDMA_LOOP },
153  { SB_2x,   1,  8, 12987, 14925, SB_DSP_HS_INPUT  },
154  { SB_PRO,  1,  8,  4000, 22727, SB_DSP_RDMA_LOOP, SB_DSP_RECORD_MONO },
155  { SB_PRO,  1,  8, 22727, 45454, SB_DSP_HS_INPUT,  SB_DSP_RECORD_MONO },
156  { SB_PRO,  2,  8, 11025, 22727, SB_DSP_HS_INPUT,  SB_DSP_RECORD_STEREO },
157  { SB_JAZZ, 1,  8,  4000, 22727, SB_DSP_RDMA_LOOP, SB_DSP_RECORD_MONO },
158  { SB_JAZZ, 1,  8, 22727, 45454, SB_DSP_HS_INPUT,  SB_DSP_RECORD_MONO },
159  { SB_JAZZ, 2,  8, 11025, 22727, SB_DSP_HS_INPUT,  SB_DSP_RECORD_STEREO },
160  { SB_JAZZ, 1, 16,  4000, 22727, SB_DSP_RDMA_LOOP, JAZZ16_RECORD_MONO },
161  { SB_JAZZ, 1, 16, 22727, 45454, SB_DSP_HS_INPUT,  JAZZ16_RECORD_MONO },
162  { SB_JAZZ, 2, 16, 11025, 22727, SB_DSP_HS_INPUT,  JAZZ16_RECORD_STEREO },
163  { SB_16,   1,  8,  5000, 45000, SB_DSP16_RDMA_8  },
164  { SB_16,   2,  8,  5000, 45000, SB_DSP16_RDMA_8  },
165  { SB_16,   1, 16,  5000, 45000, SB_DSP16_RDMA_16 },
166  { SB_16,   2, 16,  5000, 45000, SB_DSP16_RDMA_16 },
167  { -1 }
168 };
169 
170 void	sbversion __P((struct sbdsp_softc *));
171 void	sbdsp_jazz16_probe __P((struct sbdsp_softc *));
172 void	sbdsp_set_mixer_gain __P((struct sbdsp_softc *sc, int port));
173 int	sbdsp16_wait __P((struct sbdsp_softc *));
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 #ifdef AUDIO_DEBUG
183 void sb_printsc __P((struct sbdsp_softc *));
184 
185 void
186 sb_printsc(sc)
187 	struct sbdsp_softc *sc;
188 {
189 	int i;
190 
191 	printf("open %d dmachan %d/%d/%d iobase 0x%x irq %d\n",
192 	    (int)sc->sc_open, sc->dmachan, sc->sc_drq8, sc->sc_drq16,
193 	    sc->sc_iobase, sc->sc_irq);
194 	printf("irate %d itc %x orate %d otc %x\n",
195 	    sc->sc_irate, sc->sc_itc,
196 	    sc->sc_orate, sc->sc_otc);
197 	printf("outport %u inport %u spkron %u nintr %lu\n",
198 	    sc->out_port, sc->in_port, sc->spkr_state, sc->sc_interrupts);
199 	printf("intr %p arg %p\n",
200 	    sc->sc_intr, sc->sc_arg);
201 	printf("gain:");
202 	for (i = 0; i < SB_NDEVS; i++)
203 		printf(" %u,%u", sc->gain[i][SB_LEFT], sc->gain[i][SB_RIGHT]);
204 	printf("\n");
205 }
206 #endif /* AUDIO_DEBUG */
207 
208 /*
209  * Probe / attach routines.
210  */
211 
212 /*
213  * Probe for the soundblaster hardware.
214  */
215 int
216 sbdsp_probe(sc)
217 	struct sbdsp_softc *sc;
218 {
219 
220 	if (sbdsp_reset(sc) < 0) {
221 		DPRINTF(("sbdsp: couldn't reset card\n"));
222 		return 0;
223 	}
224 	/* if flags set, go and probe the jazz16 stuff */
225 	if (sc->sc_dev.dv_cfdata->cf_flags & 1)
226 		sbdsp_jazz16_probe(sc);
227 	else
228 		sbversion(sc);
229 	if (sc->sc_model == SB_UNK) {
230 		/* Unknown SB model found. */
231 		DPRINTF(("sbdsp: unknown SB model found\n"));
232 		return 0;
233 	}
234 	return 1;
235 }
236 
237 /*
238  * Try add-on stuff for Jazz16.
239  */
240 void
241 sbdsp_jazz16_probe(sc)
242 	struct sbdsp_softc *sc;
243 {
244 	static u_char jazz16_irq_conf[16] = {
245 	    -1, -1, 0x02, 0x03,
246 	    -1, 0x01, -1, 0x04,
247 	    -1, 0x02, 0x05, -1,
248 	    -1, -1, -1, 0x06};
249 	static u_char jazz16_drq_conf[8] = {
250 	    -1, 0x01, -1, 0x02,
251 	    -1, 0x03, -1, 0x04};
252 
253 	bus_space_tag_t iot = sc->sc_iot;
254 	bus_space_handle_t ioh;
255 
256 	sbversion(sc);
257 
258 	DPRINTF(("jazz16 probe\n"));
259 
260 	if (bus_space_map(iot, JAZZ16_CONFIG_PORT, 1, 0, &ioh)) {
261 		DPRINTF(("bus map failed\n"));
262 		return;
263 	}
264 
265 	if (jazz16_drq_conf[sc->sc_drq8] == (u_char)-1 ||
266 	    jazz16_irq_conf[sc->sc_irq] == (u_char)-1) {
267 		DPRINTF(("drq/irq check failed\n"));
268 		goto done;		/* give up, we can't do it. */
269 	}
270 
271 	bus_space_write_1(iot, ioh, 0, JAZZ16_WAKEUP);
272 	delay(10000);			/* delay 10 ms */
273 	bus_space_write_1(iot, ioh, 0, JAZZ16_SETBASE);
274 	bus_space_write_1(iot, ioh, 0, sc->sc_iobase & 0x70);
275 
276 	if (sbdsp_reset(sc) < 0) {
277 		DPRINTF(("sbdsp_reset check failed\n"));
278 		goto done;		/* XXX? what else could we do? */
279 	}
280 
281 	if (sbdsp_wdsp(sc, JAZZ16_READ_VER)) {
282 		DPRINTF(("read16 setup failed\n"));
283 		goto done;
284 	}
285 
286 	if (sbdsp_rdsp(sc) != JAZZ16_VER_JAZZ) {
287 		DPRINTF(("read16 failed\n"));
288 		goto done;
289 	}
290 
291 	/* XXX set both 8 & 16-bit drq to same channel, it works fine. */
292 	sc->sc_drq16 = sc->sc_drq8;
293 	if (sbdsp_wdsp(sc, JAZZ16_SET_DMAINTR) ||
294 	    sbdsp_wdsp(sc, (jazz16_drq_conf[sc->sc_drq16] << 4) |
295 		jazz16_drq_conf[sc->sc_drq8]) ||
296 	    sbdsp_wdsp(sc, jazz16_irq_conf[sc->sc_irq])) {
297 		DPRINTF(("sbdsp: can't write jazz16 probe stuff\n"));
298 	} else {
299 		DPRINTF(("jazz16 detected!\n"));
300 		sc->sc_model = SB_JAZZ;
301 		sc->sc_mixer_model = SBM_CT1345; /* XXX really? */
302 	}
303 
304 done:
305 	bus_space_unmap(iot, ioh, 1);
306 }
307 
308 /*
309  * Attach hardware to driver, attach hardware driver to audio
310  * pseudo-device driver .
311  */
312 void
313 sbdsp_attach(sc)
314 	struct sbdsp_softc *sc;
315 {
316 	struct audio_params xparams;
317         int i;
318         u_int v;
319 
320 	/*
321 	 * Create our DMA maps.
322 	 */
323 	if (sc->sc_drq8 != -1) {
324 		if (isa_dmamap_create(sc->sc_isa, sc->sc_drq8,
325 		    MAXPHYS /* XXX */, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
326 			printf("%s: can't create map for drq %d\n",
327 			    sc->sc_dev.dv_xname, sc->sc_drq8);
328 			return;
329 		}
330 	}
331 	if (sc->sc_drq16 != -1 && sc->sc_drq16 != sc->sc_drq8) {
332 		if (isa_dmamap_create(sc->sc_isa, sc->sc_drq16,
333 		    MAXPHYS /* XXX */, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
334 			printf("%s: can't create map for drq %d\n",
335 			    sc->sc_dev.dv_xname, sc->sc_drq16);
336 			return;
337 		}
338 	}
339 
340         sbdsp_set_params(sc, AUMODE_RECORD, &audio_default, &xparams);
341         sbdsp_set_params(sc, AUMODE_PLAY,   &audio_default, &xparams);
342 
343 	sbdsp_set_in_port(sc, SB_MIC_VOL);
344 	sbdsp_set_out_port(sc, SB_MASTER_VOL);
345 
346 	if (sc->sc_mixer_model != SBM_NONE) {
347         	/* Reset the mixer.*/
348 		sbdsp_mix_write(sc, SBP_MIX_RESET, SBP_MIX_RESET);
349                 /* And set our own default values */
350 		for (i = 0; i < SB_NDEVS; i++) {
351 			switch(i) {
352 			case SB_MIC_VOL:
353 			case SB_LINE_IN_VOL:
354 				v = 0;
355 				break;
356 			case SB_BASS:
357 			case SB_TREBLE:
358 				v = SB_ADJUST_GAIN(sc, AUDIO_MAX_GAIN/2);
359 				break;
360 			default:
361 				v = SB_ADJUST_GAIN(sc, AUDIO_MAX_GAIN * 3 / 4);
362 				break;
363 			}
364 			sc->gain[i][SB_LEFT] = sc->gain[i][SB_RIGHT] = v;
365 			sbdsp_set_mixer_gain(sc, i);
366 		}
367 		sc->in_filter = 0;	/* no filters turned on, please */
368 	}
369 
370 	printf(": dsp v%d.%02d%s\n",
371 	       SBVER_MAJOR(sc->sc_version), SBVER_MINOR(sc->sc_version),
372 	       sc->sc_model == SB_JAZZ ? ": <Jazz16>" : "");
373 }
374 
375 void
376 sbdsp_mix_write(sc, mixerport, val)
377 	struct sbdsp_softc *sc;
378 	int mixerport;
379 	int val;
380 {
381 	bus_space_tag_t iot = sc->sc_iot;
382 	bus_space_handle_t ioh = sc->sc_ioh;
383 	int s;
384 
385 	s = splaudio();
386 	bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
387 	delay(20);
388 	bus_space_write_1(iot, ioh, SBP_MIXER_DATA, val);
389 	delay(30);
390 	splx(s);
391 }
392 
393 int
394 sbdsp_mix_read(sc, mixerport)
395 	struct sbdsp_softc *sc;
396 	int mixerport;
397 {
398 	bus_space_tag_t iot = sc->sc_iot;
399 	bus_space_handle_t ioh = sc->sc_ioh;
400 	int val;
401 	int s;
402 
403 	s = splaudio();
404 	bus_space_write_1(iot, ioh, SBP_MIXER_ADDR, mixerport);
405 	delay(20);
406 	val = bus_space_read_1(iot, ioh, SBP_MIXER_DATA);
407 	delay(30);
408 	splx(s);
409 	return val;
410 }
411 
412 /*
413  * Various routines to interface to higher level audio driver
414  */
415 
416 int
417 sbdsp_query_encoding(addr, fp)
418 	void *addr;
419 	struct audio_encoding *fp;
420 {
421 	struct sbdsp_softc *sc = addr;
422 	int emul;
423 
424 	emul = ISSB16CLASS(sc) ? 0 : AUDIO_ENCODINGFLAG_EMULATED;
425 
426 	switch (fp->index) {
427 	case 0:
428 		strcpy(fp->name, AudioEulinear);
429 		fp->encoding = AUDIO_ENCODING_ULINEAR;
430 		fp->precision = 8;
431 		fp->flags = 0;
432 		return 0;
433 	case 1:
434 		strcpy(fp->name, AudioEmulaw);
435 		fp->encoding = AUDIO_ENCODING_ULAW;
436 		fp->precision = 8;
437 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
438 		return 0;
439 	case 2:
440 		strcpy(fp->name, AudioEalaw);
441 		fp->encoding = AUDIO_ENCODING_ALAW;
442 		fp->precision = 8;
443 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
444 		return 0;
445 	case 3:
446 		strcpy(fp->name, AudioElinear);
447 		fp->encoding = AUDIO_ENCODING_LINEAR;
448 		fp->precision = 8;
449 		fp->flags = emul;
450 		return 0;
451         }
452         if (!ISSB16CLASS(sc) && sc->sc_model != SB_JAZZ)
453 		return EINVAL;
454 
455         switch(fp->index) {
456         case 4:
457 		strcpy(fp->name, AudioElinear_le);
458 		fp->encoding = AUDIO_ENCODING_LINEAR_LE;
459 		fp->precision = 16;
460 		fp->flags = 0;
461 		return 0;
462 	case 5:
463 		strcpy(fp->name, AudioEulinear_le);
464 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
465 		fp->precision = 16;
466 		fp->flags = emul;
467 		return 0;
468 	case 6:
469 		strcpy(fp->name, AudioElinear_be);
470 		fp->encoding = AUDIO_ENCODING_LINEAR_BE;
471 		fp->precision = 16;
472 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
473 		return 0;
474 	case 7:
475 		strcpy(fp->name, AudioEulinear_be);
476 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
477 		fp->precision = 16;
478 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
479 		return 0;
480 	default:
481 		return EINVAL;
482 	}
483 	return 0;
484 }
485 
486 int
487 sbdsp_set_params(addr, mode, p, q)
488 	void *addr;
489 	int mode;
490 	struct audio_params *p, *q;
491 {
492 	struct sbdsp_softc *sc = addr;
493 	struct sbmode *m;
494 	u_int rate, tc = 1, bmode = -1;
495 	void (*swcode) __P((void *, u_char *buf, int cnt));
496 
497 	for(m = mode == AUMODE_PLAY ? sbpmodes : sbrmodes;
498 	    m->model != -1; m++) {
499 		if (sc->sc_model == m->model &&
500 		    p->channels == m->channels &&
501 		    p->precision == m->precision &&
502 		    p->sample_rate >= m->lowrate &&
503 		    p->sample_rate < m->highrate)
504 			break;
505 	}
506 	if (m->model == -1)
507 		return EINVAL;
508 	rate = p->sample_rate;
509 	swcode = 0;
510 	if (m->model == SB_16) {
511 		switch (p->encoding) {
512 		case AUDIO_ENCODING_LINEAR_BE:
513 			if (p->precision == 16)
514 				swcode = swap_bytes;
515 			/* fall into */
516 		case AUDIO_ENCODING_LINEAR_LE:
517 			bmode = 0x10;
518 			break;
519 		case AUDIO_ENCODING_ULINEAR_BE:
520 			if (p->precision == 16)
521 				swcode = swap_bytes;
522 			/* fall into */
523 		case AUDIO_ENCODING_ULINEAR_LE:
524 			bmode = 0;
525 			break;
526 		case AUDIO_ENCODING_ULAW:
527 			swcode = mode == AUMODE_PLAY ?
528 				mulaw_to_ulinear8 : ulinear8_to_mulaw;
529 			bmode = 0;
530 			break;
531 		case AUDIO_ENCODING_ALAW:
532 			swcode = mode == AUMODE_PLAY ?
533 				alaw_to_ulinear8 : ulinear8_to_alaw;
534 			bmode = 0;
535 			break;
536 		default:
537 			return EINVAL;
538 		}
539 		if (p->channels == 2)
540 			bmode |= 0x20;
541 	} else if (m->model == SB_JAZZ && m->precision == 16) {
542 		switch (p->encoding) {
543 		case AUDIO_ENCODING_LINEAR_LE:
544 			break;
545 		case AUDIO_ENCODING_ULINEAR_LE:
546 			swcode = change_sign16;
547 			break;
548 		case AUDIO_ENCODING_LINEAR_BE:
549 			swcode = swap_bytes;
550 			break;
551 		case AUDIO_ENCODING_ULINEAR_BE:
552 			swcode = mode == AUMODE_PLAY ?
553 			  swap_bytes_change_sign16 : change_sign16_swap_bytes;
554 			break;
555 		case AUDIO_ENCODING_ULAW:
556 			swcode = mode == AUMODE_PLAY ?
557 				mulaw_to_ulinear8 : ulinear8_to_mulaw;
558 			break;
559 		case AUDIO_ENCODING_ALAW:
560 			swcode = mode == AUMODE_PLAY ?
561 				alaw_to_ulinear8 : ulinear8_to_alaw;
562 			break;
563 		default:
564 			return EINVAL;
565 		}
566 		tc = SB_RATE_TO_TC(p->sample_rate * p->channels);
567 		p->sample_rate = SB_TC_TO_RATE(tc) / p->channels;
568 	} else {
569 		switch (p->encoding) {
570 		case AUDIO_ENCODING_LINEAR_BE:
571 		case AUDIO_ENCODING_LINEAR_LE:
572 			swcode = change_sign8;
573 			break;
574 		case AUDIO_ENCODING_ULINEAR_BE:
575 		case AUDIO_ENCODING_ULINEAR_LE:
576 			break;
577 		case AUDIO_ENCODING_ULAW:
578 			swcode = mode == AUMODE_PLAY ?
579 				mulaw_to_ulinear8 : ulinear8_to_mulaw;
580 			break;
581 		case AUDIO_ENCODING_ALAW:
582 			swcode = mode == AUMODE_PLAY ?
583 				alaw_to_ulinear8 : ulinear8_to_alaw;
584 			break;
585 		default:
586 			return EINVAL;
587 		}
588 		tc = SB_RATE_TO_TC(p->sample_rate * p->channels);
589 		p->sample_rate = SB_TC_TO_RATE(tc) / p->channels;
590 	}
591 
592 	if (mode == AUMODE_PLAY) {
593 		sc->sc_orate = rate;
594 		sc->sc_otc = tc;
595 		sc->sc_omodep = m;
596 		sc->sc_obmode = bmode;
597 	} else {
598 		sc->sc_irate = rate;
599 		sc->sc_itc = tc;
600 		sc->sc_imodep = m;
601 		sc->sc_ibmode = bmode;
602 	}
603 
604 	p->sw_code = swcode;
605 
606 	/* Update setting for the other mode. */
607 	q->encoding = p->encoding;
608 	q->channels = p->channels;
609 	q->precision = p->precision;
610 
611 	/*
612 	 * XXX
613 	 * Should wait for chip to be idle.
614 	 */
615 	sc->sc_dmadir = SB_DMA_NONE;
616 
617 	DPRINTF(("set_params: model=%d, rate=%ld, prec=%d, chan=%d, enc=%d -> tc=%02x, cmd=%02x, bmode=%02x, cmdchan=%02x, swcode=%p\n",
618 		 sc->sc_model, p->sample_rate, p->precision, p->channels,
619 		 p->encoding, tc, m->cmd, bmode, m->cmdchan, swcode));
620 
621 	return 0;
622 }
623 
624 void
625 sbdsp_set_ifilter(addr, which)
626 	void *addr;
627 	int which;
628 {
629 	struct sbdsp_softc *sc = addr;
630 	int mixval;
631 
632 	mixval = sbdsp_mix_read(sc, SBP_INFILTER) & ~SBP_IFILTER_MASK;
633 	switch (which) {
634 	case 0:
635 		mixval |= SBP_FILTER_OFF;
636 		break;
637 	case SB_TREBLE:
638 		mixval |= SBP_FILTER_ON | SBP_IFILTER_HIGH;
639 		break;
640 	case SB_BASS:
641 		mixval |= SBP_FILTER_ON | SBP_IFILTER_LOW;
642 		break;
643 	default:
644 		return;
645 	}
646 	sc->in_filter = mixval & SBP_IFILTER_MASK;
647 	sbdsp_mix_write(sc, SBP_INFILTER, mixval);
648 }
649 
650 int
651 sbdsp_get_ifilter(addr)
652 	void *addr;
653 {
654 	struct sbdsp_softc *sc = addr;
655 
656 	sc->in_filter =
657 		sbdsp_mix_read(sc, SBP_INFILTER) & SBP_IFILTER_MASK;
658 	switch (sc->in_filter) {
659 	case SBP_FILTER_ON|SBP_IFILTER_HIGH:
660 		return SB_TREBLE;
661 	case SBP_FILTER_ON|SBP_IFILTER_LOW:
662 		return SB_BASS;
663 	default:
664 		return 0;
665 	}
666 }
667 
668 int
669 sbdsp_set_out_port(addr, port)
670 	void *addr;
671 	int port;
672 {
673 	struct sbdsp_softc *sc = addr;
674 
675 	sc->out_port = port; /* Just record it */
676 
677 	return 0;
678 }
679 
680 int
681 sbdsp_get_out_port(addr)
682 	void *addr;
683 {
684 	struct sbdsp_softc *sc = addr;
685 
686 	return sc->out_port;
687 }
688 
689 
690 int
691 sbdsp_set_in_port(addr, port)
692 	void *addr;
693 	int port;
694 {
695 	return sbdsp_set_in_ports(addr, 1 << port);
696 }
697 
698 int
699 sbdsp_set_in_ports(sc, mask)
700 	struct sbdsp_softc *sc;
701 	int mask;
702 {
703 	int bitsl, bitsr;
704 	int sbport;
705 	int i;
706 
707 	DPRINTF(("sbdsp_set_in_ports: model=%d, mask=%x\n",
708 		 sc->sc_mixer_model, mask));
709 
710 	switch(sc->sc_mixer_model) {
711 	case SBM_NONE:
712 		return EINVAL;
713 	case SBM_CT1335:
714 		if (mask != (1 << SB_MIC_VOL))
715 			return EINVAL;
716 		break;
717 	case SBM_CT1345:
718 		switch (mask) {
719 		case 1 << SB_MIC_VOL:
720 			sbport = SBP_FROM_MIC;
721 			break;
722 		case 1 << SB_LINE_IN_VOL:
723 			sbport = SBP_FROM_LINE;
724 			break;
725 		case 1 << SB_CD_VOL:
726 			sbport = SBP_FROM_CD;
727 			break;
728 		default:
729 			return EINVAL;
730 		}
731 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE,
732 		    SBP_RECORD_FROM(sbport, SBP_FILTER_OFF, SBP_IFILTER_HIGH));
733 		break;
734 	case SBM_CT1745:
735 		if (mask & ~((1<<SB_MIDI_VOL) | (1<<SB_LINE_IN_VOL) |
736 			     (1<<SB_CD_VOL) | (1<<SB_MIC_VOL)))
737 			return EINVAL;
738 		bitsr = 0;
739 		if (mask & (1<<SB_MIDI_VOL))    bitsr |= SBP_MIDI_SRC_R;
740 		if (mask & (1<<SB_LINE_IN_VOL)) bitsr |= SBP_LINE_SRC_R;
741 		if (mask & (1<<SB_CD_VOL))      bitsr |= SBP_CD_SRC_R;
742 		bitsl = SB_SRC_R_TO_L(bitsr);
743 		if (mask & (1<<SB_MIC_VOL)) {
744 			bitsl |= SBP_MIC_SRC;
745 			bitsr |= SBP_MIC_SRC;
746 		}
747 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE_L, bitsl);
748 		sbdsp_mix_write(sc, SBP_RECORD_SOURCE_R, bitsr);
749 		break;
750 	}
751 
752 	sc->in_mask = mask;
753 
754 	/* XXX
755 	 * We have to fake a single port since the upper layer
756 	 * expects one.
757 	 */
758 	for(i = 0; i < SB_NPORT; i++) {
759 		if (mask & (1 << i)) {
760 			sc->in_port = i;
761 			break;
762 		}
763 	}
764 	return 0;
765 }
766 
767 int
768 sbdsp_get_in_port(addr)
769 	void *addr;
770 {
771 	struct sbdsp_softc *sc = addr;
772 
773 	return sc->in_port;
774 }
775 
776 
777 int
778 sbdsp_speaker_ctl(addr, newstate)
779 	void *addr;
780 	int newstate;
781 {
782 	struct sbdsp_softc *sc = addr;
783 
784 	if ((newstate == SPKR_ON) &&
785 	    (sc->spkr_state == SPKR_OFF)) {
786 		sbdsp_spkron(sc);
787 		sc->spkr_state = SPKR_ON;
788 	}
789 	if ((newstate == SPKR_OFF) &&
790 	    (sc->spkr_state == SPKR_ON)) {
791 		sbdsp_spkroff(sc);
792 		sc->spkr_state = SPKR_OFF;
793 	}
794 	return 0;
795 }
796 
797 int
798 sbdsp_round_blocksize(addr, blk)
799 	void *addr;
800 	int blk;
801 {
802 	if (blk > NBPG/3)
803 		blk = NBPG/3;	/* XXX allow at least 3 blocks */
804 
805 	/* Round to a multiple of the biggest sample size. */
806 	blk &= -4;
807 
808 	return blk;
809 }
810 
811 int
812 sbdsp_commit_settings(addr)
813 	void *addr;
814 {
815 	return 0;
816 }
817 
818 int
819 sbdsp_open(sc, dev, flags)
820 	struct sbdsp_softc *sc;
821 	dev_t dev;
822 	int flags;
823 {
824         DPRINTF(("sbdsp_open: sc=%p\n", sc));
825 
826 	if (sc->sc_open != 0 || sbdsp_reset(sc) != 0)
827 		return ENXIO;
828 
829 	sc->sc_open = 1;
830 	sc->sc_mintr = 0;
831 	if (ISSBPRO(sc) &&
832 	    sbdsp_wdsp(sc, SB_DSP_RECORD_MONO) < 0) {
833 		DPRINTF(("sbdsp_open: can't set mono mode\n"));
834 		/* we'll readjust when it's time for DMA. */
835 	}
836 
837 	/*
838 	 * Leave most things as they were; users must change things if
839 	 * the previous process didn't leave it they way they wanted.
840 	 * Looked at another way, it's easy to set up a configuration
841 	 * in one program and leave it for another to inherit.
842 	 */
843 	DPRINTF(("sbdsp_open: opened\n"));
844 
845 	return 0;
846 }
847 
848 void
849 sbdsp_close(addr)
850 	void *addr;
851 {
852 	struct sbdsp_softc *sc = addr;
853 
854         DPRINTF(("sbdsp_close: sc=%p\n", sc));
855 
856 	sc->sc_open = 0;
857 	sbdsp_spkroff(sc);
858 	sc->spkr_state = SPKR_OFF;
859 	sc->sc_intr = 0;
860 	sc->sc_mintr = 0;
861 	sbdsp_haltdma(sc);
862 
863 	DPRINTF(("sbdsp_close: closed\n"));
864 }
865 
866 /*
867  * Lower-level routines
868  */
869 
870 /*
871  * Reset the card.
872  * Return non-zero if the card isn't detected.
873  */
874 int
875 sbdsp_reset(sc)
876 	struct sbdsp_softc *sc;
877 {
878 	bus_space_tag_t iot = sc->sc_iot;
879 	bus_space_handle_t ioh = sc->sc_ioh;
880 
881 	sc->sc_intr = 0;
882 	if (sc->sc_dmadir != SB_DMA_NONE) {
883 		isa_dmaabort(sc->sc_isa, sc->dmachan);
884 		sc->sc_dmadir = SB_DMA_NONE;
885 	}
886 
887 	/*
888 	 * See SBK, section 11.3.
889 	 * We pulse a reset signal into the card.
890 	 * Gee, what a brilliant hardware design.
891 	 */
892 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 1);
893 	delay(10);
894 	bus_space_write_1(iot, ioh, SBP_DSP_RESET, 0);
895 	delay(30);
896 	if (sbdsp_rdsp(sc) != SB_MAGIC)
897 		return -1;
898 
899 	return 0;
900 }
901 
902 int
903 sbdsp16_wait(sc)
904 	struct sbdsp_softc *sc;
905 {
906 	bus_space_tag_t iot = sc->sc_iot;
907 	bus_space_handle_t ioh = sc->sc_ioh;
908 	int i;
909 	u_char x;
910 
911 	for (i = SBDSP_NPOLL; --i >= 0; ) {
912 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
913 		delay(10);
914 		if ((x & SB_DSP_BUSY) == 0)
915 			continue;
916 		return 0;
917 	}
918 	++sberr.wdsp;
919 	return -1;
920 }
921 
922 /*
923  * Write a byte to the dsp.
924  * XXX We are at the mercy of the card as we use a
925  * polling loop and wait until it can take the byte.
926  */
927 int
928 sbdsp_wdsp(sc, v)
929 	struct sbdsp_softc *sc;
930 	int v;
931 {
932 	bus_space_tag_t iot = sc->sc_iot;
933 	bus_space_handle_t ioh = sc->sc_ioh;
934 	int i;
935 	u_char x;
936 
937 	for (i = SBDSP_NPOLL; --i >= 0; ) {
938 		x = bus_space_read_1(iot, ioh, SBP_DSP_WSTAT);
939 		delay(10);
940 		if ((x & SB_DSP_BUSY) != 0)
941 			continue;
942 		bus_space_write_1(iot, ioh, SBP_DSP_WRITE, v);
943 		delay(10);
944 		return 0;
945 	}
946 	++sberr.wdsp;
947 	return -1;
948 }
949 
950 /*
951  * Read a byte from the DSP, using polling.
952  */
953 int
954 sbdsp_rdsp(sc)
955 	struct sbdsp_softc *sc;
956 {
957 	bus_space_tag_t iot = sc->sc_iot;
958 	bus_space_handle_t ioh = sc->sc_ioh;
959 	int i;
960 	u_char x;
961 
962 	for (i = SBDSP_NPOLL; --i >= 0; ) {
963 		x = bus_space_read_1(iot, ioh, SBP_DSP_RSTAT);
964 		delay(10);
965 		if ((x & SB_DSP_READY) == 0)
966 			continue;
967 		x = bus_space_read_1(iot, ioh, SBP_DSP_READ);
968 		delay(10);
969 		return x;
970 	}
971 	++sberr.rdsp;
972 	return -1;
973 }
974 
975 /*
976  * Doing certain things (like toggling the speaker) make
977  * the SB hardware go away for a while, so pause a little.
978  */
979 void
980 sbdsp_to(arg)
981 	void *arg;
982 {
983 	wakeup(arg);
984 }
985 
986 void
987 sbdsp_pause(sc)
988 	struct sbdsp_softc *sc;
989 {
990 	extern int hz;
991 
992 	timeout(sbdsp_to, sbdsp_to, hz/8);
993 	(void)tsleep(sbdsp_to, PWAIT, "sbpause", 0);
994 }
995 
996 /*
997  * Turn on the speaker.  The SBK documention says this operation
998  * can take up to 1/10 of a second.  Higher level layers should
999  * probably let the task sleep for this amount of time after
1000  * calling here.  Otherwise, things might not work (because
1001  * sbdsp_wdsp() and sbdsp_rdsp() will probably timeout.)
1002  *
1003  * These engineers had their heads up their ass when
1004  * they designed this card.
1005  */
1006 void
1007 sbdsp_spkron(sc)
1008 	struct sbdsp_softc *sc;
1009 {
1010 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_ON);
1011 	sbdsp_pause(sc);
1012 }
1013 
1014 /*
1015  * Turn off the speaker; see comment above.
1016  */
1017 void
1018 sbdsp_spkroff(sc)
1019 	struct sbdsp_softc *sc;
1020 {
1021 	(void)sbdsp_wdsp(sc, SB_DSP_SPKR_OFF);
1022 	sbdsp_pause(sc);
1023 }
1024 
1025 /*
1026  * Read the version number out of the card.  Return major code
1027  * in high byte, and minor code in low byte.
1028  */
1029 void
1030 sbversion(sc)
1031 	struct sbdsp_softc *sc;
1032 {
1033 	int v;
1034 
1035 	sc->sc_model = SB_UNK;
1036 	sc->sc_version = 0;
1037 	if (sbdsp_wdsp(sc, SB_DSP_VERSION) < 0)
1038 		return;
1039 	v = sbdsp_rdsp(sc) << 8;
1040 	v |= sbdsp_rdsp(sc);
1041 	if (v < 0)
1042 		return;
1043 	sc->sc_version = v;
1044 	switch(SBVER_MAJOR(v)) {
1045 	case 1:
1046 		sc->sc_mixer_model = SBM_NONE;
1047 		sc->sc_model = SB_1;
1048 		break;
1049 	case 2:
1050 		/* Some SB2 have a mixer, some don't. */
1051 		sbdsp_mix_write(sc, SBP_1335_MASTER_VOL, 0x04);
1052 		sbdsp_mix_write(sc, SBP_1335_MIDI_VOL,   0x06);
1053 		/* Check if we can read back the mixer values. */
1054 		if ((sbdsp_mix_read(sc, SBP_1335_MASTER_VOL) & 0x0e) == 0x04 &&
1055 		    (sbdsp_mix_read(sc, SBP_1335_MIDI_VOL)   & 0x0e) == 0x06)
1056 			sc->sc_mixer_model = SBM_CT1335;
1057 		else
1058 			sc->sc_mixer_model = SBM_NONE;
1059 		if (SBVER_MINOR(v) == 0)
1060 			sc->sc_model = SB_20;
1061 		else
1062 			sc->sc_model = SB_2x;
1063 		break;
1064 	case 3:
1065 		sc->sc_mixer_model = SBM_CT1345;
1066 		sc->sc_model = SB_PRO;
1067 		break;
1068 	case 4:
1069 		sc->sc_mixer_model = SBM_CT1745;
1070 		sc->sc_model = SB_16;
1071 		break;
1072 	}
1073 }
1074 
1075 /*
1076  * Halt a DMA in progress.  A low-speed transfer can be
1077  * resumed with sbdsp_contdma().
1078  */
1079 int
1080 sbdsp_haltdma(addr)
1081 	void *addr;
1082 {
1083 	struct sbdsp_softc *sc = addr;
1084 
1085 	DPRINTF(("sbdsp_haltdma: sc=%p\n", sc));
1086 
1087 	sbdsp_reset(sc);
1088 	return 0;
1089 }
1090 
1091 int
1092 sbdsp_contdma(addr)
1093 	void *addr;
1094 {
1095 	struct sbdsp_softc *sc = addr;
1096 
1097 	DPRINTF(("sbdsp_contdma: sc=%p\n", sc));
1098 
1099 	/* XXX how do we reinitialize the DMA controller state?  do we care? */
1100 	(void)sbdsp_wdsp(sc, SB_DSP_CONT);
1101 	return 0;
1102 }
1103 
1104 int
1105 sbdsp_set_timeconst(sc, tc)
1106 	struct sbdsp_softc *sc;
1107 	int tc;
1108 {
1109 	DPRINTF(("sbdsp_set_timeconst: sc=%p tc=%d\n", sc, tc));
1110 
1111 	if (sbdsp_wdsp(sc, SB_DSP_TIMECONST) < 0 ||
1112 	    sbdsp_wdsp(sc, tc) < 0)
1113 		return EIO;
1114 
1115 	return 0;
1116 }
1117 
1118 int
1119 sbdsp16_set_rate(sc, cmd, rate)
1120 	struct sbdsp_softc *sc;
1121 	int cmd, rate;
1122 {
1123 	DPRINTF(("sbdsp16_set_rate: sc=%p rate=%d\n", sc, rate));
1124 
1125 	if (sbdsp_wdsp(sc, cmd) < 0 ||
1126 	    sbdsp_wdsp(sc, rate >> 8) < 0 ||
1127 	    sbdsp_wdsp(sc, rate) < 0)
1128 		return EIO;
1129 	return 0;
1130 }
1131 
1132 int
1133 sbdsp_dma_input(addr, p, cc, intr, arg)
1134 	void *addr;
1135 	void *p;
1136 	int cc;
1137 	void (*intr) __P((void *));
1138 	void *arg;
1139 {
1140 	struct sbdsp_softc *sc = addr;
1141 	int loop = sc->sc_model != SB_1;
1142 	int stereo = sc->sc_imodep->channels == 2;
1143 	int filter;
1144 
1145 #ifdef AUDIO_DEBUG
1146 	if (sbdspdebug > 1)
1147 		Dprintf("sbdsp_dma_input: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
1148 #endif
1149 #ifdef DIAGNOSTIC
1150 	if (sc->sc_imodep->channels == 2 && (cc & 1)) {
1151 		DPRINTF(("stereo record odd bytes (%d)\n", cc));
1152 		return EIO;
1153 	}
1154 #endif
1155 
1156 	if (sc->sc_dmadir != SB_DMA_IN) {
1157 		if (ISSBPRO(sc)) {
1158 			if (sbdsp_wdsp(sc, sc->sc_imodep->cmdchan) < 0)
1159 				goto badmode;
1160 			filter = stereo ? SBP_FILTER_OFF : sc->in_filter;
1161 			sbdsp_mix_write(sc, SBP_INFILTER,
1162 					(sbdsp_mix_read(sc, SBP_INFILTER) &
1163 					 ~SBP_IFILTER_MASK) | filter);
1164 		}
1165 
1166 		if (ISSB16CLASS(sc)) {
1167 			if (sbdsp16_set_rate(sc, SB_DSP16_INPUTRATE,
1168 					     sc->sc_irate))
1169 				goto giveup;
1170 		} else {
1171 			if (sbdsp_set_timeconst(sc, sc->sc_itc))
1172 				goto giveup;
1173 		}
1174 
1175 		sc->sc_dmadir = SB_DMA_IN;
1176 		sc->dmaflags = DMAMODE_READ;
1177 		if (loop)
1178 			sc->dmaflags |= DMAMODE_LOOP;
1179 	} else {
1180 		/* If already started; just return. */
1181 		if (loop)
1182 			return 0;
1183 	}
1184 
1185 	sc->dmaaddr = p;
1186 	sc->dmacnt = loop ? (NBPG/cc)*cc : cc;
1187 	sc->dmachan = sc->sc_imodep->precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
1188 #ifdef AUDIO_DEBUG
1189 	if (sbdspdebug > 1)
1190 		Dprintf("sbdsp_dma_input: dmastart %x %p %d %d\n",
1191 			sc->dmaflags, sc->dmaaddr, sc->dmacnt, sc->dmachan);
1192 #endif
1193 	isa_dmastart(sc->sc_isa, sc->dmachan, sc->dmaaddr,
1194 	    sc->dmacnt, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
1195 	sc->sc_intr = intr;
1196 	sc->sc_arg = arg;
1197 
1198 	if ((sc->sc_model == SB_JAZZ && sc->dmachan > 3) ||
1199 	    (sc->sc_model != SB_JAZZ && sc->sc_imodep->precision == 16))
1200 		cc >>= 1;
1201 	--cc;
1202 	if (ISSB16CLASS(sc)) {
1203 		if (sbdsp_wdsp(sc, sc->sc_imodep->cmd) < 0 ||
1204 		    sbdsp_wdsp(sc, sc->sc_ibmode) < 0 ||
1205 		    sbdsp16_wait(sc) ||
1206 		    sbdsp_wdsp(sc, cc) < 0 ||
1207 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
1208 			DPRINTF(("sbdsp_dma_input: SB16 DMA start failed\n"));
1209 			goto giveup;
1210 		}
1211 	} else {
1212 		if (loop) {
1213 			DPRINTF(("sbdsp_dma_input: set blocksize=%d\n", cc));
1214 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
1215 			    sbdsp_wdsp(sc, cc) < 0 ||
1216 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
1217 				DPRINTF(("sbdsp_dma_input: SB2 DMA start failed\n"));
1218 				goto giveup;
1219 			}
1220 			if (sbdsp_wdsp(sc, sc->sc_imodep->cmd) < 0) {
1221 				DPRINTF(("sbdsp_dma_input: SB2 DMA restart failed\n"));
1222 				goto giveup;
1223 			}
1224 		} else {
1225 			if (sbdsp_wdsp(sc, sc->sc_imodep->cmd) < 0 ||
1226 			    sbdsp_wdsp(sc, cc) < 0 ||
1227 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
1228 				DPRINTF(("sbdsp_dma_input: SB1 DMA start failed\n"));
1229 				goto giveup;
1230 			}
1231 		}
1232 	}
1233 	return 0;
1234 
1235 giveup:
1236 	sbdsp_reset(sc);
1237 	return EIO;
1238 
1239 badmode:
1240 	DPRINTF(("sbdsp_dma_input: can't set mode\n"));
1241 	return EIO;
1242 }
1243 
1244 int
1245 sbdsp_dma_output(addr, p, cc, intr, arg)
1246 	void *addr;
1247 	void *p;
1248 	int cc;
1249 	void (*intr) __P((void *));
1250 	void *arg;
1251 {
1252 	struct sbdsp_softc *sc = addr;
1253 	int loop = sc->sc_model != SB_1;
1254 	int stereo = sc->sc_omodep->channels == 2;
1255 	int cmd;
1256 
1257 #ifdef AUDIO_DEBUG
1258 	if (sbdspdebug > 1)
1259 		Dprintf("sbdsp_dma_output: cc=%d 0x%x (0x%x)\n", cc, intr, arg);
1260 #endif
1261 #ifdef DIAGNOSTIC
1262 	if (stereo && (cc & 1)) {
1263 		DPRINTF(("stereo playback odd bytes (%d)\n", cc));
1264 		return EIO;
1265 	}
1266 #endif
1267 
1268 	if (sc->sc_dmadir != SB_DMA_OUT) {
1269 		if (ISSBPRO(sc)) {
1270 			/* make sure we re-set stereo mixer bit when we start
1271 			   output. */
1272 			sbdsp_mix_write(sc, SBP_STEREO,
1273 			    (sbdsp_mix_read(sc, SBP_STEREO) & ~SBP_PLAYMODE_MASK) |
1274 			    (stereo ?  SBP_PLAYMODE_STEREO : SBP_PLAYMODE_MONO));
1275 			cmd = sc->sc_omodep->cmdchan;
1276 			if (cmd && sbdsp_wdsp(sc, cmd) < 0)
1277 				goto badmode;
1278 		}
1279 
1280 		if (ISSB16CLASS(sc)) {
1281 			if (sbdsp16_set_rate(sc, SB_DSP16_OUTPUTRATE,
1282 					     sc->sc_orate))
1283 				goto giveup;
1284 		} else {
1285 			if (sbdsp_set_timeconst(sc, sc->sc_otc))
1286 				goto giveup;
1287 		}
1288 
1289 		sc->sc_dmadir = SB_DMA_OUT;
1290 		sc->dmaflags = DMAMODE_WRITE;
1291 		if (loop)
1292 			sc->dmaflags |= DMAMODE_LOOP;
1293 	} else {
1294 		/* Already started; just return. */
1295 		if (loop)
1296 			return 0;
1297 	}
1298 
1299 	sc->dmaaddr = p;
1300 	sc->dmacnt = loop ? (NBPG/cc)*cc : cc;
1301 	sc->dmachan = sc->sc_omodep->precision == 16 ? sc->sc_drq16 : sc->sc_drq8;
1302 #ifdef AUDIO_DEBUG
1303 	if (sbdspdebug > 1)
1304 		Dprintf("sbdsp_dma_output: dmastart %x %p %d %d\n",
1305 			sc->dmaflags, sc->dmaaddr, sc->dmacnt, sc->dmachan);
1306 #endif
1307 	isa_dmastart(sc->sc_isa, sc->dmachan, sc->dmaaddr,
1308 	    sc->dmacnt, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
1309 	sc->sc_intr = intr;
1310 	sc->sc_arg = arg;
1311 
1312 	if ((sc->sc_model == SB_JAZZ && sc->dmachan > 3) ||
1313 	    (sc->sc_model != SB_JAZZ && sc->sc_omodep->precision == 16))
1314 		cc >>= 1;
1315 	--cc;
1316 	if (ISSB16CLASS(sc)) {
1317 		if (sbdsp_wdsp(sc, sc->sc_omodep->cmd) < 0 ||
1318 		    sbdsp_wdsp(sc, sc->sc_obmode) < 0 ||
1319 		    sbdsp16_wait(sc) ||
1320 		    sbdsp_wdsp(sc, cc) < 0 ||
1321 		    sbdsp_wdsp(sc, cc >> 8) < 0) {
1322 			DPRINTF(("sbdsp_dma_output: SB16 DMA start failed\n"));
1323 			goto giveup;
1324 		}
1325 	} else {
1326 		if (loop) {
1327 			DPRINTF(("sbdsp_dma_output: set blocksize=%d\n", cc));
1328 			if (sbdsp_wdsp(sc, SB_DSP_BLOCKSIZE) < 0 ||
1329 			    sbdsp_wdsp(sc, cc) < 0 ||
1330 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
1331 				DPRINTF(("sbdsp_dma_output: SB2 DMA blocksize failed\n"));
1332 				goto giveup;
1333 			}
1334 			if (sbdsp_wdsp(sc, sc->sc_omodep->cmd) < 0) {
1335 				DPRINTF(("sbdsp_dma_output: SB2 DMA start failed\n"));
1336 				goto giveup;
1337 			}
1338 		} else {
1339 			if (sbdsp_wdsp(sc, sc->sc_omodep->cmd) < 0 ||
1340 			    sbdsp_wdsp(sc, cc) < 0 ||
1341 			    sbdsp_wdsp(sc, cc >> 8) < 0) {
1342 				DPRINTF(("sbdsp_dma_output: SB1 DMA start failed\n"));
1343 				goto giveup;
1344 			}
1345 		}
1346 	}
1347 	return 0;
1348 
1349 giveup:
1350 	sbdsp_reset(sc);
1351 	return EIO;
1352 
1353 badmode:
1354 	DPRINTF(("sbdsp_dma_output: can't set mode\n"));
1355 	return EIO;
1356 }
1357 
1358 /*
1359  * Only the DSP unit on the sound blaster generates interrupts.
1360  * There are three cases of interrupt: reception of a midi byte
1361  * (when mode is enabled), completion of dma transmission, or
1362  * completion of a dma reception.  The three modes are mutually
1363  * exclusive so we know a priori which event has occurred.
1364  */
1365 int
1366 sbdsp_intr(arg)
1367 	void *arg;
1368 {
1369 	struct sbdsp_softc *sc = arg;
1370 	int loop = sc->sc_model != SB_1;
1371 	u_char irq;
1372 
1373 #ifdef AUDIO_DEBUG
1374 	if (sbdspdebug > 1)
1375 		Dprintf("sbdsp_intr: intr=0x%x\n", sc->sc_intr);
1376 #endif
1377 	if (ISSB16CLASS(sc)) {
1378 		irq = sbdsp_mix_read(sc, SBP_IRQ_STATUS);
1379 		if ((irq & (SBP_IRQ_DMA8 | SBP_IRQ_DMA16)) == 0)
1380 			return 0;
1381 	} else {
1382 		if (!loop && !isa_dmafinished(sc->sc_isa, sc->dmachan))
1383 			return 0;
1384 		irq = SBP_IRQ_DMA8;
1385 	}
1386 	sc->sc_interrupts++;
1387 	delay(10);		/* XXX why? */
1388 #if 0
1389 	if (sc->sc_mintr != 0) {
1390 		x = sbdsp_rdsp(sc);
1391 		(*sc->sc_mintr)(sc->sc_arg, x);
1392 	} else
1393 #endif
1394 	if (sc->sc_intr != 0) {
1395 		/* clear interrupt */
1396 		if (irq & SBP_IRQ_DMA8)
1397 			bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_IRQACK8);
1398 		if (irq & SBP_IRQ_DMA16)
1399 			bus_space_read_1(sc->sc_iot, sc->sc_ioh, SBP_DSP_IRQACK16);
1400 		if (!loop)
1401 			isa_dmadone(sc->sc_isa, sc->dmachan);
1402 		(*sc->sc_intr)(sc->sc_arg);
1403 	} else {
1404 		return 0;
1405 	}
1406 	return 1;
1407 }
1408 
1409 #if 0
1410 /*
1411  * Enter midi uart mode and arrange for read interrupts
1412  * to vector to `intr'.  This puts the card in a mode
1413  * which allows only midi I/O; the card must be reset
1414  * to leave this mode.  Unfortunately, the card does not
1415  * use transmit interrupts, so bytes must be output
1416  * using polling.  To keep the polling overhead to a
1417  * minimum, output should be driven off a timer.
1418  * This is a little tricky since only 320us separate
1419  * consecutive midi bytes.
1420  */
1421 void
1422 sbdsp_set_midi_mode(sc, intr, arg)
1423 	struct sbdsp_softc *sc;
1424 	void (*intr)();
1425 	void *arg;
1426 {
1427 
1428 	sbdsp_wdsp(sc, SB_MIDI_UART_INTR);
1429 	sc->sc_mintr = intr;
1430 	sc->sc_intr = 0;
1431 	sc->sc_arg = arg;
1432 }
1433 
1434 /*
1435  * Write a byte to the midi port, when in midi uart mode.
1436  */
1437 void
1438 sbdsp_midi_output(sc, v)
1439 	struct sbdsp_softc *sc;
1440 	int v;
1441 {
1442 
1443 	if (sbdsp_wdsp(sc, v) < 0)
1444 		++sberr.wmidi;
1445 }
1446 #endif
1447 
1448 int
1449 sbdsp_setfd(addr, flag)
1450 	void *addr;
1451 	int flag;
1452 {
1453 	/* Can't do full-duplex */
1454 	return ENOTTY;
1455 }
1456 
1457 void
1458 sbdsp_set_mixer_gain(sc, port)
1459 	struct sbdsp_softc *sc;
1460 	int port;
1461 {
1462 	int src, gain;
1463 
1464 	switch(sc->sc_mixer_model) {
1465 	case SBM_NONE:
1466 		return;
1467 	case SBM_CT1335:
1468 		gain = SB_1335_GAIN(sc->gain[port][SB_LEFT]);
1469 		switch(port) {
1470 		case SB_MASTER_VOL:
1471 			src = SBP_1335_MASTER_VOL;
1472 			break;
1473 		case SB_MIDI_VOL:
1474 			src = SBP_1335_MIDI_VOL;
1475 			break;
1476 		case SB_CD_VOL:
1477 			src = SBP_1335_CD_VOL;
1478 			break;
1479 		case SB_VOICE_VOL:
1480 			src = SBP_1335_VOICE_VOL;
1481 			gain = SB_1335_MASTER_GAIN(sc->gain[port][SB_LEFT]);
1482 			break;
1483 		default:
1484 			return;
1485 		}
1486 		sbdsp_mix_write(sc, src, gain);
1487 		break;
1488 	case SBM_CT1345:
1489 		gain = SB_STEREO_GAIN(sc->gain[port][SB_LEFT],
1490 				      sc->gain[port][SB_RIGHT]);
1491 		switch (port) {
1492 		case SB_MIC_VOL:
1493 			src = SBP_MIC_VOL;
1494 			gain = SB_MIC_GAIN(sc->gain[port][SB_LEFT]);
1495 			break;
1496 		case SB_MASTER_VOL:
1497 			src = SBP_MASTER_VOL;
1498 			break;
1499 		case SB_LINE_IN_VOL:
1500 			src = SBP_LINE_VOL;
1501 			break;
1502 		case SB_VOICE_VOL:
1503 			src = SBP_VOICE_VOL;
1504 			break;
1505 		case SB_MIDI_VOL:
1506 			src = SBP_MIDI_VOL;
1507 			break;
1508 		case SB_CD_VOL:
1509 			src = SBP_CD_VOL;
1510 			break;
1511 		default:
1512 			return;
1513 		}
1514 		sbdsp_mix_write(sc, src, gain);
1515 		break;
1516 	case SBM_CT1745:
1517 		switch (port) {
1518 		case SB_MIC_VOL:
1519 			src = SB16P_MIC_L;
1520 			break;
1521 		case SB_MASTER_VOL:
1522 			src = SB16P_MASTER_L;
1523 			break;
1524 		case SB_LINE_IN_VOL:
1525 			src = SB16P_LINE_L;
1526 			break;
1527 		case SB_VOICE_VOL:
1528 			src = SB16P_VOICE_L;
1529 			break;
1530 		case SB_MIDI_VOL:
1531 			src = SB16P_MIDI_L;
1532 			break;
1533 		case SB_CD_VOL:
1534 			src = SB16P_CD_L;
1535 			break;
1536 		case SB_INPUT_GAIN:
1537 			src = SB16P_INPUT_GAIN_L;
1538 			break;
1539 		case SB_OUTPUT_GAIN:
1540 			src = SB16P_OUTPUT_GAIN_L;
1541 			break;
1542 		case SB_TREBLE:
1543 			src = SB16P_TREBLE_L;
1544 			break;
1545 		case SB_BASS:
1546 			src = SB16P_BASS_L;
1547 			break;
1548 		case SB_PCSPEAKER:
1549 			sbdsp_mix_write(sc, SB16P_PCSPEAKER, sc->gain[port][SB_LEFT]);
1550 			return;
1551 		default:
1552 			return;
1553 		}
1554 		sbdsp_mix_write(sc, src, sc->gain[port][SB_LEFT]);
1555 		sbdsp_mix_write(sc, SB16P_L_TO_R(src), sc->gain[port][SB_RIGHT]);
1556 		break;
1557 	}
1558 }
1559 
1560 int
1561 sbdsp_mixer_set_port(addr, cp)
1562 	void *addr;
1563 	mixer_ctrl_t *cp;
1564 {
1565 	struct sbdsp_softc *sc = addr;
1566 	int lgain, rgain;
1567 
1568 	DPRINTF(("sbdsp_mixer_set_port: port=%d num_channels=%d\n", cp->dev,
1569 	    cp->un.value.num_channels));
1570 
1571 	if (sc->sc_mixer_model == SBM_NONE)
1572 		return EINVAL;
1573 
1574 	switch (cp->dev) {
1575 	case SB_TREBLE:
1576 	case SB_BASS:
1577 		if (sc->sc_mixer_model == SBM_CT1345) {
1578 			if (cp->type != AUDIO_MIXER_ENUM)
1579 				return EINVAL;
1580 			switch (cp->dev) {
1581 			case SB_TREBLE:
1582 				sbdsp_set_ifilter(addr, cp->un.ord ? SB_TREBLE : 0);
1583 				return 0;
1584 			case SB_BASS:
1585 				sbdsp_set_ifilter(addr, cp->un.ord ? SB_BASS : 0);
1586 				return 0;
1587 			}
1588 		}
1589 	case SB_PCSPEAKER:
1590 	case SB_INPUT_GAIN:
1591 	case SB_OUTPUT_GAIN:
1592 		if (sc->sc_mixer_model != SBM_CT1745)
1593 			return EINVAL;
1594 	case SB_MIC_VOL:
1595 	case SB_LINE_IN_VOL:
1596 		if (sc->sc_mixer_model == SBM_CT1335)
1597 			return EINVAL;
1598 	case SB_VOICE_VOL:
1599 	case SB_MIDI_VOL:
1600 	case SB_CD_VOL:
1601 	case SB_MASTER_VOL:
1602 		if (cp->type != AUDIO_MIXER_VALUE)
1603 			return EINVAL;
1604 
1605 		/*
1606 		 * All the mixer ports are stereo except for the microphone.
1607 		 * If we get a single-channel gain value passed in, then we
1608 		 * duplicate it to both left and right channels.
1609 		 */
1610 
1611 		switch (cp->dev) {
1612 		case SB_MIC_VOL:
1613 			if (cp->un.value.num_channels != 1)
1614 				return EINVAL;
1615 
1616 			lgain = rgain = SB_ADJUST_MIC_GAIN(sc,
1617 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1618 			break;
1619 		case SB_PCSPEAKER:
1620 			if (cp->un.value.num_channels != 1)
1621 				return EINVAL;
1622 			/* fall into */
1623 		case SB_INPUT_GAIN:
1624 		case SB_OUTPUT_GAIN:
1625 			lgain = rgain = SB_ADJUST_2_GAIN(sc,
1626 			  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1627 			break;
1628 		default:
1629 			switch (cp->un.value.num_channels) {
1630 			case 1:
1631 				lgain = rgain = SB_ADJUST_GAIN(sc,
1632 				  cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]);
1633 				break;
1634 			case 2:
1635 				if (sc->sc_mixer_model == SBM_CT1335)
1636 					return EINVAL;
1637 				lgain = SB_ADJUST_GAIN(sc,
1638 				  cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]);
1639 				rgain = SB_ADJUST_GAIN(sc,
1640 				  cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]);
1641 				break;
1642 			default:
1643 				return EINVAL;
1644 			}
1645 			break;
1646 		}
1647 		sc->gain[cp->dev][SB_LEFT]  = lgain;
1648 		sc->gain[cp->dev][SB_RIGHT] = rgain;
1649 
1650 		sbdsp_set_mixer_gain(sc, cp->dev);
1651 		break;
1652 
1653 	case SB_RECORD_SOURCE:
1654 		if (sc->sc_mixer_model == SBM_CT1745) {
1655 			if (cp->type != AUDIO_MIXER_SET)
1656 				return EINVAL;
1657 			return sbdsp_set_in_ports(sc, cp->un.mask);
1658 		} else {
1659 			if (cp->type != AUDIO_MIXER_ENUM)
1660 				return EINVAL;
1661 			return sbdsp_set_in_port(sc, cp->un.ord);
1662 		}
1663 		break;
1664 
1665 	case SB_AGC:
1666 		if (sc->sc_mixer_model != SBM_CT1745 || cp->type != AUDIO_MIXER_ENUM)
1667 			return EINVAL;
1668 		sbdsp_mix_write(sc, SB16P_AGC, cp->un.ord & 1);
1669 		break;
1670 
1671 	default:
1672 		return EINVAL;
1673 	}
1674 
1675 	return 0;
1676 }
1677 
1678 int
1679 sbdsp_mixer_get_port(addr, cp)
1680 	void *addr;
1681 	mixer_ctrl_t *cp;
1682 {
1683 	struct sbdsp_softc *sc = addr;
1684 
1685 	DPRINTF(("sbdsp_mixer_get_port: port=%d\n", cp->dev));
1686 
1687 	if (sc->sc_mixer_model == SBM_NONE)
1688 		return EINVAL;
1689 
1690 	switch (cp->dev) {
1691 	case SB_TREBLE:
1692 	case SB_BASS:
1693 		if (sc->sc_mixer_model == SBM_CT1345) {
1694 			switch (cp->dev) {
1695 			case SB_TREBLE:
1696 				cp->un.ord = sbdsp_get_ifilter(addr) == SB_TREBLE;
1697 				return 0;
1698 			case SB_BASS:
1699 				cp->un.ord = sbdsp_get_ifilter(addr) == SB_BASS;
1700 				return 0;
1701 			}
1702 		}
1703 	case SB_PCSPEAKER:
1704 	case SB_INPUT_GAIN:
1705 	case SB_OUTPUT_GAIN:
1706 		if (sc->sc_mixer_model != SBM_CT1745)
1707 			return EINVAL;
1708 	case SB_MIC_VOL:
1709 	case SB_LINE_IN_VOL:
1710 		if (sc->sc_mixer_model == SBM_CT1335)
1711 			return EINVAL;
1712 	case SB_VOICE_VOL:
1713 	case SB_MIDI_VOL:
1714 	case SB_CD_VOL:
1715 	case SB_MASTER_VOL:
1716 		switch (cp->dev) {
1717 		case SB_MIC_VOL:
1718 		case SB_PCSPEAKER:
1719 			if (cp->un.value.num_channels != 1)
1720 				return EINVAL;
1721 			/* fall into */
1722 		default:
1723 			switch (cp->un.value.num_channels) {
1724 			case 1:
1725 				cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1726 					sc->gain[cp->dev][SB_LEFT];
1727 				break;
1728 			case 2:
1729 				cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
1730 					sc->gain[cp->dev][SB_LEFT];
1731 				cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
1732 					sc->gain[cp->dev][SB_RIGHT];
1733 				break;
1734 			default:
1735 				return EINVAL;
1736 			}
1737 			break;
1738 		}
1739 		break;
1740 
1741 	case SB_RECORD_SOURCE:
1742 		if (sc->sc_mixer_model == SBM_CT1745)
1743 			cp->un.mask = sc->in_mask;
1744 		else
1745 			cp->un.ord = sc->in_port;
1746 		break;
1747 
1748 	case SB_AGC:
1749 		if (sc->sc_mixer_model != SBM_CT1745)
1750 			return EINVAL;
1751 		cp->un.ord = sbdsp_mix_read(sc, SB16P_AGC);
1752 		break;
1753 
1754 	default:
1755 		return EINVAL;
1756 	}
1757 
1758 	return 0;
1759 }
1760 
1761 int
1762 sbdsp_mixer_query_devinfo(addr, dip)
1763 	void *addr;
1764 	mixer_devinfo_t *dip;
1765 {
1766 	struct sbdsp_softc *sc = addr;
1767 	int chan, class;
1768 
1769 	DPRINTF(("sbdsp_mixer_query_devinfo: model=%d index=%d\n",
1770 		 sc->sc_mixer_model, dip->index));
1771 
1772 	if (sc->sc_mixer_model == SBM_NONE)
1773 		return ENXIO;
1774 
1775 	chan = sc->sc_mixer_model == SBM_CT1335 ? 1 : 2;
1776 	class = sc->sc_mixer_model == SBM_CT1745 ? SB_INPUT_CLASS : SB_OUTPUT_CLASS;
1777 
1778 	switch (dip->index) {
1779 	case SB_MASTER_VOL:
1780 		dip->type = AUDIO_MIXER_VALUE;
1781 		dip->mixer_class = SB_OUTPUT_CLASS;
1782 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1783 		strcpy(dip->label.name, AudioNmaster);
1784 		dip->un.v.num_channels = chan;
1785 		strcpy(dip->un.v.units.name, AudioNvolume);
1786 		return 0;
1787 	case SB_MIDI_VOL:
1788 		dip->type = AUDIO_MIXER_VALUE;
1789 		dip->mixer_class = class;
1790 		dip->prev = AUDIO_MIXER_LAST;
1791 		dip->next = AUDIO_MIXER_LAST;
1792 		strcpy(dip->label.name, AudioNfmsynth);
1793 		dip->un.v.num_channels = chan;
1794 		strcpy(dip->un.v.units.name, AudioNvolume);
1795 		return 0;
1796 	case SB_CD_VOL:
1797 		dip->type = AUDIO_MIXER_VALUE;
1798 		dip->mixer_class = class;
1799 		dip->prev = AUDIO_MIXER_LAST;
1800 		dip->next = AUDIO_MIXER_LAST;
1801 		strcpy(dip->label.name, AudioNcd);
1802 		dip->un.v.num_channels = chan;
1803 		strcpy(dip->un.v.units.name, AudioNvolume);
1804 		return 0;
1805 	case SB_VOICE_VOL:
1806 		dip->type = AUDIO_MIXER_VALUE;
1807 		dip->mixer_class = class;
1808 		dip->prev = AUDIO_MIXER_LAST;
1809 		dip->next = AUDIO_MIXER_LAST;
1810 		strcpy(dip->label.name, AudioNdac);
1811 		dip->un.v.num_channels = chan;
1812 		strcpy(dip->un.v.units.name, AudioNvolume);
1813 		return 0;
1814 	case SB_OUTPUT_CLASS:
1815 		dip->type = AUDIO_MIXER_CLASS;
1816 		dip->mixer_class = SB_OUTPUT_CLASS;
1817 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1818 		strcpy(dip->label.name, AudioCOutputs);
1819 		return 0;
1820 	}
1821 
1822 	if (sc->sc_mixer_model == SBM_CT1335)
1823 		return ENXIO;
1824 
1825 	switch (dip->index) {
1826 	case SB_MIC_VOL:
1827 		dip->type = AUDIO_MIXER_VALUE;
1828 		dip->mixer_class = class;
1829 		dip->prev = AUDIO_MIXER_LAST;
1830 		dip->next = AUDIO_MIXER_LAST;
1831 		strcpy(dip->label.name, AudioNmicrophone);
1832 		dip->un.v.num_channels = 1;
1833 		strcpy(dip->un.v.units.name, AudioNvolume);
1834 		return 0;
1835 
1836 	case SB_LINE_IN_VOL:
1837 		dip->type = AUDIO_MIXER_VALUE;
1838 		dip->mixer_class = class;
1839 		dip->prev = AUDIO_MIXER_LAST;
1840 		dip->next = AUDIO_MIXER_LAST;
1841 		strcpy(dip->label.name, AudioNline);
1842 		dip->un.v.num_channels = 2;
1843 		strcpy(dip->un.v.units.name, AudioNvolume);
1844 		return 0;
1845 
1846 	case SB_RECORD_SOURCE:
1847 		dip->mixer_class = SB_RECORD_CLASS;
1848 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1849 		strcpy(dip->label.name, AudioNsource);
1850 		if (sc->sc_mixer_model == SBM_CT1745) {
1851 			dip->type = AUDIO_MIXER_SET;
1852 			dip->un.s.num_mem = 4;
1853 			strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
1854 			dip->un.s.member[0].mask = 1 << SB_MIC_VOL;
1855 			strcpy(dip->un.s.member[1].label.name, AudioNcd);
1856 			dip->un.s.member[1].mask = 1 << SB_CD_VOL;
1857 			strcpy(dip->un.s.member[2].label.name, AudioNline);
1858 			dip->un.s.member[2].mask = 1 << SB_LINE_IN_VOL;
1859 			strcpy(dip->un.s.member[3].label.name, AudioNfmsynth);
1860 			dip->un.s.member[3].mask = 1 << SB_MIDI_VOL;
1861 		} else {
1862 			dip->type = AUDIO_MIXER_ENUM;
1863 			dip->un.e.num_mem = 3;
1864 			strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
1865 			dip->un.e.member[0].ord = SB_MIC_VOL;
1866 			strcpy(dip->un.e.member[1].label.name, AudioNcd);
1867 			dip->un.e.member[1].ord = SB_CD_VOL;
1868 			strcpy(dip->un.e.member[2].label.name, AudioNline);
1869 			dip->un.e.member[2].ord = SB_LINE_IN_VOL;
1870 		}
1871 		return 0;
1872 
1873 	case SB_BASS:
1874 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1875 		strcpy(dip->label.name, AudioNbass);
1876 		if (sc->sc_mixer_model == SBM_CT1745) {
1877 			dip->type = AUDIO_MIXER_VALUE;
1878 			dip->mixer_class = SB_EQUALIZATION_CLASS;
1879 			dip->un.v.num_channels = 2;
1880 			strcpy(dip->un.v.units.name, AudioNbass);
1881 		} else {
1882 			dip->type = AUDIO_MIXER_ENUM;
1883 			dip->mixer_class = SB_INPUT_CLASS;
1884 			dip->un.e.num_mem = 2;
1885 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
1886 			dip->un.e.member[0].ord = 0;
1887 			strcpy(dip->un.e.member[1].label.name, AudioNon);
1888 			dip->un.e.member[1].ord = 1;
1889 		}
1890 		return 0;
1891 
1892 	case SB_TREBLE:
1893 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1894 		strcpy(dip->label.name, AudioNtreble);
1895 		if (sc->sc_mixer_model == SBM_CT1745) {
1896 			dip->type = AUDIO_MIXER_VALUE;
1897 			dip->mixer_class = SB_EQUALIZATION_CLASS;
1898 			dip->un.v.num_channels = 2;
1899 			strcpy(dip->un.v.units.name, AudioNtreble);
1900 		} else {
1901 			dip->type = AUDIO_MIXER_ENUM;
1902 			dip->mixer_class = SB_INPUT_CLASS;
1903 			dip->un.e.num_mem = 2;
1904 			strcpy(dip->un.e.member[0].label.name, AudioNoff);
1905 			dip->un.e.member[0].ord = 0;
1906 			strcpy(dip->un.e.member[1].label.name, AudioNon);
1907 			dip->un.e.member[1].ord = 1;
1908 		}
1909 		return 0;
1910 
1911 	case SB_RECORD_CLASS:			/* record source class */
1912 		dip->type = AUDIO_MIXER_CLASS;
1913 		dip->mixer_class = SB_RECORD_CLASS;
1914 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1915 		strcpy(dip->label.name, AudioCRecord);
1916 		return 0;
1917 
1918 	}
1919 
1920 	if (sc->sc_mixer_model == SBM_CT1345)
1921 		return ENXIO;
1922 
1923 	switch(dip->index) {
1924 	case SB_PCSPEAKER:
1925 		dip->type = AUDIO_MIXER_VALUE;
1926 		dip->mixer_class = SB_INPUT_CLASS;
1927 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1928 		strcpy(dip->label.name, "pc_speaker");
1929 		dip->un.v.num_channels = 1;
1930 		strcpy(dip->un.v.units.name, AudioNvolume);
1931 		return 0;
1932 
1933 	case SB_INPUT_GAIN:
1934 		dip->type = AUDIO_MIXER_VALUE;
1935 		dip->mixer_class = SB_INPUT_CLASS;
1936 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1937 		strcpy(dip->label.name, AudioNinput);
1938 		dip->un.v.num_channels = 2;
1939 		strcpy(dip->un.v.units.name, AudioNvolume);
1940 		return 0;
1941 
1942 	case SB_OUTPUT_GAIN:
1943 		dip->type = AUDIO_MIXER_VALUE;
1944 		dip->mixer_class = SB_OUTPUT_CLASS;
1945 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1946 		strcpy(dip->label.name, AudioNoutput);
1947 		dip->un.v.num_channels = 2;
1948 		strcpy(dip->un.v.units.name, AudioNvolume);
1949 		return 0;
1950 
1951 	case SB_AGC:
1952 		dip->type = AUDIO_MIXER_ENUM;
1953 		dip->mixer_class = SB_INPUT_CLASS;
1954 		dip->prev = dip->next = AUDIO_MIXER_LAST;
1955 		strcpy(dip->label.name, "AGC");
1956 		dip->un.e.num_mem = 2;
1957 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
1958 		dip->un.e.member[0].ord = 0;
1959 		strcpy(dip->un.e.member[1].label.name, AudioNon);
1960 		dip->un.e.member[1].ord = 1;
1961 		return 0;
1962 
1963 	case SB_INPUT_CLASS:
1964 		dip->type = AUDIO_MIXER_CLASS;
1965 		dip->mixer_class = SB_INPUT_CLASS;
1966 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1967 		strcpy(dip->label.name, AudioCInputs);
1968 		return 0;
1969 
1970 	case SB_EQUALIZATION_CLASS:
1971 		dip->type = AUDIO_MIXER_CLASS;
1972 		dip->mixer_class = SB_EQUALIZATION_CLASS;
1973 		dip->next = dip->prev = AUDIO_MIXER_LAST;
1974 		strcpy(dip->label.name, AudioCEqualization);
1975 		return 0;
1976 	}
1977 
1978 	return ENXIO;
1979 }
1980