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