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