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