Home | History | Annotate | Line # | Download | only in ic
interwave.c revision 1.17
      1 /*	$NetBSD: interwave.c,v 1.17 2003/04/06 18:20:13 wiz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Kari Mettinen
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: interwave.c,v 1.17 2003/04/06 18:20:13 wiz Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/errno.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/syslog.h>
     46 #include <sys/device.h>
     47 #include <sys/proc.h>
     48 #include <sys/buf.h>
     49 #include <sys/fcntl.h>
     50 #include <sys/malloc.h>
     51 #include <sys/kernel.h>
     52 
     53 #include <machine/cpu.h>
     54 #include <machine/intr.h>
     55 #include <machine/pio.h>
     56 #include <sys/audioio.h>
     57 #include <dev/audio_if.h>
     58 #include <dev/mulaw.h>
     59 
     60 #include <dev/isa/isavar.h>
     61 #include <dev/isa/isadmavar.h>
     62 
     63 #include <dev/ic/interwavereg.h>
     64 #include <dev/ic/interwavevar.h>
     65 
     66 
     67 static void iwreset __P((struct iw_softc *, int));
     68 
     69 static int iw_set_speed __P((struct iw_softc *, u_long, char));
     70 static u_long iw_set_format __P((struct iw_softc *, u_long, int));
     71 static void iw_mixer_line_level __P((struct iw_softc *, int, int, int));
     72 static void iw_trigger_dma __P((struct iw_softc *, u_char));
     73 static void iw_stop_dma __P((struct iw_softc *, u_char, u_char));
     74 static void iw_dma_count __P((struct iw_softc *, u_short, int));
     75 static int iwintr __P((void *));
     76 static void iw_meminit __P((struct iw_softc *));
     77 static void iw_mempoke __P((struct iw_softc *, u_long, u_char));
     78 static u_char iw_mempeek __P((struct iw_softc *, u_long));
     79 
     80 #ifdef USE_WAVETABLE
     81 static void iw_set_voice_place __P((struct iw_softc *, u_char, u_long));
     82 static void iw_voice_pan __P((struct iw_softc *, u_char, u_short, u_short));
     83 static void iw_voice_freq __P((struct iw_softc *, u_char, u_long));
     84 static void iw_set_loopmode __P((struct iw_softc *, u_char, u_char, u_char));
     85 static void iw_set_voice_pos __P((struct iw_softc *, u_short, u_long, u_long));
     86 static void iw_start_voice __P((struct iw_softc *, u_char));
     87 static void iw_play_voice __P((struct iw_softc *, u_long, u_long, u_short));
     88 static void iw_stop_voice __P((struct iw_softc *, u_char));
     89 static void iw_move_voice_end __P((struct iw_softc *, u_short, u_long));
     90 static void iw_initvoices __P((struct iw_softc *));
     91 #endif
     92 
     93 struct audio_device iw_device = {
     94 	"Am78C201",
     95 	"0.1",
     96 	"guspnp"
     97 };
     98 
     99 #ifdef AUDIO_DEBUG
    100 int iw_debug;
    101 #define DPRINTF(p)       if (iw_debug) printf p
    102 #else
    103 #define DPRINTF(p)
    104 #endif
    105 
    106 static int      iw_cc = 1;
    107 #ifdef DIAGNOSTIC
    108 static int      outputs = 0;
    109 static int      iw_ints = 0;
    110 static int      inputs = 0;
    111 static int      iw_inints = 0;
    112 #endif
    113 
    114 int
    115 iwintr(arg)
    116 	void	*arg;
    117 {
    118 	struct	iw_softc *sc = arg;
    119 	int	val = 0;
    120 	u_char	intrs = 0;
    121 
    122 	IW_READ_DIRECT_1(6, sc->p2xr_h, intrs);	/* UISR */
    123 
    124 	/* codec ints */
    125 
    126 	/*
    127 	 * The proper order to do this seems to be to read CSR3 to get the
    128 	 * int cause and fifo over underrrun status, then deal with the ints
    129 	 * (new dma set up), and to clear ints by writing the respective bit
    130 	 * to 0.
    131 	 */
    132 
    133 	/* read what ints happened */
    134 
    135 	IW_READ_CODEC_1(CSR3I, intrs);
    136 
    137 	/* clear them */
    138 
    139 	IW_WRITE_DIRECT_1(2, sc->codec_index_h, 0x00);
    140 
    141 	/* and process them */
    142 
    143 	if (intrs & 0x20) {
    144 #ifdef DIAGNOSTIC
    145 		iw_inints++;
    146 #endif
    147 		sc->sc_reclocked = 0;
    148 		if (sc->sc_recintr != 0)
    149 			sc->sc_recintr(sc->sc_recarg);
    150 		val = 1;
    151 	}
    152 	if (intrs & 0x10) {
    153 #ifdef DIAGNOSTIC
    154 		iw_ints++;
    155 #endif
    156 		sc->sc_playlocked = 0;
    157 		if (sc->sc_playintr != 0)
    158 			sc->sc_playintr(sc->sc_playarg);
    159 		val = 1;
    160 	}
    161 	return val;
    162 
    163 }
    164 
    165 void
    166 iwattach(sc)
    167 	struct	iw_softc *sc;
    168 {
    169 	int	got_irq = 0;
    170 
    171 	DPRINTF(("iwattach sc %p\n", sc));
    172 
    173 	sc->cdatap = 1;		/* relative offsets in region */
    174 	sc->csr1r = 2;
    175 	sc->cxdr = 3;		/* CPDR or CRDR */
    176 
    177 	sc->gmxr = 0;		/* sc->p3xr */
    178 	sc->gmxdr = 1;		/* GMTDR or GMRDR */
    179 	sc->svsr = 2;
    180 	sc->igidxr = 3;
    181 	sc->i16dp = 4;
    182 	sc->i8dp = 5;
    183 	sc->lmbdr = 7;
    184 
    185 	sc->rec_precision = sc->play_precision = 8;
    186 	sc->rec_channels = sc->play_channels = 1;
    187 	sc->rec_encoding = sc->play_encoding = AUDIO_ENCODING_ULAW;
    188 	sc->sc_irate = 8000;
    189 	sc->sc_orate = 8000;
    190 
    191 	sc->sc_fullduplex = 1;
    192 
    193 	sc->sc_reclocked = 0;
    194 	sc->sc_playlocked = 0;
    195 
    196 	sc->sc_dma_flags = 0;
    197 
    198 	/*
    199 	 * We can only use a few selected irqs, see if we got one from pnp
    200 	 * code that suits us.
    201 	 */
    202 
    203 	if (sc->sc_irq > 0) {
    204 		sc->sc_ih = isa_intr_establish(sc->sc_p2xr_ic,
    205 					       sc->sc_irq,
    206 					   IST_EDGE, IPL_AUDIO, iwintr, sc);
    207 		got_irq = 1;
    208 	}
    209 	if (!got_irq) {
    210 		printf("\niwattach: couldn't get a suitable irq\n");
    211 		return;
    212 	}
    213 	printf("\n");
    214 	iwreset(sc, 0);
    215 	iw_set_format(sc, AUDIO_ENCODING_ULAW, 0);
    216 	iw_set_format(sc, AUDIO_ENCODING_ULAW, 1);
    217 	printf("%s: interwave version %s\n",
    218 		sc->sc_dev.dv_xname, iw_device.version);
    219 	audio_attach_mi(sc->iw_hw_if, sc, &sc->sc_dev);
    220  }
    221 
    222 int
    223 iwopen(sc, flags)
    224 	struct	iw_softc *sc;
    225 	int	flags;
    226 {
    227 	int	s;
    228 
    229 	s = splaudio();
    230 	if (sc->sc_open) {
    231 		splx(s);
    232 		DPRINTF(("iwopen: open %x sc %p\n", sc->sc_open, sc));
    233 		return EBUSY;
    234 	} else
    235 		sc->sc_open = 1;
    236 	splx(s);
    237 
    238 	DPRINTF(("iwopen: open %x sc %p\n", sc->sc_open, sc));
    239 
    240 #ifdef DIAGNOSTIC
    241 	outputs = 0;
    242 	iw_ints = 0;
    243 	inputs = 0;
    244 	iw_inints = 0;
    245 #endif
    246 
    247 	iwreset(sc, 1);
    248 
    249 	/* READ/WRITE or both */
    250 
    251 	if (flags == FREAD) {
    252 		sc->sc_mode |= IW_READ;
    253 		sc->sc_reclocked = 0;
    254 	}
    255 	if (flags == FWRITE) {
    256 		sc->sc_mode |= IW_WRITE;
    257 		sc->sc_playlocked = 0;
    258 	}
    259 	sc->sc_playdma_cnt = 0;
    260 	sc->sc_recdma_cnt = 0;
    261 	sc->playfirst = 1;
    262 	sc->sc_playintr = 0;
    263 	sc->sc_recintr = 0;
    264 
    265 	return 0;
    266 }
    267 
    268 
    269 
    270 void
    271 iwclose(addr)
    272 	void	*addr;
    273 {
    274 	struct	iw_softc *sc = addr;
    275 
    276 	DPRINTF(("iwclose sc %p\n", sc));
    277 
    278 #ifdef DIAGNOSTIC
    279 	DPRINTF(("iwclose: outputs %d ints %d inputs %d in_ints %d\n",
    280 		outputs, iw_ints, inputs, iw_inints));
    281 #endif
    282 
    283 	/* close hardware */
    284 	sc->sc_open = 0;
    285 	sc->sc_flags = 0;
    286 	sc->sc_mode = 0;
    287 	sc->sc_playlocked = 0;
    288 	sc->sc_reclocked = 0;
    289 
    290 	iw_stop_dma(sc, IW_DMA_PLAYBACK, 1);
    291 	iw_stop_dma(sc, IW_DMA_RECORD, 1);
    292 
    293 	sc->sc_playdma_cnt = 0;
    294 	sc->sc_recdma_cnt = 0;
    295 }
    296 
    297 #define RAM_STEP          64*1024
    298 
    299 static void
    300 iw_mempoke(sc, addy, val)
    301 	struct	iw_softc *sc;
    302 	u_long	addy;
    303 	u_char	val;
    304 {
    305 	IW_WRITE_GENERAL_2(LMALI, (u_short) addy);
    306 	IW_WRITE_GENERAL_1(LMAHI, (u_char) (addy >> 16));
    307 
    308 	/* Write byte to LMBDR */
    309 	IW_WRITE_DIRECT_1(sc->p3xr + 7, sc->p3xr_h, val);
    310 }
    311 
    312 static u_char
    313 iw_mempeek(sc, addy)
    314 	struct	iw_softc *sc;
    315 	u_long	addy;
    316 {
    317 	u_char	ret;
    318 
    319 	IW_WRITE_GENERAL_2(LMALI, (u_short) addy);
    320 	IW_WRITE_GENERAL_1(LMAHI, (u_char) (addy >> 16));
    321 
    322 	IW_READ_DIRECT_1(sc->p3xr + 7, sc->p3xr_h, ret);
    323 	return ret;		/* return byte from LMBDR */
    324 }
    325 
    326 static void
    327 iw_meminit(sc)
    328 	struct iw_softc *sc;
    329 {
    330 	u_long          bank[4] = {0L, 0L, 0L, 0L};
    331 	u_long          addr = 0L, base = 0L, cnt = 0L;
    332 	u_char          i, ram = 0 /* ,memval=0 */ ;
    333 	u_short         lmcfi;
    334 	u_long          temppi;
    335 	u_long         *lpbanks = &temppi;
    336 
    337 	IW_WRITE_GENERAL_1(LDMACI, 0x00);
    338 
    339 	IW_READ_GENERAL_2(LMCFI, lmcfi);	/* 0x52 */
    340 	lmcfi |= 0x0A0C;
    341 	IW_WRITE_GENERAL_2(LMCFI, lmcfi);	/* max addr span */
    342 	IW_WRITE_GENERAL_1(LMCI, 0x00);
    343 
    344 	/* fifo addresses */
    345 
    346 	IW_WRITE_GENERAL_2(LMRFAI, ((4 * 1024 * 1024) >> 8));
    347 	IW_WRITE_GENERAL_2(LMPFAI, ((4 * 1024 * 1024 + 16 * 1024) >> 8));
    348 
    349 	IW_WRITE_GENERAL_2(LMFSI, 0x000);
    350 
    351 	IW_WRITE_GENERAL_2(LDICI, 0x0000);
    352 
    353 	while (addr < (16 * 1024 * 1024)) {
    354 		iw_mempoke(sc, addr, 0x00);
    355 		addr += RAM_STEP;
    356 	}
    357 
    358 	printf("%s:", sc->sc_dev.dv_xname);
    359 
    360 	for (i = 0; i < 4; i++) {
    361 		iw_mempoke(sc, base, 0xAA);	/* mark start of bank */
    362 		iw_mempoke(sc, base + 1L, 0x55);
    363 		if (iw_mempeek(sc, base) == 0xAA  &&
    364 		    iw_mempeek(sc, base + 1L) == 0x55)
    365 			ram = 1;
    366 		if (ram) {
    367 			while (cnt < (4 * 1024 * 1024)) {
    368 				bank[i] += RAM_STEP;
    369 				cnt += RAM_STEP;
    370 				addr = base + cnt;
    371 				if (iw_mempeek(sc, addr) == 0xAA)
    372 					break;
    373 			}
    374 		}
    375 		if (lpbanks != NULL) {
    376 			*lpbanks = bank[i];
    377 			lpbanks++;
    378 		}
    379 		bank[i] = bank[i] >> 10;
    380 		printf("%s bank[%d]: %ldK", i ? "," : "", i, bank[i]);
    381 		base += 4 * 1024 * 1024;
    382 		cnt = 0L;
    383 		ram = 0;
    384 	}
    385 
    386 	printf("\n");
    387 
    388 	/*
    389 	 * this is not really useful since GUS PnP supports memory
    390 	 * configurations that aren't really supported by Interwave...beware
    391 	 * of holes! Also, we don't use the memory for anything in this
    392 	 * version of the driver.
    393 	 *
    394 	 * we've configured for 4M-4M-4M-4M
    395 	 */
    396 }
    397 
    398 
    399 static
    400 void
    401 iwreset(sc, warm)
    402 	struct iw_softc *sc;
    403 	int             warm;
    404 {
    405 	u_char          reg, cmode, val = 0, mixer_image = 0;
    406 
    407 	reg = 0;		/* XXX gcc -Wall */
    408 
    409 	cmode = 0x6c;		/* enhanced codec mode (full duplex) */
    410 
    411 	/* reset */
    412 
    413 	IW_WRITE_GENERAL_1(URSTI, 0x00);
    414 	delay(10);
    415 	IW_WRITE_GENERAL_1(URSTI, 0x07);
    416 	IW_WRITE_GENERAL_1(ICMPTI, 0x1f);	/* disable DSP and uici and
    417 						 * udci writes */
    418 	IW_WRITE_GENERAL_1(IDECI, 0x7f);	/* enable ints to ISA and
    419 						 * codec access */
    420 	IW_READ_GENERAL_1(IVERI, reg);
    421 	IW_WRITE_GENERAL_1(IVERI, reg | 0x01);	/* hidden reg lock disable */
    422 	IW_WRITE_GENERAL_1(UASBCI, 0x00);
    423 
    424 	/* synth enhanced mode (default), 0 active voices, disable ints */
    425 
    426 	IW_WRITE_GENERAL_1(SGMI_WR, 0x01);	/* enhanced mode, LFOs
    427 						 * disabled */
    428 	for (val = 0; val < 32; val++) {
    429 		/* set each synth sound volume to 0 */
    430 		IW_WRITE_DIRECT_1(sc->p3xr + 2, sc->p3xr_h, val);
    431 		IW_WRITE_GENERAL_1(SVSI_WR, 0x00);
    432 		IW_WRITE_GENERAL_2(SASLI_WR, 0x0000);
    433 		IW_WRITE_GENERAL_2(SASHI_WR, 0x0000);
    434 		IW_WRITE_GENERAL_2(SAELI_WR, 0x0000);
    435 		IW_WRITE_GENERAL_2(SAEHI_WR, 0x0000);
    436 		IW_WRITE_GENERAL_2(SFCI_WR, 0x0000);
    437 		IW_WRITE_GENERAL_1(SACI_WR, 0x02);
    438 		IW_WRITE_GENERAL_1(SVSI_WR, 0x00);
    439 		IW_WRITE_GENERAL_1(SVEI_WR, 0x00);
    440 		IW_WRITE_GENERAL_2(SVLI_WR, 0x0000);
    441 		IW_WRITE_GENERAL_1(SVCI_WR, 0x02);
    442 		IW_WRITE_GENERAL_1(SMSI_WR, 0x02);
    443 	}
    444 
    445 	IW_WRITE_GENERAL_1(SAVI_WR, 0x00);
    446 
    447 	/* codec mode/init */
    448 
    449 	/* first change mode to 1 */
    450 
    451 	IW_WRITE_CODEC_1(CMODEI, 0x00);
    452 
    453 	/* and mode 3 */
    454 
    455 	IW_WRITE_CODEC_1(CMODEI, cmode);
    456 
    457 	IW_READ_CODEC_1(CMODEI, reg);
    458 
    459 	DPRINTF(("cmode %x\n", reg));
    460 
    461 	sc->revision = ((reg & 0x80) >> 3) | (reg & 0x0f);
    462 
    463 	IW_WRITE_DIRECT_1(sc->codec_index + 2, sc->p2xr_h, 0x00);
    464 
    465 	IW_WRITE_CODEC_1(CFIG1I | IW_MCE, 0x00);	/* dma 2 chan access */
    466 	IW_WRITE_CODEC_1(CEXTI, 0x00);	/* disable ints for now */
    467 
    468 
    469 	IW_WRITE_CODEC_1(CLPCTI, 0x00);	/* reset playback sample counters */
    470 	IW_WRITE_CODEC_1(CUPCTI, 0x00);	/* always upper byte last */
    471 	IW_WRITE_CODEC_1(CFIG2I, 0x80);	/* full voltage range, enable record
    472 					 * and playback sample counters, and
    473 					 * don't center output in case or
    474 					 * FIFO underrun */
    475 	IW_WRITE_CODEC_1(CFIG3I, 0xc0);	/* enable record/playback irq (still
    476 					 * turned off from CEXTI), max dma
    477 					 * rate */
    478 	IW_WRITE_CODEC_1(CSR3I, 0x00);	/* clear status 3 reg */
    479 
    480 
    481 	IW_WRITE_CODEC_1(CLRCTI, 0x00);	/* reset record sample counters */
    482 	IW_WRITE_CODEC_1(CURCTI, 0x00);	/* always upper byte last */
    483 
    484 
    485 	IW_READ_GENERAL_1(IVERI, reg);
    486 
    487 	sc->vers = reg >> 4;
    488 	if (!warm)
    489 		sprintf(iw_device.version, "%d.%d", sc->vers, sc->revision);
    490 
    491 	IW_WRITE_GENERAL_1(IDECI, 0x7f);	/* irqs and codec decode
    492 						 * enable */
    493 
    494 
    495 	/* ports */
    496 
    497 	if (!warm) {
    498 		iw_mixer_line_level(sc, IW_LINE_OUT, 255, 255);
    499 		iw_mixer_line_level(sc, IW_LINE_IN, 0, 0);
    500 		iw_mixer_line_level(sc, IW_AUX1, 0, 0);
    501 		iw_mixer_line_level(sc, IW_AUX2, 200, 200); /* CD */
    502 		sc->sc_dac.off = 0;
    503 		iw_mixer_line_level(sc, IW_DAC, 200, 200);
    504 
    505 		iw_mixer_line_level(sc, IW_MIC_IN, 0, 0);
    506 		iw_mixer_line_level(sc, IW_REC, 0, 0);
    507 		iw_mixer_line_level(sc, IW_LOOPBACK, 0, 0);
    508 		iw_mixer_line_level(sc, IW_MONO_IN, 0, 0);
    509 
    510 		/* mem stuff */
    511 		iw_meminit(sc);
    512 
    513 	}
    514 	IW_WRITE_CODEC_1(CEXTI, 0x02);	/* codec int enable */
    515 
    516 	/* clear _LDMACI */
    517 
    518 	IW_WRITE_GENERAL_1(LDMACI, 0x00);
    519 
    520 	/* enable mixer paths */
    521 	mixer_image = 0x0c;
    522 	IW_WRITE_DIRECT_1(sc->p2xr, sc->p2xr_h, mixer_image);
    523 	/*
    524 	 * enable output, line in. disable mic in bit 0 = 0 -> line in on
    525 	 * (from codec?) bit 1 = 0 -> output on bit 2 = 1 -> mic in on bit 3
    526 	 * = 1 -> irq&drq pin enable bit 4 = 1 -> channel interrupts to chan
    527 	 * 1 bit 5 = 1 -> enable midi loop back bit 6 = 0 -> irq latches
    528 	 * URCR[2:0] bit 6 = 1 -> dma latches URCR[2:0]
    529 	 */
    530 
    531 
    532 	IW_READ_DIRECT_1(sc->p2xr, sc->p2xr_h, mixer_image);
    533 #ifdef AUDIO_DEBUG
    534 	if (!warm)
    535 		DPRINTF(("mix image %x \n", mixer_image));
    536 #endif
    537 }
    538 
    539 struct iw_codec_freq {
    540 	u_long	freq;
    541 	u_char	bits;
    542 };
    543 
    544 int
    545 iw_set_speed(sc, freq, in)
    546 	struct	iw_softc *sc;
    547 	u_long	freq;
    548 	char	in;
    549 {
    550 	u_char	var, cfig3, reg;
    551 
    552 	static struct iw_codec_freq iw_cf[17] = {
    553 #define FREQ_1 24576000
    554 #define FREQ_2 16934400
    555 #define XTAL1 0
    556 #define XTAL2 1
    557 		{5510, 0x00 | XTAL2}, {6620, 0x0E | XTAL2},
    558 		{8000, 0x00 | XTAL1}, {9600, 0x0E | XTAL1},
    559 		{11025, 0x02 | XTAL2}, {16000, 0x02 | XTAL1},
    560 		{18900, 0x04 | XTAL2}, {22050, 0x06 | XTAL2},
    561 		{27420, 0x04 | XTAL1}, {32000, 0x06 | XTAL1},
    562 		{33075, 0x0C | XTAL2}, {37800, 0x08 | XTAL2},
    563 		{38400, 0x0A | XTAL1}, {44100, 0x0A | XTAL2},
    564 		{44800, 0x08 | XTAL1}, {48000, 0x0C | XTAL1},
    565 		{48000, 0x0C | XTAL1}	/* really a dummy for indexing later */
    566 #undef XTAL1
    567 #undef XTAL2
    568 	};
    569 
    570 	cfig3 = 0;		/* XXX gcc -Wall */
    571 
    572 	/*
    573 	 * if the frequency is between 3493Hz and 32KHz we can use a more
    574 	 * accurate frequency than the ones listed above base on the formula
    575 	 * FREQ/((16*(48+x))) where FREQ is either FREQ_1 (24576000Hz) or
    576 	 * FREQ_2 (16934400Hz) and x is the value to be written to either
    577 	 * CPVFI or CRVFI. To enable this option, bit 2 in CFIG3 needs to be
    578 	 * set high
    579 	 *
    580 	 * NOT IMPLEMENTED!
    581 	 *
    582 	 * Note that if you have a 'bad' XTAL_1 (higher than 18.5 MHz), 44.8KHz
    583 	 * and 38.4KHz modes will provide wrong frequencies to output.
    584 	 */
    585 
    586 
    587 	if (freq > 48000)
    588 		freq = 48000;
    589 	if (freq < 5510)
    590 		freq = 5510;
    591 
    592 	/* reset CFIG3[2] */
    593 
    594 	IW_READ_CODEC_1(CFIG3I, cfig3);
    595 
    596 	cfig3 |= 0xc0;		/* not full fifo treshhold */
    597 
    598 	DPRINTF(("cfig3i = %x -> ", cfig3));
    599 
    600 	cfig3 &= ~0x04;
    601 	IW_WRITE_CODEC_1(CFIG3I, cfig3);
    602 	IW_READ_CODEC_1(CFIG3I, cfig3);
    603 
    604 	DPRINTF(("%x\n", cfig3));
    605 
    606 	for (var = 0; var < 16; var++)	/* select closest frequency */
    607 		if (freq <= iw_cf[var].freq)
    608 			break;
    609 	if (var != 16)
    610 		if (abs(freq - iw_cf[var].freq) > abs(iw_cf[var + 1].freq - freq))
    611 			var++;
    612 
    613 	if (in)
    614 		IW_WRITE_CODEC_1(CRDFI | IW_MCE, sc->recfmtbits | iw_cf[var].bits);
    615 	else
    616 		IW_WRITE_CODEC_1(CPDFI | IW_MCE, sc->playfmtbits | iw_cf[var].bits);
    617 	freq = iw_cf[var].freq;
    618 	DPRINTF(("setting %s frequency to %d bits %x \n",
    619 	       in ? "in" : "out", (int) freq, iw_cf[var].bits));
    620 
    621 	IW_READ_CODEC_1(CPDFI, reg);
    622 
    623 	DPRINTF((" CPDFI %x ", reg));
    624 
    625 	IW_READ_CODEC_1(CRDFI, reg);
    626 
    627 	DPRINTF((" CRDFI %x ", reg));
    628 
    629 	return freq;
    630 }
    631 
    632 /* Encoding. */
    633 int
    634 iw_query_encoding(addr, fp)
    635 	void	*addr;
    636 	struct	audio_encoding *fp;
    637 {
    638 	/*
    639 	 * LINEAR, ALAW, ULAW, ADPCM in HW, we'll use linear unsigned
    640 	 * hardware mode for all 8-bit modes due to buggy (?) codec.
    641 	 */
    642 
    643 	/*
    644 	 * except in wavetable synth. there we have only mu-law and 8 and 16
    645 	 * bit linear data
    646 	 */
    647 
    648 	switch (fp->index) {
    649 	case 0:
    650 		strcpy(fp->name, AudioEulinear);
    651 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    652 		fp->precision = 8;
    653 		fp->flags = 0;
    654 		break;
    655 	case 1:
    656 		strcpy(fp->name, AudioEmulaw);
    657 		fp->encoding = AUDIO_ENCODING_ULAW;
    658 		fp->precision = 8;
    659 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    660 		break;
    661 	case 2:
    662 		strcpy(fp->name, AudioEalaw);
    663 		fp->encoding = AUDIO_ENCODING_ALAW;
    664 		fp->precision = 8;
    665 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    666 		break;
    667 	case 3:
    668 		strcpy(fp->name, AudioEadpcm);
    669 		fp->encoding = AUDIO_ENCODING_ADPCM;
    670 		fp->precision = 8;	/* really 4 bit */
    671 		fp->flags = 0;
    672 		break;
    673 	case 4:
    674 		strcpy(fp->name, AudioEslinear_le);
    675 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    676 		fp->precision = 16;
    677 		fp->flags = 0;
    678 		break;
    679 	case 5:
    680 		strcpy(fp->name, AudioEslinear_be);
    681 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    682 		fp->precision = 16;
    683 		fp->flags = 0;
    684 		break;
    685 	default:
    686 		return (EINVAL);
    687 		/* NOTREACHED */
    688 	}
    689 	return (0);
    690 }
    691 
    692 
    693 
    694 u_long
    695 iw_set_format(sc, precision, in)
    696 	struct	iw_softc *sc;
    697 	u_long	precision;
    698 	int	in;
    699 {
    700 	u_char	data;
    701 	int	encoding, channels;
    702 
    703 	encoding = in ? sc->rec_encoding : sc->play_encoding;
    704 	channels = in ? sc->rec_channels : sc->play_channels;
    705 
    706 	DPRINTF(("iw_set_format\n"));
    707 
    708 	switch (encoding) {
    709 	case AUDIO_ENCODING_ULAW:
    710 		data = 0x00;
    711 		break;
    712 
    713 	case AUDIO_ENCODING_ALAW:
    714 		data = 0x00;
    715 		break;
    716 
    717 	case AUDIO_ENCODING_SLINEAR_LE:
    718 		if (precision == 16)
    719 			data = 0x40;	/* little endian. 0xc0 is big endian */
    720 		else
    721 			data = 0x00;
    722 		break;
    723 
    724 	case AUDIO_ENCODING_SLINEAR_BE:
    725 		if (precision == 16)
    726 			data = 0xc0;
    727 		else
    728 			data = 0x00;
    729 		break;
    730 
    731 	case AUDIO_ENCODING_ADPCM:
    732 		data = 0xa0;
    733 		break;
    734 
    735 	default:
    736 		return -1;
    737 	}
    738 
    739 	if (channels == 2)
    740 		data |= 0x10;	/* stereo */
    741 
    742 	if (in) {
    743 		/* in */
    744 		sc->recfmtbits = data;
    745 		/* This will zero the normal codec frequency,
    746 		 * iw_set_speed should always be called afterwards.
    747 		 */
    748 		IW_WRITE_CODEC_1(CRDFI | IW_MCE, data);
    749 	} else {
    750 		/* out */
    751 		sc->playfmtbits = data;
    752 		IW_WRITE_CODEC_1(CPDFI | IW_MCE, data);
    753 	}
    754 
    755 	DPRINTF(("formatbits %s %x", in ? "in" : "out", data));
    756 
    757 	return encoding;
    758 }
    759 
    760 
    761 
    762 int
    763 iw_set_params(addr, setmode, usemode, p, q)
    764 	void	*addr;
    765 	int	setmode;
    766 	int	usemode;
    767 	struct	audio_params *p;
    768 	struct	audio_params *q;
    769 {
    770 	struct	iw_softc *sc = addr;
    771 	void	(*swcode)__P((void *, u_char * buf, int cnt)) = NULL;
    772 	int	factor = 1;
    773 	DPRINTF(("iw_setparams: code %d, prec %d, rate %d, chan %d\n",
    774 		(int) p->encoding, (int) p->precision, (int) p->sample_rate,
    775 		(int) p->channels));
    776 
    777 
    778 	switch (p->encoding) {
    779 	case AUDIO_ENCODING_ULAW:
    780 		if (p->precision != 8)
    781 			return EINVAL;
    782 		swcode = setmode & AUMODE_PLAY ? mulaw_to_ulinear8 : ulinear8_to_mulaw;
    783 		factor = 1;
    784 		break;
    785 	case AUDIO_ENCODING_ALAW:
    786 		if (p->precision != 8)
    787 			return EINVAL;
    788 		swcode = setmode & AUMODE_PLAY ? alaw_to_ulinear8 : ulinear8_to_alaw;
    789 		factor = 1;
    790 		break;
    791 	case AUDIO_ENCODING_ADPCM:
    792 		if (p->precision != 8)
    793 			return EINVAL;
    794 		else
    795 			break;
    796 
    797 	case AUDIO_ENCODING_SLINEAR_LE:
    798 	case AUDIO_ENCODING_SLINEAR_BE:
    799 		if (p->precision != 8 && p->precision != 16)
    800 			return EINVAL;
    801 		else
    802 			break;
    803 
    804 	default:
    805 		return EINVAL;
    806 
    807 	}
    808 
    809 	if (setmode & AUMODE_PLAY) {
    810 		sc->play_channels = p->channels;
    811 	        sc->play_encoding = p->encoding;
    812 		sc->play_precision = p->precision;
    813 		p->factor = factor;
    814 		p->sw_code = swcode;
    815 		iw_set_format(sc, p->precision, 0);
    816 		q->sample_rate = p->sample_rate = sc->sc_orate =
    817 			iw_set_speed(sc, p->sample_rate, 0);
    818 	} else {
    819 #if 0
    820 		q->channels = sc->rec_channels = p->channels;
    821 		q->encoding = sc->rec_encoding = p->encoding;
    822 		q->precision = sc->rec_precision = p->precision;
    823 #endif
    824 		sc->rec_channels = q->channels;
    825 		sc->rec_encoding = q->encoding;
    826 		sc->rec_precision = q->precision;
    827 		q->factor = factor;
    828 		q->sw_code = swcode;
    829 
    830 		iw_set_format(sc, p->precision, 1);
    831 		q->sample_rate = sc->sc_irate =
    832 			iw_set_speed(sc, q->sample_rate, 1);
    833 	}
    834 	return 0;
    835 }
    836 
    837 
    838 int
    839 iw_round_blocksize(addr, blk)
    840 	void	*addr;
    841 	int	blk;
    842 {
    843 	/* Round to a multiple of the biggest sample size. */
    844 	return blk &= -4;
    845 }
    846 
    847 void
    848 iw_mixer_line_level(sc, line, levl, levr)
    849 	struct	iw_softc *sc;
    850 	int	line;
    851 	int	levl, levr;
    852 {
    853 	u_char	gainl, gainr, attenl, attenr;
    854 
    855 	switch (line) {
    856 	case IW_REC:
    857 		gainl = sc->sc_recsrcbits | (levl >> 4);
    858 		gainr = sc->sc_recsrcbits | (levr >> 4);
    859 		DPRINTF(("recording with %x", gainl));
    860 		IW_WRITE_CODEC_1(CLICI, gainl);
    861 		IW_WRITE_CODEC_1(CRICI, gainr);
    862 		sc->sc_rec.voll = levl & 0xf0;
    863 		sc->sc_rec.volr = levr & 0xf0;
    864 		break;
    865 
    866 	case IW_AUX1:
    867 
    868 		gainl = (255 - levl) >> 3;
    869 		gainr = (255 - levr) >> 3;
    870 
    871 		/* mute if 0 level */
    872 		if (levl == 0)
    873 			gainl |= 0x80;
    874 		if (levr == 0)
    875 			gainr |= 0x80;
    876 
    877 		IW_WRITE_CODEC_1(IW_LEFT_AUX1_PORT, gainl);
    878 		IW_WRITE_CODEC_1(IW_RIGHT_AUX1_PORT, gainr);
    879 		sc->sc_aux1.voll = levl & 0xf8;
    880 		sc->sc_aux1.volr = levr & 0xf8;
    881 
    882 		break;
    883 
    884 	case IW_AUX2:
    885 
    886 		gainl = (255 - levl) >> 3;
    887 		gainr = (255 - levr) >> 3;
    888 
    889 		/* mute if 0 level */
    890 		if (levl == 0)
    891 			gainl |= 0x80;
    892 		if (levr == 0)
    893 			gainr |= 0x80;
    894 
    895 		IW_WRITE_CODEC_1(IW_LEFT_AUX2_PORT, gainl);
    896 		IW_WRITE_CODEC_1(IW_RIGHT_AUX2_PORT, gainr);
    897 		sc->sc_aux2.voll = levl & 0xf8;
    898 		sc->sc_aux2.volr = levr & 0xf8;
    899 		break;
    900 	case IW_DAC:
    901 		attenl = ((255 - levl) >> 2) | ((levl && !sc->sc_dac.off) ? 0 : 0x80);
    902 		attenr = ((255 - levr) >> 2) | ((levr && !sc->sc_dac.off) ? 0 : 0x80);
    903 		IW_WRITE_CODEC_1(CLDACI, attenl);
    904 		IW_WRITE_CODEC_1(CRDACI, attenr);
    905 		sc->sc_dac.voll = levl & 0xfc;
    906 		sc->sc_dac.volr = levr & 0xfc;
    907 		break;
    908 	case IW_LOOPBACK:
    909 		attenl = ((255 - levl) & 0xfc) | (levl ? 0x01 : 0);
    910 		IW_WRITE_CODEC_1(CLCI, attenl);
    911 		sc->sc_loopback.voll = levl & 0xfc;
    912 		break;
    913 	case IW_LINE_IN:
    914 		gainl = (levl >> 3) | (levl ? 0 : 0x80);
    915 		gainr = (levr >> 3) | (levr ? 0 : 0x80);
    916 		IW_WRITE_CODEC_1(CLLICI, gainl);
    917 		IW_WRITE_CODEC_1(CRLICI, gainr);
    918 		sc->sc_linein.voll = levl & 0xf8;
    919 		sc->sc_linein.volr = levr & 0xf8;
    920 		break;
    921 	case IW_MIC_IN:
    922 		gainl = ((255 - levl) >> 3) | (levl ? 0 : 0x80);
    923 		gainr = ((255 - levr) >> 3) | (levr ? 0 : 0x80);
    924 		IW_WRITE_CODEC_1(CLMICI, gainl);
    925 		IW_WRITE_CODEC_1(CRMICI, gainr);
    926 		sc->sc_mic.voll = levl & 0xf8;
    927 		sc->sc_mic.volr = levr & 0xf8;
    928 		break;
    929 	case IW_LINE_OUT:
    930 		attenl = ((255 - levl) >> 3) | (levl ? 0 : 0x80);
    931 		attenr = ((255 - levr) >> 3) | (levr ? 0 : 0x80);
    932 		IW_WRITE_CODEC_1(CLOAI, attenl);
    933 		IW_WRITE_CODEC_1(CROAI, attenr);
    934 		sc->sc_lineout.voll = levl & 0xf8;
    935 		sc->sc_lineout.volr = levr & 0xf8;
    936 		break;
    937 	case IW_MONO_IN:
    938 		attenl = ((255 - levl) >> 4) | (levl ? 0 : 0xc0);	/* in/out mute */
    939 		IW_WRITE_CODEC_1(CMONOI, attenl);
    940 		sc->sc_monoin.voll = levl & 0xf0;
    941 		break;
    942 	}
    943 }
    944 
    945 int
    946 iw_commit_settings(addr)
    947 	void	*addr;
    948 {
    949 	return 0;
    950 }
    951 
    952 
    953 void
    954 iw_trigger_dma(sc, io)
    955 	struct	iw_softc *sc;
    956 	u_char	io;
    957 {
    958 	u_char	reg;
    959 	int	s;
    960 
    961 	s = splaudio();
    962 
    963 	IW_READ_CODEC_1(CSR3I, reg);
    964 	IW_WRITE_CODEC_1(CSR3I, reg & ~(io == IW_DMA_PLAYBACK ? 0x10 : 0x20));
    965 
    966 	IW_READ_CODEC_1(CFIG1I, reg);
    967 
    968 	IW_WRITE_CODEC_1(CFIG1I, reg | io);
    969 
    970 	/* let the counter run */
    971 	IW_READ_CODEC_1(CFIG2I, reg);
    972 	IW_WRITE_CODEC_1(CFIG2I, reg & ~(io << 4));
    973 
    974 	splx(s);
    975 }
    976 
    977 void
    978 iw_stop_dma(sc, io, hard)
    979 	struct	iw_softc *sc;
    980 	u_char	io, hard;
    981 {
    982 	u_char	reg;
    983 
    984 	/* just stop the counter, no need to flush the fifo */
    985 	IW_READ_CODEC_1(CFIG2I, reg);
    986 	IW_WRITE_CODEC_1(CFIG2I, (reg | (io << 4)));
    987 
    988 	if (hard) {
    989 		/* unless we're closing the device */
    990 		IW_READ_CODEC_1(CFIG1I, reg);
    991 		IW_WRITE_CODEC_1(CFIG1I, reg & ~io);
    992 	}
    993 }
    994 
    995 void
    996 iw_dma_count(sc, count, io)
    997 	struct	iw_softc *sc;
    998 	u_short	count;
    999 	int	io;
   1000 {
   1001 	if (io == IW_DMA_PLAYBACK) {
   1002 		IW_WRITE_CODEC_1(CLPCTI, (u_char) (count & 0x00ff));
   1003 		IW_WRITE_CODEC_1(CUPCTI, (u_char) ((count >> 8) & 0x00ff));
   1004 	} else {
   1005 		IW_WRITE_CODEC_1(CLRCTI, (u_char) (count & 0x00ff));
   1006 		IW_WRITE_CODEC_1(CURCTI, (u_char) ((count >> 8) & 0x00ff));
   1007 	}
   1008 }
   1009 
   1010 int
   1011 iw_init_output(addr, buf, cc)
   1012 	void	*addr;
   1013 	void	*buf;
   1014 	int	cc;
   1015 {
   1016 	struct iw_softc *sc = (struct iw_softc *) addr;
   1017 
   1018 	DPRINTF(("iw_init_output\n"));
   1019 
   1020 	isa_dmastart(sc->sc_ic, sc->sc_playdrq, buf,
   1021 		     cc, NULL, DMAMODE_WRITE | DMAMODE_LOOP, BUS_DMA_NOWAIT);
   1022 	return 0;
   1023 }
   1024 
   1025 int
   1026 iw_init_input(addr, buf, cc)
   1027 	void	*addr;
   1028 	void	*buf;
   1029 	int	cc;
   1030 {
   1031 	struct	iw_softc *sc = (struct iw_softc *) addr;
   1032 
   1033 	DPRINTF(("iw_init_input\n"));
   1034 
   1035 	isa_dmastart(sc->sc_ic, sc->sc_recdrq, buf,
   1036 		     cc, NULL, DMAMODE_READ | DMAMODE_LOOP, BUS_DMA_NOWAIT);
   1037 	return 0;
   1038 }
   1039 
   1040 
   1041 int
   1042 iw_start_output(addr, p, cc, intr, arg)
   1043 	void	*addr;
   1044 	void	*p;
   1045 	int	cc;
   1046 	void	(*intr)__P((void *));
   1047 	void	*arg;
   1048 {
   1049 	struct	iw_softc *sc = addr;
   1050 	int	counter;
   1051 
   1052 #ifdef AUDIO_DEBUG
   1053 	if (sc->sc_playlocked) {
   1054 		DPRINTF(("iw_start_output: playback dma already going on\n"));
   1055 		/* return 0; */
   1056 	}
   1057 #endif
   1058 
   1059 	sc->sc_playlocked = 1;
   1060 #ifdef DIAGNOSTIC
   1061 	if (!intr) {
   1062 		printf("iw_start_output: no callback!\n");
   1063 		return 1;
   1064 	}
   1065 #endif
   1066 
   1067 	sc->sc_playintr = intr;
   1068 	sc->sc_playarg = arg;
   1069 	sc->sc_dma_flags |= DMAMODE_WRITE;
   1070 	sc->sc_playdma_bp = p;
   1071 
   1072 	counter = 0;
   1073 
   1074 	isa_dmastart(sc->sc_ic, sc->sc_playdrq, sc->sc_playdma_bp,
   1075 		     cc, NULL, DMAMODE_WRITE, BUS_DMA_NOWAIT);
   1076 
   1077 
   1078 	if (sc->play_encoding == AUDIO_ENCODING_ADPCM)
   1079 		cc >>= 2;
   1080 	if (sc->play_precision == 16)
   1081 		cc >>= 1;
   1082 
   1083 	if (sc->play_channels == 2 && sc->play_encoding != AUDIO_ENCODING_ADPCM)
   1084 		cc >>= 1;
   1085 
   1086 	cc -= iw_cc;
   1087 
   1088 
   1089 	/* iw_dma_access(sc,1); */
   1090 	if (cc != sc->sc_playdma_cnt) {
   1091 		iw_dma_count(sc, (u_short) cc, IW_DMA_PLAYBACK);
   1092 		sc->sc_playdma_cnt = cc;
   1093 
   1094 		iw_trigger_dma(sc, IW_DMA_PLAYBACK);
   1095 	}
   1096 
   1097 #ifdef DIAGNOSTIC
   1098 	if (outputs != iw_ints)
   1099 		printf("iw_start_output: out %d, int %d\n", outputs, iw_ints);
   1100 	outputs++;
   1101 #endif
   1102 	return 0;
   1103 }
   1104 
   1105 
   1106 int
   1107 iw_start_input(addr, p, cc, intr, arg)
   1108 	void	*addr;
   1109 	void	*p;
   1110 	int	cc;
   1111 	void	(*intr)__P((void *));
   1112 	void	*arg;
   1113 {
   1114 	struct	iw_softc *sc = addr;
   1115 	int	counter;
   1116 
   1117 #if AUDIO_DEBUG
   1118 	if (sc->sc_reclocked) {
   1119 		DPRINTF(("iw_start_input: record dma already going on\n"));
   1120 		/* return 0; */
   1121 	}
   1122 #endif
   1123 
   1124 	sc->sc_reclocked = 1;
   1125 #ifdef DIAGNOSTIC
   1126 	if (!intr) {
   1127 		printf("iw_start_input: no callback!\n");
   1128 		return 1;
   1129 	}
   1130 #endif
   1131 
   1132 
   1133 	sc->sc_recintr = intr;
   1134 	sc->sc_recarg = arg;
   1135 	sc->sc_dma_flags |= DMAMODE_READ;
   1136 	sc->sc_recdma_bp = p;
   1137 
   1138 	counter = 0;
   1139 
   1140 	isa_dmastart(sc->sc_ic, sc->sc_recdrq, sc->sc_recdma_bp,
   1141 		     cc, NULL, DMAMODE_READ, BUS_DMA_NOWAIT);
   1142 
   1143 
   1144 	if (sc->rec_encoding == AUDIO_ENCODING_ADPCM)
   1145 		cc >>= 2;
   1146 	if (sc->rec_precision == 16)
   1147 		cc >>= 1;
   1148 
   1149 	if (sc->rec_channels == 2 && sc->rec_encoding != AUDIO_ENCODING_ADPCM)
   1150 		cc >>= 1;
   1151 
   1152 	cc -= iw_cc;
   1153 
   1154 	/* iw_dma_access(sc,0); */
   1155 	if (sc->sc_recdma_cnt != cc) {
   1156 		iw_dma_count(sc, (u_short) cc, IW_DMA_RECORD);
   1157 		sc->sc_recdma_cnt = cc;
   1158 		/* iw_dma_ctrl(sc, IW_DMA_RECORD); */
   1159 		iw_trigger_dma(sc, IW_DMA_RECORD);
   1160 	}
   1161 
   1162 #ifdef DIAGNOSTIC
   1163 	if ((inputs != iw_inints))
   1164 		printf("iw_start_input: in %d, inints %d\n", inputs, iw_inints);
   1165 	inputs++;
   1166 #endif
   1167 
   1168 	return 0;
   1169 }
   1170 
   1171 
   1172 int
   1173 iw_halt_output(addr)
   1174 	void	*addr;
   1175 {
   1176 	struct	iw_softc *sc = addr;
   1177 	iw_stop_dma(sc, IW_DMA_PLAYBACK, 0);
   1178 	/* sc->sc_playlocked = 0; */
   1179 	return 0;
   1180 }
   1181 
   1182 
   1183 int
   1184 iw_halt_input(addr)
   1185 	void	*addr;
   1186 {
   1187 	struct	iw_softc *sc = addr;
   1188 	iw_stop_dma(sc, IW_DMA_RECORD, 0);
   1189 	/* sc->sc_reclocked = 0; */
   1190 	return 0;
   1191 }
   1192 
   1193 
   1194 int
   1195 iw_speaker_ctl(addr, newstate)
   1196 	void	*addr;
   1197 	int	newstate;
   1198 {
   1199 	struct	iw_softc *sc = addr;
   1200 	u_char          reg;
   1201 	if (newstate == SPKR_ON) {
   1202 		sc->sc_dac.off = 0;
   1203 		IW_READ_CODEC_1(CLDACI, reg);
   1204 		IW_WRITE_CODEC_1(CLDACI, reg & 0x7f);
   1205 		IW_READ_CODEC_1(CRDACI, reg);
   1206 		IW_WRITE_CODEC_1(CRDACI, reg & 0x7f);
   1207 	} else {
   1208 		/* SPKR_OFF */
   1209 		sc->sc_dac.off = 1;
   1210 		IW_READ_CODEC_1(CLDACI, reg);
   1211 		IW_WRITE_CODEC_1(CLDACI, reg | 0x80);
   1212 		IW_READ_CODEC_1(CRDACI, reg);
   1213 		IW_WRITE_CODEC_1(CRDACI, reg | 0x80);
   1214 	}
   1215 	return 0;
   1216 }
   1217 
   1218 
   1219 int
   1220 iw_getdev(addr, retp)
   1221 	void	*addr;
   1222 	struct	audio_device *retp;
   1223 {
   1224 	*retp = iw_device;
   1225 	return 0;
   1226 }
   1227 
   1228 
   1229 int
   1230 iw_setfd(addr, flag)
   1231 	void	*addr;
   1232 	int	flag;
   1233 {
   1234 	return 0;
   1235 }
   1236 
   1237 
   1238 /* Mixer (in/out ports) */
   1239 int
   1240 iw_set_port(addr, cp)
   1241 	void	*addr;
   1242 	mixer_ctrl_t *cp;
   1243 {
   1244 	struct	iw_softc *sc = addr;
   1245 	u_char	vall = 0, valr = 0;
   1246 	int	error = EINVAL;
   1247 
   1248 	switch (cp->dev) {
   1249 	case IW_MIC_IN_LVL:
   1250 		if (cp->type == AUDIO_MIXER_VALUE) {
   1251 			error = 0;
   1252 			if (cp->un.value.num_channels == 1) {
   1253 				vall = valr = cp->un.value.level[0];
   1254 			} else {
   1255 				vall = cp->un.value.level[0];
   1256 				valr = cp->un.value.level[1];
   1257 			}
   1258 			sc->sc_mic.voll = vall;
   1259 			sc->sc_mic.volr = valr;
   1260 			iw_mixer_line_level(sc, IW_MIC_IN, vall, valr);
   1261 		}
   1262 		break;
   1263 	case IW_AUX1_LVL:
   1264 		if (cp->type == AUDIO_MIXER_VALUE) {
   1265 			error = 0;
   1266 			if (cp->un.value.num_channels == 1) {
   1267 				vall = valr = cp->un.value.level[0];
   1268 			} else {
   1269 				vall = cp->un.value.level[0];
   1270 				valr = cp->un.value.level[1];
   1271 			}
   1272 			sc->sc_aux1.voll = vall;
   1273 			sc->sc_aux1.volr = valr;
   1274 			iw_mixer_line_level(sc, IW_AUX1, vall, valr);
   1275 		}
   1276 		break;
   1277 	case IW_AUX2_LVL:
   1278 		if (cp->type == AUDIO_MIXER_VALUE) {
   1279 			error = 0;
   1280 			if (cp->un.value.num_channels == 1) {
   1281 				vall = valr = cp->un.value.level[0];
   1282 			} else {
   1283 				vall = cp->un.value.level[0];
   1284 				valr = cp->un.value.level[1];
   1285 			}
   1286 			sc->sc_aux2.voll = vall;
   1287 			sc->sc_aux2.volr = valr;
   1288 			iw_mixer_line_level(sc, IW_AUX2, vall, valr);
   1289 		}
   1290 		break;
   1291 	case IW_LINE_IN_LVL:
   1292 		if (cp->type == AUDIO_MIXER_VALUE) {
   1293 			error = 0;
   1294 			if (cp->un.value.num_channels == 1) {
   1295 				vall = valr = cp->un.value.level[0];
   1296 			} else {
   1297 				vall = cp->un.value.level[0];
   1298 				valr = cp->un.value.level[1];
   1299 			}
   1300 			sc->sc_linein.voll = vall;
   1301 			sc->sc_linein.volr = valr;
   1302 			iw_mixer_line_level(sc, IW_LINE_IN, vall, valr);
   1303 		}
   1304 		break;
   1305 	case IW_LINE_OUT_LVL:
   1306 		if (cp->type == AUDIO_MIXER_VALUE) {
   1307 			error = 0;
   1308 			if (cp->un.value.num_channels == 1) {
   1309 				vall = valr = cp->un.value.level[0];
   1310 			} else {
   1311 				vall = cp->un.value.level[0];
   1312 				valr = cp->un.value.level[1];
   1313 			}
   1314 			sc->sc_lineout.voll = vall;
   1315 			sc->sc_lineout.volr = valr;
   1316 			iw_mixer_line_level(sc, IW_LINE_OUT, vall, valr);
   1317 		}
   1318 		break;
   1319 	case IW_REC_LVL:
   1320 		if (cp->type == AUDIO_MIXER_VALUE) {
   1321 			error = 0;
   1322 			if (cp->un.value.num_channels == 1) {
   1323 				vall = valr = cp->un.value.level[0];
   1324 			} else {
   1325 				vall = cp->un.value.level[0];
   1326 				valr = cp->un.value.level[1];
   1327 			}
   1328 			sc->sc_rec.voll = vall;
   1329 			sc->sc_rec.volr = valr;
   1330 			iw_mixer_line_level(sc, IW_REC, vall, valr);
   1331 		}
   1332 		break;
   1333 
   1334 	case IW_DAC_LVL:
   1335 		if (cp->type == AUDIO_MIXER_VALUE) {
   1336 			error = 0;
   1337 			if (cp->un.value.num_channels == 1) {
   1338 				vall = valr = cp->un.value.level[0];
   1339 			} else {
   1340 				vall = cp->un.value.level[0];
   1341 				valr = cp->un.value.level[1];
   1342 			}
   1343 			sc->sc_dac.voll = vall;
   1344 			sc->sc_dac.volr = valr;
   1345 			iw_mixer_line_level(sc, IW_DAC, vall, valr);
   1346 		}
   1347 		break;
   1348 
   1349 	case IW_LOOPBACK_LVL:
   1350 		if (cp->type == AUDIO_MIXER_VALUE) {
   1351 			error = 0;
   1352 			if (cp->un.value.num_channels != 1) {
   1353 				return EINVAL;
   1354 			} else {
   1355 				valr = vall = cp->un.value.level[0];
   1356 			}
   1357 			sc->sc_loopback.voll = vall;
   1358 			sc->sc_loopback.volr = valr;
   1359 			iw_mixer_line_level(sc, IW_LOOPBACK, vall, valr);
   1360 		}
   1361 		break;
   1362 
   1363 	case IW_MONO_IN_LVL:
   1364 		if (cp->type == AUDIO_MIXER_VALUE) {
   1365 			error = 0;
   1366 			if (cp->un.value.num_channels != 1) {
   1367 				return EINVAL;
   1368 			} else {
   1369 				valr = vall = cp->un.value.level[0];
   1370 			}
   1371 			sc->sc_monoin.voll = vall;
   1372 			sc->sc_monoin.volr = valr;
   1373 			iw_mixer_line_level(sc, IW_MONO_IN, vall, valr);
   1374 		}
   1375 		break;
   1376 	case IW_RECORD_SOURCE:
   1377 		error = 0;
   1378 		sc->sc_recsrcbits = cp->un.ord << 6;
   1379 		DPRINTF(("record source %d bits %x\n", cp->un.ord, sc->sc_recsrcbits));
   1380 		iw_mixer_line_level(sc, IW_REC, sc->sc_rec.voll, sc->sc_rec.volr);
   1381 		break;
   1382 	}
   1383 
   1384 	return error;
   1385 }
   1386 
   1387 
   1388 int
   1389 iw_get_port(addr, cp)
   1390 	void	*addr;
   1391 	mixer_ctrl_t *cp;
   1392 {
   1393 	struct	iw_softc *sc = addr;
   1394 
   1395 	int	error = EINVAL;
   1396 
   1397 	switch (cp->dev) {
   1398 	case IW_MIC_IN_LVL:
   1399 		if (cp->type == AUDIO_MIXER_VALUE) {
   1400 			cp->un.value.num_channels = 2;
   1401 			cp->un.value.level[0] = sc->sc_mic.voll;
   1402 			cp->un.value.level[1] = sc->sc_mic.volr;
   1403 			error = 0;
   1404 		}
   1405 		break;
   1406 	case IW_AUX1_LVL:
   1407 		if (cp->type == AUDIO_MIXER_VALUE) {
   1408 			cp->un.value.num_channels = 2;
   1409 			cp->un.value.level[0] = sc->sc_aux1.voll;
   1410 			cp->un.value.level[1] = sc->sc_aux1.volr;
   1411 			error = 0;
   1412 		}
   1413 		break;
   1414 	case IW_AUX2_LVL:
   1415 		if (cp->type == AUDIO_MIXER_VALUE) {
   1416 			cp->un.value.num_channels = 2;
   1417 			cp->un.value.level[0] = sc->sc_aux2.voll;
   1418 			cp->un.value.level[1] = sc->sc_aux2.volr;
   1419 			error = 0;
   1420 		}
   1421 		break;
   1422 	case IW_LINE_OUT_LVL:
   1423 		if (cp->type == AUDIO_MIXER_VALUE) {
   1424 			cp->un.value.num_channels = 2;
   1425 			cp->un.value.level[0] = sc->sc_lineout.voll;
   1426 			cp->un.value.level[1] = sc->sc_lineout.volr;
   1427 			error = 0;
   1428 		}
   1429 		break;
   1430 	case IW_LINE_IN_LVL:
   1431 		if (cp->type == AUDIO_MIXER_VALUE) {
   1432 			cp->un.value.num_channels = 2;
   1433 			cp->un.value.level[0] = sc->sc_linein.voll;
   1434 			cp->un.value.level[1] = sc->sc_linein.volr;
   1435 			error = 0;
   1436 		}
   1437 	case IW_REC_LVL:
   1438 		if (cp->type == AUDIO_MIXER_VALUE) {
   1439 			cp->un.value.num_channels = 2;
   1440 			cp->un.value.level[0] = sc->sc_rec.voll;
   1441 			cp->un.value.level[1] = sc->sc_rec.volr;
   1442 			error = 0;
   1443 		}
   1444 		break;
   1445 
   1446 	case IW_DAC_LVL:
   1447 		if (cp->type == AUDIO_MIXER_VALUE) {
   1448 			cp->un.value.num_channels = 2;
   1449 			cp->un.value.level[0] = sc->sc_dac.voll;
   1450 			cp->un.value.level[1] = sc->sc_dac.volr;
   1451 			error = 0;
   1452 		}
   1453 		break;
   1454 
   1455 	case IW_LOOPBACK_LVL:
   1456 		if (cp->type == AUDIO_MIXER_VALUE) {
   1457 			cp->un.value.num_channels = 1;
   1458 			cp->un.value.level[0] = sc->sc_loopback.voll;
   1459 			error = 0;
   1460 		}
   1461 		break;
   1462 
   1463 	case IW_MONO_IN_LVL:
   1464 		if (cp->type == AUDIO_MIXER_VALUE) {
   1465 			cp->un.value.num_channels = 1;
   1466 			cp->un.value.level[0] = sc->sc_monoin.voll;
   1467 			error = 0;
   1468 		}
   1469 		break;
   1470 	case IW_RECORD_SOURCE:
   1471 		cp->un.ord = sc->sc_recsrcbits >> 6;
   1472 		error = 0;
   1473 		break;
   1474 	}
   1475 
   1476 	return error;
   1477 }
   1478 
   1479 
   1480 
   1481 int
   1482 iw_query_devinfo(addr, dip)
   1483 	void	*addr;
   1484 	mixer_devinfo_t *dip;
   1485 {
   1486 
   1487 	switch (dip->index) {
   1488 	case IW_MIC_IN_LVL:	/* Microphone */
   1489 		dip->type = AUDIO_MIXER_VALUE;
   1490 		dip->mixer_class = IW_INPUT_CLASS;
   1491 		dip->prev = AUDIO_MIXER_LAST;
   1492 		dip->next = AUDIO_MIXER_LAST;
   1493 		strcpy(dip->label.name, AudioNmicrophone);
   1494 		dip->un.v.num_channels = 2;
   1495 		strcpy(dip->un.v.units.name, AudioNvolume);
   1496 		break;
   1497 	case IW_AUX1_LVL:
   1498 		dip->type = AUDIO_MIXER_VALUE;
   1499 		dip->mixer_class = IW_INPUT_CLASS;
   1500 		dip->prev = AUDIO_MIXER_LAST;
   1501 		dip->next = AUDIO_MIXER_LAST;
   1502 		strcpy(dip->label.name, AudioNline);
   1503 		dip->un.v.num_channels = 2;
   1504 		strcpy(dip->un.v.units.name, AudioNvolume);
   1505 		break;
   1506 	case IW_AUX2_LVL:
   1507 		dip->type = AUDIO_MIXER_VALUE;
   1508 		dip->mixer_class = IW_INPUT_CLASS;
   1509 		dip->prev = AUDIO_MIXER_LAST;
   1510 		dip->next = AUDIO_MIXER_LAST;
   1511 		strcpy(dip->label.name, AudioNcd);
   1512 		dip->un.v.num_channels = 2;
   1513 		strcpy(dip->un.v.units.name, AudioNvolume);
   1514 		break;
   1515 	case IW_LINE_OUT_LVL:
   1516 		dip->type = AUDIO_MIXER_VALUE;
   1517 		dip->mixer_class = IW_OUTPUT_CLASS;
   1518 		dip->prev = AUDIO_MIXER_LAST;
   1519 		dip->next = AUDIO_MIXER_LAST;
   1520 		strcpy(dip->label.name, AudioNline);
   1521 		dip->un.v.num_channels = 2;
   1522 		strcpy(dip->un.v.units.name, AudioNvolume);
   1523 		break;
   1524 	case IW_DAC_LVL:
   1525 		dip->type = AUDIO_MIXER_VALUE;
   1526 		dip->mixer_class = IW_OUTPUT_CLASS;
   1527 		dip->prev = AUDIO_MIXER_LAST;
   1528 		dip->next = AUDIO_MIXER_LAST;
   1529 		strcpy(dip->label.name, AudioNdac);
   1530 		dip->un.v.num_channels = 2;
   1531 		strcpy(dip->un.v.units.name, AudioNvolume);
   1532 		break;
   1533 	case IW_LINE_IN_LVL:
   1534 		dip->type = AUDIO_MIXER_VALUE;
   1535 		dip->mixer_class = IW_INPUT_CLASS;
   1536 		dip->prev = AUDIO_MIXER_LAST;
   1537 		dip->next = AUDIO_MIXER_LAST;
   1538 		strcpy(dip->label.name, AudioNinput);
   1539 		dip->un.v.num_channels = 2;
   1540 		strcpy(dip->un.v.units.name, AudioNvolume);
   1541 		break;
   1542 	case IW_MONO_IN_LVL:
   1543 		dip->type = AUDIO_MIXER_VALUE;
   1544 		dip->mixer_class = IW_INPUT_CLASS;
   1545 		dip->prev = AUDIO_MIXER_LAST;
   1546 		dip->next = AUDIO_MIXER_LAST;
   1547 		strcpy(dip->label.name, AudioNmono);
   1548 		dip->un.v.num_channels = 1;
   1549 		strcpy(dip->un.v.units.name, AudioNvolume);
   1550 		break;
   1551 
   1552 	case IW_REC_LVL:	/* record level */
   1553 		dip->type = AUDIO_MIXER_VALUE;
   1554 		dip->mixer_class = IW_RECORD_CLASS;
   1555 		dip->prev = AUDIO_MIXER_LAST;
   1556 		dip->next = AUDIO_MIXER_LAST;
   1557 		strcpy(dip->label.name, AudioNrecord);
   1558 		dip->un.v.num_channels = 2;
   1559 		strcpy(dip->un.v.units.name, AudioNvolume);
   1560 		break;
   1561 
   1562 	case IW_LOOPBACK_LVL:
   1563 		dip->type = AUDIO_MIXER_VALUE;
   1564 		dip->mixer_class = IW_RECORD_CLASS;
   1565 		dip->prev = AUDIO_MIXER_LAST;
   1566 		dip->next = AUDIO_MIXER_LAST;
   1567 		strcpy(dip->label.name, "filter");
   1568 		dip->un.v.num_channels = 1;
   1569 		strcpy(dip->un.v.units.name, AudioNvolume);
   1570 		break;
   1571 
   1572 	case IW_RECORD_SOURCE:
   1573 		dip->mixer_class = IW_RECORD_CLASS;
   1574 		dip->type = AUDIO_MIXER_ENUM;
   1575 		dip->prev = AUDIO_MIXER_LAST;
   1576 		dip->next = AUDIO_MIXER_LAST;
   1577 		strcpy(dip->label.name, AudioNsource);
   1578 		dip->un.e.num_mem = 4;
   1579 		strcpy(dip->un.e.member[0].label.name, AudioNline);
   1580 		dip->un.e.member[0].ord = IW_LINE_IN_SRC;
   1581 		strcpy(dip->un.e.member[1].label.name, "aux1");
   1582 		dip->un.e.member[1].ord = IW_AUX1_SRC;
   1583 		strcpy(dip->un.e.member[2].label.name, AudioNmicrophone);
   1584 		dip->un.e.member[2].ord = IW_MIC_IN_SRC;
   1585 		strcpy(dip->un.e.member[3].label.name, AudioNmixerout);
   1586 		dip->un.e.member[3].ord = IW_MIX_OUT_SRC;
   1587 		break;
   1588 	case IW_INPUT_CLASS:
   1589 		dip->type = AUDIO_MIXER_CLASS;
   1590 		dip->mixer_class = IW_INPUT_CLASS;
   1591 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1592 		strcpy(dip->label.name, AudioCinputs);
   1593 		break;
   1594 	case IW_OUTPUT_CLASS:
   1595 		dip->type = AUDIO_MIXER_CLASS;
   1596 		dip->mixer_class = IW_OUTPUT_CLASS;
   1597 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1598 		strcpy(dip->label.name, AudioCoutputs);
   1599 		break;
   1600 	case IW_RECORD_CLASS:	/* record source class */
   1601 		dip->type = AUDIO_MIXER_CLASS;
   1602 		dip->mixer_class = IW_RECORD_CLASS;
   1603 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1604 		strcpy(dip->label.name, AudioCrecord);
   1605 		return 0;
   1606 	default:
   1607 		return ENXIO;
   1608 	}
   1609 	return 0;
   1610 }
   1611 
   1612 
   1613 void *
   1614 iw_malloc(addr, direction, size, pool, flags)
   1615 	void	*addr;
   1616 	int	direction;
   1617 	size_t	size;
   1618 	struct malloc_type *pool;
   1619 	int	flags;
   1620 {
   1621 	struct iw_softc *sc = addr;
   1622 	int drq;
   1623 
   1624 	if (direction == AUMODE_PLAY)
   1625 		drq = sc->sc_playdrq;
   1626 	else
   1627 		drq = sc->sc_recdrq;
   1628 	return (isa_malloc(sc->sc_ic, drq, size, pool, flags));
   1629 }
   1630 
   1631 void
   1632 iw_free(addr, ptr, pool)
   1633 	void	*addr;
   1634 	void	*ptr;
   1635 	struct malloc_type *pool;
   1636 {
   1637 	isa_free(ptr, pool);
   1638 }
   1639 
   1640 size_t
   1641 iw_round_buffersize(addr, direction, size)
   1642 	void	*addr;
   1643 	int	direction;
   1644 	size_t	size;
   1645 {
   1646 	struct iw_softc *sc = addr;
   1647 	bus_size_t maxsize;
   1648 
   1649 	if (direction == AUMODE_PLAY)
   1650 		maxsize = sc->sc_play_maxsize;
   1651 	else
   1652 		maxsize = sc->sc_rec_maxsize;
   1653 
   1654 	if (size > maxsize)
   1655 		size = maxsize;
   1656 	return (size);
   1657 }
   1658 
   1659 paddr_t
   1660 iw_mappage(addr, mem, off, prot)
   1661 	void	*addr;
   1662 	void	*mem;
   1663 	off_t	off;
   1664 	int	prot;
   1665 {
   1666 	return isa_mappage(mem, off, prot);
   1667 }
   1668 
   1669 int
   1670 iw_get_props(addr)
   1671 	void	*addr;
   1672 {
   1673 	struct iw_softc *sc = addr;
   1674 	return AUDIO_PROP_MMAP |
   1675 		(sc->sc_fullduplex ? AUDIO_PROP_FULLDUPLEX : 0);
   1676 }
   1677