xref: /netbsd/sys/dev/ic/ad1848.c (revision bf9ec67e)
1 /*	$NetBSD: ad1848.c,v 1.14 2002/03/23 23:40:32 itohy Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Ken Hornstein and John Kohl.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*
39  * Copyright (c) 1994 John Brezak
40  * Copyright (c) 1991-1993 Regents of the University of California.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the Computer Systems
54  *	Engineering Group at Lawrence Berkeley Laboratory.
55  * 4. Neither the name of the University nor of the Laboratory may be used
56  *    to endorse or promote products derived from this software without
57  *    specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  */
72 
73 /*
74  * Copyright by Hannu Savolainen 1994
75  *
76  * Redistribution and use in source and binary forms, with or without
77  * modification, are permitted provided that the following conditions are
78  * met: 1. Redistributions of source code must retain the above copyright
79  * notice, this list of conditions and the following disclaimer. 2.
80  * Redistributions in binary form must reproduce the above copyright notice,
81  * this list of conditions and the following disclaimer in the documentation
82  * and/or other materials provided with the distribution.
83  *
84  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
85  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
86  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
87  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
88  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
90  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
91  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94  * SUCH DAMAGE.
95  *
96  */
97 /*
98  * Portions of this code are from the VOXware support for the ad1848
99  * by Hannu Savolainen <hannu@voxware.pp.fi>
100  *
101  * Portions also supplied from the SoundBlaster driver for NetBSD.
102  */
103 
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: ad1848.c,v 1.14 2002/03/23 23:40:32 itohy Exp $");
106 
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/errno.h>
110 #include <sys/ioctl.h>
111 #include <sys/device.h>
112 #include <sys/fcntl.h>
113 /*#include <sys/syslog.h>*/
114 /*#include <sys/proc.h>*/
115 
116 #include <machine/cpu.h>
117 #include <machine/bus.h>
118 
119 #include <sys/audioio.h>
120 
121 #include <dev/audio_if.h>
122 #include <dev/auconv.h>
123 
124 #include <dev/ic/ad1848reg.h>
125 #include <dev/ic/cs4231reg.h>
126 #include <dev/ic/cs4237reg.h>
127 #include <dev/ic/ad1848var.h>
128 #if 0
129 #include <dev/isa/cs4231var.h>
130 #endif
131 
132 /*
133  * AD1845 on some machines don't match the AD1845 doc
134  * and defining AD1845_HACK to 1 works around the problems.
135  * options AD1845_HACK=0  should work if you have ``correct'' one.
136  */
137 #ifndef AD1845_HACK
138 #define AD1845_HACK	1	/* weird mixer, can't play slinear_be */
139 #endif
140 
141 #ifdef AUDIO_DEBUG
142 #define DPRINTF(x)	if (ad1848debug) printf x
143 int	ad1848debug = 0;
144 #else
145 #define DPRINTF(x)
146 #endif
147 
148 /*
149  * Initial values for the indirect registers of CS4248/AD1848.
150  */
151 static const int ad1848_init_values[] = {
152     GAIN_12|INPUT_MIC_GAIN_ENABLE,	/* Left Input Control */
153     GAIN_12|INPUT_MIC_GAIN_ENABLE,	/* Right Input Control */
154     ATTEN_12,				/* Left Aux #1 Input Control */
155     ATTEN_12,				/* Right Aux #1 Input Control */
156     ATTEN_12,				/* Left Aux #2 Input Control */
157     ATTEN_12,				/* Right Aux #2 Input Control */
158     /* bits 5-0 are attenuation select */
159     ATTEN_12,				/* Left DAC output Control */
160     ATTEN_12,				/* Right DAC output Control */
161     CLOCK_XTAL1|FMT_PCM8,		/* Clock and Data Format */
162     SINGLE_DMA|AUTO_CAL_ENABLE,		/* Interface Config */
163     INTERRUPT_ENABLE,			/* Pin control */
164     0x00,				/* Test and Init */
165     MODE2,				/* Misc control */
166     ATTEN_0<<2,				/* Digital Mix Control */
167     0,					/* Upper base Count */
168     0,					/* Lower base Count */
169 
170     /* These are for CS4231 &c. only (additional registers): */
171     0,					/* Alt feature 1 */
172     0,					/* Alt feature 2 */
173     ATTEN_12,				/* Left line in */
174     ATTEN_12,				/* Right line in */
175     0,					/* Timer low */
176     0,					/* Timer high */
177     0,					/* unused */
178     0,					/* unused */
179     0,					/* IRQ status */
180     0,					/* unused */
181 			/* Mono input (a.k.a speaker) (mic) Control */
182     MONO_INPUT_MUTE|ATTEN_6,		/* mute speaker by default */
183     0,					/* unused */
184     0,					/* record format */
185     0,					/* Crystal Clock Select */
186     0,					/* upper record count */
187     0					/* lower record count */
188 };
189 
190 
191 __inline int ad_read __P((struct ad1848_softc *, int));
192 __inline void ad_write __P((struct ad1848_softc *, int, int));
193 static void ad_set_MCE __P((struct ad1848_softc *, int));
194 static void wait_for_calibration __P((struct ad1848_softc *));
195 
196 
197 int
198 ad1848_to_vol(cp, vol)
199 	mixer_ctrl_t *cp;
200 	struct ad1848_volume *vol;
201 {
202 	if (cp->un.value.num_channels == 1) {
203 		vol->left =
204 		vol->right = cp->un.value.level[AUDIO_MIXER_LEVEL_MONO];
205 		return(1);
206 	}
207 	else if (cp->un.value.num_channels == 2) {
208 		vol->left  = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
209 		vol->right = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
210 		return(1);
211 	}
212 	return(0);
213 }
214 
215 int
216 ad1848_from_vol(cp, vol)
217 	mixer_ctrl_t *cp;
218 	struct ad1848_volume *vol;
219 {
220 	if (cp->un.value.num_channels == 1) {
221 		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] = vol->left;
222 		return(1);
223 	}
224 	else if (cp->un.value.num_channels == 2) {
225 		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = vol->left;
226 		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = vol->right;
227 		return(1);
228 	}
229 	return(0);
230 }
231 
232 
233 __inline int
234 ad_read(sc, reg)
235 	struct ad1848_softc *sc;
236 	int reg;
237 {
238 	int x;
239 
240 	ADWRITE(sc, AD1848_IADDR, (reg & 0xff) | sc->MCE_bit);
241 	x = ADREAD(sc, AD1848_IDATA);
242 	/*  printf("(%02x<-%02x) ", reg|sc->MCE_bit, x); */
243 	return x;
244 }
245 
246 __inline void
247 ad_write(sc, reg, data)
248 	struct ad1848_softc *sc;
249 	int reg;
250 	int data;
251 {
252 	ADWRITE(sc, AD1848_IADDR, (reg & 0xff) | sc->MCE_bit);
253 	ADWRITE(sc, AD1848_IDATA, data & 0xff);
254 	/* printf("(%02x->%02x) ", reg|sc->MCE_bit, data); */
255 }
256 
257 /*
258  * extended registers (mode 3) require an additional level of
259  * indirection through CS_XREG (I23).
260  */
261 
262 __inline int
263 ad_xread(sc, reg)
264 	struct ad1848_softc *sc;
265 	int reg;
266 {
267 	int x;
268 
269 	ADWRITE(sc, AD1848_IADDR, CS_XREG | sc->MCE_bit);
270 	ADWRITE(sc, AD1848_IDATA, (reg | ALT_F3_XRAE) & 0xff);
271 	x = ADREAD(sc, AD1848_IDATA);
272 
273 	return x;
274 }
275 
276 __inline void
277 ad_xwrite(sc, reg, val)
278 	struct ad1848_softc *sc;
279 	int reg, val;
280 {
281 	ADWRITE(sc, AD1848_IADDR, CS_XREG | sc->MCE_bit);
282 	ADWRITE(sc, AD1848_IDATA, (reg | ALT_F3_XRAE) & 0xff);
283 	ADWRITE(sc, AD1848_IDATA, val & 0xff);
284 }
285 
286 static void
287 ad_set_MCE(sc, state)
288 	struct ad1848_softc *sc;
289 	int state;
290 {
291 	if (state)
292 		sc->MCE_bit = MODE_CHANGE_ENABLE;
293 	else
294 		sc->MCE_bit = 0;
295 	ADWRITE(sc, AD1848_IADDR, sc->MCE_bit);
296 }
297 
298 static void
299 wait_for_calibration(sc)
300 	struct ad1848_softc *sc;
301 {
302 	int timeout;
303 
304 	DPRINTF(("ad1848: Auto calibration started.\n"));
305 	/*
306 	 * Wait until the auto calibration process has finished.
307 	 *
308 	 * 1) Wait until the chip becomes ready (reads don't return 0x80).
309 	 * 2) Wait until the ACI bit of I11 gets on and then off.
310 	 *    Because newer chips are fast we may never see the ACI
311 	 *    bit go on.  Just delay a little instead.
312 	 */
313 	timeout = 10000;
314 	while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT) {
315 		delay(10);
316 		timeout--;
317 	}
318 	if (timeout <= 0)
319 		DPRINTF(("ad1848: Auto calibration timed out(1).\n"));
320 
321 	/* Set register addr */
322 	ADWRITE(sc, AD1848_IADDR, SP_TEST_AND_INIT);
323 	/* Wait for address to appear when read back. */
324 	timeout = 100000;
325 	while (timeout > 0 &&
326 	       (ADREAD(sc, AD1848_IADDR)&SP_IADDR_MASK) != SP_TEST_AND_INIT) {
327 		delay(10);
328 		timeout--;
329 	}
330 	if (timeout <= 0)
331 		DPRINTF(("ad1848: Auto calibration timed out(1.5).\n"));
332 
333 	if (!(ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG)) {
334 		if (sc->mode > 1) {
335 			/* A new chip, just delay a little. */
336 			delay(100);         /* XXX what should it be? */
337 		} else {
338 			timeout = 10000;
339 			while (timeout > 0 &&
340 			       !(ad_read(sc, SP_TEST_AND_INIT) &
341 				 AUTO_CAL_IN_PROG)) {
342 				delay(10);
343 				timeout--;
344 			}
345 			if (timeout <= 0)
346 				DPRINTF(("ad1848: Auto calibration timed out(2).\n"));
347 		}
348 	}
349 
350 	timeout = 10000;
351 	while (timeout > 0 &&
352 	       ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG) {
353 		delay(10);
354 		timeout--;
355 	}
356 	if (timeout <= 0)
357 		DPRINTF(("ad1848: Auto calibration timed out(3).\n"));
358 }
359 
360 #ifdef AUDIO_DEBUG
361 void
362 ad1848_dump_regs(sc)
363 	struct ad1848_softc *sc;
364 {
365 	int i;
366 	u_char r;
367 
368 	printf("ad1848 status=%02x", ADREAD(sc, AD1848_STATUS));
369 	printf(" regs: ");
370 	for (i = 0; i < 16; i++) {
371 		r = ad_read(sc, i);
372 		printf("%02x ", r);
373 	}
374 	if (sc->mode >= 2) {
375 		for (i = 16; i < 32; i++) {
376 			r = ad_read(sc, i);
377 			printf("%02x ", r);
378 		}
379 	}
380 	printf("\n");
381 }
382 #endif
383 
384 
385 /*
386  * Attach hardware to driver, attach hardware driver to audio
387  * pseudo-device driver .
388  */
389 void
390 ad1848_attach(sc)
391 	struct ad1848_softc *sc;
392 {
393 	int i;
394 	static struct ad1848_volume vol_mid = {220, 220};
395 	static struct ad1848_volume vol_0   = {0, 0};
396 	struct audio_params pparams, rparams;
397 	int timeout;
398 
399 	/* Initialize the ad1848... */
400 	for (i = 0; i < 0x10; i++) {
401 		ad_write(sc, i, ad1848_init_values[i]);
402 		timeout = 100000;
403 		while (timeout > 0 && ADREAD(sc, AD1848_IADDR) & SP_IN_INIT)
404 			timeout--;
405 	}
406 	/* ...and additional CS4231 stuff too */
407 	if (sc->mode >= 2) {
408 		ad_write(sc, SP_INTERFACE_CONFIG, 0); /* disable SINGLE_DMA */
409 		for (i = 0x10; i < 0x20; i++)
410 			if (ad1848_init_values[i] != 0) {
411 				ad_write(sc, i, ad1848_init_values[i]);
412 				timeout = 100000;
413 				while (timeout > 0 &&
414 				       ADREAD(sc, AD1848_IADDR) & SP_IN_INIT)
415 					timeout--;
416 			}
417 	}
418 	ad1848_reset(sc);
419 
420 	pparams = audio_default;
421 	rparams = audio_default;
422 	ad1848_set_params(sc, AUMODE_RECORD|AUMODE_PLAY, 0, &pparams, &rparams);
423 
424 	/* Set default gains */
425 	ad1848_set_rec_gain(sc, &vol_mid);
426 	ad1848_set_channel_gain(sc, AD1848_DAC_CHANNEL, &vol_mid);
427 	ad1848_set_channel_gain(sc, AD1848_MONITOR_CHANNEL, &vol_0);
428 	ad1848_set_channel_gain(sc, AD1848_AUX1_CHANNEL, &vol_mid);	/* CD volume */
429 	sc->mute[AD1848_MONITOR_CHANNEL] = MUTE_ALL;
430 	if (sc->mode >= 2
431 #if AD1845_HACK
432 	    && sc->is_ad1845 == 0
433 #endif
434 		) {
435 		ad1848_set_channel_gain(sc, AD1848_AUX2_CHANNEL, &vol_mid); /* CD volume */
436 		ad1848_set_channel_gain(sc, AD1848_LINE_CHANNEL, &vol_mid);
437 		ad1848_set_channel_gain(sc, AD1848_MONO_CHANNEL, &vol_0);
438 		sc->mute[AD1848_MONO_CHANNEL] = MUTE_ALL;
439 	} else
440 		ad1848_set_channel_gain(sc, AD1848_AUX2_CHANNEL, &vol_0);
441 
442 	/* Set default port */
443 	ad1848_set_rec_port(sc, MIC_IN_PORT);
444 
445 	printf(": %s", sc->chip_name);
446 }
447 
448 /*
449  * Various routines to interface to higher level audio driver
450  */
451 const struct ad1848_mixerinfo {
452 	int  left_reg;
453 	int  right_reg;
454 	int  atten_bits;
455 	int  atten_mask;
456 } mixer_channel_info[] =
457 {
458   { SP_LEFT_AUX2_CONTROL, SP_RIGHT_AUX2_CONTROL, AUX_INPUT_ATTEN_BITS,
459     AUX_INPUT_ATTEN_MASK },
460   { SP_LEFT_AUX1_CONTROL, SP_RIGHT_AUX1_CONTROL, AUX_INPUT_ATTEN_BITS,
461     AUX_INPUT_ATTEN_MASK },
462   { SP_LEFT_OUTPUT_CONTROL, SP_RIGHT_OUTPUT_CONTROL,
463     OUTPUT_ATTEN_BITS, OUTPUT_ATTEN_MASK },
464   { CS_LEFT_LINE_CONTROL, CS_RIGHT_LINE_CONTROL, LINE_INPUT_ATTEN_BITS,
465     LINE_INPUT_ATTEN_MASK },
466   { CS_MONO_IO_CONTROL, 0, MONO_INPUT_ATTEN_BITS, MONO_INPUT_ATTEN_MASK },
467   { SP_DIGITAL_MIX, 0, OUTPUT_ATTEN_BITS, MIX_ATTEN_MASK }
468 };
469 
470 /*
471  *  This function doesn't set the mute flags but does use them.
472  *  The mute flags reflect the mutes that have been applied by the user.
473  *  However, the driver occasionally wants to mute devices (e.g. when chaing
474  *  sampling rate). These operations should not affect the mute flags.
475  */
476 
477 void
478 ad1848_mute_channel(sc, device, mute)
479 	struct ad1848_softc *sc;
480 	int device;
481 	int mute;
482 {
483 	u_char reg;
484 
485 	reg = ad_read(sc, mixer_channel_info[device].left_reg);
486 
487 	if (mute & MUTE_LEFT) {
488 		if (device == AD1848_MONITOR_CHANNEL) {
489 			if (sc->open_mode & FREAD)
490 				ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 0);
491 			ad_write(sc, mixer_channel_info[device].left_reg,
492 				 reg & ~DIGITAL_MIX1_ENABLE);
493 		} else
494 			ad_write(sc, mixer_channel_info[device].left_reg,
495 				 reg | 0x80);
496 	} else if (!(sc->mute[device] & MUTE_LEFT)) {
497 		if (device == AD1848_MONITOR_CHANNEL) {
498 			ad_write(sc, mixer_channel_info[device].left_reg,
499 				 reg | DIGITAL_MIX1_ENABLE);
500 			if (sc->open_mode & FREAD)
501 				ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 1);
502 		} else
503 			ad_write(sc, mixer_channel_info[device].left_reg,
504 				 reg & ~0x80);
505 	}
506 
507 	if (!mixer_channel_info[device].right_reg)
508 		return;
509 
510 	reg = ad_read(sc, mixer_channel_info[device].right_reg);
511 
512 	if (mute & MUTE_RIGHT) {
513 		ad_write(sc, mixer_channel_info[device].right_reg, reg | 0x80);
514 	} else if (!(sc->mute[device] & MUTE_RIGHT)) {
515 		ad_write(sc, mixer_channel_info[device].right_reg, reg &~0x80);
516 	}
517 }
518 
519 
520 int
521 ad1848_set_channel_gain(sc, device, gp)
522 	struct ad1848_softc *sc;
523 	int device;
524 	struct ad1848_volume *gp;
525 {
526 	const struct ad1848_mixerinfo *info = &mixer_channel_info[device];
527 	u_char reg;
528 	u_int atten;
529 
530 	sc->gains[device] = *gp;
531 
532 	atten = (AUDIO_MAX_GAIN - gp->left) * (info->atten_bits + 1) /
533 		(AUDIO_MAX_GAIN + 1);
534 
535 	reg = ad_read(sc, info->left_reg) & (info->atten_mask);
536 	if (device == AD1848_MONITOR_CHANNEL)
537 		reg |= ((atten & info->atten_bits) << 2);
538 	else
539 		reg |= ((atten & info->atten_bits));
540 
541 	ad_write(sc, info->left_reg, reg);
542 
543 	if (!info->right_reg)
544 		return (0);
545 
546 	atten = (AUDIO_MAX_GAIN - gp->right) * (info->atten_bits + 1) /
547 		(AUDIO_MAX_GAIN + 1);
548 	reg = ad_read(sc, info->right_reg);
549 	reg &= info->atten_mask;
550 	ad_write(sc, info->right_reg, (atten & info->atten_bits) | reg);
551 
552 	return(0);
553 }
554 
555 
556 int
557 ad1848_get_device_gain(sc, device, gp)
558 	struct ad1848_softc *sc;
559 	int device;
560 	struct ad1848_volume *gp;
561 {
562 	*gp = sc->gains[device];
563 	return(0);
564 }
565 
566 int
567 ad1848_get_rec_gain(sc, gp)
568 	struct ad1848_softc *sc;
569 	struct ad1848_volume *gp;
570 {
571 	*gp = sc->rec_gain;
572 	return(0);
573 }
574 
575 int
576 ad1848_set_rec_gain(sc, gp)
577 	struct ad1848_softc *sc;
578 	struct ad1848_volume *gp;
579 {
580 	u_char reg, gain;
581 
582 	DPRINTF(("ad1848_set_rec_gain: %d:%d\n", gp->left, gp->right));
583 
584 	sc->rec_gain = *gp;
585 
586 	gain = (gp->left * (GAIN_22_5 + 1)) / (AUDIO_MAX_GAIN + 1);
587 	reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
588 	reg &= INPUT_GAIN_MASK;
589 	ad_write(sc, SP_LEFT_INPUT_CONTROL, (gain & 0x0f) | reg);
590 
591 	gain = (gp->right * (GAIN_22_5 + 1)) / (AUDIO_MAX_GAIN + 1);
592 	reg = ad_read(sc, SP_RIGHT_INPUT_CONTROL);
593 	reg &= INPUT_GAIN_MASK;
594 	ad_write(sc, SP_RIGHT_INPUT_CONTROL, (gain & 0x0f) | reg);
595 
596 	return(0);
597 }
598 
599 
600 void
601 ad1848_mute_wave_output(sc, mute, set)
602 	struct ad1848_softc *sc;
603 	int mute, set;
604 {
605 	int m;
606 
607 	DPRINTF(("ad1848_mute_wave_output: %d, %d\n", mute, set));
608 
609 	if (mute == WAVE_MUTE2_INIT) {
610 		sc->wave_mute_status = 0;
611 		mute = WAVE_MUTE2;
612 	}
613 	if (set)
614 		m = sc->wave_mute_status |= mute;
615 	else
616 		m = sc->wave_mute_status &= ~mute;
617 
618 	if (m & WAVE_MUTE0 || ((m & WAVE_UNMUTE1) == 0 && m & WAVE_MUTE2))
619 		ad1848_mute_channel(sc, AD1848_DAC_CHANNEL, MUTE_ALL);
620 	else
621 		ad1848_mute_channel(sc, AD1848_DAC_CHANNEL,
622 					    sc->mute[AD1848_DAC_CHANNEL]);
623 }
624 
625 int
626 ad1848_set_mic_gain(sc, gp)
627 	struct ad1848_softc *sc;
628 	struct ad1848_volume *gp;
629 {
630 	u_char reg;
631 
632 	DPRINTF(("cs4231_set_mic_gain: %d\n", gp->left));
633 
634 	if (gp->left > AUDIO_MAX_GAIN/2) {
635 		sc->mic_gain_on = 1;
636 		reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
637 		ad_write(sc, SP_LEFT_INPUT_CONTROL,
638 			 reg | INPUT_MIC_GAIN_ENABLE);
639 	} else {
640 		sc->mic_gain_on = 0;
641 		reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
642 		ad_write(sc, SP_LEFT_INPUT_CONTROL,
643 			 reg & ~INPUT_MIC_GAIN_ENABLE);
644 	}
645 
646 	return(0);
647 }
648 
649 int
650 ad1848_get_mic_gain(sc, gp)
651 	struct ad1848_softc *sc;
652 	struct ad1848_volume *gp;
653 {
654 	if (sc->mic_gain_on)
655 		gp->left = gp->right = AUDIO_MAX_GAIN;
656 	else
657 		gp->left = gp->right = AUDIO_MIN_GAIN;
658 	return(0);
659 }
660 
661 
662 static ad1848_devmap_t *
663 	ad1848_mixer_find_dev __P((ad1848_devmap_t *, int, mixer_ctrl_t *));
664 
665 static ad1848_devmap_t *
666 ad1848_mixer_find_dev(map, cnt, cp)
667 	ad1848_devmap_t *map;
668 	int cnt;
669 	mixer_ctrl_t *cp;
670 {
671 	int i;
672 
673 	for (i = 0; i < cnt; i++) {
674 		if (map[i].id == cp->dev) {
675 			return (&map[i]);
676 		}
677 	}
678 	return (0);
679 }
680 
681 int
682 ad1848_mixer_get_port(ac, map, cnt, cp)
683 	struct ad1848_softc *ac;
684 	struct ad1848_devmap *map;
685 	int cnt;
686 	mixer_ctrl_t *cp;
687 {
688 	ad1848_devmap_t *entry;
689 	struct ad1848_volume vol;
690 	int error = EINVAL;
691 	int dev;
692 
693 	if (!(entry = ad1848_mixer_find_dev(map, cnt, cp)))
694 		return (ENXIO);
695 
696 	dev = entry->dev;
697 
698 	switch (entry->kind) {
699 	case AD1848_KIND_LVL:
700 		if (cp->type != AUDIO_MIXER_VALUE)
701 			break;
702 
703 		if (dev < AD1848_AUX2_CHANNEL ||
704 		    dev > AD1848_MONITOR_CHANNEL)
705 			break;
706 
707 		if (cp->un.value.num_channels != 1 &&
708 		    mixer_channel_info[dev].right_reg == 0)
709 			break;
710 
711 		error = ad1848_get_device_gain(ac, dev, &vol);
712 		if (!error)
713 			ad1848_from_vol(cp, &vol);
714 
715 		break;
716 
717 	case AD1848_KIND_MUTE:
718 		if (cp->type != AUDIO_MIXER_ENUM) break;
719 
720 		cp->un.ord = ac->mute[dev] ? 1 : 0;
721 		error = 0;
722 		break;
723 
724 	case AD1848_KIND_RECORDGAIN:
725 		if (cp->type != AUDIO_MIXER_VALUE) break;
726 
727 		error = ad1848_get_rec_gain(ac, &vol);
728 		if (!error)
729 			ad1848_from_vol(cp, &vol);
730 
731 		break;
732 
733 	case AD1848_KIND_MICGAIN:
734 		if (cp->type != AUDIO_MIXER_VALUE) break;
735 
736 		error = ad1848_get_mic_gain(ac, &vol);
737 		if (!error)
738 			ad1848_from_vol(cp, &vol);
739 
740 		break;
741 
742 	case AD1848_KIND_RECORDSOURCE:
743 		if (cp->type != AUDIO_MIXER_ENUM) break;
744 		cp->un.ord = ad1848_get_rec_port(ac);
745 		error = 0;
746 		break;
747 
748 	default:
749 		printf ("Invalid kind\n");
750 		break;
751 	}
752 
753 	return (error);
754 }
755 
756 int
757 ad1848_mixer_set_port(ac, map, cnt, cp)
758 	struct ad1848_softc *ac;
759 	struct ad1848_devmap *map;
760 	int cnt;
761 	mixer_ctrl_t *cp;
762 {
763 	ad1848_devmap_t *entry;
764 	struct ad1848_volume vol;
765 	int error = EINVAL;
766 	int dev;
767 
768 	if (!(entry = ad1848_mixer_find_dev(map, cnt, cp)))
769 		return (ENXIO);
770 
771 	dev = entry->dev;
772 
773 	switch (entry->kind) {
774 	case AD1848_KIND_LVL:
775 		if (cp->type != AUDIO_MIXER_VALUE)
776 			break;
777 
778 		if (dev < AD1848_AUX2_CHANNEL ||
779 		    dev > AD1848_MONITOR_CHANNEL)
780 			break;
781 
782 		if (cp->un.value.num_channels != 1 &&
783 		    mixer_channel_info[dev].right_reg == 0)
784 			break;
785 
786 		ad1848_to_vol(cp, &vol);
787 		error = ad1848_set_channel_gain(ac, dev, &vol);
788 		break;
789 
790 	case AD1848_KIND_MUTE:
791 		if (cp->type != AUDIO_MIXER_ENUM) break;
792 
793 		ac->mute[dev] = (cp->un.ord ? MUTE_ALL : 0);
794 		ad1848_mute_channel(ac, dev, ac->mute[dev]);
795 		error = 0;
796 		break;
797 
798 	case AD1848_KIND_RECORDGAIN:
799 		if (cp->type != AUDIO_MIXER_VALUE) break;
800 
801 		ad1848_to_vol(cp, &vol);
802 		error = ad1848_set_rec_gain(ac, &vol);
803 		break;
804 
805 	case AD1848_KIND_MICGAIN:
806 		if (cp->type != AUDIO_MIXER_VALUE) break;
807 
808 		ad1848_to_vol(cp, &vol);
809 		error = ad1848_set_mic_gain(ac, &vol);
810 		break;
811 
812 	case AD1848_KIND_RECORDSOURCE:
813 		if (cp->type != AUDIO_MIXER_ENUM) break;
814 
815 		error = ad1848_set_rec_port(ac,  cp->un.ord);
816 		break;
817 
818 	default:
819 		printf ("Invalid kind\n");
820 		break;
821 	}
822 
823 	return (error);
824 }
825 
826 
827 int
828 ad1848_query_encoding(addr, fp)
829 	void *addr;
830 	struct audio_encoding *fp;
831 {
832 	struct ad1848_softc *sc = addr;
833 
834 	switch (fp->index) {
835 	case 0:
836 		strcpy(fp->name, AudioEmulaw);
837 		fp->encoding = AUDIO_ENCODING_ULAW;
838 		fp->precision = 8;
839 		fp->flags = 0;
840 		break;
841 	case 1:
842 		strcpy(fp->name, AudioEalaw);
843 		fp->encoding = AUDIO_ENCODING_ALAW;
844 		fp->precision = 8;
845 		fp->flags = 0;
846 		break;
847 	case 2:
848 		strcpy(fp->name, AudioEslinear_le);
849 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
850 		fp->precision = 16;
851 		fp->flags = 0;
852 		break;
853 	case 3:
854 		strcpy(fp->name, AudioEulinear);
855 		fp->encoding = AUDIO_ENCODING_ULINEAR;
856 		fp->precision = 8;
857 		fp->flags = 0;
858 		break;
859 
860 	case 4: /* only on CS4231 */
861 		strcpy(fp->name, AudioEslinear_be);
862 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
863 		fp->precision = 16;
864 		fp->flags = sc->mode == 1
865 #if AD1845_HACK
866 		    || sc->is_ad1845
867 #endif
868 			? AUDIO_ENCODINGFLAG_EMULATED : 0;
869 		break;
870 
871 		/* emulate some modes */
872 	case 5:
873 		strcpy(fp->name, AudioEslinear);
874 		fp->encoding = AUDIO_ENCODING_SLINEAR;
875 		fp->precision = 8;
876 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
877 		break;
878 	case 6:
879 		strcpy(fp->name, AudioEulinear_le);
880 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
881 		fp->precision = 16;
882 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
883 		break;
884 	case 7:
885 		strcpy(fp->name, AudioEulinear_be);
886 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
887 		fp->precision = 16;
888 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
889 		break;
890 
891 	case 8: /* only on CS4231 */
892 		if (sc->mode == 1 || sc->is_ad1845)
893 			return EINVAL;
894 		strcpy(fp->name, AudioEadpcm);
895 		fp->encoding = AUDIO_ENCODING_ADPCM;
896 		fp->precision = 4;
897 		fp->flags = 0;
898 		break;
899 	default:
900 		return EINVAL;
901 		/*NOTREACHED*/
902 	}
903 	return (0);
904 }
905 
906 int
907 ad1848_set_params(addr, setmode, usemode, p, r)
908 	void *addr;
909 	int setmode, usemode;
910 	struct audio_params *p, *r;
911 {
912 	struct ad1848_softc *sc = addr;
913 	int error, bits, enc;
914 	void (*pswcode) __P((void *, u_char *buf, int cnt));
915 	void (*rswcode) __P((void *, u_char *buf, int cnt));
916 
917 	DPRINTF(("ad1848_set_params: %d %d %d %ld\n",
918 		 p->encoding, p->precision, p->channels, p->sample_rate));
919 
920 	enc = p->encoding;
921 	pswcode = rswcode = 0;
922 	switch (enc) {
923 	case AUDIO_ENCODING_SLINEAR_LE:
924 		if (p->precision == 8) {
925 			enc = AUDIO_ENCODING_ULINEAR_LE;
926 			pswcode = rswcode = change_sign8;
927 		}
928 		break;
929 	case AUDIO_ENCODING_SLINEAR_BE:
930 		if (p->precision == 16 && (sc->mode == 1
931 #if AD1845_HACK
932 		    || sc->is_ad1845
933 #endif
934 			)) {
935 			enc = AUDIO_ENCODING_SLINEAR_LE;
936 			pswcode = rswcode = swap_bytes;
937 		}
938 		break;
939 	case AUDIO_ENCODING_ULINEAR_LE:
940 		if (p->precision == 16) {
941 			enc = AUDIO_ENCODING_SLINEAR_LE;
942 			pswcode = rswcode = change_sign16_le;
943 		}
944 		break;
945 	case AUDIO_ENCODING_ULINEAR_BE:
946 		if (p->precision == 16) {
947 			if (sc->mode == 1
948 #if AD1845_HACK
949 			    || sc->is_ad1845
950 #endif
951 				) {
952 				enc = AUDIO_ENCODING_SLINEAR_LE;
953 				pswcode = swap_bytes_change_sign16_le;
954 				rswcode = change_sign16_swap_bytes_le;
955 			} else {
956 				enc = AUDIO_ENCODING_SLINEAR_BE;
957 				pswcode = rswcode = change_sign16_be;
958 			}
959 		}
960 		break;
961 	}
962 	switch (enc) {
963 	case AUDIO_ENCODING_ULAW:
964 		bits = FMT_ULAW >> 5;
965 		break;
966 	case AUDIO_ENCODING_ALAW:
967 		bits = FMT_ALAW >> 5;
968 		break;
969 	case AUDIO_ENCODING_ADPCM:
970 		bits = FMT_ADPCM >> 5;
971 		break;
972 	case AUDIO_ENCODING_SLINEAR_LE:
973 		if (p->precision == 16)
974 			bits = FMT_TWOS_COMP >> 5;
975 		else
976 			return EINVAL;
977 		break;
978 	case AUDIO_ENCODING_SLINEAR_BE:
979 		if (p->precision == 16)
980 			bits = FMT_TWOS_COMP_BE >> 5;
981 		else
982 			return EINVAL;
983 		break;
984 	case AUDIO_ENCODING_ULINEAR_LE:
985 		if (p->precision == 8)
986 			bits = FMT_PCM8 >> 5;
987 		else
988 			return EINVAL;
989 		break;
990 	default:
991 		return EINVAL;
992 	}
993 
994 	if (p->channels < 1 || p->channels > 2)
995 		return EINVAL;
996 
997 	error = ad1848_set_speed(sc, &p->sample_rate);
998 	if (error)
999 		return error;
1000 
1001 	p->sw_code = pswcode;
1002 	r->sw_code = rswcode;
1003 
1004 	sc->format_bits = bits;
1005 	sc->channels = p->channels;
1006 	sc->precision = p->precision;
1007 	sc->need_commit = 1;
1008 
1009 	DPRINTF(("ad1848_set_params succeeded, bits=%x\n", bits));
1010 	return (0);
1011 }
1012 
1013 int
1014 ad1848_set_rec_port(sc, port)
1015 	struct ad1848_softc *sc;
1016 	int port;
1017 {
1018 	u_char inp, reg;
1019 
1020 	DPRINTF(("ad1848_set_rec_port: 0x%x\n", port));
1021 
1022 	if (port == MIC_IN_PORT)
1023 		inp = MIC_INPUT;
1024 	else if (port == LINE_IN_PORT)
1025 		inp = LINE_INPUT;
1026 	else if (port == DAC_IN_PORT)
1027 		inp = MIXED_DAC_INPUT;
1028 	else if (sc->mode >= 2 && port == AUX1_IN_PORT)
1029 		inp = AUX_INPUT;
1030 	else
1031 		return(EINVAL);
1032 
1033 	reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
1034 	reg &= INPUT_SOURCE_MASK;
1035 	ad_write(sc, SP_LEFT_INPUT_CONTROL, (inp|reg));
1036 
1037 	reg = ad_read(sc, SP_RIGHT_INPUT_CONTROL);
1038 	reg &= INPUT_SOURCE_MASK;
1039 	ad_write(sc, SP_RIGHT_INPUT_CONTROL, (inp|reg));
1040 
1041 	sc->rec_port = port;
1042 
1043 	return (0);
1044 }
1045 
1046 int
1047 ad1848_get_rec_port(sc)
1048 	struct ad1848_softc *sc;
1049 {
1050 	return (sc->rec_port);
1051 }
1052 
1053 int
1054 ad1848_round_blocksize(addr, blk)
1055 	void *addr;
1056 	int blk;
1057 {
1058 
1059 	/* Round to a multiple of the biggest sample size. */
1060 	return (blk &= -4);
1061 }
1062 
1063 int
1064 ad1848_open(addr, flags)
1065 	void *addr;
1066 	int flags;
1067 {
1068 	struct ad1848_softc *sc = addr;
1069 	u_char reg;
1070 
1071 	DPRINTF(("ad1848_open: sc=%p\n", sc));
1072 
1073 	sc->open_mode = flags;
1074 
1075 	/* Enable interrupts */
1076 	DPRINTF(("ad1848_open: enable intrs\n"));
1077 	reg = ad_read(sc, SP_PIN_CONTROL);
1078 	ad_write(sc, SP_PIN_CONTROL, reg | INTERRUPT_ENABLE);
1079 
1080 	/* If recording && monitoring, the playback part is also used. */
1081 	if (flags & FREAD && sc->mute[AD1848_MONITOR_CHANNEL] == 0)
1082 		ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 1);
1083 
1084 #ifdef AUDIO_DEBUG
1085 	if (ad1848debug)
1086 		ad1848_dump_regs(sc);
1087 #endif
1088 
1089 	return 0;
1090 }
1091 
1092 /*
1093  * Close function is called at splaudio().
1094  */
1095 void
1096 ad1848_close(addr)
1097 	void *addr;
1098 {
1099 	struct ad1848_softc *sc = addr;
1100 	u_char reg;
1101 
1102 	sc->open_mode = 0;
1103 
1104 	ad1848_mute_wave_output(sc, WAVE_UNMUTE1, 0);
1105 
1106 	/* Disable interrupts */
1107 	DPRINTF(("ad1848_close: disable intrs\n"));
1108 	reg = ad_read(sc, SP_PIN_CONTROL);
1109 	ad_write(sc, SP_PIN_CONTROL, reg & ~INTERRUPT_ENABLE);
1110 
1111 #ifdef AUDIO_DEBUG
1112 	if (ad1848debug)
1113 		ad1848_dump_regs(sc);
1114 #endif
1115 }
1116 
1117 /*
1118  * Lower-level routines
1119  */
1120 int
1121 ad1848_commit_settings(addr)
1122 	void *addr;
1123 {
1124 	struct ad1848_softc *sc = addr;
1125 	int timeout;
1126 	u_char fs;
1127 	int s;
1128 
1129 	if (!sc->need_commit)
1130 		return 0;
1131 
1132 	s = splaudio();
1133 
1134 	ad1848_mute_wave_output(sc, WAVE_MUTE0, 1);
1135 
1136 	ad_set_MCE(sc, 1);	/* Enables changes to the format select reg */
1137 
1138 	fs = sc->speed_bits | (sc->format_bits << 5);
1139 
1140 	if (sc->channels == 2)
1141 		fs |= FMT_STEREO;
1142 
1143 	/*
1144 	 * OPL3-SA2 (YMF711) is sometimes busy here.
1145 	 * Wait until it becomes ready.
1146 	 */
1147 	for (timeout = 0;
1148 	    timeout < 1000 && ADREAD(sc, AD1848_IADDR) & SP_IN_INIT; timeout++)
1149 		delay(10);
1150 
1151 	ad_write(sc, SP_CLOCK_DATA_FORMAT, fs);
1152 
1153 	/*
1154 	 * If mode >= 2 (CS4231), set I28 also.
1155 	 * It's the capture format register.
1156 	 */
1157 	if (sc->mode >= 2) {
1158 		/*
1159 		 * Gravis Ultrasound MAX SDK sources says something about
1160 		 * errata sheets, with the implication that these inb()s
1161 		 * are necessary.
1162 		 */
1163 		(void)ADREAD(sc, AD1848_IDATA);
1164 		(void)ADREAD(sc, AD1848_IDATA);
1165 		/* Write to I8 starts resyncronization. Wait for completion. */
1166 		timeout = 100000;
1167 		while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
1168 			timeout--;
1169 
1170 		ad_write(sc, CS_REC_FORMAT, fs);
1171 		(void)ADREAD(sc, AD1848_IDATA);
1172 		(void)ADREAD(sc, AD1848_IDATA);
1173 		/* Now wait for resync for capture side of the house */
1174 	}
1175 	/*
1176 	 * Write to I8 starts resyncronization. Wait until it completes.
1177 	 */
1178 	timeout = 100000;
1179 	while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT) {
1180 		delay(10);
1181 		timeout--;
1182 	}
1183 
1184 	if (ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
1185 		printf("ad1848_commit: Auto calibration timed out\n");
1186 
1187 	/*
1188 	 * Starts the calibration process and
1189 	 * enters playback mode after it.
1190 	 */
1191 	ad_set_MCE(sc, 0);
1192 	wait_for_calibration(sc);
1193 
1194 	ad1848_mute_wave_output(sc, WAVE_MUTE0, 0);
1195 
1196 	splx(s);
1197 
1198 	sc->need_commit = 0;
1199 	return 0;
1200 }
1201 
1202 void
1203 ad1848_reset(sc)
1204 	struct ad1848_softc *sc;
1205 {
1206 	u_char r;
1207 
1208 	DPRINTF(("ad1848_reset\n"));
1209 
1210 	/* Clear the PEN and CEN bits */
1211 	r = ad_read(sc, SP_INTERFACE_CONFIG);
1212 	r &= ~(CAPTURE_ENABLE | PLAYBACK_ENABLE);
1213 	ad_write(sc, SP_INTERFACE_CONFIG, r);
1214 
1215 	if (sc->mode >= 2) {
1216 		ADWRITE(sc, AD1848_IADDR, CS_IRQ_STATUS);
1217 		ADWRITE(sc, AD1848_IDATA, 0);
1218 	}
1219 	/* Clear interrupt status */
1220 	ADWRITE(sc, AD1848_STATUS, 0);
1221 #ifdef AUDIO_DEBUG
1222 	if (ad1848debug)
1223 		ad1848_dump_regs(sc);
1224 #endif
1225 }
1226 
1227 int
1228 ad1848_set_speed(sc, argp)
1229 	struct ad1848_softc *sc;
1230 	u_long *argp;
1231 {
1232 	/*
1233 	 * The sampling speed is encoded in the least significant nible of I8.
1234 	 * The LSB selects the clock source (0=24.576 MHz, 1=16.9344 Mhz) and
1235 	 * other three bits select the divisor (indirectly):
1236 	 *
1237 	 * The available speeds are in the following table. Keep the speeds in
1238 	 * the increasing order.
1239 	 */
1240 	typedef struct {
1241 		int	speed;
1242 		u_char	bits;
1243 	} speed_struct;
1244 	u_long arg = *argp;
1245 
1246 	static const speed_struct speed_table[] =  {
1247 		{5510, (0 << 1) | 1},
1248 		{5510, (0 << 1) | 1},
1249 		{6620, (7 << 1) | 1},
1250 		{8000, (0 << 1) | 0},
1251 		{9600, (7 << 1) | 0},
1252 		{11025, (1 << 1) | 1},
1253 		{16000, (1 << 1) | 0},
1254 		{18900, (2 << 1) | 1},
1255 		{22050, (3 << 1) | 1},
1256 		{27420, (2 << 1) | 0},
1257 		{32000, (3 << 1) | 0},
1258 		{33075, (6 << 1) | 1},
1259 		{37800, (4 << 1) | 1},
1260 		{44100, (5 << 1) | 1},
1261 		{48000, (6 << 1) | 0}
1262 	};
1263 
1264 	int i, n, selected = -1;
1265 
1266 	n = sizeof(speed_table) / sizeof(speed_struct);
1267 
1268 	if (arg < speed_table[0].speed)
1269 		selected = 0;
1270 	if (arg > speed_table[n - 1].speed)
1271 		selected = n - 1;
1272 
1273 	for (i = 1 /*really*/ ; selected == -1 && i < n; i++)
1274 		if (speed_table[i].speed == arg)
1275 			selected = i;
1276 		else if (speed_table[i].speed > arg) {
1277 			int diff1, diff2;
1278 
1279 			diff1 = arg - speed_table[i - 1].speed;
1280 			diff2 = speed_table[i].speed - arg;
1281 
1282 			if (diff1 < diff2)
1283 				selected = i - 1;
1284 			else
1285 				selected = i;
1286 		}
1287 
1288 	if (selected == -1) {
1289 		printf("ad1848: Can't find speed???\n");
1290 		selected = 3;
1291 	}
1292 
1293 	sc->speed_bits = speed_table[selected].bits;
1294 	sc->need_commit = 1;
1295 	*argp = speed_table[selected].speed;
1296 
1297 	return (0);
1298 }
1299 
1300 /*
1301  * Halt I/O
1302  */
1303 int
1304 ad1848_halt_output(addr)
1305 	void *addr;
1306 {
1307 	struct ad1848_softc *sc = addr;
1308 	u_char reg;
1309 
1310 	DPRINTF(("ad1848: ad1848_halt_output\n"));
1311 
1312 	reg = ad_read(sc, SP_INTERFACE_CONFIG);
1313 	ad_write(sc, SP_INTERFACE_CONFIG, reg & ~PLAYBACK_ENABLE);
1314 
1315 	return(0);
1316 }
1317 
1318 int
1319 ad1848_halt_input(addr)
1320 	void *addr;
1321 {
1322 	struct ad1848_softc *sc = addr;
1323 	u_char reg;
1324 
1325 	DPRINTF(("ad1848: ad1848_halt_input\n"));
1326 
1327 	reg = ad_read(sc, SP_INTERFACE_CONFIG);
1328 	ad_write(sc, SP_INTERFACE_CONFIG, reg & ~CAPTURE_ENABLE);
1329 
1330 	return(0);
1331 }
1332